commit 27c6f1e0b691e3d4cdb9af65fa038c40de723e96 Author: Anna Wiggins Date: Wed Jun 22 16:12:22 2011 -0400 Fundamental framework for an SDL application diff --git a/drawutils.cpp b/drawutils.cpp new file mode 100644 index 0000000..e1f3b30 --- /dev/null +++ b/drawutils.cpp @@ -0,0 +1,51 @@ +#include "drawutils.h" + +SDL_Surface* DrawUtils::load(string file) +{ + SDL_Surface* raw = NULL; + SDL_Surface* cooked = NULL; + + raw = SDL_LoadBMP(file.c_str()); + if (raw == NULL) return NULL; + + cooked = SDL_DisplayFormat(raw); + SDL_FreeSurface(raw); // don't consume raw surfaces + + return cooked; +} + + +bool DrawUtils::draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y) +{ + if (dest == NULL || drawable == NULL) return false; + + SDL_Rect dest_r; + + dest_r.x = x; + dest_r.y = y; + + SDL_BlitSurface(drawable, NULL, dest, &dest_r); + + return true; +} + + +bool DrawUtils::draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y, + int x2, int y2, int w, int h) +{ + SDL_Rect dest_r; + + dest_r.x = x; + dest_r.y = y; + + SDL_Rect src_r; + + src_r.x = x2; + src_r.y = y2; + src_r.w = w; + src_r.h = h; + + SDL_BlitSurface(drawable, &src_r, dest, &dest_r); + + return true; +} diff --git a/drawutils.h b/drawutils.h new file mode 100644 index 0000000..9114ea2 --- /dev/null +++ b/drawutils.h @@ -0,0 +1,21 @@ +/* A class with some handy surface handling statics */ + +#ifndef _DRAWUTILS_H_ +#define _DRAWUTILS_H_ + +#include +#include + +using std::string; + +class DrawUtils +{ + public: + DrawUtils() {} + static SDL_Surface* load(string file); + static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y); + static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y, + int x2, int y2, int w, int h); +}; + +#endif diff --git a/gamecore.cpp b/gamecore.cpp new file mode 100644 index 0000000..18966b0 --- /dev/null +++ b/gamecore.cpp @@ -0,0 +1,73 @@ +#include "gamecore.h" +#include "drawutils.h" + +#ifdef DEBUG +#include +#endif + +GameCore::GameCore() +{ + display = NULL; + is_running = true; +} + +int GameCore::execute() +{ + if (!init()) return 1; + + SDL_Event event; + + while (is_running) + { + while(SDL_PollEvent(&event)) + handle_event(&event); + iterate(); + render(); + } + + cleanup(); + + return 0; +} + + +bool GameCore::init() +{ + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false; + + display = SDL_SetVideoMode(800,600,32, SDL_HWSURFACE | SDL_DOUBLEBUF); + if (display == NULL) + { +#ifdef DEBUG + std::cerr << "GameCore::init(): error: Couldn't create main surface\n"; +#endif + return false; + } + + return true; +} + + +void GameCore::render() +{ + +} + + +void GameCore::iterate() +{ + +} + + +void GameCore::cleanup() +{ + SDL_FreeSurface(display); + SDL_Quit(); +} + + +void GameCore::on_exit() +{ + is_running = false; +} diff --git a/gamecore.h b/gamecore.h new file mode 100644 index 0000000..f396085 --- /dev/null +++ b/gamecore.h @@ -0,0 +1,38 @@ +/* This is the heart of the application. + This contains the basic game looping code, sets up event handlers, etc. + + All the hard work will eventually get farmed out to other objects, for now, + we're doing almost everything in here. + */ + + +#ifndef _GAME_CORE_H_ +#define _GAME_CORE_H_ + +#include +#include "mainevent.h" + +class GameCore : public MainEvent +{ + public: + GameCore(); + int execute(); + + protected: + // event handlers + void on_exit(); + + + private: + bool init(); + + void iterate(); // updates the game state + void render(); + void cleanup(); + + bool is_running; + + SDL_Surface* display; +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..46e6417 --- /dev/null +++ b/main.cpp @@ -0,0 +1,9 @@ +/* Do basic initialization, get the loop going */ + +#include "gamecore.h" + +int main() +{ + GameCore game_core; + return game_core.execute(); +} diff --git a/mainevent.cpp b/mainevent.cpp new file mode 100644 index 0000000..933c851 --- /dev/null +++ b/mainevent.cpp @@ -0,0 +1,108 @@ +#include "mainevent.h" + +void MainEvent::handle_event(SDL_Event* event) +{ + switch(event->type) + { + // Interactions with the WM + case SDL_ACTIVEEVENT: + switch (event->active.state) + { + case SDL_APPMOUSEFOCUS: + if (event->active.gain) on_mouse_focus(); + else on_mouse_blur(); + break; + case SDL_APPINPUTFOCUS: + if (event->active.gain) on_input_focus(); + else on_input_blur(); + break; + case SDL_APPACTIVE: + if (event->active.gain) on_restore(); + else on_minimize(); + break; + } + break; + + // keyboard keypresses + case SDL_KEYDOWN: + on_key_down(event->key.keysym.sym, event->key.keysym.mod, + event->key.keysym.unicode); + break; + case SDL_KEYUP: + on_key_up(event->key.keysym.sym, event->key.keysym.mod, + event->key.keysym.unicode); + break; + + // mouse events + case SDL_MOUSEMOTION: + on_mouse_move(event->motion.x, event->motion.y, event->motion.xrel, + event->motion.yrel, + event->motion.state & SDL_BUTTON(SDL_BUTTON_LEFT), + event->motion.state & SDL_BUTTON(SDL_BUTTON_RIGHT), + event->motion.state & SDL_BUTTON(SDL_BUTTON_MIDDLE)); + break; + case SDL_MOUSEBUTTONDOWN: + switch(event->button.button) + { + case SDL_BUTTON_LEFT: + on_lbutton_down(event->button.x, event->button.y); + break; + case SDL_BUTTON_RIGHT: + on_rbutton_down(event->button.x, event->button.y); + break; + case SDL_BUTTON_MIDDLE: + on_mbutton_down(event->button.x, event->button.y); + break; + } + break; + case SDL_MOUSEBUTTONUP: + switch(event->button.button) + { + case SDL_BUTTON_LEFT: + on_lbutton_up(event->button.x, event->button.y); + break; + case SDL_BUTTON_RIGHT: + on_rbutton_up(event->button.x, event->button.y); + break; + case SDL_BUTTON_MIDDLE: + on_mbutton_up(event->button.x, event->button.y); + break; + } + break; + + // joystick events + case SDL_JOYAXISMOTION: + on_joy_axis(event->jaxis.which, event->jaxis.axis, event->jaxis.value); + break; + case SDL_JOYBALLMOTION: + on_joy_ball(event->jball.which, event->jball.ball, event->jball.xrel, + event->jball.yrel); + break; + case SDL_JOYHATMOTION: + on_joy_hat(event->jhat.which, event->jhat.hat, event->jhat.value); + break; + case SDL_JOYBUTTONDOWN: + on_joy_button_down(event->jbutton.which, event->jbutton.button); + break; + case SDL_JOYBUTTONUP: + on_joy_button_up(event->jbutton.which, event->jbutton.button); + break; + + // Other events + case SDL_QUIT: + on_exit(); + break; + + case SDL_VIDEORESIZE: + on_resize(event->resize.w, event->resize.h); + break; + + case SDL_VIDEOEXPOSE: + on_expose(); + break; + + default: + on_user(event->user.type, event->user.code, event->user.data1, + event->user.data2); + } +} // handle_event diff --git a/mainevent.h b/mainevent.h new file mode 100644 index 0000000..d0e01f9 --- /dev/null +++ b/mainevent.h @@ -0,0 +1,59 @@ +/* + The core class for event handling - sets up the logic for calling those + handlers as needed via handle_event (which + should be called by the class using us, which is likely a subclass). + the actual handlers are all virtual - please define the ones you need + + It is a mystery to me why this class doesn't already exist in SDL... +*/ + +#ifndef _MAINEVENT_H_ +#define _MAINEVENT_H_ + +#include + +class MainEvent +{ + public: + MainEvent() {} + virtual ~MainEvent() {} + + protected: + virtual void handle_event(SDL_Event* event); + + virtual void on_input_focus() {} + virtual void on_input_blur() {} + + virtual void on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode) {} + virtual void on_key_up(SDLKey sym, SDLMod mod, Uint16 unicode) {} + + virtual void on_mouse_focus() {} + virtual void on_mouse_blur() {} + virtual void on_mouse_move(int mX, int mY, int relX, int relY, bool left, bool right, bool middle) {} + virtual void on_mouse_wheel(bool up, bool down) {} + virtual void on_lbutton_down(int mX, int mY) {} + virtual void on_lbutton_up(int mX, int mY) {} + virtual void on_mbutton_down(int mX, int mY) {} + virtual void on_mbutton_up(int mX, int mY) {} + virtual void on_rbutton_down(int mX, int mY) {} + virtual void on_rbutton_up(int mX, int mY) {} + + virtual void on_joy_axis(Uint8 which, Uint8 axis, Sint16 value) {} + virtual void on_joy_button_down(Uint8 which, Uint8 button) {} + virtual void on_joy_button_up(Uint8 which, Uint8 button) {} + virtual void on_joy_hat(Uint8 which, Uint8 hat, Uint8 value) {} + virtual void on_joy_ball(Uint8 which, Uint8 ball, Sint16 xrel, + Sint16 yrel) {} + + virtual void on_minimize() {} + virtual void on_restore() {} + virtual void on_resize(int w,int h) {} + + virtual void on_expose() {} + + virtual void on_exit() {} + + virtual void on_user(Uint8 type, int code, void* data1, void* data2) {} +}; + +#endif