Advertisement

while key pressed

Started by October 29, 2004 02:57 PM
11 comments, last by raptorstrike 19 years, 10 months ago
ok i just want to have an object move around the screen while a botton is down not every time i press it how would i code this in sdl thanks[smile]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
you mean a GUI style button, or a keypress, or what?

if its a keyboard, post what you have tried. it should look something like this

Uint8 *keys = SDL_GetKeyState();

if(keys[SDLK_SOMEKEY])
{
the key is pressed
}
FTA, my 2D futuristic action MMORPG
Advertisement
yeah thats what i have (sorry it took a while to get back to you) and it just dose the movment when the key is pressed not if its held down
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
thats odd. for me it works when the key is held down. post the code that you use to take input and stuff.
FTA, my 2D futuristic action MMORPG
ok its pretty bare bones
#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <string.h>#include <libiberty.h>#include "Ship.h"//#include "Particals.h"#include "SDL.h"/* The screen surface */SDL_Surface *screen = NULL;SDL_Surface *I_Ship = NULL;SDL_Surface *I_Exaust=NULL;SDL_Rect* Partical_rects; int SDL_Keys[5];Ship* a = new Ship;int X = 5;int Y = 5;static void draw();/* This function draws to the screen; replace this with your own code! */    intmain (int argc, char *argv[]){    Uint8 *keys = SDL_GetKeyState(SDL_Keys);    char *msg;    int done;    /* Initialize SDL */    if (SDL_Init (SDL_INIT_VIDEO) < 0)    {        asprintf (&msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());        MessageBox (0, msg, "Error", MB_ICONHAND);        free (msg);        exit (1);    }    atexit (SDL_Quit);    /* Set 640x480 16-bits video mode */    screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);    if (screen == NULL)    {       asprintf (&msg, "Couldn't set 640x480x16 video mode: %s\n",       SDL_GetError ());        MessageBox (0, msg, "Error", MB_ICONHAND);        free (msg);        exit (2);    }    SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);    I_Ship = SDL_LoadBMP("Ship.bmp");    I_Exaust = SDL_LoadBMP("Exaust.bmp");           done = 0;    while (!done)    {        SDL_Event event;        /* Check for events */        while (SDL_PollEvent (&event))        {                          switch (event.type)            {             case SDL_KEYDOWN:                                    if(keys[SDLK_UP])                    {                    a->R_Ship.x+=5;                    };                    break;            case SDL_QUIT:                done = 1;                break;            default:                break;            }        }                /* Draw to screen */        draw ();    }    return 0;}static voiddraw (){     SDL_BlitSurface(I_Ship,NULL,screen,&a->R_Ship);    SDL_Flip (screen);    /* Don't run too fast */    SDL_Delay (1);    }

thanks agin for your help [grin]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
I had a similar issue...try adding another case for SDL_KEYUP that will zero the x value. If that doesn't work, post again.
Things change.
Advertisement
Quote: Original post by raptorstrike
ok its pretty bare bones
*** Source Snippet Removed ***
thanks agin for your help [grin]


That would be something like this.. (don't forget to look at the in-code comments).

#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <string.h>#include <libiberty.h>#include "Ship.h"//#include "Particals.h"#include "SDL.h"/* The screen surface */SDL_Surface *screen = NULL;SDL_Surface *I_Ship = NULL;SDL_Surface *I_Exaust=NULL;SDL_Rect* Partical_rects; int SDL_Keys[5];Ship* a = new Ship;int X = 5;int Y = 5;static void draw();/* This function draws to the screen; replace this with your own code! */    intmain (int argc, char *argv[]){    Uint8 *keys = SDL_GetKeyState(SDL_Keys);    char *msg;    int done;    /* Initialize SDL */    if (SDL_Init (SDL_INIT_VIDEO) < 0)    {        asprintf (&msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());        MessageBox (0, msg, "Error", MB_ICONHAND);        free (msg);        exit (1);    }    atexit (SDL_Quit);    /* Set 640x480 16-bits video mode */    screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);    if (screen == NULL)    {       asprintf (&msg, "Couldn't set 640x480x16 video mode: %s\n",       SDL_GetError ());        MessageBox (0, msg, "Error", MB_ICONHAND);        free (msg);        exit (2);    }    SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);    I_Ship = SDL_LoadBMP("Ship.bmp");    I_Exaust = SDL_LoadBMP("Exaust.bmp");           done = 0;    while (!done)    {        SDL_Event event;        Uint8 *keys; // this is where we store the pressed keys        /* Check for events */        while (SDL_PollEvent (&event))        {            keys = SDL_GetKeyState(); /* get the pressed keys */            if(keys[SDLK_UP]) /* check for up */            {                a->R_Ship.x+=5; /* do something */            }               switch (event.type)            {             case SDL_QUIT:                done = 1;                break;            default:                break;            }        }                /* Draw to screen */        draw ();    }    return 0;}static voiddraw (){     SDL_BlitSurface(I_Ship,NULL,screen,&a->R_Ship);    SDL_Flip (screen);    /* Don't run too fast */    SDL_Delay (1);    }


Good luck :)

/edit:
Oh, C style comments :)
ok it works not it dose lag witch is kinda annoying i would normaly not mind much but seeing as this is going to be my first
A)SDL game
and
B)My first pollished game

i kinda want to avoid this so is there any way to fix this?

another note:

i think the code is getting stuck in the while loop you seggested because when i try to exit the X botton dosnt work leading me to belive that the Quit function is exicuted witch makes me think that the whole code isnt exicuted
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Quote: Original post by raptorstrike
so do you want me to do this?? when you mean "seprate case"

*** Source Snippet Removed ***


Uhh...no, that's not what I meant. From the SDL documentation:

    /* Alien screen coordinates */    int alien_x=0, alien_y=0;    int alien_xvel=0, alien_yvel=0;    .    .    /* Initialise SDL and video modes and all that */    .    /* Main game loop */    /* Check for events */    while( SDL_PollEvent( &event ) ){        switch( event.type ){            /* Look for a keypress */            case SDL_KEYDOWN:                /* Check the SDLKey values and move change the coords */                switch( event.key.keysym.sym ){                    case SDLK_LEFT:                        alien_xvel = -1;                        break;                    case SDLK_RIGHT:                        alien_xvel =  1;                        break;                    case SDLK_UP:                        alien_yvel = -1;                        break;                    case SDLK_DOWN:                        alien_yvel =  1;                        break;                    default:                        break;                }                break;            /* We must also use the SDL_KEYUP events to zero the x */            /* and y velocity variables. But we must also be       */            /* careful not to zero the velocities when we shouldn't*/            case SDL_KEYUP:                switch( event.key.keysym.sym ){                    case SDLK_LEFT:                        /* We check to make sure the alien is moving */                        /* to the left. If it is then we zero the    */                        /* velocity. If the alien is moving to the   */                        /* right then the right key is still press   */                        /* so we don't tocuh the velocity            */                        if( alien_xvel < 0 )                            alien_xvel = 0;                        break;                    case SDLK_RIGHT:                        if( alien_xvel > 0 )                            alien_xvel = 0;                        break;                    case SDLK_UP:                        if( alien_yvel < 0 )                            alien_yvel = 0;                        break;                    case SDLK_DOWN:                        if( alien_yvel > 0 )                            alien_yvel = 0;                        break;                    default:                        break;                }                break;                        default:                break;        }    }    .    .    /* Update the alien position */    alien_x += alien_xvel;    alien_y += alien_yvel;


I hope that all formatted correctly. Anyway, SDL only returns ONE keypress event, the first time it's pressed.
Things change.
i find the Windows Key method much better (although thats one of the few things) is there any way to utalize it with SDL?

[Edited by - raptorstrike on October 30, 2004 9:27:30 PM]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie

This topic is closed to new replies.

Advertisement