From 988f7c2885068460c8d747be646270ab08785c63 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 21 Aug 2010 17:50:47 +0000 Subject: [PATCH] Replace mouse scrolling with a joystick simulated by the e/s/d/f keys. --- src/Makefile.am | 8 +++---- src/bitfield.h | 44 ++++++++++++++++++++++++++++++++++ src/core.h | 40 +++++++++++++++++++++++++++++++ src/debugfps.cpp | 3 +-- src/debugrecord.cpp | 2 +- src/debugsprite.cpp | 20 ++++++++++++---- src/entity.cpp | 3 +-- src/font.cpp | 2 +- src/forge.cpp | 2 +- src/game.cpp | 3 +-- src/gtk/editor.cpp | 4 +--- src/input.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++ src/input.h | 23 ++++++++++++++++++ src/joystick.cpp | 40 ------------------------------- src/joystick.h | 34 --------------------------- src/layer.cpp | 2 +- src/map.cpp | 4 +--- src/matrix.h | 28 ++++++++++++++++++++++ src/profiler.cpp | 3 +-- src/scene.cpp | 3 +-- src/sdlinput.cpp | 25 +++++++++++--------- src/test-map.cpp | 7 ++---- src/ticker.cpp | 5 +--- src/tiler.cpp | 3 +-- src/tileset.cpp | 2 +- src/timer.cpp | 2 +- src/video.cpp | 2 +- src/video.h | 2 ++ 28 files changed, 245 insertions(+), 128 deletions(-) create mode 100644 src/bitfield.h create mode 100644 src/core.h create mode 100644 src/input.cpp create mode 100644 src/input.h delete mode 100644 src/joystick.cpp delete mode 100644 src/joystick.h create mode 100644 src/matrix.h diff --git a/src/Makefile.am b/src/Makefile.am index d955da8a..2d8815d9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,11 +4,11 @@ noinst_PROGRAMS = test-map editor noinst_LIBRARIES = libcommon.a libcommon_a_SOURCES = \ - game.cpp game.h tiler.cpp tiler.h tileset.cpp tileset.h \ + core.h matrix.h game.cpp game.h tiler.cpp tiler.h \ scene.cpp scene.h font.cpp font.h layer.cpp layer.h map.cpp map.h \ - joystick.cpp joystick.h entity.cpp entity.h ticker.cpp ticker.h \ - forge.cpp forge.h video.cpp video.h timer.cpp timer.h \ - profiler.cpp profiler.h \ + entity.cpp entity.h ticker.cpp ticker.h tileset.cpp tileset.h \ + forge.cpp forge.h video.cpp video.h timer.cpp timer.h bitfield.h \ + profiler.cpp profiler.h input.h input.cpp \ debugfps.cpp debugfps.h debugsprite.cpp debugsprite.h \ debugrecord.cpp debugrecord.h libcommon_a_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` diff --git a/src/bitfield.h b/src/bitfield.h new file mode 100644 index 00000000..5d65eb0f --- /dev/null +++ b/src/bitfield.h @@ -0,0 +1,44 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010 Sam Hocevar +// + +// +// The BitField class +// ------------------ +// + +#if !defined __DH_BITFIELD_H__ +#define __DH_BITFIELD_H__ + +template class BitField +{ +public: + BitField() + { + memset(bits, 0, sizeof(bits)); + } + + inline unsigned int IsSet(unsigned int index) + { + return bits[index / 32] & (1 << (index & 31)); + } + + inline void Set(unsigned int index) + { + bits[index / 32] |= (1 << (index & 31)); + } + + inline void Unset(unsigned int index) + { + bits[index / 32] &= ~(1 << (index & 31)); + } + + +private: + /* FIXME: use uint32_t here instead */ + unsigned int bits[(COUNT + 31) / 32]; +}; + +#endif // __DH_BITFIELD_H__ + diff --git a/src/core.h b/src/core.h new file mode 100644 index 00000000..c91201d5 --- /dev/null +++ b/src/core.h @@ -0,0 +1,40 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010 Sam Hocevar +// + +// +// The main header +// --------------- +// + +#if !defined __DH_CORE_H__ +#define __DH_CORE_H__ + +// Base types +#include "matrix.h" +#include "timer.h" + +// Static classes +#include "video.h" +#include "scene.h" +#include "input.h" +#include "profiler.h" + +// Entities +#include "entity.h" +#include "font.h" +#include "game.h" +#include "tileset.h" + +// Other objects +#include "map.h" +#include "layer.h" + +// Managers +#include "ticker.h" +#include "forge.h" +#include "tiler.h" + +#endif // __DH_CORE_H__ + diff --git a/src/debugfps.cpp b/src/debugfps.cpp index 1e89a1f8..5e162579 100644 --- a/src/debugfps.cpp +++ b/src/debugfps.cpp @@ -9,9 +9,8 @@ #include +#include "core.h" #include "debugfps.h" -#include "forge.h" -#include "profiler.h" /* * DebugFps implementation class diff --git a/src/debugrecord.cpp b/src/debugrecord.cpp index 84b4f669..22ebc039 100644 --- a/src/debugrecord.cpp +++ b/src/debugrecord.cpp @@ -12,8 +12,8 @@ #include +#include "core.h" #include "debugrecord.h" -#include "video.h" /* * DebugRecord implementation class diff --git a/src/debugsprite.cpp b/src/debugsprite.cpp index 9e6fae6c..4ebdf504 100644 --- a/src/debugsprite.cpp +++ b/src/debugsprite.cpp @@ -9,9 +9,8 @@ #include +#include "core.h" #include "debugsprite.h" -#include "game.h" -#include "tiler.h" /* * DebugSprite implementation class @@ -24,7 +23,7 @@ class DebugSpriteData private: Game *game; int tiler; - int frame; + float x, y, z; }; /* @@ -36,6 +35,9 @@ DebugSprite::DebugSprite(Game *game) data = new DebugSpriteData(); data->game = game; data->tiler = Tiler::Register("art/test/character-dress.png"); + data->x = 320; + data->y = 206; + data->z = 0; } Entity::Group DebugSprite::GetGroup() @@ -46,14 +48,22 @@ Entity::Group DebugSprite::GetGroup() void DebugSprite::TickGame(float deltams) { Entity::TickGame(deltams); + + Float2 axis = Input::GetAxis(0); + data->x += 0.1f * deltams * axis.x; + data->y += 0.1f * deltams * axis.y; } void DebugSprite::TickRender(float deltams) { Entity::TickRender(deltams); - data->game->GetScene()->AddTile((data->tiler << 16) | 15, 320, 240, 32, 1); - data->game->GetScene()->AddTile((data->tiler << 16) | 31, 320, 240, 0, 1); + int x = data->x; + int y = data->y; + int z = data->z; + + data->game->GetScene()->AddTile((data->tiler << 16) | 15, x, y, z + 32, 1); + data->game->GetScene()->AddTile((data->tiler << 16) | 31, x, y, z, 1); } DebugSprite::~DebugSprite() diff --git a/src/entity.cpp b/src/entity.cpp index 11cd3136..4fb0f617 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -10,8 +10,7 @@ #include #include -#include "entity.h" -#include "ticker.h" +#include "core.h" /* * Public Entity class diff --git a/src/font.cpp b/src/font.cpp index a70f5b56..7734683a 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -21,7 +21,7 @@ #include #include -#include "font.h" +#include "core.h" /* * Font implementation class diff --git a/src/forge.cpp b/src/forge.cpp index 772eae15..bf8ed7fc 100644 --- a/src/forge.cpp +++ b/src/forge.cpp @@ -11,7 +11,7 @@ #include #include -#include "forge.h" +#include "core.h" #if defined WIN32 # define strcasecmp _stricmp diff --git a/src/game.cpp b/src/game.cpp index 988aca50..6f53c117 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -9,8 +9,7 @@ #include -#include "game.h" -#include "map.h" +#include "core.h" /* * Game implementation class diff --git a/src/gtk/editor.cpp b/src/gtk/editor.cpp index 72946ae4..5c16d07a 100644 --- a/src/gtk/editor.cpp +++ b/src/gtk/editor.cpp @@ -14,10 +14,8 @@ #include #include -#include "ticker.h" +#include "core.h" #include "debugfps.h" -#include "video.h" -#include "game.h" static volatile int quit = 0; diff --git a/src/input.cpp b/src/input.cpp new file mode 100644 index 00000000..77fc42e7 --- /dev/null +++ b/src/input.cpp @@ -0,0 +1,57 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010 Sam Hocevar +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include +#include +#include + +#include "core.h" + +/* + * Input implementation class + */ + +static class InputData +{ + friend class Input; + +public: + int dummy; +} +inputdata; + +static InputData * const data = &inputdata; + +/* + * Public Input class + */ + +Float2 Input::GetAxis(int axis) +{ + float invsqrt2 = sqrtf(0.5f); + Float2 f; + + /* Simulate a joystick using the keyboard. This SDL call is free. */ + Uint8 *keystate = SDL_GetKeyState(NULL); + f.y -= keystate[SDLK_e]; + f.y += keystate[SDLK_d]; + f.x -= keystate[SDLK_s]; + f.x += keystate[SDLK_f]; + if (keystate[SDLK_e] + keystate[SDLK_d] + == keystate[SDLK_s] + keystate[SDLK_f]) + { + f.x *= invsqrt2; + f.y *= invsqrt2; + } + + return f; +} + diff --git a/src/input.h b/src/input.h new file mode 100644 index 00000000..d61bf961 --- /dev/null +++ b/src/input.h @@ -0,0 +1,23 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010 Sam Hocevar +// + +// +// The Input static class +// ---------------------- +// + +#if !defined __DH_INPUT_H__ +#define __DH_INPUT_H__ + +#include "matrix.h" + +class Input +{ +public: + static Float2 GetAxis(int axis); +}; + +#endif // __DH_INPUT_H__ + diff --git a/src/joystick.cpp b/src/joystick.cpp deleted file mode 100644 index 51837f32..00000000 --- a/src/joystick.cpp +++ /dev/null @@ -1,40 +0,0 @@ -// -// Deus Hax (working title) -// Copyright (c) 2010 Sam Hocevar -// - -#if defined HAVE_CONFIG_H -# include "config.h" -#endif - -#include - -#include "joystick.h" - -/* - * Joystick implementation class - */ - -class JoystickData -{ - friend class Joystick; - -private: - int dummy; -}; - -/* - * Public Joystick class - */ - -Joystick::Joystick() -{ - data = new JoystickData(); - - SDL_WM_GrabInput(SDL_GRAB_ON); -} - -Joystick::~Joystick() -{ -} - diff --git a/src/joystick.h b/src/joystick.h deleted file mode 100644 index a634ccd8..00000000 --- a/src/joystick.h +++ /dev/null @@ -1,34 +0,0 @@ -// -// Deus Hax (working title) -// Copyright (c) 2010 Sam Hocevar -// - -// -// The Joystick class -// ------------------ -// - -#if !defined __DH_JOYSTICK_H__ -#define __DH_JOYSTICK_H__ - -class JoystickData; - -class Joystick -{ -public: - Joystick(); - ~Joystick(); - - int GetState(); - - static int const NORTH = (1<<0); - static int const EAST = (1<<1); - static int const SOUTH = (1<<2); - static int const WEST = (1<<3); - -private: - JoystickData *data; -}; - -#endif // __DH_JOYSTICK_H__ - diff --git a/src/layer.cpp b/src/layer.cpp index 399f265b..13cccfa6 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -9,7 +9,7 @@ #include -#include "layer.h" +#include "core.h" Layer::Layer(int w, int h, int z, int o, uint32_t *in_data) { diff --git a/src/map.cpp b/src/map.cpp index a630ade7..2836d118 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -12,9 +12,7 @@ #include #include -#include "map.h" -#include "layer.h" -#include "tiler.h" +#include "core.h" /* * Map implementation class diff --git a/src/matrix.h b/src/matrix.h new file mode 100644 index 00000000..5e4ee6be --- /dev/null +++ b/src/matrix.h @@ -0,0 +1,28 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010 Sam Hocevar +// + +// +// The Matrix classes +// ------------------ +// + +#if !defined __DH_MATRIX_H__ +#define __DH_MATRIX_H__ + +struct Float2 +{ + Float2() { x = y = 0.0f; } + Float2(float _x, float _y) { x = _x; y = _y; } + + float x, y; +}; + +struct Float3 +{ + float x, y, z; +}; + +#endif // __DH_MATRIX_H__ + diff --git a/src/profiler.cpp b/src/profiler.cpp index bdaddeff..8f8ebc92 100644 --- a/src/profiler.cpp +++ b/src/profiler.cpp @@ -11,8 +11,7 @@ #include #include -#include "profiler.h" -#include "timer.h" +#include "core.h" /* * Profiler implementation class diff --git a/src/scene.cpp b/src/scene.cpp index 03a58b62..3c79518a 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -21,8 +21,7 @@ # include #endif -#include "scene.h" -#include "tiler.h" +#include "core.h" struct Tile { diff --git a/src/sdlinput.cpp b/src/sdlinput.cpp index 67bf9478..4380228f 100644 --- a/src/sdlinput.cpp +++ b/src/sdlinput.cpp @@ -9,6 +9,7 @@ #include +#include "core.h" #include "sdlinput.h" /* @@ -21,6 +22,7 @@ class SdlInputData private: Game *game; + int mx, my; }; /* @@ -33,6 +35,7 @@ SdlInput::SdlInput(Game *game) data = new SdlInputData(); data->game = game; + SDL_GetMouseState(&data->mx, &data->my); } Entity::Group SdlInput::GetGroup() @@ -48,27 +51,27 @@ void SdlInput::TickGame(float deltams) destroy = 1; /* Handle mouse input */ - int mx, my; - SDL_GetMouseState(&mx, &my); - data->game->SetMouse(mx * (640 - 32) / 320 - 320, my * (480 - 32) / 240 - 240); + SDL_GetMouseState(&data->mx, &data->my); - /* Handle keyboard and WM input */ + /* Handle keyboard and WM events */ SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) data->game->Quit(); - if (event.type == SDL_KEYDOWN) - { - if (event.key.keysym.sym == SDLK_ESCAPE) - data->game->Quit(); #if 0 - else if (event.key.keysym.sym == SDLK_RETURN) - video->FullScreen(); + else if (event.type == SDL_KEYDOWN) + Input::KeyPressed(event.key.keysym.sym, deltams); #endif - } } + /* Send the whole keyboard state to the input system */ +#if 0 + Uint8 *keystate = SDL_GetKeyState(NULL); + for (int i = 0; i < 256; i++) + if (keystate[i]) + Input::KeyPressed(i, deltams); +#endif } SdlInput::~SdlInput() diff --git a/src/test-map.cpp b/src/test-map.cpp index 3e936d01..b0aecd5b 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -12,14 +12,11 @@ #include +#include "core.h" #include "sdlinput.h" #include "debugfps.h" #include "debugsprite.h" #include "debugrecord.h" -#include "game.h" -#include "ticker.h" -#include "profiler.h" -#include "video.h" static float const FPS = 30.0f; @@ -42,7 +39,7 @@ int main(int argc, char **argv) SDL_WM_SetCaption("Map Test (SDL)", NULL); SDL_ShowCursor(0); - SDL_WM_GrabInput(SDL_GRAB_ON); + //SDL_WM_GrabInput(SDL_GRAB_ON); /* Initialise OpenGL */ Video::Setup(video->w, video->h); diff --git a/src/ticker.cpp b/src/ticker.cpp index 71d017e9..8d976d92 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -11,10 +11,7 @@ #include #include -#include "profiler.h" -#include "ticker.h" -#include "entity.h" -#include "timer.h" +#include "core.h" /* * Ticker implementation class diff --git a/src/tiler.cpp b/src/tiler.cpp index cfdd2644..1c407953 100644 --- a/src/tiler.cpp +++ b/src/tiler.cpp @@ -11,8 +11,7 @@ #include #include -#include "tiler.h" -#include "tileset.h" +#include "core.h" #if defined WIN32 # define strcasecmp _stricmp diff --git a/src/tileset.cpp b/src/tileset.cpp index 70acd9d8..290de810 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -24,7 +24,7 @@ #include #include -#include "tileset.h" +#include "core.h" /* * TileSet implementation class diff --git a/src/timer.cpp b/src/timer.cpp index 8ebb51cc..c145493e 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -22,7 +22,7 @@ # include #endif -#include "timer.h" +#include "core.h" /* * Timer implementation class diff --git a/src/video.cpp b/src/video.cpp index ca3e69db..28b48522 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -18,7 +18,7 @@ # include #endif -#include "video.h" +#include "core.h" /* * Public Video class diff --git a/src/video.h b/src/video.h index ecdfa723..ab02cb5c 100644 --- a/src/video.h +++ b/src/video.h @@ -12,6 +12,8 @@ #if !defined __DH_VIDEO_H__ #define __DH_VIDEO_H__ +#include + class Video { public: