From e322cb6cb1dc6ae57ad9122584be00b4105894a3 Mon Sep 17 00:00:00 2001 From: James Turk Date: Tue, 3 Dec 2002 06:19:43 +0000 Subject: [PATCH] KeyPress added. --- include/ZE_ZEngine.h | 30 ++++++++++++++++++++++++++---- src/ZE_ZEngine.cpp | 36 ++++++++++++++++++++++++++++++------ test/ZRectTest.cpp | 10 +++++----- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/include/ZE_ZEngine.h b/include/ZE_ZEngine.h index 1d0d958..f7de9d3 100644 --- a/include/ZE_ZEngine.h +++ b/include/ZE_ZEngine.h @@ -13,7 +13,7 @@ File: ZE_ZEngine.h
Description: Header file for ZEngine class, the core of the ZEngine.
Author(s): James Turk
-$Id: ZE_ZEngine.h,v 1.3 2002/12/01 07:56:17 cozman Exp $
+$Id: ZE_ZEngine.h,v 1.4 2002/12/03 06:19:43 cozman Exp $
\file ZE_ZEngine.h \brief Definition file for core ZEngine class. @@ -275,7 +275,9 @@ class ZEngine //! bool for checking if a Quit event has been detected bool mQuit; //! Pointer to array of Keys - Uint8 *mKeyPressed; + Uint8 *mKeyIsPressed; + //! Array of keys, used by KeyPress + bool mKeyPress[SDLK_LAST]; //! X Position of Mouse int mMouseX; //! Y Position of Mouse @@ -309,15 +311,35 @@ class ZEngine **/ bool QuitRequested(); + /*! + \brief Set Key repeat rate. + + Calls SDL_EnableKeyRepeat(rate,rate) because usually this is the desired movement style for games. + The rate is set to 30 upon the creation of the display, pass zero to disable this. + SDL_EnableKeyRepeat can be called separately: http://sdldoc.csn.ul.ie/sdlenablekeyrepeat.php. + + \param rate Desired key repeat rate. + **/ + void SetKeyRepeatRate(int rate); + /*! \brief Find the state of a key. Function returns true/false based on if key is currently pressed or not. - \param key code of key to find status of. - \return bool state of requested key. + \param key Code of key to find status of. + \return State of requested key. **/ bool KeyIsPressed(SDLKey key); + /*! + \brief Find if key has been pressed since last check. + + Function returns true/false based on if key has been pressed since last check. + \param key Code of key to find status of. + \return State of requested key. + **/ + bool KeyPress(SDLKey key); + /*! \brief Hide mouse cursor. diff --git a/src/ZE_ZEngine.cpp b/src/ZE_ZEngine.cpp index 8c6956c..e22b88b 100644 --- a/src/ZE_ZEngine.cpp +++ b/src/ZE_ZEngine.cpp @@ -13,7 +13,7 @@ File: ZE_ZEngine.cpp
Description: Implementation source file for ZEngine library main singleton class.
Author(s): James Turk
-$Id: ZE_ZEngine.cpp,v 1.3 2002/12/01 07:56:17 cozman Exp $
+$Id: ZE_ZEngine.cpp,v 1.4 2002/12/03 06:19:43 cozman Exp $
\file ZE_ZEngine.cpp \brief Central source file for ZEngine. @@ -43,10 +43,13 @@ ZEngine::ZEngine() mScreen = NULL; mActive = mQuit = false; - mKeyPressed = NULL; + mKeyIsPressed = NULL; mMouseX = mMouseY = 0; mMouseB = 0; + for(int k = 0; k < SDLK_LAST; k++) + mKeyPress[k] = false; + mUnpauseOnActive = mPaused = false; mLastPause = mPausedTime = mLastTime = 0; mSecPerFrame = 0.0; @@ -140,6 +143,7 @@ void ZEngine::CreateDisplay(string title, string icon) flags = SDL_OPENGL; //Window Manager settings// + SDL_EnableKeyRepeat(30,30); if(!icon.length()) SDL_WM_SetCaption(title.c_str(),NULL); else @@ -167,7 +171,7 @@ void ZEngine::CreateDisplay(string title, string icon) SetGL2D(); - mKeyPressed = SDL_GetKeyState(NULL); + mKeyIsPressed = SDL_GetKeyState(NULL); #ifdef USE_SDL_TTF TTF_Init(); @@ -294,9 +298,21 @@ bool ZEngine::QuitRequested() return mQuit; } +void ZEngine::SetKeyRepeatRate(int rate) +{ + SDL_EnableKeyRepeat(rate,rate); +} + bool ZEngine::KeyIsPressed(SDLKey key) { - return mKeyPressed[key] == 1; + return mKeyIsPressed[key] == 1; +} + +bool ZEngine::KeyPress(SDLKey key) +{ + bool temp = mKeyPress[key]; + mKeyPress[key] = false; + return temp; } void ZEngine::HideCursor() @@ -366,6 +382,12 @@ void ZEngine::CheckEvents() } } break; + case SDL_KEYDOWN: + mKeyPress[event.key.keysym.sym] = true; + break; + case SDL_KEYUP: + mKeyPress[event.key.keysym.sym] = false; + break; case SDL_QUIT: mQuit = true; break; @@ -374,9 +396,11 @@ void ZEngine::CheckEvents() } } - mKeyPressed = SDL_GetKeyState(NULL); //recommended but not needed (says Sam) + mKeyIsPressed = SDL_GetKeyState(NULL); //recommended but not needed (says Sam) - if(mKeyPressed[SDLK_F4] && (mKeyPressed[SDLK_LALT] || mKeyPressed[SDLK_RALT])) + //Alt-X or Alt-F4 + if((mKeyIsPressed[SDLK_x] || mKeyIsPressed[SDLK_F4]) && + (mKeyIsPressed[SDLK_LALT] || mKeyIsPressed[SDLK_RALT])) mQuit = true; mMouseB = SDL_GetMouseState(&mMouseX,&mMouseY); diff --git a/test/ZRectTest.cpp b/test/ZRectTest.cpp index eff8d96..902875f 100644 --- a/test/ZRectTest.cpp +++ b/test/ZRectTest.cpp @@ -49,13 +49,13 @@ void Test() if(engine->KeyIsPressed(SDLK_ESCAPE)) engine->RequestQuit(); //movement// - if(engine->KeyIsPressed(SDLK_LEFT)) + if(engine->KeyPress(SDLK_LEFT)) moveRect.MoveRel(-3,0); - if(engine->KeyIsPressed(SDLK_RIGHT)) + if(engine->KeyPress(SDLK_RIGHT)) moveRect.MoveRel(3,0); - if(engine->KeyIsPressed(SDLK_UP)) + if(engine->KeyPress(SDLK_UP)) moveRect.MoveRel(0,-3); - if(engine->KeyIsPressed(SDLK_DOWN)) + if(engine->KeyPress(SDLK_DOWN)) moveRect.MoveRel(0,3); if(engine->KeyIsPressed(SDLK_EQUALS)) { @@ -73,7 +73,7 @@ void Test() stillRect.Draw(0,0,255,128); moveRect.Intersection(stillRect).Draw(0,255,0); engine->UpdateScreen(); - + } while(!engine->QuitRequested()); }