SDL how to move an object by holding a button rather then pressing the button over and over
#1 Members - Reputation: 142
Posted 06 December 2012 - 07:55 PM
I just learned how to move an object which was successful. You press a button once to move it, but you can't hold it down to keep the object moving, you have to keep tapping the button to make move more.
Is there a certain way or certain code to make it so that you can just hold the button to keep moving and let go whenever you want to stop?
#2 Marketplace Seller - Reputation: 8941
Posted 06 December 2012 - 08:29 PM
A) On button press, set bool "move" to true. On button release, set bool "move" to false. (You have to create such a bool yourself). Every frame, if "move" is true, move the player. Realistically, you'll want several bools, because you can move in several directions, or alternatively, two ints (x,y) holding the amount to move each frame, positively or negatively.
B) Let SDL handle the "move" boolean for you, and instead call SDL_GetKeyState() which returns a bool array of most of the keyboard keys and whether they are currently being held or not (see documentation link). Every frame you can then check if the key is held down, and move the player.
Edited by Servant of the Lord, 06 December 2012 - 08:30 PM.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
#3 Members - Reputation: 142
Posted 07 December 2012 - 12:44 AM
ship.bmp 2.05K
24 downloads
#include "SDL.h"
int main(int argc, char *argv[])
{
bool bRun = true;
SDL_Surface *screen , *ship;
SDL_Rect shipRect;
shipRect.x = 100 ;
shipRect.y = 100 ;
SDL_WM_SetCaption("Fryday", NULL);
screen = SDL_SetVideoMode( 256 , 224 , 32 , SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_ANYFORMAT);
SDL_FillRect(screen , NULL , 0x221122);
ship = SDL_LoadBMP("./ship.bmp");
SDL_SetColorKey( ship, SDL_SRCCOLORKEY, SDL_MapRGB(ship->format, 255, 0, 255) );
SDL_BlitSurface( ship , NULL , screen , &shipRect );
SDL_Flip(screen);
SDL_Event event;
while(bRun) {
bool keysHeld[323] = {false}; // everything will be initialized to false
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
bRun = false;
}
if (event.type == SDL_KEYDOWN)
{
keysHeld[event.key.keysym.sym] = true;
}
if (event.type == SDL_KEYUP)
{
keysHeld[event.key.keysym.sym] = false;
}
if ( keysHeld[SDLK_ESCAPE] )
{
bRun = false;
}
if ( keysHeld[SDLK_LEFT] )
{
shipRect.x -= 1;
}
if ( keysHeld[SDLK_RIGHT] )
{
shipRect.x += 1;
}
if ( keysHeld[SDLK_UP] )
{
shipRect.y -= 1;
}
if (keysHeld[SDLK_DOWN])
{
shipRect.y += 1;
}
}
}; // while(bRun) { END
return 0;
}
#4 Members - Reputation: 326
Posted 07 December 2012 - 01:25 AM
if event.key == event.keydown: if event.ket == event.K_UP: ship.location.x -= 10 // Pygame takes (0,0) top left screen MAX_width,MAX_height) to be bottom right screen
Your code handles keydown alone versus handling with some specific key being pressed AND held. Hope this helps.
Edited by Cdrandin, 07 December 2012 - 01:26 AM.
#5 Members - Reputation: 810
Posted 07 December 2012 - 01:30 AM
#6 Marketplace Seller - Reputation: 8941
Posted 07 December 2012 - 02:41 PM
- Load resources
- Initialize game settings
- While (still playing)
- Get all the player input (
ifwhile(SDL_PollEvent(&event)) ) - Update the game objects (by deltaTime*)
- Do any game thinking (optionally part of the updating code instead)
- Render:
- Clear the entire screen
- Draw every object from back to front
- Draw the GUI
- Flip the screen buffers
[*]Free game resources
[/list]*A simple deltaTime is 'currentTime - previousTime'. This is the amount of time passed since the last frame. See SDL_GetTicks(). Others prefer a fixed timestep, but for beginner projects, 'currentTime - previousTime' might be simpler, and works just fine.
Your game should follow that basic pattern. 99% of games follow that basic pattern (with minor tweaks and variations here or there).
You should go through Lazy Foo's SDL tutorials.
Particularly:
- Lesson 4: Event Driven Programming
- Lesson 10: Keystates
- Lesson 14: Regulating Frame Rate
- Lesson 32: Frame independent movement
You also should read Lazy Foo's article on Game Loops, and if you have any questions, ask here and we'll be glad to help.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
#7 Crossbones+ - Reputation: 264
Posted 08 December 2012 - 05:43 AM
The language I use has KeyDown(Upkey) vs. KeyHit(Upkey) to test whether a key is held or simply pressed.
Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!
#8 Marketplace Seller - Reputation: 8941
Posted 08 December 2012 - 11:30 AM
A) You do your event looping in an if() instead of a while()
B) You don't ever actually initialize keyHeld[], leaving it set as false, thus never triggering the movement.
C) You have your keyHeld[] code inside your event loop when it should be outside. (Polling input states should usually be outside the event loop, handling input events should be inside the event loop)
Not in C++.BigBadBear - I am no expert in the language you are using, but ( IF ) I am reading your code correctly ( THEN ) in your IF Keysheld statement, Romove The Equal Sign ( = ) after your Plus/Minus ( + / - ) signs and your ship should move.
var1 + var2 doesn't change the result of 'var1', you'd have to do var1 = var1 + var2. Using += is shorthand for that. var1 += var2 does change var1 properly.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
#9 Crossbones+ - Reputation: 264
Posted 08 December 2012 - 03:21 PM
Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!
#10 Members - Reputation: 1551
Posted 10 December 2012 - 01:04 PM
struct TInput {
bool IsLeftPressed;
bool IsRightPressed;
bool IsUpPressed;
bool IsFirePressed;
bool IsJumpPressed;
};
// create this funciton to get the current keys being pressed
void GetInput(TInput& input)
{
while(PollKeyEvent(&keyEvent) {
if (keyEvent.IsPressed) {
if (keyEvent.keycode == LEFT_KEY) {
input.IsLeftPressed;
}
if (keyEvent.keycode == UP_KEY) {
input.IsUpPressed;
}
// Continue checking them all
}
}
}
// in Main loop
while(IsRunning) {
TInput input;
GetInput(input);
// now pass the input into your Update functions, which would check input.IsFirePressed to see if the user is pressing fire, etc.
Player.Update(input);
// Call other update functions for enemies, objects, background, etc as well as rendering for them.
Edited by BeerNutts, 10 December 2012 - 01:06 PM.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)






