www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/21/04:55:57

Date: Sun, 21 Sep 1997 02:44:34 -0500 (CDT)
From: Andrew Deren <aderen AT eecs DOT uic DOT edu>
To: Laine <laine AT iwn DOT fi>
cc: djgpp AT delorie DOT com
Subject: Re: Sprites with Allegro
In-Reply-To: <01bcc5e3$05bc3600$423e64c2@default>
Message-ID: <Pine.SUN.3.95.970921022257.2325A-100000@ernie.eecs.uic.edu>
MIME-Version: 1.0

On 19 Sep 1997, Laine wrote:

> Hello
> 
> I'm doing a game with DJGPP and Allegro (actually it's my first PC-game)
> and
> I've got a little problem. How can I move sprites, is there a command in
> Allegro
> to do it? 
> And another question: How can I do the double buffering?

Wasn't this posted already before? I guess nobody answered. I'll try to
answer this briefly:

for simplicity of this example let's create a structure that is the
sprite:
typedef struct SPRITE {
   int x_loc, y_loc;  //location of the sprite
   int x_off, y_off; //this the offset of the sprite. How much it moves
each frame or just call it speed
   BITMAP *bmp; //this is the graphics for the sprite
} SPRITE;

SPRITE main_sprite; //this is the sprite variable
RGB *main_pallete;  //this is the pallete

then in your program you have to initizlize all that stuff
void Initialize(SPRITE *spr)
{
   spr->x_loc = 10;
   spr->y_loc = 10;
   spr->x_off = 3;
   spr->y_off = 3;
   spr->bmp = load_bitmap("somebmp.pcx", main_pallete);
}

main()
{
//do all the allegro initization stuff 
//
//...
Initialize(&main_sprite);
while (1) {
   if (key[KEY_ESC]) break; //if esc pressed exit
   else if (key[KEY_UP]) spr.y_loc -= spr.y_off; //move up
   else if (key[KEY_DOWN]) spr.y_loc += spr.y_off;  //move down
   else if (key[KEY_RIGHT]) spr.x_loc += spr.x_off; //move right
   else if (key[KEY_LEFT]) spr.x_loc -= spr.x_off; //move left
   //in here do all the drawing stuff for the sprite depending which 
  //method you use; if you use dirty rectangles you might have to
  //save the location of the sprite before you change it to restore 
  //the previous space
} //end while
}

as of the question about double buffering you can modify the previous
piece of code to make an example out of that
for your global variable add:
BITMAP *double_buffer;
and in main before the while loop
double_buffer = create_bitmap(size_x, size_y);
size_x and size_y are the size of the buffer you want to use. It's the
drawing area, you might not want to blit the whole screen every frame
and then you can add a function for drawing:
void DrawScreen(void)
{
   clear(double_buffer); //clear the double buffer
   //now draw the sprite
   draw_sprite(double_buffer, spr.bmp, spr.x_loc, spr.y_loc);
   //and now blit the double buffer to screen
   blit(double_buffer, screen, 0, 0, screen_x, screen_y, double_buffer->w,
double_buffer->h);
   //screen_x and screen_y is the location of the bitmap on screen
}

you might also change a while loop of this example to add some delay to
it.  
I did not test this piece of code so I do not guarantee it will work.
I hope that helps, and don't hesitate to ask more questions. The more you
ask the more you know.
   

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019