SHARE
TWEET

Untitled

a guest Aug 24th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void draw_pixel(Buffer *buffer, int32 x, int32 y, uint32 color)
  2. {
  3.     buffer->pixels[y * buffer->width + x] = color;
  4. }
  5.  
  6. void draw_rect_in_pixels(Buffer *buffer, int32 x, int32 y, int32 width, int32 height, uint32 color)
  7. {
  8.     int32 xpos = x;
  9.     int32 ypos = y;
  10.  
  11.     for(; ypos < height; ypos++)
  12.     {
  13.         for(; xpos < width; xpos++)
  14.         {
  15.             draw_pixel(buffer, xpos, ypos, color);
  16.         }
  17.     }
  18. }
  19.  
  20. Result buffer_to_texture(SDL_Renderer *renderer, Buffer *buffer)
  21. {
  22.     Result result;
  23.     SDL_Texture *buffer_texture = SDL_CreateTexture(
  24.             renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, buffer->width, buffer->height
  25.             );
  26.  
  27.     if(!buffer_texture)
  28.     {
  29.         result = result_error("Failed to create SDL Texture for Buffer");
  30.         return result;
  31.     }
  32.  
  33.     SDL_UpdateTexture(buffer_texture, 0, buffer->pixels, buffer->width * 4);
  34.  
  35.     return result_some(buffer_texture);
  36. }
  37.  
  38. void game_window_loop(GameWindow *game_window, Buffer *buffer)
  39. {
  40.     int running = 1;
  41.     while(running)
  42.     {
  43.         while(SDL_PollEvent(&game_window->event) > 0)
  44.         {
  45.             switch(game_window->event.type)
  46.             {
  47.                 case SDL_QUIT:
  48.                     running = 0;
  49.                     break;
  50.             }
  51.         }
  52.  
  53.         SDL_RenderClear(game_window->renderer);
  54.  
  55.         clear_screen(buffer, color_from_rgba(110, 120, 100, 255));
  56.  
  57.         draw_rect_in_pixels(buffer, 10, 10, 20, 20, color_from_rgba(255, 0, 0, 255));
  58.         Result screen_texture_result = buffer_to_texture(game_window->renderer, buffer);
  59.         if(is_error(screen_texture_result))
  60.         {
  61.             printf("Failed to render!\nError: %s\n", screen_texture_result.error);
  62.             running = 0;
  63.         }
  64.  
  65.         SDL_Texture *screen_texture = screen_texture_result.value;
  66.  
  67.         SDL_RenderCopy(game_window->renderer, screen_texture, 0, 0);
  68.         SDL_RenderPresent(game_window->renderer);
  69.  
  70.         SDL_DestroyTexture(screen_texture);
  71.     }
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top