Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. /* compile with -lSDL2 */
  2. #include <SDL2/SDL.h>
  3. #include <errno.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. enum {
  9.     WIDTH = 320,
  10.     HEIGHT = 240,
  11.     PFORMAT = SDL_PIXELFORMAT_ARGB8888,
  12. };
  13.  
  14. static inline Uint32 rgb(Uint8 r, Uint8 g, Uint8 b)
  15. {
  16.     static SDL_PixelFormat *pf = NULL;
  17.     Uint32 ret;
  18.     if (pf == NULL) {
  19.         pf = SDL_AllocFormat(PFORMAT);
  20.         if (pf == NULL) {
  21.             fprintf(stderr, "Could not allocte pixel format: %s\n",
  22.                 SDL_GetError());
  23.             exit(EXIT_FAILURE);
  24.         }
  25.     }
  26.     ret = SDL_MapRGB(pf, r, g, b);
  27.     SDL_FreeFormat(pf);
  28.     return ret;
  29. }
  30.  
  31. int main(void)
  32. {
  33.     SDL_Window *sw;
  34.     SDL_Renderer *sr;
  35.     SDL_Texture *st;
  36.     Uint32 *buffer;
  37.  
  38.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  39.         fprintf(stderr, "Could not initialise SDL: %s\n", SDL_GetError());
  40.         return EXIT_FAILURE;
  41.     }
  42.  
  43.     sw = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  44.                   WIDTH, HEIGHT, 0);
  45.     if (sw == NULL) {
  46.         fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
  47.         return EXIT_FAILURE;
  48.     }
  49.  
  50.     sr = SDL_CreateRenderer(sw, -1, SDL_RENDERER_PRESENTVSYNC);
  51.     if (sr == NULL) {
  52.         fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError());
  53.         return EXIT_FAILURE;
  54.     }
  55.  
  56.     st = SDL_CreateTexture(sr, PFORMAT, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
  57.     if (st == NULL) {
  58.         fprintf(stderr, "Could not create texture: %s\n", SDL_GetError());
  59.         return EXIT_FAILURE;
  60.     }
  61.  
  62.     buffer = malloc(WIDTH * HEIGHT * sizeof *buffer);
  63.     if (buffer == NULL) {
  64.         fprintf(stderr, "Could not allocate memory: %s\n", strerror(errno));
  65.         return EXIT_FAILURE;
  66.     }
  67.  
  68.     for (int x = 0; x < WIDTH; x++)
  69.         for (int y = 0; y < HEIGHT; y++)
  70.             buffer[x + WIDTH * y] = rgb(rand(), rand(), rand());
  71.  
  72.     SDL_UpdateTexture(st, NULL, buffer, WIDTH * sizeof *buffer);
  73.     SDL_RenderCopy(sr, st, NULL, NULL);
  74.     SDL_RenderPresent(sr);
  75.  
  76.     int shouldquit = 0;
  77.     SDL_Event se;
  78.     while (!shouldquit) {
  79.         if(0 == SDL_WaitEvent(&se)) continue;
  80.         if (se.type == SDL_QUIT)
  81.             shouldquit = 1;
  82.     }
  83.  
  84.     SDL_DestroyTexture(st);
  85.     SDL_DestroyRenderer(sr);
  86.     SDL_DestroyWindow(sw);
  87.  
  88.     free(buffer);
  89.     SDL_Quit();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement