[DJGPP] Animation d'un sprite avec Allegro (compatible MS-DOS).

Post Reply
Kitsune
Site Admin
Posts: 21
Joined: Sun Nov 28, 2021 7:09 pm

[DJGPP] Animation d'un sprite avec Allegro (compatible MS-DOS).

Post by Kitsune »

Code source a compiler avec DJGPP et Allegro pour DOS. Celui-ci comporte l'animation d'un sprite d'après un sprite sheet (image contenant plusieurs sprites).
L'animation se compose d'un personnage de type Japan RPG marchant de gauche à droite.

Ce code source fonctionne normalement sour MS-DOS, il a été testé sur Dosbox ainsi que dans un environnement MS-DOS via PCem.

Le fichier image "walk.bmp" est a placer dans le même répertoire que le fichier exécutable.

Code: Select all

#include <stdio.h>
#include <allegro.h>

const int scrx = 640;
const int scry = 480;
volatile int counter = 0; 

void update_counter(){
	counter++;
}
END_OF_FUNCTION(update_counter);
void redraw();



int x=0;
int y=0;

int collumn=0;
int dir=2;

BITMAP *my_pic=NULL;
BITMAP *buffer=NULL;

int main(int argc, char* argv[]) {
	
	int depth=0;
	printf("Before loading the BMP\n");

  if(allegro_init()){
    allegro_message("Cannot initalize Allegro.\n");
    return 1;
  }
  
  if (install_timer()) {
    allegro_message("Cannot initalize Timer.\n");
    return 1;
  }
  
  
  counter = 0;
  LOCK_FUNCTION(update_counter);
  LOCK_VARIABLE(counter);
  install_int_ex(update_counter,MSEC_TO_TIMER(250)); 
  
  //Set the window title when in a GUI environment
  set_window_title("Walk");

  if (install_keyboard()) {
    allegro_message("Cannot initalize keyboard input.\n");
    return 1;
  }

  //set graphics mode, trying all acceptable depths
  set_color_depth(32);
  if (set_gfx_mode(GFX_AUTODETECT, scrx, scry, 0, 0)) {
    set_color_depth(24);
    if (set_gfx_mode(GFX_AUTODETECT, scrx, scry, 0, 0)) {
      set_color_depth(16);
      if (set_gfx_mode(GFX_AUTODETECT, scrx, scry, 0, 0)) {
	set_color_depth(15);
	if (set_gfx_mode(GFX_AUTODETECT, scrx, scry, 0, 0)) {
	  allegro_message("Video Error: %s.\n", allegro_error);
	  return 1;
	}
      }
    }
  }
	//Get the current color depth
	depth=get_color_depth();
		
	//      BITMAP *my_pic=NULL;
		buffer = create_bitmap(SCREEN_W, SCREEN_H);
		my_pic = load_bitmap("WALK.BMP", NULL);
		
		if (!my_pic){
			printf("Error loading picture2.pcx");
			return 1;
		}
		
		
		
       lock_bitmap(my_pic);
//       blit(my_pic,screen,0,0,0,0,my_pic->w,my_pic->h);//Draw the whole bitmap to the screen at (0,0)

  //Wait for a key to be pressed
	while (!keypressed()){
	      if(counter > 0)  //this is going to happen every 3 seconds
	      {
				redraw();
				counter = 0;
				collumn++;
				if(collumn==4)
					collumn=0;
		  }
	    }
		
	//Return to text mode
	set_color_depth(8); 
	set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0);       
	destroy_bitmap(my_pic); 
	       
	remove_int(update_counter);
	allegro_exit();
	return 0;
  //Allegro will automatically deinitalize itself on exit
}
END_OF_MAIN()

void redraw()
{
	int posx=0;
	posx=collumn*64;

 clear_bitmap(buffer);  //clearing the buffer to not leave any trails
 blit(my_pic,buffer,posx,dir*64,x,y,64,64);  //blitting the image to the buffer
 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);  //blitting the buffer to the screen


	if(x>SCREEN_W-64)
	{
		dir=1;
	}
	else if(x<10)
	{
		dir=2;
	}
	
	if(dir== 2)
	{
		x += 10;
	}
	else if(dir==1)
	{
		x -=10;
	}

}
Image
Post Reply