Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Plik main.cpp:
- #include "engine.h"
- int main(int argc, char* args[])
- {
- try
- {
- Engine* e = new Engine(800, 600);
- e->init();
- e->mainLoop();
- e->close();
- delete e;
- }
- catch(string &message)
- {
- cout << message << endl;
- }
- return 0;
- }
- /// Plik engine.h:
- #ifndef ENGINE_H
- #define ENGINE_H
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_image.h>
- #include <vector>
- using namespace std;
- class Engine
- {
- public:
- Engine(int _screenWidth, int _screenHeight, int _delay = 10);
- virtual ~Engine();
- void init();
- void draw();
- void animate(float dt);
- void mainLoop();
- void close();
- protected:
- SDL_Window* window;
- SDL_Renderer* renderer;
- SDL_TimerID timerID;
- const int screenWidth;
- const int screenHeight;
- int delay;
- float deltaTime;
- private:
- };
- Uint32 callback(Uint32 interval, void *param);
- #endif // ENGINE_H
- /// plik engine.cpp:
- #include <engine.h>
- #include <sstream>
- #include <math.h>
- Engine::Engine(int _screenWidth, int _screenHeight, int _delay)
- : screenWidth(_screenWidth), screenHeight(_screenHeight), delay(_delay)
- {
- window = NULL;
- renderer = NULL;
- timerID = SDL_AddTimer(delay, callback, (void*)"");
- deltaTime = (float)delay*0.0001;
- deltaTime = delay*0.0001;
- }
- Engine::~Engine()
- {
- }
- void Engine::init()
- {
- stringstream err;
- if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
- {
- err << "Engine::init: Blad przy inicjalizacji SDL: " << SDL_GetError();
- throw err.str();
- }
- if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
- {
- cout << "Engine::init: Filtrowanie liniowe nie zostalo wlaczone" << endl;
- }
- window = SDL_CreateWindow( "Kolizje", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth,
- screenHeight, SDL_WINDOW_SHOWN );
- if( window == NULL )
- {
- err << "Engine::init: Nie mozna utworzyc okna " << SDL_GetError();
- throw err.str();
- }
- renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- if ( renderer == NULL )
- {
- err << "Engine::init: Renderer nie zostal utworzony: " << SDL_GetError();
- throw err.str();
- }
- int imgFlags = IMG_INIT_PNG;
- if (!(IMG_Init(imgFlags) & imgFlags))
- {
- err << "Engine::init: SDL_image nie zostal zainicjowany: " << IMG_GetError();
- throw err.str();
- }
- if (TTF_Init() == -1)
- {
- err << "Engine::init: SDL_TTF nie zostal zainicjowany: " << TTF_GetError();
- throw err.str();
- }
- }
- void Engine::draw()
- {
- SDL_RenderClear(renderer);
- SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
- SDL_RenderPresent(renderer);
- }
- void Engine::animate(float dt)
- {
- }
- void Engine::mainLoop()
- {
- bool quit = false;
- SDL_Event event;
- while (!quit)
- {
- while (SDL_PollEvent(&event) != 0)
- {
- if (event.type == SDL_QUIT)
- {
- quit = true;
- }
- if (event.type == SDL_USEREVENT)
- {
- animate(deltaTime);
- }
- if (event.type == SDL_KEYDOWN)
- {
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- quit = true;
- break;
- }
- }
- }
- draw();
- }
- }
- void Engine::close()
- {
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- window = NULL;
- renderer = NULL;
- SDL_RemoveTimer(timerID);
- TTF_CloseFont(fps.font);
- TTF_Quit();
- IMG_Quit();
- SDL_Quit();
- }
- Uint32 callback(Uint32 interval, void *param)
- {
- SDL_Event event;
- SDL_UserEvent userevent;
- userevent.type = SDL_USEREVENT;
- userevent.code = 0;
- userevent.data1 = NULL;
- userevent.data2 = NULL;
- event.type = SDL_USEREVENT;
- event.user = userevent;
- SDL_PushEvent(&event);
- return(interval);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement