#include <allegro.h> //allegro library
#include "camera.h" //including camera class
#define BACK 1
#define FRONT 2
#define LEFT 3
#define RIGHT 4
/* defining character movements which will be used for
animation in void characterAnimation later on*/
volatile long speed_counter = 0; // A long integer which will store the value of the timer
void increment_speed_counter();
void moveCharacter(void); //character movement
void characterAnimation(void);
void collisionSide(void); //colision up-down
void collisionUp(void); //collision left-right
void change_frame(void);
int character_y; //character movement left-right integer
int character_x;//character movement up-down integer
int direction;
int resolution;
int frame;
int movement;
int character_offset;
int character_width;
camera GameCamera; //declaring the camera class as a global veriable
BITMAP *character_right;
BITMAP *character_left;
BITMAP *character_front;
BITMAP *character_back;
BITMAP *tree;
BITMAP *background; //declaring background bitmap
BITMAP *buffer; //pointer to the buffer
int main(void) // main program starts
{
allegro_init(); //initialising allegro
install_keyboard(); //installing keyboard
install_timer(); //timer so the game will run at the same speed on all PCs
LOCK_VARIABLE(speed_counter); // Used to set the timer which regulates the game speed
LOCK_FUNCTION(increment_speed_counter); //speed counter
set_color_depth(32); // colour depth
resolution=set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); //setting game window resolution
if(0 != resolution)
{
allegro_message("Graphics error. Check dekstop resolution and try again.");
return 0;
}
GameCamera.Init(); // initializing the camera for screen changing
tree = load_bitmap("tree.bmp",NULL);
if(!tree)
{
allegro_message("Error! No tree.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}
character_right = load_bitmap("character_right.bmp",NULL);// ********************************************************
if(!character_right)
{
allegro_message("Error! No character_right.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}
character_left = load_bitmap("character_left.bmp",NULL);// ********************************************************
if(!character_left)
{
allegro_message("Error! No character_left.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}
character_back = load_bitmap("character_back.bmp",NULL);// ********************************************************
if(!character_back)
{
allegro_message("Error! No character_up.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}
character_front = load_bitmap("character_front.bmp",NULL);// ********************************************************
if(!character_front)
{
allegro_message("Error! No character_front.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}
background = load_bitmap("background.bmp",NULL);// ********************************************************
if(!background)
{
allegro_message("Error! No background.bmp in this folder..."); //if no background.bmp in game folder showing error message
return 0;
}
character_x = 93;
character_y = SCREEN_H /2;
buffer = create_bitmap(SCREEN_W, SCREEN_H);
install_int(moveCharacter,6);
install_int(change_frame,100);
frame =0;
character_offset =0;
character_width =60; //width of the character bitmap. No images exceed 60 pixels
direction = FRONT;
movement = 0;
while(!key[KEY_ESC])
{
blit(background,buffer,GameCamera.getcameraX(),GameCamera.getcameraY(),0,0,SCREEN_W,SCREEN_H); //drawing the background to the screen
switch(direction) //animation function for character movement
{
case RIGHT:
switch(frame)
{
case 0:
character_offset= 0;
character_width= 50;
break;
case 1:
character_offset= 51;
character_width= 74;
break;
case 2:
character_offset= 125;
character_width= 65;
break;
case 3:
character_offset= 190;
character_width= 58;
break;
}
if(movement==0)
{
character_offset= 0;
character_width= 50;
}
masked_blit(character_right,buffer,character_offset,0,character_x,character_y,character_width,104);
break;
case LEFT:
switch(frame)
{
case 0:
character_offset= 0;
character_width= 61;
break;
case 1:
character_offset= 62;
character_width= 72;
break;
case 2:
character_offset= 134;
character_width= 58;
break;
case 3:
character_offset= 192;
character_width= 63;
break;
}
if(movement==0)
{
character_offset= 0;
character_width= 61;
}
masked_blit(character_left,buffer,character_offset,0,character_x,character_y,character_width,104);
break;
case FRONT:
switch(frame)
{
case 0:
character_offset= 0;
character_width= 59;
break;
case 1:
character_offset= 60;
character_width= 66;
break;
case 2:
character_offset= 126;
character_width= 67;
break;
case 3:
character_offset= 193;
character_width= 64;
break;
}
if(movement==0)
{
character_offset= 0;
character_width= 59;
}
masked_blit(character_front,buffer,character_offset,0,character_x,character_y,character_width,104);
break;
case BACK:
switch(frame)
{
case 0:
character_offset= 0;
character_width= 59;
break;
case 1:
character_offset= 60;
character_width= 62;
break;
case 2:
character_offset= 122;
character_width= 66;
break;
case 3:
character_offset= 188;
character_width= 57;
break;
}
if(movement==0)
{
character_offset= 0;
character_width= 59;
}
masked_blit(character_back,buffer,character_offset,0,character_x,character_y,character_width,104);
break;
}
blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
clear_bitmap(buffer); //clearing the bitmap
}
return 0;
destroy_bitmap(character_right);
destroy_bitmap(character_left);
destroy_bitmap(character_back);
destroy_bitmap(character_front);
destroy_bitmap(buffer);
destroy_bitmap(screen);
// destroying all bitmaps allows the computer to clear it from the system memory
}
END_OF_MAIN()
void moveCharacter(void) //character movement function
{
int tempx;
int tempy;
movement=0;
if(key[KEY_LEFT])
{
movement = 1;
direction = LEFT;
character_x-=1;
tempx=GameCamera.getcameraX();
if((character_x < 0) && (tempx == 0 ))
{
character_x = 0;
GameCamera.setcameraX(0);
}
if((character_x < 0) && (tempx == 800 ))
{
character_x = 800;
GameCamera.setcameraX(0);
}
if((character_x < 0) && (tempx == 1600 ))
{
character_x = 800;
GameCamera.setcameraX(800);
}
if((character_x < 0) && (tempx == 2400 ))
{
character_x = 800;
GameCamera.setcameraX(1600);
}
}
if(key[KEY_RIGHT])
{
movement =1;
direction = RIGHT;
character_x+=1;
tempx=GameCamera.getcameraX();
if (character_x > 0 && (character_x == 600));
if((character_x > 800) && (tempx == 0 ))
{
character_x = 0;
GameCamera.setcameraX(800);
}
if((character_x > 800) && (tempx == 800))
{
character_x = 0;
GameCamera.setcameraX(1600);
}
if((character_x > 800) && (tempx == 1600 ))
{
character_x = 0;
GameCamera.setcameraX(2400);
}
if((character_x > 800 ) && (tempx == 2400) )
{
character_x = 800;
GameCamera.setcameraX(2400);
}
}
if(key[KEY_UP])
{
movement = 1;
direction = BACK;
character_y-=1;
tempy=GameCamera.getcameraY();
if((character_y < 0) && (tempy == 0 ))
{
character_y = 0;
GameCamera.setcameraY(0);
}
if((character_y < 0) && (tempy == 600 ))
{
character_y = 600;
GameCamera.setcameraY(0);
}
if((character_y < 0) && (tempy == 1200 ))
{
character_y = 600;
GameCamera.setcameraY(600);
}
if((character_y < 0) && (tempy == 1800 ))
{
character_y = 600;
GameCamera.setcameraY(1200);
}
}
if(key[KEY_DOWN])
{
movement =1;
direction = FRONT;
character_y +=1;
tempy=GameCamera.getcameraY();
if((character_y > 600) && (tempy == 0 ))
{
character_y = 0;
GameCamera.setcameraY(600);
}
if((character_y > 600) && (tempy == 600 ))
{
character_y = 0;
GameCamera.setcameraY(1200);
}
if((character_y > 600) && (tempy == 1200 ))
{
character_y = 0;
GameCamera.setcameraY(1800);
}
if((character_y > 600 ) && (tempy == 1800 ))
{
character_y = 600;
GameCamera.setcameraY(1800);
}
}
}
void change_frame() // A function to increment the speed counter
{
frame++; // increment the speed counter by one.
if(frame > 3)
frame=0;
}
END_OF_FUNCTION(change_frame); </code>