Moving bitmap

Hi all

I am trying my hand at a simple little lunar lander style game. I have
the background bitmap and spaceship bitmapl loaded and I am trying to
get the ship to change postions by setting the dst.x and dst.y rect
members to a temp position, say 582 and 0.

When I make the changes to the code and execute it, the ship still
appears in the upper right of the screen. I have tried the
SDL_UpdateRect function, but have read that this is not needed when
using double buffering?

Here is my code snippet.
// Lunar Rescue Version 1.0
// Copyright © 2004
// Donald Cathcart

#include <sdl.h>
#include <stdlib.h>
#include <stdio.h>

bool Running;
SDL_Surface *screen,*background,playership;
SDL_Rect src, dst;
SDL_Event event;
int SetUp();
int GameLoop();
int main( int argc, char
argv[] )
{

if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
    printf("Unable to initialize SDL: %s\n", SDL_GetError());
    return 1;
}
//exit function
atexit ( SDL_Quit );

//create a window
screen = SDL_SetVideoMode ( 

1024,768,32,SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_FULLSCREEN);
if (screen == NULL)
{
printf(“Unable to set video mode: %s\n”, SDL_GetError());
return 1;
}
SetUp();

//Poll Event Loop

for(;;)
{
   
    if(SDL_PollEvent(&event)!=0)
    {
        printf("inside event poll \n");
        switch(event.type)
        {
        case SDL_KEYDOWN:
           
                if(event.key.keysym.sym==SDLK_ESCAPE)
                {
                    printf("escape pressed \n");
                    return 0;
                }
                break;
        case SDL_QUIT:
            printf("Quitting");
            exit(0);
        }

    }

GameLoop();
}

return 0;

}
int SetUp()
{
// Load player bmp
playership=SDL_LoadBMP(“player.bmp”);
src.x=0;
src.y=0;
src.h=playership->h;
src.w=playership->w;
dst.x=512;
dst.y=0;
dst.h=playership->h;
dst.w=playership->w;
// Load background bmp
background=SDL_LoadBMP(“background.bmp”);
src.x=0;
src.y=0;
src.h=background->h;
src.w=background->w;
dst.x=0;
dst.y=0;
dst.h=background->h;
dst.w=background->w;
return 0;
}
int GameLoop()
{
SDL_BlitSurface(background,&src,screen,&dst);
SDL_BlitSurface(playership,&src,screen,&dst);
SDL_Flip(screen);
//SDL_UpdateRect(screen,0,0,0,0);
return 0;
}

Thanks

You appear to be using src/dst variables for BOTh the background AND the
sprite (space ship).

You should set dst.x and dst.y before your calls SDL_BlitSurface().

Check out some example SDL demos or games out there.
(My “Bug Squish” game took about 2 evenings to write, and is very simple
and small: http://www.newbreedsoftware.com/bugsquish/ … I’m sure there
are some good tutorials out there, too :^) )

Good luck!

-bill!
(wrote a vector-based lunar game recently :wink: )On Wed, Oct 06, 2004 at 08:39:43PM -0300, Donald wrote:

Hi all

I am trying my hand at a simple little lunar lander style game. I have
the background bitmap and spaceship bitmapl loaded and I am trying to
get the ship to change postions by setting the dst.x and dst.y rect
members to a temp position, say 582 and 0.

for(;:wink:
{
if(SDL_PollEvent(&event)!=0)

Usually I see this as “while(…)” rather than “if(…)”.

The problem is that with using an if statement, you will get a maximum
of 1 event per frame. If 20 events happen at the same time, it will take
20 frames for them all to get processed. If a user is typing ___ keys
per minute, unless the game is running at least ____ x 2 frames per
minute, it won’t even be able to keep up with just the SDL_KEYDOWN and
SDL_KEYUP events.

Using a while(…) loop reads all events currently needing processing
that frame.

int SetUp()
{
// Load player bmp
playership=SDL_LoadBMP(“player.bmp”);

Many of these lines end up doing nothing useful in the end, because…
(see next comment)

src.x=0;
src.y=0;
src.h=playership->h;
src.w=playership->w;
dst.x=512;
dst.y=0;
dst.h=playership->h;
dst.w=playership->w;
// Load background bmp
background=SDL_LoadBMP(“background.bmp”);

As soon as you get to the line bellow this comment, you start
overwriting them with new values!!!

src.x=0;
src.y=0;
src.h=background->h;
src.w=background->w;
dst.x=0;
dst.y=0;
dst.h=background->h;
dst.w=background->w;
return 0;
}

int GameLoop()
{
SDL_BlitSurface(background,&src,screen,&dst);
SDL_BlitSurface(playership,&src,screen,&dst);

You’re using the exact same rectanges for both surfaces here. Since you
loaded the values for the background second (which overwrote the player
image rectangle settings) it will use those rectangles for both of them.

Now, if you rearange your code, setting the rectangles when you use them
instead of during your SetUp code:

int GameLoop()
{
//Set up the rectangles for the background
src.x=0;
src.y=0;
src.h=background->h;
src.w=background->w;
dst.x=0;
dst.y=0;
dst.h=background->h;
dst.w=background->w;
//Draw the background
SDL_BlitSurface(background,&src,screen,&dst);
//Set up the rectangles for the player’s ship
src.x=0;
src.y=0;
src.h=playership->h;
src.w=playership->w;
dst.x=512;
dst.y=0;
dst.h=playership->h;
dst.w=playership->w;
//Draw the player’s ship
SDL_BlitSurface(playership,&src,screen,&dst);
//Do other cool stuff
SDL_Flip(screen);
//SDL_UpdateRect(screen,0,0,0,0);
return 0;
}

Your code has a much better chance of working how you wanted (with the
player’s ship showing up at x=512, y=0). The other way to do it would be
to make it so the background and the ship have seperate rectangles, aka
playership_src, playership_dst, background_src, and background_dst.

-Mike