1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#include <iostream>
#include "SDL/SDL.h"
class Window{
public:
int width, height, bpp;
Uint32 flags;
SDL_Surface* _window;
std::string title;
Window(){
width = 0;
height = 0;
bpp = 0;
_window = NULL;
title = " ";
}
void set_title(std::string title){
this->title = title;
SDL_WM_SetCaption(title.c_str(), NULL);
}
bool init(int width, int height, int bpp, Uint32 flags){
this->width = width;
this->height = height;
this->bpp = bpp;
this->flags = flags;
if ((_window = SDL_SetVideoMode(width, height, bpp, flags)) == NULL){
return false;
}
return true;
}
bool resize(int width, int height){
this->width = width;
this->height = height;
if ((_window = SDL_SetVideoMode(width, height, bpp, flags)) == NULL){
return false;
}
return true;
}
};
class Input{
public:
Input(){
}
void on_event(SDL_Event* event){
switch(event->type){
case SDL_KEYDOWN:
key_press(event->key.keysym.sym,
event->key.keysym.mod,
event->key.keysym.unicode);
break;
case SDL_KEYUP:
key_release(event->key.keysym.sym,
event->key.keysym.mod,
event->key.keysym.unicode);
break;
case SDL_MOUSEMOTION:
mouse_motion(event->motion.x, event->motion.y,
event->motion.xrel, event->motion.yrel,
(event->motion.state & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0,
(event->motion.state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0,
(event->motion.state & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0);
break;
case SDL_MOUSEBUTTONDOWN:
mouse_press(event->button.button, event->button.x, event->button.y);
break;
case SDL_MOUSEBUTTONUP:
mouse_release(event->button.button, event->button.x, event->button.y);
break;
case SDL_ACTIVEEVENT:
focus_change(event->active.state, event->active.gain);
break;
case SDL_VIDEORESIZE:
resize(event->resize.w, event->resize.h);
break;
case SDL_QUIT:
exiting();
break;
}
}
virtual void key_press(SDLKey key, SDLMod mod, Uint16 unicode){}
virtual void key_release(SDLKey key, SDLMod mod, Uint16 unicode){}
virtual void mouse_motion(int x, int y, int relx, int rely, bool left, bool middle, bool right){}
virtual void mouse_press(Uint8 button, int x, int y){}
virtual void mouse_release(Uint8 button, int x, int y){}
virtual void focus_change(Uint8 state, Uint8 gain){}
virtual void resize(int width, int height){}
virtual void exiting(){}
};
class Application : public Input{
public:
bool done;
Window window;
Application(){
done = false;
}
virtual ~Application(){
}
bool init(){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
return false;
}
if (window.init(800,600, 32, SDL_SWSURFACE) != true){
return false;
}
window.set_title("SDL window");
return true;
}
void run(){
SDL_Event event;
while( ! done){
input(&event);
update();
render();
}
}
void input(SDL_Event* event){
while(SDL_PollEvent(event)){
on_event(event);
}
}
void update(){
SDL_Delay(50);
}
void render(){
}
void quit(){
SDL_Quit();
}
void resize(int width, int height){
if ( ! window.resize(width, height)){
exiting();
}
}
void exiting(){
done = true;
}
};
int main(){
Application* app;
app = new Application();
if ( ! app->init())
return -1;
app->run();
app->quit();
delete app;
app = NULL;
}
|