ps-sdl2.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* ps-sdl2 --- Pre-Scheme wrappers for SDL2
  2. **
  3. ** This header defines some C macros around the SDL2 API to make it
  4. ** usable from Pre-Scheme. It's intended to be used via the ps-sdl2
  5. ** Pre-Scheme package. Corresponding Pre-Scheme definitions can be
  6. ** found in "ps-sdl2.scm".
  7. */
  8. #include <SDL2/SDL.h>
  9. #include <stdlib.h>
  10. #define DEFINE_STRUCT_WRAPPER(wrapper, actual) \
  11. struct wrapper; \
  12. static inline struct wrapper* wrap_##wrapper(actual* ptr) { \
  13. return (struct wrapper*)ptr; \
  14. } \
  15. static inline actual* unwrap_##wrapper(struct wrapper* ptr) { \
  16. return (actual*)ptr; \
  17. }
  18. DEFINE_STRUCT_WRAPPER(sdl_window, SDL_Window);
  19. DEFINE_STRUCT_WRAPPER(sdl_renderer, SDL_Renderer);
  20. DEFINE_STRUCT_WRAPPER(sdl_event, SDL_Event);
  21. static inline struct sdl_window* PS_SDL_CreateWindow(const char* title,
  22. long x, long y, long w, long h,
  23. long flags) {
  24. return wrap_sdl_window(SDL_CreateWindow(title, x, y, w, h, flags));
  25. }
  26. static inline void PS_SDL_DestroyWindow(struct sdl_window* window) {
  27. SDL_DestroyWindow(unwrap_sdl_window(window));
  28. }
  29. static inline struct sdl_renderer* PS_SDL_CreateRenderer(struct sdl_window* window,
  30. long index, long flags) {
  31. return wrap_sdl_renderer(SDL_CreateRenderer(unwrap_sdl_window(window), index, flags));
  32. }
  33. static inline void PS_SDL_DestroyRenderer(struct sdl_renderer* renderer) {
  34. SDL_DestroyRenderer(unwrap_sdl_renderer(renderer));
  35. }
  36. static inline long PS_SDL_SetRenderDrawColor(struct sdl_renderer* renderer,
  37. long r, long g, long b, long a) {
  38. return SDL_SetRenderDrawColor(unwrap_sdl_renderer(renderer), r, g, b, a);
  39. }
  40. static inline long PS_SDL_RenderClear(struct sdl_renderer* renderer) {
  41. return SDL_RenderClear(unwrap_sdl_renderer(renderer));
  42. }
  43. static inline void PS_SDL_RenderPresent(struct sdl_renderer* renderer) {
  44. SDL_RenderPresent(unwrap_sdl_renderer(renderer));
  45. }
  46. static inline long PS_SDL_RenderFillRect(struct sdl_renderer* renderer,
  47. long x, long y, long w, long h) {
  48. SDL_Rect rect = { x, y, w, h };
  49. return SDL_RenderFillRect(unwrap_sdl_renderer(renderer), &rect);
  50. }
  51. static inline struct sdl_event* PS_SDL_CreateEvent(void) {
  52. return wrap_sdl_event((SDL_Event*)calloc(1, sizeof(SDL_Event)));
  53. }
  54. static inline void PS_SDL_DestroyEvent(struct sdl_event* event) {
  55. free(unwrap_sdl_event(event));
  56. }
  57. static inline long PS_SDL_PollEvent(struct sdl_event* event) {
  58. return SDL_PollEvent(unwrap_sdl_event(event));
  59. }
  60. static inline long PS_SDL_EventType(struct sdl_event* event) {
  61. return unwrap_sdl_event(event)->type;
  62. }