Created
April 30, 2018 16:49
-
-
Save bit-hack/f70ae7b6718a52e6329629ab7a2aa8a8 to your computer and use it in GitHub Desktop.
A very simple SDL app framework
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 <cassert> | |
#include <cstdint> | |
#define _SDL_main_h | |
#include <SDL/SDL.h> | |
struct app_t { | |
app_t() : _screen(nullptr), _active(false) {} | |
virtual bool init(uint32_t w, uint32_t h) { | |
const uint32_t mode = SDL_INIT_VIDEO; | |
if (SDL_WasInit(mode)) { | |
return false; | |
} | |
if (SDL_Init(mode)) { | |
return false; | |
} | |
assert(!_screen); | |
_screen = SDL_SetVideoMode(w, h, 32, 0); | |
if (!_screen) { | |
return false; | |
} | |
_active = true; | |
return true; | |
} | |
virtual void on_event(const SDL_Event &event) { | |
switch (event.type) { | |
case SDL_QUIT: | |
_active = false; | |
break; | |
} | |
} | |
virtual void tick() { | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) { | |
on_event(event); | |
} | |
} | |
bool active() const { return _active; } | |
protected: | |
SDL_Surface *_screen; | |
bool _active; | |
}; | |
struct my_app_t : public app_t { | |
virtual void tick() { | |
app_t::tick(); | |
SDL_FillRect(_screen, nullptr, 0x202020); | |
SDL_Delay(1); | |
SDL_Flip(_screen); | |
} | |
}; | |
int main(int argc, const char **args) { | |
my_app_t app; | |
if (!app.init(320, 240)) { | |
return 1; | |
} | |
while (app.active()) { | |
app.tick(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment