Original Post
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]
#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); }Quote:
Original post by raptorstrike
ok its pretty bare bones
*** Source Snippet Removed ***
thanks agin for your help [grin]
#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); }Quote:
Original post by raptorstrike
so do you want me to do this?? when you mean "seprate case"
*** Source Snippet Removed ***
/* 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;class Input{ public: Input(); ~Input(); void Update(); void Process_Input_Message(const SDL_Event &event); //getting key functions bool Cur_Key(int index) { return keys[index] != 0; } bool Old_Key(int index) { return old_keys[index] != 0; } bool Key_Down(int index) { return ( Cur_Key(index))&&(!Old_Key(index)); } bool Key_Still_Down(int index) { return ( Cur_Key(index))&&( Old_Key(index)); } bool Key_Up(int index) { return (!Cur_Key(index))&&( Old_Key(index)); } bool Key_Still_Up(int index) { return (!Cur_Key(index))&&(!Old_Key(index)); } //now the mouse functions bool Cur_Mouse(int button) { return (buttons&SDL_BUTTON(button))!=0; } bool Old_Mouse(int button) { return (old_buttons&SDL_BUTTON(button))!=0; } bool Mouse_Down(int button) { int x,y; SDL_GetMouseState(&x,&y); if(y >= 0) return ( Cur_Mouse(button))&&(!Old_Mouse(button)); } bool Mouse_Still_Down(int button){ return ( Cur_Mouse(button))&&( Old_Mouse(button)); } bool Mouse_Up(int button){ return (!Cur_Mouse(button))&&( Old_Mouse(button)); } bool Mouse_Still_Up(int button){ return (!Cur_Mouse(button))&&(!Old_Mouse(button)); } private: //keyboard int key_count; unsigned char *keys, *old_keys; //mouse unsigned int buttons; unsigned int old_buttons;};Input::Input(){ unsigned char *temp_keys = SDL_GetKeyState(&key_count); keys = new unsigned char [key_count];//(unsigned char*)malloc(sizeof(unsigned char) * key_count); memcpy(keys,temp_keys,key_count); old_keys = new unsigned char [key_count];// (unsigned char*)malloc(sizeof(unsigned char) * key_count);}void Input::Process_Input_Message(const SDL_Event &event){ switch(event.type) { case SDL_QUIT: { } break; case SDL_KEYDOWN: { } break; }//end of switch event.type}void Input::Update(){ SDL_Event event; while(SDL_PollEvent(&event)) { Process_Input_Message(event); } //for keyboard memcpy(old_keys,keys,key_count); unsigned char *temp_keys = SDL_GetKeyState(&key_count); memcpy(keys,temp_keys,key_count); //for mouse old_buttons = buttons; buttons = SDL_GetMouseState(NULL,NULL);}Input::~Input(){ delete [] keys; delete [] old_keys;}This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more