[SDL] Problem moving a blitted image,
Lamonte(Scheols/Demonic)
scheols at gmail.com
Mon Mar 5 19:20:59 PST 2007
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 <string>
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 = n*32;
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 <wizard at syntheticsw.com> 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!
More information about the SDL
mailing list