Jump to content
  • Advertisement
Sign in to follow this  
Yamian

Unity Funky keys

This topic is 4956 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

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
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.

Share this post


Link to post
Share on other sites
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
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
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
bizaare. If you switch around the order and make the right paddle checked on the top then only the right one shows up

Share this post


Link to post
Share on other sites
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!