[SOLVED]SDL previous keystate system (Only works sometimes?)
Posted: Sat May 18, 2013 5:00 pm
The issue I'm having is interesting. The function KeyHit() works now, but it appears to not always get the key input for some reason. By pressing q, it should negate the boolean renderQuuad. It does this, but only about half the time that I press q..
SDLKeys.h
SDLKeys.cpp
Main.cpp
SDLKeys.h
Code: Select all
#pragma once
#ifndef SDLKeys_h
#define SDLKeys_h
#include <SDL.h>
class SDLKeys
{
private:
Uint8 *mPreviousKeys;
Uint8 *mCurrentKeys;
int *mMouseX;
int *mMouseY;
public:
SDLKeys(void);
~SDLKeys(void);
void UpdateKeys();
void UpdateCursor();
int GetMouseX() const;
int GetMouseY() const;
bool KeyHit( int index ) const;
bool KeyDown( int index ) const;
};
#endif // SDLKeys_h
Code: Select all
#include "SDLKeys.h"
SDLKeys::SDLKeys(void)
{
mPreviousKeys = new Uint8[SDLK_LAST];
mCurrentKeys = SDL_GetKeyState( NULL );
mMouseX = NULL;
mMouseY = NULL;
}
SDLKeys::~SDLKeys(void)
{
delete mMouseX;
delete mMouseY;
}
void SDLKeys::UpdateCursor()
{
SDL_PumpEvents();
SDL_GetMouseState( mMouseX, mMouseY );
}
void SDLKeys::UpdateKeys()
{
Uint8* tempKeys = NULL;
SDL_PumpEvents();
tempKeys = mPreviousKeys;
mPreviousKeys = mCurrentKeys;
mCurrentKeys = tempKeys;
tempKeys = SDL_GetKeyState( NULL );
memcpy( mCurrentKeys, tempKeys, SDLK_LAST );
}
int SDLKeys::GetMouseX() const
{
return *mMouseX;
}
int SDLKeys::GetMouseY() const
{
return *mMouseY;
}
bool SDLKeys::KeyHit(int index) const
{
return mCurrentKeys[index] && !mPreviousKeys[index];
}
bool SDLKeys::KeyDown(int index) const
{
return mCurrentKeys[index] && mPreviousKeys[index];
}
Code: Select all
#include <SDL.h>
#include <SDL_opengl.h>
#include "SDLTimer.h"
#include "SDLKeys.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int FPS = 60;
SDL_Event event;
bool renderQuad = true;
bool InitGL()
{
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );
//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
return false;
}
return true;
}
bool Init()
{
//Initialize SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
return false;
if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
return false;
SDL_EnableUNICODE( SDL_TRUE );
// Init GL
if( InitGL() == false )
return false;
SDL_WM_SetCaption( "Archaic: The Chaos Gauntlets", NULL );
return true;
}
void Update()
{
}
void Render()
{
glClear( GL_COLOR_BUFFER_BIT );
if( renderQuad == true )
{
glBegin( GL_QUADS );
glVertex2f( -0.5f, -0.5f );
glVertex2f( 0.5f, -0.5f );
glVertex2f( 0.5f, 0.5f );
glVertex2f( -0.5f, 0.5f );
glEnd();
}
SDL_GL_SwapBuffers();
}
void Clean()
{
SDL_Quit();
}
int main( int argc, char *argv[] )
{
SDLTimer *t = new SDLTimer();
SDLKeys *k = new SDLKeys();
bool quit = false;
if( Init() == false )
return 1;
while( quit == false )
{
t->Start(); // Frame timer
//While there are events to handle
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT ) {
quit = true;
} // else if( event.type == SDL_KEYDOWN ) {}
}
k->UpdateKeys();
k->UpdateCursor();
if( k->KeyHit( SDLK_q ) )
{
renderQuad = !renderQuad;
printf("q ");
}
/* Frame functions */
Update();
Render();
if( t->GetTicks() < 1000/FPS )
{
SDL_Delay( ( 1000/FPS ) - t->GetTicks() );
}
}
delete k;
delete t;
Clean();
return 0;
}