Advertisement
Combreal

SimplestSDL2Sample.cpp

May 4th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int main(int arc, char ** argv)
  7. {
  8.     SDL_Window* pWindow = NULL;
  9.     SDL_Event event;
  10.     SDL_Renderer *pRenderer = NULL;
  11.     SDL_Texture* pTexture = NULL;
  12.     SDL_Surface* pSprite = NULL;
  13.     SDL_Rect dest;
  14.     bool cont = true;
  15.     int CurrentTime = 0, seconds = 0, PassedTime = 0;
  16.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  17.     {
  18.         cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
  19.     }
  20.     else
  21.     {
  22.         pWindow = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
  23.     }
  24.     pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);
  25.     if (pRenderer)
  26.     {  
  27.         pSprite = SDL_LoadBMP("resource/deck/grid.bmp");
  28.         if (pSprite)
  29.         {
  30.             pTexture = SDL_CreateTextureFromSurface(pRenderer, pSprite);
  31.             if (pTexture)
  32.             {
  33.                 dest = {0, 0, 800, 600}; //{800, 600, pSprite->w, pSprite->h }; //{ 640 / 2 - pSprite->w / 2,480 / 2 - pSprite->h / 2, pSprite->w, pSprite->h };
  34.                 SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
  35.                 SDL_RenderPresent(pRenderer);
  36.             }
  37.             else
  38.             {
  39.                 cout << "Couldn't display image : " << SDL_GetError() << endl;
  40.             }
  41.         }
  42.         else
  43.         {
  44.             cout << "Sprite loading error : " << SDL_GetError() <<endl;
  45.         }
  46.     }
  47.     else
  48.     {
  49.         cout << "Couldn't create renderer : " << SDL_GetError() << endl;
  50.     }
  51.     while (cont != 0)
  52.     {
  53.         while (SDL_PollEvent(&event))
  54.         {
  55.             //SDL_FlushEvent(SDL_QUIT);
  56.             switch (event.type)
  57.             {
  58.             case SDL_KEYDOWN:
  59.                 if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
  60.                 {
  61.                     cout << "Quit" << endl;
  62.                     cont = 0;
  63.                 }
  64.                 break;
  65.             //case SDL_WINDOWEVENT:
  66.             case SDL_QUIT:
  67.                 cont = false;
  68.                 break;
  69.             default:
  70.                 break;
  71.             }
  72.         }
  73.         SDL_RenderClear(pRenderer);
  74.         SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
  75.         SDL_RenderPresent(pRenderer);
  76.         CurrentTime = SDL_GetTicks();
  77.         if (CurrentTime - PassedTime > 1000)
  78.         {
  79.             seconds++;
  80.             cout << seconds << endl;
  81.             PassedTime = CurrentTime;
  82.         }
  83.     }
  84.     SDL_DestroyTexture(pTexture);
  85.     SDL_DestroyRenderer(pRenderer);
  86.     SDL_FreeSurface(pSprite);
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement