Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <SDL2/SDL.h>
  3.  
  4. #define PICTURE_SIZE_WIDTH 640
  5. #define PICTURE_SIZE_HEIGHT 480
  6. #define PICTURE_AT(x, y) (y * PICTURE_SIZE_WIDTH + x)
  7.  
  8. /*
  9. * Declarations
  10. */
  11.  
  12. typedef struct SDL_Context {
  13. SDL_Window *window;
  14. SDL_Renderer *renderer;
  15. SDL_Texture *texture;
  16. uint32_t pixels[PICTURE_SIZE_HEIGHT * PICTURE_SIZE_WIDTH];
  17. void (*update)(struct SDL_Context *ctx);
  18. void (*draw )(struct SDL_Context *ctx);
  19. bool (*events)(struct SDL_Context *ctx);
  20. } SDL_Context;
  21.  
  22. typedef void (*SDL_ContextUpdateFunc)(SDL_Context *ctx);
  23. typedef void (*SDL_ContextDrawFunc )(SDL_Context *ctx);
  24. typedef bool (*SDL_ContextEventsFunc)(SDL_Context *ctx);
  25.  
  26. SDL_Context * SDL_CreateContext(SDL_ContextUpdateFunc update,
  27. SDL_ContextDrawFunc draw,
  28. SDL_ContextEventsFunc events);
  29. void SDL_MainLoop(SDL_Context *ctx);
  30. void SDL_DeleteContext(SDL_Context *ctx);
  31.  
  32. /*
  33. * Internal functions
  34. */
  35.  
  36. SDL_Context * SDL_CreateContext(SDL_ContextUpdateFunc update,
  37. SDL_ContextDrawFunc draw,
  38. SDL_ContextEventsFunc events)
  39. {
  40. SDL_Context *ctx = NULL;
  41.  
  42. ctx = malloc(sizeof(SDL_Context));
  43. if (ctx == NULL) {
  44. return NULL;
  45. }
  46.  
  47. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  48. goto err_cleanup;
  49. }
  50.  
  51. ctx->window = SDL_CreateWindow("SDL_Context",
  52. SDL_WINDOWPOS_UNDEFINED,
  53. SDL_WINDOWPOS_UNDEFINED,
  54. PICTURE_SIZE_WIDTH,
  55. PICTURE_SIZE_HEIGHT,
  56. SDL_WINDOW_SHOWN);
  57. if (ctx->window == NULL) {
  58. goto err_cleanup;
  59. }
  60.  
  61. ctx->renderer = SDL_CreateRenderer(ctx->window,
  62. -1, SDL_RENDERER_ACCELERATED);
  63. if (ctx->renderer == NULL) {
  64. goto err_cleanup;
  65. }
  66.  
  67. ctx->texture = SDL_CreateTexture(ctx->renderer,
  68. SDL_PIXELFORMAT_ARGB8888,
  69. SDL_TEXTUREACCESS_STATIC,
  70. PICTURE_SIZE_WIDTH,
  71. PICTURE_SIZE_HEIGHT);
  72. if (ctx->texture == NULL) {
  73. goto err_cleanup;
  74. }
  75.  
  76. memset(ctx->pixels, 0xFF, PICTURE_SIZE_HEIGHT * PICTURE_SIZE_WIDTH * 4);
  77.  
  78. if (update == NULL || draw == NULL || events == NULL) {
  79. goto err_cleanup;
  80. }
  81. ctx->update = update;
  82. ctx->draw = draw;
  83. ctx->events = events;
  84.  
  85. return ctx;
  86.  
  87. err_cleanup:
  88. SDL_DeleteContext(ctx);
  89. return NULL;
  90. }
  91.  
  92. void SDL_MainLoop(SDL_Context *ctx)
  93. {
  94. for (bool quit = false; quit != true;) {
  95. ctx->update(ctx);
  96. quit = ctx->events(ctx);
  97. ctx->draw(ctx);
  98. }
  99. }
  100.  
  101. void SDL_DeleteContext(SDL_Context *ctx)
  102. {
  103. if (ctx != NULL) {
  104. SDL_DestroyTexture(ctx->texture);
  105. SDL_DestroyRenderer(ctx->renderer);
  106. SDL_DestroyWindow(ctx->window);
  107. }
  108. free(ctx);
  109. }
  110.  
  111. /*
  112. * Context manipulation functions provided by the user
  113. */
  114.  
  115. static void UpdatePicture(SDL_Context *ctx)
  116. {
  117. SDL_UpdateTexture(ctx->texture, NULL, ctx->pixels, PICTURE_SIZE_WIDTH * 4);
  118. }
  119.  
  120. static void DrawPicture(SDL_Context *ctx)
  121. {
  122. SDL_RenderClear(ctx->renderer);
  123. SDL_RenderCopy(ctx->renderer, ctx->texture, NULL, NULL);
  124. SDL_RenderPresent(ctx->renderer);
  125. }
  126.  
  127. static bool HandleEvents(SDL_Context *ctx)
  128. {
  129. SDL_Event e;
  130. static bool clicked = false;
  131.  
  132. while (SDL_PollEvent(&e)) {
  133. switch (e.type) {
  134. case SDL_MOUSEBUTTONUP:
  135. clicked = (e.button.button == SDL_BUTTON_LEFT) ? false : clicked;
  136. break;
  137. case SDL_MOUSEBUTTONDOWN:
  138. clicked = (e.button.button == SDL_BUTTON_LEFT) ? true : clicked;
  139. break;
  140. case SDL_MOUSEMOTION:
  141. if (clicked) {
  142. ctx->pixels[PICTURE_AT(e.motion.x, e.motion.y)] = 0x00;
  143. }
  144. break;
  145. case SDL_QUIT:
  146. return true;
  147. }
  148. }
  149.  
  150. return false;
  151. }
  152.  
  153. /*
  154. * Main game procedure
  155. */
  156.  
  157. int main(int argc, char *argv[])
  158. {
  159. SDL_Context *ctx = NULL;
  160.  
  161. ctx = SDL_CreateContext(UpdatePicture, DrawPicture, HandleEvents);
  162. if (ctx == NULL) {
  163. fprintf(stderr, "Could not create the SDL context\n");
  164. return -1;
  165. }
  166.  
  167. SDL_MainLoop(ctx);
  168.  
  169. SDL_DeleteContext(ctx);
  170. SDL_Quit();
  171.  
  172. return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement