Problem moving a blitted image,

Alright I’ve came to a problem and found out that even time I move my
image I have to constantly click down on the DOWN arrow key to move it
and It won’t keep moving as I hold the arrow keys. Does anyone know
how to make my script so when I hold down on the arrow keys that it
keeps moving until I release up on a arrow key?

Source below:###################################

//The headers #include “SDL.h”
#include “SDL/SDL.h”
#include

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 420;
const char* WINDOW_TITLE = “Manage Image Movement Inside a window!”;
int n;

int main(int argc, char **argv)
{
SDL_Init( SDL_INIT_VIDEO );

SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( WINDOW_TITLE, 0 );

SDL_Surface* bgMAP = SDL_LoadBMP(“6-5.bmp”);
SDL_Surface* bitmap = SDL_LoadBMP(“image1.bmp”);

// Part of the bitmap that we want to draw
SDL_Rect source;
source.x = 0;
source.y = 0;
source.w = 41;
source.h = 41;

// Part of the screen we want to draw the sprite to
SDL_Rect destination;
destination.x = 0;
destination.y = 0;
destination.w = 41;
destination.h = 41;

SDL_Event event;
bool gameRunning = true;

while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RIGHT)
if(destination.x >= (WINDOW_WIDTH-source.w))
destination.x–;
else
destination.x++;
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_LEFT)
if(destination.x <= 0)
destination.x++;
else
destination.x–;
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_DOWN)
if(destination.y >= (WINDOW_HEIGHT-source.h))
destination.y–;
else
destination.y++;
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_UP)
if(destination.x <= 0)
destination.y++;
else
destination.y–;
}

// Part of the bitmap that we want to draw
SDL_Rect MAP;
MAP.x = 0;
MAP.y = 0;
MAP.w = 32;
MAP.h = 32;

// Part of the screen we want to draw the sprite to
SDL_Rect MAPDES;
for ( n=0 ; n <= 40 ; n++ ){
for( int me=0; me <= 40;me++){
MAPDES.x = n32;
MAPDES.y = me
32;
//MAPDES.y = 0;
MAPDES.w = 32;
MAPDES.h = 32;

  SDL_BlitSurface(bgMAP, &MAP, screen, &MAPDES);
  SDL_BlitSurface(bitmap, &source, screen, &destination);

}}
SDL_Flip(screen);
}

SDL_FreeSurface(bitmap);
SDL_FreeSurface(bgMAP);

SDL_Quit();

return 0;
}

###################################


Join phpdiscovery.com Now!

Hello !

Alright I’ve came to a problem and found out that even time I move my
image I have to constantly click down on the DOWN arrow key to move it
and It won’t keep moving as I hold the arrow keys. Does anyone know
how to make my script so when I hold down on the arrow keys that it
keeps moving until I release up on a arrow key?

SDL uses the OS event system, which most of the time
acts like a state machine. Which means it only send events
when something changes.

bool left = false;
bool right = false;
bool up = false;
bool down = false;

Now if you get an Keydown event set the meaned boolean true
like Keydown Event + Left Key: left = true;

KeyUp Event + Left Key: left=false;

For the rest of the keys do the same or use SDL_GetKeyState.

Then in your loop simply asks if left == true the move left.
The same with the other keys.

CU

Alright ill try it, im going to recode this script using a switch
statement then make the bools go back to false when I release the key
:slight_smile: ill report with errors that I have and such.On 3/5/07, Torsten Giebl wrote:

Hello !

Alright I’ve came to a problem and found out that even time I move my
image I have to constantly click down on the DOWN arrow key to move it
and It won’t keep moving as I hold the arrow keys. Does anyone know
how to make my script so when I hold down on the arrow keys that it
keeps moving until I release up on a arrow key?

SDL uses the OS event system, which most of the time
acts like a state machine. Which means it only send events
when something changes.

bool left = false;
bool right = false;
bool up = false;
bool down = false;

Now if you get an Keydown event set the meaned boolean true
like Keydown Event + Left Key: left = true;

KeyUp Event + Left Key: left=false;

For the rest of the keys do the same or use SDL_GetKeyState.

Then in your loop simply asks if left == true the move left.
The same with the other keys.

CU


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Join phpdiscovery.com Now!

Hello !

Alright I’ve came to a problem and found out that even time I move my
image I have to constantly click down on the DOWN arrow key to move it
and It won’t keep moving as I hold the arrow keys. Does anyone know
how to make my script so when I hold down on the arrow keys that it
keeps moving until I release up on a arrow key?

Look at this, too :

http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fGetKeyState

CU

meh If you looked at my code it already does everything your talking
about(bet you aint even look at the source…)

Problem: The Blitted image is moving slow and won’t keep moving when
i hold the DOWN arrow… How can I fix my code to do that?

[SOURCE]
//The headers #include “SDL.h”
#include “SDL/SDL.h”
#include

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 420;
const char* WINDOW_TITLE = “Manage Image Movement Inside a window!”;
int n;

bool left = false;
bool right = false;
bool up = false;
bool down = false;

int main(int argc, char **argv)
{
SDL_Init( SDL_INIT_VIDEO );

SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( WINDOW_TITLE, 0 );

SDL_Surface* bgMAP = SDL_LoadBMP(“6-5.bmp”);
SDL_Surface* bitmap = SDL_LoadBMP(“image1.bmp”);

// Part of the bitmap that we want to draw
SDL_Rect source;
source.x = 0;
source.y = 0;
source.w = 41;
source.h = 41;

// Part of the screen we want to draw the sprite to
SDL_Rect destination;
destination.x = 0;
destination.y = 0;
destination.w = 41;
destination.h = 41;

SDL_Event event;
bool gameRunning = true;

while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}

if(event.type == SDL_KEYDOWN){

	switch(event.key.keysym.sym){


		case SDLK_RIGHT:

	          if(destination.x >= (WINDOW_WIDTH-source.w))
                            destination.x--;
                      else
                            destination.x++;


		break;
		case SDLK_LEFT:

                      if(destination.x <= 0)
                            destination.x++;
                      else
                            destination.x--;


		break;
		case SDLK_DOWN:

       	          if(destination.y >= (WINDOW_HEIGHT-source.h))
                            destination.y--;
                       else
                            destination.y++;


		break;
		case SDLK_UP:
		
		
                      if(destination.x <= 0)
                            destination.y++;
                      else
                            destination.y--;

		break;
	

	}


}else if(event.type == SDL_KEYUP){

	switch(event.key.keysym.sym){


		case SDLK_RIGHT:

		right = false;

		break;
		case SDLK_LEFT:

		left = false;

		break;
		case SDLK_DOWN:

		down = false;

		break;
		case SDLK_UP:
		
		up = false;

		break;
	

	}

}
  }

// Part of the bitmap that we want to draw
SDL_Rect MAP;
MAP.x = 0;
MAP.y = 0;
MAP.w = 32;
MAP.h = 32;

// Part of the screen we want to draw the sprite to
SDL_Rect MAPDES;
for ( n=0 ; n <= 40 ; n++ ){
for( int me=0; me <= 40;me++){
MAPDES.x = n32;
MAPDES.y = me
32;
//MAPDES.y = 0;
MAPDES.w = 32;
MAPDES.h = 32;

  SDL_BlitSurface(bgMAP, &MAP, screen, &MAPDES);
  SDL_BlitSurface(bitmap, &source, screen, &destination);

}}
SDL_Flip(screen);
}

SDL_FreeSurface(bitmap);
SDL_FreeSurface(bgMAP);

SDL_Quit();

return 0;
}
[/SOURCE]On 3/5/07, Torsten Giebl wrote:

Hello !

Alright I’ve came to a problem and found out that even time I move my
image I have to constantly click down on the DOWN arrow key to move it
and It won’t keep moving as I hold the arrow keys. Does anyone know
how to make my script so when I hold down on the arrow keys that it
keeps moving until I release up on a arrow key?

Look at this, too :

http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fGetKeyState

CU


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Join phpdiscovery.com Now!

SDL_EnableKeyRepeat(delay, interval);On Mon March 5 2007 17:03, Lamonte(Scheols/Demonic) wrote:

Alright I’ve came to a problem and found out that even time I move my
image I have to constantly click down on the DOWN arrow key to move it
and It won’t keep moving as I hold the arrow keys. Does anyone know
how to make my script so when I hold down on the arrow keys that it
keeps moving until I release up on a arrow key?

meh If you looked at my code it already does everything your talking
about(bet you aint even look at the source…)

The users of this mailing list aren’t required to fix your bugs. Please
treat them graciously even if you feel their answers are not satisfactory.

Torsten gave you good advice, and actually told you exactly what the
problem was. SDL_KEYDOWN does not fire with any frequency. It happens
once when you push down the key, and SDL_KEYUP happens when you let go.
Checking for SDL_KEYDOWN each time through the loop won’t help, since it
will only happen once.

If you want to know if a key is (still) pressed down every frame, use
SDL_GetKeyState() or track it yourself. Torsten told you this, too.

Jeff correctly told you that if you want to have SDL_KEYDOWN show up
multiple times when you hold down a key, use SDL_EnableKeyState().

And I, who did read your source code, will tell you: you have a bug
with where you read and write the “up”, “down”, “left” and "right"
variables…and also, where you don’t.

Also, this:

                      if(destination.x <= 0)
                            destination.x++;
                      else
                            destination.x--;

…will keep destination.x between 0 and 1, which probably wasn’t what
you wanted.

–ryan.

actually:
if(destination.x <= 0)
destination.x++;
else
destination.x–;

will keep my image to stay inside the window making it stoping it self
from leaving the window so if I tried to go beyond 0(as in -1,-2,-3)
in the coordinates it will make my image stop.

I didn’t know Jeff said anything because I was sleep and didn’t reat this email

meh If you looked at my code it already does everything your talking
about(bet you aint even look at the source…)

The users of this mailing list aren’t required to fix your bugs. Please
treat them graciously even if you feel their answers are not satisfactory.
I was asking how could I fix this bug because my image was moving slow
and I had to repeatedly press the different keys. Plus im new to SDL
give me a break -_-

Torsten gave you good advice, and actually told you exactly what the
problem was. SDL_KEYDOWN does not fire with any frequency. It happens
once when you push down the key, and SDL_KEYUP happens when you let go.
Checking for SDL_KEYDOWN each time through the loop won’t help, since it
will only happen once.

If you want to know if a key is (still) pressed down every frame, use
SDL_GetKeyState() or track it yourself. Torsten told you this, too.

Jeff correctly told you that if you want to have SDL_KEYDOWN show up
multiple times when you hold down a key, use SDL_EnableKeyState().
naw he said: SDL_EnableKeyRepeat(delay, interval);On 3/5/07, Ryan C. Gordon wrote:

And I, who did read your source code, will tell you: you have a bug
with where you read and write the “up”, “down”, “left” and "right"
variables…and also, where you don’t.

Also, this:

                      if(destination.x <= 0)
                            destination.x++;
                      else
                            destination.x--;

…will keep destination.x between 0 and 1, which probably wasn’t what
you wanted.

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Join phpdiscovery.com Now!

if(destination.x <= 0)
destination.x++;
else
destination.x–;

This looks like you try to increase the destination only when its
lower or equal to zero. I assume you try to move to the left.

Although this will not fix your problem, it is already mentioned btw,
i think you can better rewrite the lines in a more logical term. What
i mean, is that is clear what the code should DO. Instead of telling
it should ‘correct’ itself. Ie, ONLY move to the left when you may do
that, instead of saying “move to the left, but correct it when you
move to far to the left”.

In short:

// Only move to the left when we can move to the left…
if (destination.x > 0)
destination.x–;

My 2 cents.

Hey btw, i am new too.

-Stefan

2007/3/6, Lamonte(Scheols/Demonic) :

actually:
if(destination.x <= 0)
destination.x++;
else
destination.x–;

will keep my image to stay inside the window making it stoping it self
from leaving the window so if I tried to go beyond 0(as in -1,-2,-3)
in the coordinates it will make my image stop.

I didn’t know Jeff said anything because I was sleep and didn’t reat this email

> On 3/5/07, Ryan C. Gordon wrote:

Stefan Hendriks

http://www.fundynamic.nl

Well none of that was really needed since my code wasn’t the problem
Thanks jeff " SDL_EnableKeyRepeat(25, 20);" works like a charm :).
Rest of my code works perfectly.

Thanks Stefan but what I have works the same as what you have so that
really was a problem(since I really didn’t ask that question ;)).

Regards,
Lamonte.On 3/6/07, Stefan Hendriks wrote:

if(destination.x <= 0)
destination.x++;
else
destination.x–;

This looks like you try to increase the destination only when its
lower or equal to zero. I assume you try to move to the left.

Although this will not fix your problem, it is already mentioned btw,
i think you can better rewrite the lines in a more logical term. What
i mean, is that is clear what the code should DO. Instead of telling
it should ‘correct’ itself. Ie, ONLY move to the left when you may do
that, instead of saying “move to the left, but correct it when you
move to far to the left”.

In short:

// Only move to the left when we can move to the left…
if (destination.x > 0)
destination.x–;

My 2 cents.

Hey btw, i am new too.

-Stefan

2007/3/6, Lamonte(Scheols/Demonic) <@Lamonte>:

actually:
if(destination.x <= 0)
destination.x++;
else
destination.x–;

will keep my image to stay inside the window making it stoping it self
from leaving the window so if I tried to go beyond 0(as in -1,-2,-3)
in the coordinates it will make my image stop.

I didn’t know Jeff said anything because I was sleep and didn’t reat this email

On 3/5/07, Ryan C. Gordon wrote:

Stefan Hendriks

http://www.fundynamic.nl


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Join phpdiscovery.com Now!