Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MPG

I need help with SDL keyboard events.

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know how to cheack if any key is pressed,but I don know how to check if a certain key is pressed like arrow key up. HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Share this post


Link to post
Share on other sites
Advertisement
Take a look at this, and a ways down, about 3/4, there will be how to handle individual key identities.


case SDL_KEYDOWN:
{
switch( event.key.keysym.sym )
{
case SDLK_UP:
{
...

Share this post


Link to post
Share on other sites
nah, even easier. From the keyboard header file:

00092 /*
00093 * Get a snapshot of the current state of the keyboard.
00094 * Returns an array of keystates, indexed by the SDLK_* syms.
00095 * Used:
00096 * Uint8 *keystate = SDL_GetKeyState(NULL);
00097 * if ( keystate[SDLK_RETURN] ) ... <RETURN> is pressed.
00098 */

00099 extern DECLSPEC Uint8 * SDLCALL SDL_GetKeyState(int *numkeys);


tadaa!

Share this post


Link to post
Share on other sites

#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
#include "SDL.h"

int main( int argc, char* argv[] )
{
SDL_Init( SDL_INIT_VIDEO );
atexit( SDL_Quit );
SDL_Surface * screen = SDL_SetVideoMode( 800,600, 24, SDL_ANYFORMAT|SDL_DOUBLEBUF );
SDL_Surface * img = SDL_LoadBMP( "filename.bmp" );
SDL_Rect dest;
dest.x = 200;
dest.y = 300;
SDL_Event event;
SDL_Event a;
SDL_Event b;
SDL_Event Event
;SDL_Rect coord
;coord.x = 200;
coord.y = 500;
SDL_Surface * tdisplay = SDL_SetVideoMode( 800,600,24,SDL_ANYFORMAT|SDL_DOUBLEBUF );
SDL_Surface * imi = SDL_LoadBMP( "ad.bmp" );
SDL_Surface * bckg = SDL_LoadBMP ( "bg.bmp" );
int playerposx;
playerposx = 0;
#define TICK_INTERVAL 30

Uint32 move(void//)time code
{
static Uint32 next_time = 0;
Uint32 now;

now = SDL_GetTicks();
if ( next_time <= now ) {
next_time = now+TICK_INTERVAL;
return(0);
}
return(next_time-now);
}


;while(true) {
SDL_BlitSurface(img,NULL,screen, &dest);
SDL_PollEvent(&event);
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{

SDL_BlitSurface(imi,NULL,tdisplay,&coord);
SDL_Flip(tdisplay);

;}
}
case SDL_KEYDOWN:{
switch( event.key.keysym.sym )
{
case SDLK_UP:
{

else if(event.type == SDL_QUIT) { return 0; }
SDL_Flip( screen );
}
return 0;
}



I need someone to exlan to me how to use tha time for to move the image in display.

Share this post


Link to post
Share on other sites
Do you mean move based on the time it takes to render a frame?

Multiply your movment factor by a variable that takes the delta time between frames.

Share this post


Link to post
Share on other sites
quote:
Original post by MPG
Can you give me an example please?


Sure, here is an example from my harmonics physics project in Opengl.

Timer functions to the time.

//Initiate Timer Function::nehe.gamedev.net/

void TimerInit(void)
{
memset(&timer, 0, sizeof(timer));
if (!QueryPerformanceFrequency((LARGE_INTEGER *) &timer.frequency))
{
timer.performance_timer = FALSE;
timer.mm_timer_start = timeGetTime();
timer.resolution = 1.0f/1000.0f;
timer.frequency = 1000;
timer.mm_timer_elapsed = timer.mm_timer_start;
}
else
{
QueryPerformanceCounter((LARGE_INTEGER *) &timer.performance_timer_start);
timer.performance_timer = TRUE;
timer.resolution = (float)(((double)1.0f)/((double)timer.frequency));
timer.performance_timer_elapsed = timer.performance_timer_start;
}
}

//Very Precise Get Time Function::nehe.gamedev.net/

float TimerGetTime()
{
__int64 time;
if(timer.performance_timer)
{
QueryPerformanceCounter((LARGE_INTEGER *) &time);
return((float)(time-timer.performance_timer_start)*timer.resolution)*1000.0f;
}
else
{
return((float)(timeGetTime()-timer.mm_timer_start)*timer.resolution)*1000.0f;
}
}



The implementation into the message loop. This simply takes the time before the frame is rendered, the time after the frame is rendered, and the delta time resulting from the subtraction of the two times.

//Windows Message Loop

while(!done)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
done=TRUE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
float start=TimerGetTime();
if(active && !DrawScene())
{
done=TRUE;
}
else
{
Sleep(0);
SwapBuffers(hDC);
/*FrameRateAndTime();
if(keys[VK_RETURN])
{
scene++;
MenuEntryCheck(scene);
}*/

}
speed_mod=(TimerGetTime()-start)/1000.0f;
}
}

Notice the "start" variable used to hold the time and the speed_mod variable used to hold the delta time. The /1000.0f is used to set the resolution of the correction factor to something like a "percent".

This is an implementation in the drawing function.

case ID_TITLE:
{
static float fade=1.0;
//Fading Texture

glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[IDB_PRODUCTIONS]);
glPushMatrix();
glTranslatef(0.0,0.0,0.0);
glColor4f(1.0,1.0,1.0,fade);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.2, 1.3,-2.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.2, -1.3, -2.0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.2, -1.3, -2.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.2, 1.3, -2.0);
glEnd();
glPopMatrix();
fade-=speed_mod*.4;

This piece of code fades a texture regardless of how fast your computer is rendering the frames by modifying the fade variable's constant. The line below the glPopMatrix() states that it each time this is executed speed_mod*.4 will be subtracted from the integer variable "fade", but since speed_mod is the delta time between frames the number being subtracted from "fade" will INCREASE if the frames are rendered too slow or DECREASE if the frames are rendered too fast.

So essentially the speed_mod variable is a percentage modifier:
Frames are rendered too fast: multiply the constant by a percentage < 1.00 or 100%
Frames are rendered too slow: multiply the constant by a percentage that is > 1.00 or 100%
Frames are somehow being rendered just right, just multiply the constant by 100% or 1.00 so that the original value of fade is there.

This will ensure that the object you are manipulating whether it be fading "glColor4f(1.0, 1.0, 1.0, fade);" or moving "glTranslatef(0.0, constant+=speed_mod*10, 0.0);" will almost be exactly where you want it regardless of how fast/slow the computer is rendering.

Hope this helps, if you need a detailed explaination, I will be happy to talk; also if you need a demo showcasing this with such things as translation or particle generation then tell me.

[edited by - Xiachunyi on June 7, 2004 2:33:05 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!