Advertisement
Nattack

Untitled

Nov 23rd, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. // SDL_HelloWorld.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <SDL.h>
  7.  
  8. bool init();
  9. bool loadMedia();
  10. void close();
  11.  
  12. //The window to render to
  13. SDL_Window* gWindow = nullptr;
  14.  
  15. //The surface contained by the window
  16. SDL_Surface* gScreenSurface = nullptr;
  17.  
  18. //The image to load and show on screen
  19. SDL_Surface* gHelloWorld = nullptr;
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.  
  24.     if (!init())
  25.     {
  26.         std::cerr << "Failed to initialize.\n";
  27.     }
  28.     else
  29.     {
  30.         //Load media
  31.         if (!loadMedia())
  32.         {
  33.             std::cerr << "Failed to load media.\n";
  34.         }
  35.         else
  36.         {
  37.             SDL_BlitSurface(
  38.                 gHelloWorld,
  39.                 NULL,
  40.                 gScreenSurface,
  41.                 NULL
  42.             );
  43.  
  44.             // Update the surface
  45.             SDL_UpdateWindowSurface( gWindow );
  46.  
  47.             //Wait two seconds
  48.             SDL_Delay( 2000 );
  49.         }
  50.     }
  51.  
  52.     //Free resources and close SDL
  53.     close();
  54.  
  55.     return 0;
  56. }
  57.  
  58. bool init()
  59. {
  60.     bool success = true;
  61.  
  62.     // Initialize SDL
  63.     if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
  64.     {
  65.         std::cerr << "Could not initialize SDL. SDL Error: " << SDL_GetError() << "\n";
  66.         success = false;
  67.     }
  68.     else
  69.     {
  70.         gWindow = SDL_CreateWindow(
  71.             "SDL Tutorial",
  72.             SDL_WINDOWPOS_UNDEFINED,
  73.             SDL_WINDOWPOS_UNDEFINED,
  74.             640,
  75.             480,
  76.             SDL_WINDOW_SHOWN
  77.         );
  78.         if (gWindow == NULL)
  79.         {
  80.             std::cerr << "Window could not be created. SDL Error: " << SDL_GetError() << "\n";
  81.             success = false;
  82.         }
  83.         else
  84.         {
  85.             //Get window surface
  86.             gScreenSurface = SDL_GetWindowSurface(gWindow);
  87.         }
  88.     }
  89.  
  90.     return success;
  91. }
  92.  
  93. bool loadMedia()
  94. {
  95.     //Load success flag
  96.     bool success = true;
  97.  
  98.     //Load splash image
  99.     gHelloWorld = SDL_LoadBMP( "./res/imageToLoad.bmp" );
  100.     if (gHelloWorld == NULL)
  101.     {
  102.         std::cerr << "unable to load image ./res/imageToLoad.bmp: " << SDL_GetError() << "\n";
  103.         success = false;
  104.     }
  105.  
  106.     return success;
  107. }
  108.  
  109. void close()
  110. {
  111.     //deallocate surface
  112.     SDL_FreeSurface( gHelloWorld );
  113.     gHelloWorld = nullptr;
  114.  
  115.     //Destroy Window
  116.     SDL_DestroyWindow( gWindow );
  117.     gWindow = nullptr;
  118.  
  119.     //Quit SDL
  120.     SDL_Quit();
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement