Sign in to follow this  
Yamian

Unity Funky keys

Recommended Posts

Yamian    100
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]

Share this post


Link to post
Share on other sites
Feidias    92
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.

Share this post


Link to post
Share on other sites
Rob Loach    1504
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
}

Share this post


Link to post
Share on other sites
Yamian    100
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);
}
}

Share this post


Link to post
Share on other sites
Yamian    100
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.

Share this post


Link to post
Share on other sites
Yamian    100
I also switch around the variables int h drawing. Maybe theer is something bad about calling the keys array multiple time or something.

Share this post


Link to post
Share on other sites
Nairb    766
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

Share this post


Link to post
Share on other sites
Rob Loach    1504
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

}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this