// main #include #include #include "units.h" volatile long speed_counter = 0; void increment_speed_counter() // A function to increment the speed counter { speed_counter++; } int main() { allegro_init(); install_keyboard(); install_timer(); install_mouse(); show_os_cursor(2); unit test; test.assign_bitmap(); 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(60)); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT_WINDOWED , 800 , 600 , 0 , 0); show_os_cursor(MOUSE_CURSOR_ARROW); BITMAP *cursor = load_bitmap("cursor.bmp", 0); BITMAP *buffer = create_bitmap(800, 600); 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("image1.bmp", NULL);//Load image 1 if(image1 == NULL) { set_gfx_mode(GFX_TEXT,0,0,0,0); //Set the screen mode for allegro messages allegro_message("Could not load image1.bmp"); exit(EXIT_FAILURE); } BITMAP *image2 = load_bitmap("image2.bmp", NULL);//Load image 2 if(image2 == NULL) { set_gfx_mode(GFX_TEXT,0,0,0,0); //Set the screen mode for allegro messages allegro_message("Could not load image2.bmp"); exit(EXIT_FAILURE); } int image1_x_position = 0; // Set the x position of image 1 to 0. int image1_y_position = 0; // Set the y position of image 1 to 0. int image2_x_position = 100; // Set the x position of image 2 to 100. int image2_y_position = 100; // Set the y position of image 2 to 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; int clicked = FALSE; int tank_X = 0; int tank_Y = 0; int frame_counter = 0; int nf = 0, nt = 0; int test; while(!key[KEY_ESC])//If the user hits escape, quit the program { while(speed_counter > 0) { if(key[KEY_LEFT]) image1_x_position --; // Move image 1 left. if(key[KEY_RIGHT]) image1_x_position ++; // Move image 1 right. if(key[KEY_DOWN]) image1_y_position ++; // Move image 1 down. if(key[KEY_UP]) image1_y_position --; // Move image 1 up. if(key[KEY_A]) image2_x_position --; // Move image 2 left. if(key[KEY_D]) image2_x_position ++; // Move image 2 right. if(key[KEY_S]) image2_y_position ++; // Move image 2 down. if(key[KEY_W]) image2_y_position --; // Move image 2 up. if(key[KEY_SPACE]) show_bbox = TRUE; else if(!key[KEY_SPACE]) show_bbox = FALSE; // mouse test if (mouse_b) { clicked = TRUE; /* any mouse button is pressed */ } else if(!mouse_b) clicked = 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 = image2_x_position; image2_bb_top = image2_y_position; image2_bb_right = (image2_bb_left + image2->w); image2_bb_bottom = (image2_bb_top + image2->h); // Assume that there is no collision collision = FALSE; if( (image1_bb_left >= image2_bb_left) && (image1_bb_top >= image2_bb_top) && (image1_bb_left <= image2_bb_right) && (image1_bb_top <= image2_bb_bottom) ) { collision = TRUE; } else if( (image1_bb_right <= image2_bb_right) && (image1_bb_right >= image2_bb_left) && (image1_bb_top >= image2_bb_top) && (image1_bb_top <= image2_bb_bottom) ) { collision = TRUE; } else if( (image1_bb_left >= image2_bb_left) && (image1_bb_left <= image2_bb_right) && (image1_bb_bottom <= image2_bb_bottom) && (image1_bb_bottom >= image2_bb_top) ) { collision = TRUE; } else if( (image1_bb_right <= image2_bb_right) && (image1_bb_right >= image2_bb_left) && (image1_bb_bottom <= image2_bb_bottom) && (image1_bb_bottom >= image2_bb_top) ) { collision = TRUE; } if( (image2_bb_left >= image1_bb_left) && (image2_bb_top >= image1_bb_top) && (image2_bb_left <= image1_bb_right) && (image2_bb_top <= image1_bb_bottom) ) { collision = TRUE; } else if( (image2_bb_right <= image1_bb_right) && (image2_bb_right >= image1_bb_left) && (image2_bb_top >= image1_bb_top) && (image2_bb_top <= image1_bb_bottom) ) { collision = TRUE; } else if( (image2_bb_right <= image1_bb_right) && (image2_bb_right >= image1_bb_left) && (image2_bb_bottom <= image1_bb_bottom) && (image2_bb_bottom >= image1_bb_top) ) { collision = TRUE; } else if( (image2_bb_right <= image1_bb_right) && (image2_bb_right >= image1_bb_left) && (image2_bb_bottom <= image1_bb_bottom) && (image2_bb_bottom >= image1_bb_top) ) { collision = TRUE; } if( (image1_bb_top > image2_bb_top) && (image1_bb_bottom < image2_bb_bottom) && (image2_bb_left > image1_bb_left) && (image2_bb_right < image1_bb_right) ) { collision = TRUE; } if( (image2_bb_top > image1_bb_top) && (image2_bb_bottom < image1_bb_bottom) && (image1_bb_left > image2_bb_left) && (image1_bb_right < image2_bb_right) ) { collision = TRUE; } nt++; speed_counter--; } // speed counter loop //draw_sprite(buffer, image1, image1_x_position, image1_y_position); // Draw image1 //draw_sprite(buffer, image2, image2_x_position, image2_y_position); // Draw image2 test.draw(); 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!"); } if(clicked == TRUE) { textprintf_ex(buffer, font, 0,0, makecol(255,255,255), -1, "kliked"); } textprintf_ex(buffer, font, 0, 0, -1, 0, "%f", (float) nf * 60 / nt); // blit(buffer, screen, 0, 0, 0, 0, 800, 600);//Blit the buffer nf++; clear(buffer);//Clear the buffer } //Destroy all bitmaps destroy_bitmap(buffer); destroy_bitmap(image1); destroy_bitmap(image2); return 0; } // units.h class unit { private: BITMAP *hold; int X , Y; public: void assign_bitmap(); void draw(); } // units.cpp #include "units.h" void unit::draw() { draw_sprite(buffer, hold , X, X; // Draw image1 } void unit::assign_bitmap() { *hold = load_bitmap("tank.bmp"); }