Advertisement
PanZefir

SDL_Keypresses

Feb 18th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int SCREEN_WIDTH = 800;
  7. const int SCREEN_HEIGHT = 600;
  8.  
  9. enum KeyPressSurfaces
  10. {
  11.     Up,
  12.     Down,
  13.     Left,
  14.     Right,
  15.     Default,
  16.     Total
  17. };
  18.  
  19. SDL_Window *gWindow = nullptr;
  20. SDL_Surface *gWindowSurface = nullptr;
  21. SDL_Surface* gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Total)];
  22. SDL_Surface *gCurrentSurface = nullptr;
  23.  
  24.  
  25. bool init();
  26. SDL_Surface *loadSurface(string path);
  27. bool loadMedia();
  28. bool render();
  29. void keyboardEvents(SDL_Event &e);
  30. void close();
  31.  
  32. int main(int, char **)
  33. {
  34.     bool success = true;
  35.  
  36.     success = init();
  37.  
  38.     if (true == success)
  39.     {
  40.         success = loadMedia();
  41.     }
  42.  
  43.     if (true == success)
  44.     {
  45.         while (true == render());
  46.     }
  47.  
  48.     close();
  49.  
  50.     return 0;
  51. }
  52.  
  53. bool init()
  54. {
  55.     bool success = true;
  56.  
  57.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  58.     {
  59.         cout << "SDL init failed. " << SDL_GetError() << endl;
  60.         success = false;
  61.     }
  62.  
  63.     if (true == success) {
  64.         gWindow = SDL_CreateWindow(u8"Klawiszki kurwa", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  65.         if (gWindow == nullptr)
  66.         {
  67.             cout << "Window creation failed. " << SDL_GetError() << endl;
  68.             success = false;
  69.         }
  70.     }
  71.  
  72.     if (true == success) {
  73.         gWindowSurface = SDL_GetWindowSurface(gWindow);
  74.     }
  75.  
  76.     return success;
  77. }
  78.  
  79. SDL_Surface *loadSurface(string path)
  80. {
  81.     SDL_Surface *loadedSurface = SDL_LoadBMP(path.c_str());
  82.     if (nullptr == loadedSurface) {
  83.         cout << "Could not load file: " << path.c_str() << " SDL error: " << SDL_GetError() << endl;
  84.     }
  85.  
  86.     return loadedSurface;
  87. }
  88.  
  89. bool loadMedia()
  90. {
  91.     bool success = true;
  92.     string pliki[]{ "up.bmp","down.bmp","left.bmp","right.bmp","default.bmp" };
  93.  
  94.     for (int i = 0; i < 5; i++) {
  95.         success = gKeyPressSurfaces[i] = loadSurface(pliki[i]);
  96.         if (false == success) {
  97.             break;
  98.         }
  99.     }
  100.  
  101.     if (true == success) {
  102.         gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Default)];
  103.     }
  104.  
  105.     return success;
  106. }
  107.  
  108. bool render()
  109. {
  110.     bool quit = false;
  111.     SDL_Event e;
  112.  
  113.     while (0 != SDL_PollEvent(&e))
  114.     {
  115.         if (SDL_QUIT == e.type)
  116.         {
  117.             quit = true;
  118.         }
  119.         else {
  120.             keyboardEvents(e);
  121.         }
  122.     }
  123.  
  124.     SDL_Rect rect { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
  125.     SDL_BlitScaled(gCurrentSurface, nullptr, gWindowSurface, &rect);
  126.     SDL_UpdateWindowSurface(gWindow);
  127.  
  128.     return true == quit ? false : true;
  129. }
  130.  
  131. void keyboardEvents(SDL_Event &e)
  132. {
  133.     switch (e.key.keysym.sym)
  134.     {
  135.     case SDLK_UP:
  136.         gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Up)];
  137.         break;
  138.  
  139.     case SDLK_DOWN:
  140.         gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Down)];
  141.         break;
  142.  
  143.     case SDLK_LEFT:
  144.         gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Left)];
  145.         break;
  146.  
  147.     case SDLK_RIGHT:
  148.         gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Right)];
  149.         break;
  150.  
  151.     default:
  152.         gCurrentSurface = gKeyPressSurfaces[static_cast<int>(KeyPressSurfaces::Default)];
  153.         break;
  154.     }
  155. }
  156.  
  157. void close()
  158. {
  159.     gCurrentSurface = nullptr;
  160.     for (int i = 0; i < 5; i++) {
  161.         SDL_FreeSurface(gKeyPressSurfaces[i]);
  162.         gKeyPressSurfaces[i] = nullptr;
  163.     }
  164.  
  165.     SDL_DestroyWindow(gWindow);
  166.     gWindow = nullptr;
  167.  
  168.     SDL_Quit();
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement