daily pastebin goal
95%
SHARE
TWEET

Untitled

a guest May 16th, 2016 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SDL/SDL.h>
  2. #include <stdio.h>
  3.  
  4. int main(int argc, char* args[])
  5. {
  6.     SDL_Window* window = NULL;
  7.  
  8.     SDL_Surface* background_surface = NULL;
  9.     SDL_Texture* background_texture = NULL;
  10.  
  11.     SDL_Surface* button_surface = NULL;
  12.     SDL_Texture* button_texture = NULL;
  13.  
  14.     SDL_Surface* image_3 = NULL;
  15.     SDL_Texture* image_3_texture = NULL;
  16.  
  17.     // need a renderer when using textures
  18.     SDL_Renderer* renderer = NULL;
  19.  
  20.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  21.     {
  22.         printf("SDL did not init: %s\n", SDL_GetError());
  23.         return 1;
  24.     }
  25.  
  26.     window = SDL_CreateWindow("SDL example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
  27.     if (window == NULL)
  28.     {
  29.         printf("Window could not be opened: %s\n", SDL_GetError());
  30.         return 1;
  31.     }
  32.  
  33.     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  34.     if (renderer == NULL)
  35.     {
  36.         printf("Failed to create renderer: %s\n", SDL_GetError());
  37.         return 1;
  38.     }
  39.  
  40.     background_surface = SDL_LoadBMP("./background.bmp");
  41.     background_texture = SDL_CreateTextureFromSurface(renderer, background_surface);
  42.     SDL_FreeSurface(background_surface);
  43.     background_surface = NULL;
  44.  
  45.     SDL_Rect background_texture_rect;
  46.     background_texture_rect.x = 0;
  47.     background_texture_rect.y = 0;
  48.     background_texture_rect.w = 800;
  49.     background_texture_rect.h = 600;
  50.  
  51.     button_surface = SDL_LoadBMP("./button.bmp");
  52.     button_texture = SDL_CreateTextureFromSurface(renderer, button_surface);
  53.     SDL_FreeSurface(button_surface);
  54.     button_surface = NULL;
  55.  
  56.     SDL_Rect button_texture_rect;
  57.     button_texture_rect.x = 0;
  58.     button_texture_rect.y = 0;
  59.     button_texture_rect.w = 200;
  60.     button_texture_rect.h = 100;
  61.  
  62.     image_3 = SDL_LoadBMP("./image3.bmp");
  63.     image_3_texture = SDL_CreateTextureFromSurface(renderer, image_3);
  64.     SDL_FreeSurface(image_3);
  65.     image_3 = NULL;
  66.  
  67.     SDL_Rect image_3_texture_rect;
  68.     image_3_texture_rect.x = 200;
  69.     image_3_texture_rect.y = 200;
  70.     image_3_texture_rect.w = 188;
  71.     image_3_texture_rect.h = 155;
  72.  
  73.     int running = 1;
  74.     while (running)
  75.     {
  76.         SDL_Event sevent;
  77.         while (SDL_PollEvent(&sevent))
  78.         {
  79.             switch (sevent.type)
  80.             {
  81.             case SDL_KEYDOWN:
  82.             {
  83.                 if (sevent.key.keysym.sym == SDLK_ESCAPE)
  84.                     running = 0;
  85.                 break;
  86.             }
  87.             case SDL_QUIT:
  88.             {
  89.                 running = 0;
  90.                 break;
  91.             }
  92.             }
  93.         }
  94.  
  95.         SDL_RenderClear(renderer);
  96.  
  97.         SDL_RenderCopy(renderer, background_texture, NULL, &background_texture_rect);
  98.         SDL_RenderCopy(renderer, button_texture, NULL, &button_texture_rect);
  99.         SDL_RenderCopy(renderer, image_3_texture, NULL, &image_3_texture_rect);
  100.  
  101.         SDL_RenderPresent(renderer);
  102.  
  103.         // dont make loops for games/apps like this there are better ways: http://gafferongames.com/game-physics/fix-your-timestep/
  104.         SDL_Delay(17);
  105.     }
  106.  
  107.     // clean up
  108.     SDL_DestroyTexture(background_texture);
  109.     background_texture = NULL;
  110.  
  111.     SDL_DestroyTexture(button_texture);
  112.     button_texture = NULL;
  113.  
  114.     SDL_DestroyTexture(image_3_texture);
  115.     image_3_texture = NULL;
  116.  
  117.     SDL_DestroyRenderer(renderer);
  118.     renderer = NULL;
  119.  
  120.     SDL_DestroyWindow(window);
  121.     window = NULL;
  122.  
  123.     SDL_Quit();
  124.  
  125.     return 0;
  126. }
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