Advertisement
czaffik

sdl simple engine

Nov 28th, 2017
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. /// Plik main.cpp:
  2. #include "engine.h"
  3.  
  4. int main(int argc, char* args[])
  5. {
  6.     try
  7.     {
  8.         Engine* e = new Engine(800, 600);
  9.  
  10.         e->init();
  11.         e->mainLoop();
  12.         e->close();
  13.         delete e;
  14.     }
  15.     catch(string &message)
  16.     {
  17.         cout << message << endl;
  18.     }
  19.     return 0;
  20. }
  21.  
  22. /// Plik engine.h:
  23. #ifndef ENGINE_H
  24. #define ENGINE_H
  25.  
  26. #include <SDL2/SDL.h>
  27. #include <SDL2/SDL_image.h>
  28. #include <vector>
  29. using namespace std;
  30.  
  31. class Engine
  32. {
  33.     public:
  34.         Engine(int _screenWidth, int _screenHeight, int _delay = 10);
  35.         virtual ~Engine();
  36.  
  37.         void init();
  38.         void draw();
  39.         void animate(float dt);
  40.         void mainLoop();
  41.         void close();
  42.  
  43.     protected:
  44.         SDL_Window* window;
  45.         SDL_Renderer* renderer;
  46.         SDL_TimerID timerID;
  47.  
  48.         const int screenWidth;
  49.         const int screenHeight;
  50.         int delay;
  51.  
  52.         float deltaTime;
  53.  
  54.     private:
  55. };
  56.  
  57. Uint32 callback(Uint32 interval, void *param);
  58.  
  59. #endif // ENGINE_H
  60.  
  61. /// plik engine.cpp:
  62. #include <engine.h>
  63. #include <sstream>
  64. #include <math.h>
  65.  
  66. Engine::Engine(int _screenWidth, int _screenHeight, int _delay)
  67.     : screenWidth(_screenWidth), screenHeight(_screenHeight), delay(_delay)
  68. {
  69.     window = NULL;
  70.     renderer = NULL;
  71.     timerID = SDL_AddTimer(delay, callback, (void*)"");
  72.  
  73.     deltaTime = (float)delay*0.0001;
  74.     deltaTime = delay*0.0001;
  75. }
  76.  
  77. Engine::~Engine()
  78. {
  79.  
  80. }
  81.  
  82. void Engine::init()
  83. {
  84.     stringstream err;
  85.  
  86.     if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
  87.     {
  88.         err << "Engine::init: Blad przy inicjalizacji SDL: " << SDL_GetError();
  89.         throw err.str();
  90.     }
  91.     if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
  92.     {
  93.         cout << "Engine::init: Filtrowanie liniowe nie zostalo wlaczone" << endl;
  94.     }
  95.  
  96.     window = SDL_CreateWindow( "Kolizje", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth,
  97.                                 screenHeight, SDL_WINDOW_SHOWN );
  98.     if( window == NULL )
  99.     {
  100.         err << "Engine::init: Nie mozna utworzyc okna " << SDL_GetError();
  101.         throw err.str();
  102.     }
  103.     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  104.     if ( renderer == NULL )
  105.     {
  106.         err << "Engine::init: Renderer nie zostal utworzony: " << SDL_GetError();
  107.         throw err.str();
  108.     }
  109.     int imgFlags = IMG_INIT_PNG;
  110.     if (!(IMG_Init(imgFlags) & imgFlags))
  111.     {
  112.         err << "Engine::init: SDL_image nie zostal zainicjowany: " << IMG_GetError();
  113.         throw err.str();
  114.     }
  115.  
  116.     if (TTF_Init() == -1)
  117.     {
  118.         err << "Engine::init: SDL_TTF nie zostal zainicjowany: " << TTF_GetError();
  119.         throw err.str();
  120.     }
  121. }
  122.  
  123. void Engine::draw()
  124. {
  125.     SDL_RenderClear(renderer);
  126.     SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  127.  
  128.     SDL_RenderPresent(renderer);
  129. }
  130.  
  131. void Engine::animate(float dt)
  132. {
  133.    
  134. }
  135.  
  136. void Engine::mainLoop()
  137. {
  138.     bool quit = false;
  139.     SDL_Event event;
  140.  
  141.     while (!quit)
  142.     {
  143.         while (SDL_PollEvent(&event) != 0)
  144.         {
  145.             if (event.type == SDL_QUIT)
  146.             {
  147.                 quit = true;
  148.             }
  149.             if (event.type == SDL_USEREVENT)
  150.             {
  151.                 animate(deltaTime);
  152.             }
  153.             if (event.type == SDL_KEYDOWN)
  154.             {
  155.                 switch(event.key.keysym.sym)
  156.                 {
  157.                     case SDLK_ESCAPE:
  158.                         quit = true;
  159.                         break;
  160.                 }
  161.             }
  162.         }
  163.  
  164.         draw();
  165.     }
  166. }
  167.  
  168. void Engine::close()
  169. {
  170.     SDL_DestroyRenderer(renderer);
  171.     SDL_DestroyWindow(window);
  172.     window = NULL;
  173.     renderer = NULL;
  174.  
  175.     SDL_RemoveTimer(timerID);
  176.  
  177.     TTF_CloseFont(fps.font);
  178.  
  179.     TTF_Quit();
  180.     IMG_Quit();
  181.     SDL_Quit();
  182. }
  183.  
  184. Uint32 callback(Uint32 interval, void *param)
  185. {
  186.     SDL_Event event;
  187.     SDL_UserEvent userevent;
  188.  
  189.     userevent.type = SDL_USEREVENT;
  190.     userevent.code = 0;
  191.     userevent.data1 = NULL;
  192.     userevent.data2 = NULL;
  193.  
  194.     event.type = SDL_USEREVENT;
  195.     event.user = userevent;
  196.  
  197.     SDL_PushEvent(&event);
  198.     return(interval);
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement