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>
Description: Header file for ZEngine class, the core of the ZEngine. <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
\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 <u>currently</u> 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.

View File

@ -13,7 +13,7 @@
File: ZE_ZEngine.cpp <br>
Description: Implementation source file for ZEngine library main singleton class. <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
\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);

View File

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