The Allegro Wiki is migrating to github at https://github.com/liballeg/allegro_wiki/wiki

Animation

From Allegro Wiki
Jump to: navigation, search

This article has been proposed for deletion.

  • Reason: This is just someone's game code, not a reusable animation framework.
  • Marked by: CGamesPlay 18:07, January 4, 2008 (UTC)

This article may be removed if the voting approves the decision. To discuss the deletion of this page, use Talk:Animation.


A simple sprite animation. This code needs to be cleaned up It shows usage of timers, datafile and bliting

Algorithm

To be continued...

Code

File: - sprite.h <highlightSyntax language="cpp">

  1. define asteroids 0 /* BMP 516x519 image with 64 sprites */
  2. define background4 4 /* BMP 640x480 image for background */
  3. define sphere 23 /* BMP 520x269 image with 32 sprites */

</highlightSyntax>

File: - main.cpp <highlightSyntax language="cpp">

  1. include <allegro.h>
  2. include "sprite.h"

int frame_counter = 0; // A counter for which frame to draw DATAFILE *datafile_sprite = NULL; BITMAP *buffer,*bmp,*asteroid,*sphere1,*rot_ball; int frame_asteroid_y=0; int frame_sphere1_y=0;

// Timer stuff, speed counter. 

volatile long speed_counter = 0;

// A function to increment the speed counter // This is called 100 times a second inline void increment_speed_counter() { speed_counter++; // This will just increment the speed counter by one. } END_OF_FUNCTION(increment_speed_counter); // Make sure you tell it that it's the end of the

                                         // function (allegro specific)

int init_all() { int resolution =256;//[256 1024x768], [200 800x600] int screenx = 4*resolution;//width int screeny = 3*resolution;//height if (allegro_init()) { allegro_message("Cannot initalize Allegro.\n"); return 1; }

//set graphics mode, trying all acceptable depths set_color_depth(32); if (set_gfx_mode(GFX_AUTODETECT|GFX_AUTODETECT_WINDOWED, screenx, screeny, 0, 0)) { set_color_depth(24); if (set_gfx_mode(GFX_AUTODETECT|GFX_AUTODETECT_WINDOWED, screenx, screeny, 0, 0)) { set_color_depth(16); if (set_gfx_mode(GFX_AUTODETECT|GFX_AUTODETECT_WINDOWED, screenx, screeny, 0, 0)) { set_color_depth(15); if (set_gfx_mode(GFX_AUTODETECT|GFX_AUTODETECT_WINDOWED, screenx, screeny, 0, 0)) { allegro_message("Video Error: %s.\n", allegro_error); return 1; } } } } install_keyboard(); // Initialize keyboard routines install_timer();// Initialize Timer routines

LOCK_VARIABLE(speed_counter); //Used to set the timer - which regulates the game's LOCK_FUNCTION(increment_speed_counter); //speed. install_int_ex(increment_speed_counter, BPS_TO_TIMER(24)); //Set our BPS for 24 frames per second

datafile_sprite = load_datafile("sprites.dat"); //Check that the datafile was created correctly if(datafile_sprite == NULL) { set_gfx_mode(GFX_TEXT,0,0,0,0); allegro_message("Could not load datafile!"); exit(EXIT_FAILURE); } return 0; } int speed=3; void draw_screen() { stretch_blit(bmp,buffer,0,0,bmp->w,bmp->h,0, 0, SCREEN_W, SCREEN_H);

if(frame_counter >= speed * 0 && frame_counter < speed * 1) {

               //draw the ball to memory

masked_blit(sphere1,buffer,64*0,frame_sphere1_y,SCREEN_W/2,SCREEN_H/2,64,64);

blit(sphere1,rot_ball,64*0,frame_sphere1_y,0,0,64,64);

               //rotate sphere by 90 degree

rotate_sprite(buffer,rot_ball,SCREEN_W/2+150,SCREEN_H/2,itofix(64));//256=360 degree, so 64=90 degree //double the size of asteroid masked_stretch_blit(asteroid,buffer,64*0,frame_asteroid_y,64,64,SCREEN_W/2-150,SCREEN_H/2, 64*2,64*2); } else if(frame_counter >= speed * 1 && frame_counter < speed * 2) { masked_blit(sphere1,buffer,64*1,frame_sphere1_y,SCREEN_W/2,SCREEN_H/2,64,64);

blit(sphere1,rot_ball,64*1,frame_sphere1_y,0,0,64,64); rotate_sprite(buffer,rot_ball,SCREEN_W/2+150,SCREEN_H/2,itofix(64));

masked_stretch_blit(asteroid,buffer,64*1,frame_asteroid_y,64,64,SCREEN_W/2-150,SCREEN_H/2, 64*2,64*2); } else if(frame_counter >= speed * 2 && frame_counter < speed * 3) { masked_blit(sphere1,buffer,64*2,frame_sphere1_y,SCREEN_W/2,SCREEN_H/2,64,64);

blit(sphere1,rot_ball,64*2,frame_sphere1_y,0,0,64,64); rotate_sprite(buffer,rot_ball,SCREEN_W/2+150,SCREEN_H/2,itofix(64));

masked_stretch_blit(asteroid,buffer,64*2,frame_asteroid_y,64,64,SCREEN_W/2-150,SCREEN_H/2, 64*2,64*2); } else if(frame_counter >= speed * 3 && frame_counter < speed * 4) { masked_blit(sphere1,buffer,64*3,frame_sphere1_y,SCREEN_W/2,SCREEN_H/2,64,64);

blit(sphere1,rot_ball,64*3,frame_sphere1_y,0,0,64,64); rotate_sprite(buffer,rot_ball,SCREEN_W/2+150,SCREEN_H/2,itofix(64));

masked_stretch_blit(asteroid,buffer,64*3,frame_asteroid_y,64,64,SCREEN_W/2-150,SCREEN_H/2,64*2,64*2); } else if(frame_counter >= speed * 4 && frame_counter < speed * 5) { masked_blit(sphere1,buffer,64*4,frame_sphere1_y,SCREEN_W/2,SCREEN_H/2,64,64);

blit(sphere1,rot_ball,64*4,frame_sphere1_y,0,0,64,64); rotate_sprite(buffer,rot_ball,SCREEN_W/2+150,SCREEN_H/2,itofix(64));

masked_stretch_blit(asteroid,buffer,64*4,frame_asteroid_y,64,64,SCREEN_W/2-150,SCREEN_H/2, 64*2,64*2); } else if(frame_counter >= speed * 5 && frame_counter < speed * 6) { masked_blit(sphere1,buffer,64*5,frame_sphere1_y,SCREEN_W/2,SCREEN_H/2,64,64);

blit(sphere1,rot_ball,64*5,frame_sphere1_y,0,0,64,64); rotate_sprite(buffer,rot_ball,SCREEN_W/2+150,SCREEN_H/2,itofix(64));

masked_stretch_blit(asteroid,buffer,64*5,frame_asteroid_y,64,64,SCREEN_W/2-150,SCREEN_H/2, 64*2,64*2); } else if(frame_counter >= speed * 6 && frame_counter < speed * 7) { masked_blit(sphere1,buffer,64*6,frame_sphere1_y,SCREEN_W/2,SCREEN_H/2,64,64);

blit(sphere1,rot_ball,64*6,frame_sphere1_y,0,0,64,64); rotate_sprite(buffer,rot_ball,SCREEN_W/2+150,SCREEN_H/2,itofix(64));

masked_stretch_blit(asteroid,buffer,64*6,frame_asteroid_y,64,64,SCREEN_W/2-150,SCREEN_H/2, 64*2,64*2); } else if(frame_counter >= speed * 7 && frame_counter < speed * 8) { masked_blit(sphere1,buffer,64*7,frame_sphere1_y,SCREEN_W/2,SCREEN_H/2,64,64);

blit(sphere1,rot_ball,64*7,frame_sphere1_y,0,0,64,64); rotate_sprite(buffer,rot_ball,SCREEN_W/2+150,SCREEN_H/2,itofix(64));

masked_stretch_blit(asteroid,buffer,64*7,frame_asteroid_y,64,64,SCREEN_W/2-150,SCREEN_H/2, 64*2,64*2); } //prints yellow "Hello World!!!" in middle of screen //textout_centre(buffer, font, "Hello World!!!", SCREEN_W/2, SCREEN_H/2, makecol(255, 255, 0));

textprintf(buffer,font,SCREEN_W/2, SCREEN_H/3,makecol(255, 255, 0),"GAME SPEED=%d",speed_counter);

acquire_screen(); blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);//draw the memory to screen release_screen(); clear(buffer); } bool draw = false; void do_logic() { if(frame_counter >= speed * 7 && frame_counter < speed * 8) { frame_asteroid_y=frame_asteroid_y+64; frame_sphere1_y=frame_sphere1_y+64; frame_counter=0;

if(frame_sphere1_y == 64*4) frame_sphere1_y = 0;

if(frame_asteroid_y == 64*8) frame_asteroid_y = 0; } } //http://wiki.allegro.cc/Timers#Yielding_the_CPU int main(int argc, char* argv[]) { init_all(); buffer = create_bitmap(SCREEN_W,SCREEN_H); // Check that the buffer was created correctly if(buffer == NULL) { set_gfx_mode(GFX_TEXT,0,0,0,0); allegro_message("Could not create buffer!"); exit(EXIT_FAILURE); } bmp =(BITMAP*)datafile_sprite[background4].dat; asteroid = (BITMAP*)datafile_sprite[asteroids].dat; sphere1 = (BITMAP*)datafile_sprite[sphere].dat; rot_ball = create_bitmap(64, 64); while(!key[KEY_ESC]) // while game over / ESC. { //while(speed_counter <= 0) rest(0); while(speed_counter > 0) // Do the logic loop while the speed counter is > 0. { speed_counter --; // Decrement the speed counter. /* Increment our frame counter at the same speed of the speed counter. */ frame_counter ++; if(frame_counter > 24) // (When frame_counter = 24, 1 second has passed) { // to control speed of animation include more/less frame_counter = 0; // frames within frame_counter=24,at present there are 8. } do_logic(); draw = true; }

if (draw) { draw_screen(); //do the part of draw_screen() that actually does the drawing draw = false; } if (speed_counter < 1) rest(1); }

//Destroy all bitmaps destroy_bitmap(buffer); destroy_bitmap(rot_ball); unload_datafile(datafile_sprite); allegro_exit(); return 0; } END_OF_MAIN()

</highlightSyntax>