1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <allegro.h>
#include <stdlib.h>
volatile long speed_counter = 0; //integer to store value of the speed counter
void increment_speed_counter() //function to increment the speed counter
{
speed_counter++;
}
END_OF_FUNCTION(increment_speed_counter);
int main(int argc, char *argv[])
{
allegro_init();
install_keyboard();
install_timer();
LOCK_VARIABLE(speed_counter);
LOCK_FUNCTION(increment_speed_counter);
install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, 640,480,0,0);
BITMAP *buffer = create_bitmap(640,480); //create a buffer for smooth animation
if(buffer == NULL)
{
set_gfx_mode(GFX_TEXT,0,0,0,0);
allegro_message("Could not create buffer!");
exit(EXIT_FAILURE);
}
BITMAP *image1 = load_bitmap("C:/Dev-Cpp/Teh Stuph/AL8/image1.bmp", NULL);
if(image1 == NULL)
{
set_gfx_mode(GFX_TEXT,0,0,0,0); //sets screen mode for allegro messages
allegro_message("Could not load image1.bmp");
exit(EXIT_FAILURE);
}
BITMAP *image2 = load_bitmap ("C:/Dev-Cpp/Teh Stuph/AL8/image2.bmp", NULL);
if(image2 == NULL)
{
set_gfx_mode(GFX_TEXT,0,0,0,0);
allegro_message("Could not load image2.bmp");
exit(EXIT_FAILURE);
}
int image1_x_position = 0;
int image1_y_position = 0;
int image2_x_position = 100;
int image2_y_position = 100;
int image1_bb_left = image1_x_position;
int image1_bb_top = image1_y_position;
int image1_bb_right = (image1_bb_left + image1->w);
int image1_bb_bottom = (image1_bb_top + image1->h);
int image2_bb_left = image2_x_position;
int image2_bb_top = image2_y_position;
int image2_bb_right = (image2_bb_left + image2->w);
int image2_bb_bottom = (image2_bb_top + image2->h);
int show_bbox = FALSE;
int collision = FALSE;
while(!key[KEY_ESC])
{
while(speed_counter > 0)
{
if(key[KEY_LEFT])
image1_x_position --;
if(key[KEY_RIGHT])
image1_x_position ++;
if(key[KEY_DOWN])
image1_y_position ++;
if(key[KEY_UP])
image1_y_position --;
if(key[KEY_A])
image2_x_position --;
if(key[KEY_D])
image2_x_position ++;
if(key[KEY_S])
image2_y_position --;
if(key[KEY_W])
image2_y_position ++;
if(key[KEY_SPACE])
show_bbox = TRUE;
else if(!key[KEY_SPACE])
show_bbox = FALSE;
image1_bb_left = image1_x_position;
image1_bb_top = image1_y_position;
image1_bb_right = (image1_bb_left + image1->w);
image1_bb_bottom = (image1_bb_top + image1->h);
image2_bb_left = image1_x_position;
image2_bb_top = image1_y_position;
image2_bb_right = (image2_bb_left + image2->w);
image2_bb_bottom = (image2_bb_top + image2->h);
collision = TRUE; //assume there is a collision
if(image1_bb_bottom < image2_bb_top)
{
collision = FALSE;
}
if(image1_bb_top > image2_bb_bottom)
{
collision = FALSE;
}
if(image1_bb_right < image2_bb_left)
{
collision = FALSE;
}
if(image1_bb_left < image2_bb_right)
{
collision = FALSE;
}
speed_counter --;
}
draw_sprite(buffer, image1, image1_x_position, image1_y_position);
draw_sprite(buffer, image2, image2_x_position, image2_y_position);
if(show_bbox == TRUE)
{
line(buffer, image1_bb_left, image1_bb_top, image1_bb_right, image1_bb_top, makecol(255,0,0));
line(buffer, image1_bb_left, image1_bb_bottom, image1_bb_right, image1_bb_bottom, makecol(255,0,0));
line(buffer, image1_bb_left, image1_bb_top, image1_bb_left, image1_bb_bottom, makecol(255,0,0));
line(buffer, image1_bb_right, image1_bb_top, image1_bb_right, image1_bb_bottom, makecol(255,0,0));
line(buffer, image2_bb_left, image2_bb_top, image2_bb_right, image2_bb_top, makecol(255,0,0));
line(buffer, image2_bb_left, image2_bb_bottom, image2_bb_right, image2_bb_bottom, makecol(255,0,0));
line(buffer, image2_bb_left, image2_bb_top, image2_bb_left, image2_bb_bottom, makecol(255,0,0));
line(buffer, image2_bb_right, image2_bb_top, image2_bb_right, image2_bb_bottom, makecol(255,0,0));
}
if(collision == TRUE)
{
textprintf_ex(buffer, font, 0,0, makecol(255,255,255), -1, "Collision!");
}
blit(buffer, screen, 0,0,0,0,640,480); //blit the buffer
clear(buffer); //clear the buffer
destroy_bitmap(buffer);
destroy_bitmap(image1);
destroy_bitmap(image2);
return 0;
}
END_OF_MAIN()
| |