Fundamental framework for an SDL application

This commit is contained in:
Anna Rose 2011-06-22 16:12:22 -04:00
commit 27c6f1e0b6
7 changed files with 359 additions and 0 deletions

51
drawutils.cpp Normal file
View File

@ -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;
}

21
drawutils.h Normal file
View File

@ -0,0 +1,21 @@
/* A class with some handy surface handling statics */
#ifndef _DRAWUTILS_H_
#define _DRAWUTILS_H_
#include <SDL/SDL.h>
#include <string>
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

73
gamecore.cpp Normal file
View File

@ -0,0 +1,73 @@
#include "gamecore.h"
#include "drawutils.h"
#ifdef DEBUG
#include <iostream>
#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;
}

38
gamecore.h Normal file
View File

@ -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 <SDL/SDL.h>
#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

9
main.cpp Normal file
View File

@ -0,0 +1,9 @@
/* Do basic initialization, get the loop going */
#include "gamecore.h"
int main()
{
GameCore game_core;
return game_core.execute();
}

108
mainevent.cpp Normal file
View File

@ -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

59
mainevent.h Normal file
View File

@ -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 <SDL/SDL.h>
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