Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <SDL.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. const int SCREEN_WIDTH = 640;
  7. const int SCREEN_HEIGHT = 480;
  8.  
  9.  
  10.  
  11. // returns false if initializing SDL fails or if creating windows fails
  12. bool init(SDL_Window* p_gWindow, SDL_Surface* p_gScreenSurface)
  13. {
  14.     //Initialization flag
  15.     bool success = true;
  16.  
  17.     //Initialize SDL
  18.     //checks if SDL_Init returns -1
  19.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  20.     {
  21.         printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  22.         success = false;
  23.     }
  24.     else
  25.     {
  26.         //Create window
  27.         p_gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  28.             SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  29.         //checks if SDL_CreateWindow returned null meaning that they couldn't make a window
  30.         if (p_gWindow == NULL)
  31.         {
  32.             printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
  33.             success = false;
  34.         }
  35.         else
  36.         {
  37.             //Get window surface
  38.             p_gScreenSurface = SDL_GetWindowSurface(p_gWindow);
  39.         }
  40.     }
  41.  
  42.     return success;
  43. }
  44.  
  45. // return false if SDL_LoadBMP FAILED. Else it will return true
  46. bool loadMedia(SDL_Surface* gImage)
  47. {
  48.     //Loading success flag
  49.     bool success = true;
  50.  
  51.  
  52.     //Load splash image
  53.     gImage = SDL_LoadBMP("background.bmp");
  54.     if (gImage == NULL)
  55.     {
  56.         printf("Unable to load image! SDL Error: %s\n", SDL_GetError());
  57.         success = false;
  58.     }
  59.  
  60.     return success;
  61. }
  62.  
  63. void close(SDL_Surface* p_gimage, SDL_Window* p_gWindow)
  64. {
  65.     //Deallocate surface
  66.     SDL_FreeSurface(p_gimage);
  67.     p_gimage = NULL;
  68.  
  69.     //Destroy window
  70.     SDL_DestroyWindow(p_gWindow);
  71.     p_gWindow = NULL;
  72.  
  73.     //Quit SDL subsystems
  74.     SDL_Quit();
  75. }
  76.  
  77. //main
  78. int main(int argc, char* args[])
  79. {
  80.     //The window we'll be rendering to
  81.     SDL_Window* gWindow = NULL;
  82.  
  83.     //The surface contained by the window
  84.     SDL_Surface* gScreenSurface = NULL;
  85.  
  86.     //The image we will load and show on the screen
  87.     SDL_Surface* gImage = NULL;
  88.  
  89.  
  90.  
  91.     //Start up SDL and create window
  92.     if (!init(gWindow, gScreenSurface))
  93.     {
  94.         printf("Failed to initialize!\n");
  95.     }
  96.     else
  97.     {
  98.         //Load media
  99.         if (!loadMedia(gImage))
  100.         {
  101.             printf("Failed to load media!\n");
  102.         }
  103.         else
  104.         {
  105.             //Main loop flag
  106.             bool quit = false;
  107.  
  108.             //Event handler
  109.             SDL_Event e;
  110.  
  111.             while (!quit) {
  112.                 while (SDL_PollEvent(&e) != 0)
  113.                 {
  114.                     if (e.type == SDL_QUIT)
  115.                     {
  116.                         quit = true;
  117.                     }
  118.                 }
  119.  
  120.                 //Apply the image
  121.                 SDL_BlitSurface(gImage, NULL, gScreenSurface, NULL);
  122.  
  123.                 //Update the surface
  124.                 SDL_UpdateWindowSurface(gWindow);
  125.             }
  126.         }
  127.     }
  128.  
  129.     //Free resources and close SDL
  130.     close(gImage, gWindow);
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement