Advertisement
Guest User

SDL_help

a guest
Mar 4th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <SDL2/SDL.h>
  3. #include <SDL2/SDL_image.h>
  4.  
  5. int main() {
  6.  
  7.     // attempt to initialize graphics and timer system
  8.     if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
  9.     {
  10.         printf("error initializing SDL: %s\n", SDL_GetError());
  11.         return 1;
  12.     }
  13.  
  14.     SDL_Window* win = SDL_CreateWindow("wasd",
  15.                                        SDL_WINDOWPOS_CENTERED,
  16.                                        SDL_WINDOWPOS_CENTERED,
  17.                                        640, 480, 0);
  18.     if (!win)
  19.     {
  20.         printf("error creating window: %s\n", SDL_GetError());
  21.         SDL_Quit();
  22.         return 1;
  23.     }
  24.  
  25.     // create a renderer, which sets up the graphics hardware
  26.     Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
  27.     SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
  28.     if (!rend) {
  29.       printf("error creating renderer: %s\n", SDL_GetError());
  30.       SDL_DestroyWindow(win);
  31.       SDL_Quit();
  32.       return 1;
  33.     }
  34.  
  35.     // load the image into memory using SDL_image library function
  36.     SDL_Surface* surface = IMG_Load(
  37.         "art/hello.png");
  38.     if (!surface) {
  39.         printf("error creating surface\n");
  40.         SDL_DestroyRenderer(rend);
  41.         SDL_DestroyWindow(win);
  42.         SDL_Quit();
  43.         return 1;
  44.     }
  45.  
  46.     // load the image data into the graphics hardware's memory
  47.     SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, surface);
  48.     SDL_FreeSurface(surface);
  49.     if (!tex) {
  50.         printf("error creating texture: %s\n", SDL_GetError());
  51.         SDL_DestroyRenderer(rend);
  52.         SDL_DestroyWindow(win);
  53.         SDL_Quit();
  54.         return 1;
  55.     }
  56.  
  57.     // clear the window
  58.     SDL_RenderClear(rend);
  59.    
  60.     // draw the image to the window
  61.     SDL_RenderCopy(rend, tex, NULL, NULL);
  62.     SDL_RenderPresent(rend);
  63.  
  64.     // wait a few seconds
  65.     SDL_Delay(5000);
  66.    
  67.     // clean up resources before exiting
  68.     SDL_DestroyTexture(tex);
  69.     SDL_DestroyRenderer(rend);
  70.     SDL_DestroyWindow(win);
  71.     SDL_Quit();
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement