Funky keys

Started by
12 comments, last by coderx04 19 years, 4 months ago
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]
Advertisement
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.
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] [Contact]
I still don't quite getr it but thank you very much.
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);    }}    
because your screen is 640x480 and you draw the second paddle at 640x640
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.
bizaare. If you switch around the order and make the right paddle checked on the top then only the right one shows up
I also switch around the variables int h drawing. Maybe theer is something bad about calling the keys array multiple time or something.
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

This topic is closed to new replies.

Advertisement