Skip to main content
GameDev.net gamedev.net
🔒 Locked

Funky keys

Started by Yamian Nov 22, 2004 at 12:45 PM 12 replies 2.8k views
Original Post
Yamian
Yamian
In the following program how come if you are pushing up and down at the same time and then just one immediately after, it won't move?

#include <stdlib.h>

#include <SDL\SDL.h>
#include <SDL\SDL_gfxPrimitives.h>


void Slock(SDL_Surface *screen)
{
    if ( SDL_MUSTLOCK(screen) )
    {
        if ( SDL_LockSurface(screen) < 0 )
        {
            return;
        }
    }
}

void Sulock(SDL_Surface *screen)
{
    if ( SDL_MUSTLOCK(screen) )
    {
        SDL_UnlockSurface(screen);
    }
}


int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Surface *screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
    atexit(SDL_Quit);
    SDL_WM_SetCaption("Phlong",NULL);
    
    int y=0;
    
    bool done = false;
    while(done == false)
    {
        SDL_Event event;
        while ( SDL_PollEvent(&event) )
        {
            if ( event.type == SDL_QUIT )  {  done = true;  }
            if ( event.type == SDL_KEYDOWN )
            {
                if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
            }
        }
        SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
        Slock(screen);
            
            if ( event.type == SDL_KEYDOWN )
            {
                if ( event.key.keysym.sym == SDLK_UP   ) y-=5 ;
                if ( event.key.keysym.sym == SDLK_DOWN ) y+=5 ;    
            }       
            
            boxRGBA(screen, 5, y, 25, y+80, 255,255,255,255);
        
        Sulock(screen);
        SDL_Flip(screen);
    }
}    


P.S what are the code tabs again?
edited by evolutional: The code tabs are [ source ][ /source ] (without spaces). See the FAQ for more information [smile] [Edited by - evolutional on November 22, 2004 1:55:22 PM]
Feidias
Feidias
Checking SDL_KEYDOWN is not the fast way to read the keyboard and it doesn't even have keyrepeat enabled by default. Also, you don't need to check it twice.
Rob Loach
Rob Loach
Try using the following:

Uint8 *keys = SDL_GetKeyState(NULL);if(keys[SDLK_UP]){    // Up is being pushed} else if(keys[SDLK_DOWN]){    // Down is being pushed}
Rob Loach [Website] [Projects] [
Yamian
Yamian
I still don't quite getr it but thank you very much.
Yamian
Yamian
Now I have another problem. How come two paddles don't show up in this program?

#include <stdlib.h>#include <SDL\SDL.h>#include <SDL\SDL_gfxPrimitives.h>void Slock(SDL_Surface *screen){    if ( SDL_MUSTLOCK(screen) )    {        if ( SDL_LockSurface(screen) < 0 )        {            return;        }    }}void Sulock(SDL_Surface *screen){    if ( SDL_MUSTLOCK(screen) )    {        SDL_UnlockSurface(screen);    }}int main(int argc, char *argv[]){    SDL_Init(SDL_INIT_VIDEO);    SDL_Surface *screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);    atexit(SDL_Quit);    SDL_WM_SetCaption("Phlong",NULL);        bool done = false;    while(done == false)    {        SDL_Event event;                while ( SDL_PollEvent(&event) )        {            if ( event.type == SDL_QUIT )  {  done = true;  }            if ( event.type == SDL_KEYDOWN )            {                if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }            }        }        SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));        Slock(screen);        Uint8 *keys = SDL_GetKeyState(NULL);            int ly;            if     (keys[SDLK_LSHIFT])ly-=3;            else if(keys[SDLK_LCTRL] )ly+=3;                        int ry;            if     (keys[SDLK_UP]  )ry-=3;            else if(keys[SDLK_DOWN])ry+=3;                        boxRGBA(screen,     5, ly,     25, ly+80, 255,255,255,255);            boxRGBA(screen, 640-5, ry, 640-25, ry+80, 255,255,255,255);                Sulock(screen);        SDL_Flip(screen);    }}    
Squirm
Squirm
because your screen is 640x480 and you draw the second paddle at 640x640
Yamian
Yamian
No that's not it. The 640-25 is the second x coordinate. I actually replaced ry with ly in the actual drawing stage and the one affected by shift and ctrl shows on the right.
Yamian
Yamian
bizaare. If you switch around the order and make the right paddle checked on the top then only the right one shows up
Yamian
Yamian
I also switch around the variables int h drawing. Maybe theer is something bad about calling the keys array multiple time or something.
Nairb
Nairb
It doesn't look like you're initializing ly and ry, which means you don't know what they're going to be.

I would advise moving int ly and int ry out of your main loop and initializing them both to zero.

Cheers,
--Brian
Yamian
Yamian
Okay that worked, but why does the first one I initialized work but not the second?
coderx04
coderx04
How to check if there is any key being pressed??
Rob Loach
Rob Loach
Quote:
Original post by coderx04
How to check if there is any key being pressed??


SDL_Event event;while ( SDL_PollEvent(&event) ){	if ( event.type == SDL_QUIT )  {  done = true;  }	if ( event.type == SDL_KEYDOWN ){		// User is pressing the "Any" key :-P	}}
Rob Loach [Website] [Projects] [
coderx04
coderx04
ooww thank you, Im giving funny question yeah? :P..
Thankss..

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.