Last active
June 21, 2017 20:27
-
-
Save psqq/3c855d1ddc69cf019a51ddfeaee5ff95 to your computer and use it in GitHub Desktop.
Minimal SDL2 app example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <exception> | |
#include <SDL.h> | |
using namespace std; | |
class Exception : public exception { | |
string msg; | |
public: | |
Exception(string amsg) : msg(amsg) {} | |
const char* what() const noexcept { | |
return msg.c_str(); | |
} | |
}; | |
class App { | |
const int DEFAULT_WINDOW_WIDTH = 640; | |
const int DEFAULT_WINDOW_HEIGHT = 480; | |
SDL_Window *window; | |
SDL_Event event; | |
bool running = true; | |
public: | |
App(); | |
~App(); | |
void init(); | |
void run(); | |
void finish(); | |
}; | |
App::App() {} | |
App::~App() { | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
} | |
void App::init() { | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
throw Exception(SDL_GetError()); | |
} | |
window = SDL_CreateWindow( | |
"Simple Editor", | |
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | |
DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT, | |
SDL_WINDOW_SHOWN | |
); | |
if (window == NULL) { | |
throw Exception(SDL_GetError()); | |
} | |
} | |
void App::finish() { | |
running = false; | |
} | |
void App::run() { | |
while (running) { | |
while (SDL_PollEvent(&event)) { | |
if (event.type == SDL_QUIT) { | |
finish(); | |
} | |
} | |
} | |
} | |
int main() { | |
App app; | |
try { | |
app.init(); | |
app.run(); | |
} catch(const exception &e) { | |
cout << e.what() << endl; | |
return 1; | |
} catch(...) { | |
cout << "Unknow error." << endl; | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment