KeyPress added.

This commit is contained in:
James Turk 2002-12-03 06:19:43 +00:00
parent 9ab7b05501
commit e322cb6cb1
3 changed files with 61 additions and 15 deletions

View File

@ -13,7 +13,7 @@
File: ZE_ZEngine.h <br> File: ZE_ZEngine.h <br>
Description: Header file for ZEngine class, the core of the ZEngine. <br> Description: Header file for ZEngine class, the core of the ZEngine. <br>
Author(s): James Turk <br> Author(s): James Turk <br>
$Id: ZE_ZEngine.h,v 1.3 2002/12/01 07:56:17 cozman Exp $<br> $Id: ZE_ZEngine.h,v 1.4 2002/12/03 06:19:43 cozman Exp $<br>
\file ZE_ZEngine.h \file ZE_ZEngine.h
\brief Definition file for core ZEngine class. \brief Definition file for core ZEngine class.
@ -275,7 +275,9 @@ class ZEngine
//! bool for checking if a Quit event has been detected //! bool for checking if a Quit event has been detected
bool mQuit; bool mQuit;
//! Pointer to array of Keys //! Pointer to array of Keys
Uint8 *mKeyPressed; Uint8 *mKeyIsPressed;
//! Array of keys, used by KeyPress
bool mKeyPress[SDLK_LAST];
//! X Position of Mouse //! X Position of Mouse
int mMouseX; int mMouseX;
//! Y Position of Mouse //! Y Position of Mouse
@ -309,15 +311,35 @@ class ZEngine
**/ **/
bool QuitRequested(); 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. \brief Find the state of a key.
Function returns true/false based on if key is <u>currently</u> pressed or not. Function returns true/false based on if key is <u>currently</u> pressed or not.
\param key code of key to find status of. \param key Code of key to find status of.
\return bool state of requested key. \return State of requested key.
**/ **/
bool KeyIsPressed(SDLKey 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. \brief Hide mouse cursor.

View File

@ -13,7 +13,7 @@
File: ZE_ZEngine.cpp <br> File: ZE_ZEngine.cpp <br>
Description: Implementation source file for ZEngine library main singleton class. <br> Description: Implementation source file for ZEngine library main singleton class. <br>
Author(s): James Turk <br> Author(s): James Turk <br>
$Id: ZE_ZEngine.cpp,v 1.3 2002/12/01 07:56:17 cozman Exp $<br> $Id: ZE_ZEngine.cpp,v 1.4 2002/12/03 06:19:43 cozman Exp $<br>
\file ZE_ZEngine.cpp \file ZE_ZEngine.cpp
\brief Central source file for ZEngine. \brief Central source file for ZEngine.
@ -43,10 +43,13 @@ ZEngine::ZEngine()
mScreen = NULL; mScreen = NULL;
mActive = mQuit = false; mActive = mQuit = false;
mKeyPressed = NULL; mKeyIsPressed = NULL;
mMouseX = mMouseY = 0; mMouseX = mMouseY = 0;
mMouseB = 0; mMouseB = 0;
for(int k = 0; k < SDLK_LAST; k++)
mKeyPress[k] = false;
mUnpauseOnActive = mPaused = false; mUnpauseOnActive = mPaused = false;
mLastPause = mPausedTime = mLastTime = 0; mLastPause = mPausedTime = mLastTime = 0;
mSecPerFrame = 0.0; mSecPerFrame = 0.0;
@ -140,6 +143,7 @@ void ZEngine::CreateDisplay(string title, string icon)
flags = SDL_OPENGL; flags = SDL_OPENGL;
//Window Manager settings// //Window Manager settings//
SDL_EnableKeyRepeat(30,30);
if(!icon.length()) if(!icon.length())
SDL_WM_SetCaption(title.c_str(),NULL); SDL_WM_SetCaption(title.c_str(),NULL);
else else
@ -167,7 +171,7 @@ void ZEngine::CreateDisplay(string title, string icon)
SetGL2D(); SetGL2D();
mKeyPressed = SDL_GetKeyState(NULL); mKeyIsPressed = SDL_GetKeyState(NULL);
#ifdef USE_SDL_TTF #ifdef USE_SDL_TTF
TTF_Init(); TTF_Init();
@ -294,9 +298,21 @@ bool ZEngine::QuitRequested()
return mQuit; return mQuit;
} }
void ZEngine::SetKeyRepeatRate(int rate)
{
SDL_EnableKeyRepeat(rate,rate);
}
bool ZEngine::KeyIsPressed(SDLKey key) 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() void ZEngine::HideCursor()
@ -366,6 +382,12 @@ void ZEngine::CheckEvents()
} }
} }
break; break;
case SDL_KEYDOWN:
mKeyPress[event.key.keysym.sym] = true;
break;
case SDL_KEYUP:
mKeyPress[event.key.keysym.sym] = false;
break;
case SDL_QUIT: case SDL_QUIT:
mQuit = true; mQuit = true;
break; 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; mQuit = true;
mMouseB = SDL_GetMouseState(&mMouseX,&mMouseY); mMouseB = SDL_GetMouseState(&mMouseX,&mMouseY);

View File

@ -49,13 +49,13 @@ void Test()
if(engine->KeyIsPressed(SDLK_ESCAPE)) if(engine->KeyIsPressed(SDLK_ESCAPE))
engine->RequestQuit(); engine->RequestQuit();
//movement// //movement//
if(engine->KeyIsPressed(SDLK_LEFT)) if(engine->KeyPress(SDLK_LEFT))
moveRect.MoveRel(-3,0); moveRect.MoveRel(-3,0);
if(engine->KeyIsPressed(SDLK_RIGHT)) if(engine->KeyPress(SDLK_RIGHT))
moveRect.MoveRel(3,0); moveRect.MoveRel(3,0);
if(engine->KeyIsPressed(SDLK_UP)) if(engine->KeyPress(SDLK_UP))
moveRect.MoveRel(0,-3); moveRect.MoveRel(0,-3);
if(engine->KeyIsPressed(SDLK_DOWN)) if(engine->KeyPress(SDLK_DOWN))
moveRect.MoveRel(0,3); moveRect.MoveRel(0,3);
if(engine->KeyIsPressed(SDLK_EQUALS)) if(engine->KeyIsPressed(SDLK_EQUALS))
{ {
@ -73,7 +73,7 @@ void Test()
stillRect.Draw(0,0,255,128); stillRect.Draw(0,0,255,128);
moveRect.Intersection(stillRect).Draw(0,255,0); moveRect.Intersection(stillRect).Draw(0,255,0);
engine->UpdateScreen(); engine->UpdateScreen();
} while(!engine->QuitRequested()); } while(!engine->QuitRequested());
} }