[SDL] Problem moving a blitted image,

Lamonte(Scheols/Demonic) scheols at gmail.com
Mon Mar 5 17:03:58 PST 2007


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 <string>

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 = 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;
}


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

-- 
Join phpdiscovery.com Now!


More information about the SDL mailing list