From: Patrik Gornicz Date: Thu, 17 Jul 2008 01:36:26 +0000 (-0400) Subject: input system setup, debug header started X-Git-Tag: physics-premerge~203 X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=commitdiff_plain;h=f7b3b2eb35e8047d247dfd7cf7b110a2b6e907af;p=libbear.git input system setup, debug header started --- diff --git a/src/Makefile b/src/Makefile index 7ef5006..4a51706 100644 --- a/src/Makefile +++ b/src/Makefile @@ -28,6 +28,8 @@ SRCS += GameStates/GameState.cpp SRCS += GameStates/Paused.cpp SRCS += GameStates/Running.cpp +SRCS += input/inputManager.cpp + OBJS = ${SRCS:.cpp=.o} TARGET = ../run_physics diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..c8dfdb4 --- /dev/null +++ b/src/debug.h @@ -0,0 +1,12 @@ +#ifndef DEBUG_H +#define DEBUG_H + +// comment out when not debugging +#define DEBUGGING + +#include +using std::cerr; +using std::cout; +using std::endl; + +#endif // DEBUG_H diff --git a/src/graphics.cpp b/src/graphics.cpp index 42c2e6d..0f84d20 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -5,15 +5,12 @@ #include #include -#include -using std::cerr; -using std::cout; -using std::endl; - +#include "debug.h" static const float PI = 3.1415926535897; /// ***** Private Method Headers ***** +void drawCircle(int); void sdlInit(); void glInit(); @@ -30,23 +27,35 @@ void graphicsCleanUp() } -void glDrawCircle() +void glDrawCircle(float radius, float x, float y, const float* color) { - int num = 32; + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(x, y, -1); + glScalef(radius, radius, radius); + + if(color != NULL) + glColor3fv(color); + + drawCircle(32); +} + +/// ***** Private Methods ***** +void drawCircle(int pieces) +{ glBegin(GL_POLYGON); - for(int n = 0; n < num; n++) + for(int n = 0; n < pieces; n++) { - float angle = 2 * PI * n / num; - float x = cos(angle); - float y = sin(angle); + float angle = 2 * PI * n / pieces; + float ix = cos(angle); + float iy = sin(angle); - glVertex3f(x, y, -1); + glVertex3f(ix, iy, 0); } glEnd(); } -/// ***** Private Methods ***** void sdlInit() { if(SDL_Init(SDL_INIT_VIDEO) < 0) @@ -74,7 +83,7 @@ void glInit() glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(-20.0, 20.0, -15.0, 15.0, -0.01, 1.01); + glOrtho(0, 800.0, 600.0, 0.0, -0.01, 1.01); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); diff --git a/src/graphics.h b/src/graphics.h index c3d5276..43505a6 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -5,6 +5,6 @@ void graphicsInit(); void graphicsCleanUp(); -void glDrawCircle(); +void glDrawCircle(float radius, float x, float y, const float* color = 0); #endif // GRAPHICS_H diff --git a/src/input/inputManager.cpp b/src/input/inputManager.cpp new file mode 100644 index 0000000..e8b92c6 --- /dev/null +++ b/src/input/inputManager.cpp @@ -0,0 +1,64 @@ + +#include "inputManager.h" +#include "../debug.h" + +enum State +{ + isR, + wasP, + isP, + wasR +}; + +static const int keySize = 323; +static State keyState[keySize]; + +void inputInit() +{ + for(int i=0; i< keySize; i++) + keyState[i] = isR; +} + +void inputUpdate() +{ + SDL_Event event; + + for(int i=0; i< keySize; i++) + { + if(keyState[i] == wasR) + keyState[i] = isR; + else if(keyState[i] == wasP) + keyState[i] = isP; + } + + while(SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_KEYUP: + keyState[event.key.keysym.sym] = wasR; + break; + case SDL_KEYDOWN: + keyState[event.key.keysym.sym] = wasP; + break; + } + } +} + +bool isPressed(Uint8 key) +{ + return keyState[key] == isP || keyState[key] == wasP; +} +bool isReleased(Uint8 key) +{ + return keyState[key] == isR || keyState[key] == wasR; +} + +bool wasPressed(Uint8 key) +{ + return keyState[key] == wasP; +} +bool wasReleased(Uint8 key) +{ + return keyState[key] == wasR; +} diff --git a/src/input/inputManager.h b/src/input/inputManager.h new file mode 100644 index 0000000..411dcc2 --- /dev/null +++ b/src/input/inputManager.h @@ -0,0 +1,15 @@ +#ifndef INPUT_H +#define INPUT_H + +#include + +/// ***** Header Methods ***** +void inputUpdate(); + +bool isPressed(Uint8); +bool isReleased(Uint8); + +bool wasPressed(Uint8); +bool wasReleased(Uint8); + +#endif // INPUT_H diff --git a/src/main.cpp b/src/main.cpp index df9f490..fd14612 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,17 +2,15 @@ #include #include -#include -using std::cerr; -using std::cout; -using std::endl; - #include using std::vector; +#include "debug.h" + #include "game.h" #include "ticks.h" #include "graphics.h" +#include "input/inputManager.h" /// ***** Private Method Headers ***** @@ -74,11 +72,15 @@ void init() // TODO // add a game state +#ifdef DEBUGGING cout << "Initialization Complete" << endl; +#endif // create starting entities +#ifdef DEBUGGING cout << "World Created" << endl; +#endif } void run() @@ -145,12 +147,12 @@ void updateFPSCounters() void input() { + inputUpdate(); + gameInput(); - /* - if(key[KEY_ESC]) + if(isPressed(SDLK_ESCAPE)) is_Running = false; - */ } void update(float time_step)