Advertisement
Guest User

Untitled

a guest
Apr 27th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. /*This source code copyrighted by Lazy Foo' Productions (2004-2019)
  2. and may not be redistributed without written permission.*/
  3.  
  4. //Using SDL and standard IO
  5. #include <SDL2/SDL.h>
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8.  
  9. //Screen dimension constants
  10. const int SCREEN_WIDTH = 640;
  11. const int SCREEN_HEIGHT = 480;
  12.  
  13. //Starts up SDL and creates window
  14. bool init();
  15.  
  16. //Loads media
  17. bool loadMedia();
  18.  
  19. //Frees media and shuts down SDL
  20. void close();
  21.  
  22. //The window we'll be rendering to
  23. SDL_Window* gWindow = NULL;
  24.    
  25. //The surface contained by the window
  26. SDL_Surface* gScreenSurface = NULL;
  27.  
  28. //The image we will load and show on the screen
  29. SDL_Surface* gXOut = NULL;
  30.  
  31. bool init()
  32. {
  33.     //Initialization flag
  34.     bool success = true;
  35.  
  36.     //Initialize SDL
  37.     if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  38.     {
  39.         printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
  40.         success = false;
  41.     }
  42.     else
  43.     {
  44.         //Create window
  45.         gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  46.         if( gWindow == NULL )
  47.         {
  48.             printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
  49.             success = false;
  50.         }
  51.         else
  52.         {
  53.             //Get window surface
  54.             gScreenSurface = SDL_GetWindowSurface( gWindow );
  55.         }
  56.     }
  57.  
  58.     return success;
  59. }
  60.  
  61. bool loadMedia()
  62. {
  63.     //Loading success flag
  64.     bool success = true;
  65.  
  66.     //Load splash image
  67.     gXOut = SDL_LoadBMP( "x.bmp" );
  68.     if( gXOut == NULL )
  69.     {
  70.         printf( "Unable to load image %s! SDL Error: %s\n", "03_event_driven_programming/x.bmp", SDL_GetError() );
  71.         success = false;
  72.     }
  73.  
  74.     return success;
  75. }
  76.  
  77. void close()
  78. {
  79.     //Deallocate surface
  80.     SDL_FreeSurface( gXOut );
  81.     gXOut = NULL;
  82.  
  83.     //Destroy window
  84.     SDL_DestroyWindow( gWindow );
  85.     gWindow = NULL;
  86.  
  87.     //Quit SDL subsystems
  88.     SDL_Quit();
  89. }
  90.  
  91. int main( int argc, char* args[] )
  92. {
  93.     //Start up SDL and create window
  94.     if( !init() )
  95.     {
  96.         printf( "Failed to initialize!\n" );
  97.     }
  98.     else
  99.     {
  100.         //Load media
  101.         if( !loadMedia() )
  102.         {
  103.             printf( "Failed to load media!\n" );
  104.         }
  105.         else
  106.         {          
  107.             //Main loop flag
  108.             bool quit = false;
  109.  
  110.             //Event handler
  111.             SDL_Event e;
  112.  
  113.             //While application is running
  114.             while( !quit )
  115.             {
  116.                 //Handle events on queue
  117.                 while( SDL_PollEvent( &e ) != 0 )
  118.                 {
  119.                     //User requests quit
  120.                     if( e.type == SDL_QUIT )
  121.                     {
  122.                         quit = true;
  123.                     }
  124.                 }
  125.  
  126.                 //Apply the image
  127.                 SDL_BlitSurface( gXOut, NULL, gScreenSurface, NULL );
  128.            
  129.                 //Update the surface
  130.                 SDL_UpdateWindowSurface( gWindow );
  131.             }
  132.         }
  133.     }
  134.  
  135.     //Free resources and close SDL
  136.     close();
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement