int main(int argc, char **argv) { al_init(); al_init_image_addon(); al_init_primitives_addon(); al_init_font_addon(); al_init_ttf_addon(); al_get_display_mode(al_get_num_display_modes()-1, &disp_data); //al_set_new_display_flags(ALLEGRO_FULLSCREEN); al_set_new_display_flags(ALLEGRO_WINDOWED); al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); disp_data.width*=0.8; disp_data.height*=0.8; display=al_create_display(disp_data.width, disp_data.height); if(!display) { return -1; } al_clear_to_color(al_map_rgb(0, 0, 0)); al_flip_display(); al_install_keyboard(); al_install_mouse(); al_install_audio(); al_init_acodec_addon(); al_reserve_samples(5); al_rest(5.0); al_destroy_display(display); return 0; }
/** Returns the fullscreen display mode in ID as string * @param id Id of the display mode. MUST BE in range of 0 to cbeGetGfxModesCount()-1. * @returns Display mode as string. I.e. cbeGetGfxMode(0) returns a string "640,480,60,32" (in most cases). */ void cbeGetGfxMode(CBEnchanted *cb) { // Store the old flags for later restoring int32_t oldFlags = al_get_display_flags(cb->gfxInterface->getWindow()); // Set the display flags for fullscreen al_set_new_display_flags(ALLEGRO_FULLSCREEN); // Get display modes count int32_t displayModesCount = al_get_num_display_modes(); // Pop the ID from input int32_t displayId = cb->popValue().toInt(); // Check if displayId is valid if (displayId < 0 || displayId >= displayModesCount) { string id = std::to_string(displayId); string count = std::to_string(displayModesCount); bool ignore = cb->errors->createError("cbeGetGfxMode() failed!", "Trying to get gfx mode with ID " + id + " out of " + count + " modes\nWhen ignored, the first display mode available in the list is returned."); // If ignored, displayId is 0. Otherwise push empty string and return. if (ignore) { displayId = 0; } else { cb->pushValue(ISString("")); return; } } // Where the display modes data is stored ALLEGRO_DISPLAY_MODE *displayData = new ALLEGRO_DISPLAY_MODE; // Get the display mode with id al_get_display_mode(displayId, displayData); // Restore old flags al_set_new_display_flags(oldFlags); if (displayData != NULL) { // Initialize the string to be returned string displayModeString; // Construct the string displayModeString = std::to_string(displayData->width); displayModeString += "," + std::to_string(displayData->height); displayModeString += "," + std::to_string(displayData->refresh_rate); displayModeString += "," + std::to_string(al_get_pixel_format_bits(displayData->format)); // Return the display mode cb->pushValue(displayModeString.substr(0 , displayModeString.length() )); } else { INFO("Something funny happened in cbeGetGfxMode(), you got an empty display mode...") cb->pushValue(ISString("")); } // Free memory delete displayData; }
/** Get fullscreen display modes count * @returns List size */ void cbeGetGfxModesCount(CBEnchanted *cb) { // Store the old flags for later restoring int32_t oldFlags = al_get_display_flags(cb->gfxInterface->getWindow()); // Set the display flags for fullscreen al_set_new_display_flags(ALLEGRO_FULLSCREEN); // Get display modes count int32_t displayModesCount = al_get_num_display_modes(); // Restore old flags al_set_new_display_flags(oldFlags); // Return the modes count cb->pushValue(displayModesCount); }
int main(int argc, char **argv){ ALLEGRO_DISPLAY *display = NULL; ALLEGRO_DISPLAY_MODE disp_data; al_init(); // I'm not checking the return value for simplicity. al_init_image_addon(); al_init_primitives_addon(); al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); al_set_new_display_flags(ALLEGRO_FULLSCREEN); display = al_create_display(disp_data.width, disp_data.height); al_rest(3); al_destroy_display(display); return 0; }
int main(void) { ALLEGRO_MONITOR_INFO info; int num_adapters; int i, j; if (!al_init()) { abort_example("Could not init Allegro.\n"); } open_log(); num_adapters = al_get_num_video_adapters(); log_printf("%d adapters found...\n", num_adapters); for (i = 0; i < num_adapters; i++) { al_get_monitor_info(i, &info); log_printf("Adapter %d: ", i); log_printf("(%d, %d) - (%d, %d)\n", info.x1, info.y1, info.x2, info.y2); al_set_new_display_adapter(i); log_printf(" Available fullscreen display modes:\n"); for (j = 0; j < al_get_num_display_modes(); j++) { ALLEGRO_DISPLAY_MODE mode; al_get_display_mode(j, &mode); log_printf(" Mode %3d: %4d x %4d, %d Hz\n", j, mode.width, mode.height, mode.refresh_rate); } } close_log(true); return 0; }
void GameEngine::initialise() { if (!al_init()) { THROW_GAME_EXCEPTION(EXCEP_ALLEG_ENGINE_FAILED); } if (!al_init_font_addon()) { THROW_GAME_EXCEPTION(EXCEP_ALLEG_FONT_FAILED); } if (!al_init_image_addon()) { THROW_GAME_EXCEPTION(EXCEP_ALLEG_IMAGE_FAILED); } if (!al_install_keyboard()) { THROW_GAME_EXCEPTION(EXCEP_ALLEG_KEYBOARD_FAILED); } if (!al_install_mouse()) { THROW_GAME_EXCEPTION(EXCEP_ALLEG_MOUSE_FAILED); } //Temporary Display Options al_set_new_display_flags(ALLEGRO_OPENGL); al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_RENDER_METHOD, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_VSYNC, 2, ALLEGRO_REQUIRE); al_set_new_display_option(ALLEGRO_CAN_DRAW_INTO_BITMAP, 1, ALLEGRO_SUGGEST); int originalDisplayFlags = al_get_new_display_flags(); int displayFlags = 0; if (m_Config->getFullscreen()) { displayFlags = displayFlags | ALLEGRO_FULLSCREEN_WINDOW | ALLEGRO_NOFRAME; } al_set_new_display_flags(displayFlags); al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR | ALLEGRO_VIDEO_BITMAP); if (m_Config->getResolutionWidth() < 0 || m_Config->getResolutionHeight() < 0) { ALLEGRO_DISPLAY_MODE largest_display_mode; al_get_display_mode(al_get_num_display_modes() - 1, &largest_display_mode); m_Config->setResolutionWidth(largest_display_mode.width); m_Config->setResolutionHeight(largest_display_mode.height); //std::cout << m_Config->getResolutionWidth() << " " << m_Config->getResolutionHeight() << endl; } m_Display = al_create_display(m_Config->getResolutionWidth(), m_Config->getResolutionHeight()); if (!m_Display) { THROW_GAME_EXCEPTION(EXCEP_ALLEG_DISPLAY_FAILED); } al_set_new_display_flags(originalDisplayFlags); m_RedrawTimer = al_create_timer(ALLEGRO_BPS_TO_SECS(m_Config->getFrameSpeed())); if (!m_RedrawTimer) { THROW_GAME_EXCEPTION(EXCEP_ALLEG_TIMER_FAILED); } m_EventQueue = al_create_event_queue(); if (!m_EventQueue) { THROW_GAME_EXCEPTION(EXCEP_ALLEG_EQUEUE_FAILED); } al_register_event_source(m_EventQueue, al_get_display_event_source(m_Display)); al_register_event_source(m_EventQueue, al_get_timer_event_source(m_RedrawTimer)); al_register_event_source(m_EventQueue, al_get_keyboard_event_source()); }
void cbeGetBestGfxMode(CBEnchanted *cb) { // Initials int32_t bestHertz = 0; int32_t bestDisplayId = -1; bool no32BitFound = true; // Store the old flags for later restoring int32_t oldFlags = al_get_display_flags(cb->gfxInterface->getWindow()); // Set the display flags for fullscreen al_set_new_display_flags(ALLEGRO_FULLSCREEN); // Get display modes count int32_t displayModesCount = al_get_num_display_modes(); // Pop the input int32_t displayHeight = cb->popValue().getInt(); int32_t displayWidth = cb->popValue().getInt(); // Where the display modes data is stored ALLEGRO_DISPLAY_MODE *displayData = new ALLEGRO_DISPLAY_MODE; // Initialize the string to be returned string displayModeString; for (int currentId = 0; currentId < displayModesCount; currentId++) { // Get the display mode with id al_get_display_mode(currentId, displayData); if (displayData != NULL) { if (displayData->width == displayWidth && displayData->height == displayHeight && displayData->refresh_rate >= bestHertz) { if (al_get_pixel_format_bits(displayData->format) >= 32) { no32BitFound = false; } bestHertz = displayData->refresh_rate; bestDisplayId = currentId; } } } // If we got something, get that display mode and return it if (bestDisplayId > -1) { al_get_display_mode(bestDisplayId, displayData); // Construct the string displayModeString = std::to_string(displayData->width); displayModeString += "," + std::to_string(displayData->height); displayModeString += "," + std::to_string(displayData->refresh_rate); if (no32BitFound) { displayModeString += ",16"; } else { displayModeString += ",32"; } } else { displayModeString = ""; } // Restore old flags al_set_new_display_flags(oldFlags); // Return the display mode cb->pushValue(displayModeString.substr(0 , displayModeString.length() )); }
int main(int argc, char **argv) { //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Variable Initialization" const float FPS = 60; int screenw = 900; int screenh = 650; float bounce = 32; float bouncer_x = screenw / 2.0 - bounce / 2.0; float bouncer_y = screenh / 2.0 - bounce / 2.0; float wall1_x = 30.0; float wall1_y = 40.0; float wall1_w = 30.0; float wall1_h = 30.0; bool key[4] = { false, false, false, false }; bool redraw = true; bool exiting = false; bool menu = false; enum MYKEYS { KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT }; pac game; game.setpacmanx(bouncer_x); game.setpacmany(bouncer_y); ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *bouncer = NULL; ALLEGRO_BITMAP *wall1 = NULL; #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "System Protection" // Begining Initialization // Checks if Allegro is initialized if (!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } //Checks if keyboard is initialized if (!al_install_keyboard()) { fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; } //Checks if mouse is initialized if (!al_install_mouse()) { fprintf(stderr, "failed to initialize the mouse!\n"); return -1; } //Intializes timer to fps and then checks if there is a timer timer = al_create_timer(1.0 / FPS); if (!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } //checks if there is a display display = al_create_display(screenw , screenh); if (!display) { fprintf(stderr, "failed to create display!\n"); return -1; } al_init_font_addon(); al_init_ttf_addon(); ALLEGRO_FONT *font = al_load_ttf_font("DroidSans.ttf", 36, 0); ALLEGRO_FONT *font2 = al_load_ttf_font("DroidSans.ttf", 12, 0); if (!font){ fprintf(stderr, "Could not load 'pirulen.ttf'.\n"); return -1; } bouncer = al_create_bitmap(bounce, bounce); if (!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } wall1 = al_create_bitmap(wall1_w, wall1_h); if (!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_init_image_addon(); ALLEGRO_DISPLAY_MODE disp_data; al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); al_set_target_bitmap(bouncer); al_clear_to_color(al_map_rgb(255, 0, 0)); al_set_target_bitmap(al_get_backbuffer(display)); al_set_target_bitmap(wall1); al_clear_to_color(al_map_rgb(0, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if (!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Event Initialization" al_register_event_source(event_queue, al_get_display_event_source(display)); //display event handler al_register_event_source(event_queue, al_get_timer_event_source(timer)); //time envent handler al_register_event_source(event_queue, al_get_keyboard_event_source()); //keyboard event handler al_register_event_source(event_queue, al_get_mouse_event_source()); //mouse event handler al_clear_to_color(al_map_rgb(0, 0, 0)); al_flip_display(); al_start_timer(timer); #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Simple Menu" while (!menu) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_KEY_UP) { if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_ENTER: menu = true; break; } } } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } if (redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(255, 255, 255)); al_draw_text(font, al_map_rgb(0, 0, 0), screenw / 2, (screenh / 2), ALLEGRO_ALIGN_CENTRE, "Press Enter to Start"); al_flip_display(); } } #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Driver Program" while (!exiting) { #pragma region "Timer Events" ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_TIMER) { if(key[KEY_UP] && game.getpacmany() >= 4.0) { game.uppacman(); } if (key[KEY_DOWN] && game.getpacmany() <= screenh - bounce - 4.0) { game.downpacman(); } if (key[KEY_LEFT] && game.getpacmanx() >= 4.0) { game.leftpacman(); } if (key[KEY_RIGHT] && game.getpacmany() <= screenw - bounce - 4.0) { game.rightpacman(); } redraw = true; } // "Closes the Window When Pressing X button" else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } #pragma endregion #pragma region "Checks for when key was pressed down" else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = true; break; } } #pragma endregion #pragma region "Checks for when key was released" else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = false; break; case ALLEGRO_KEY_ESCAPE: exiting = true; break; case ALLEGRO_KEY_Q: exiting = true; break; //Full screen when f is pressed case ALLEGRO_KEY_F: if (screenw != disp_data.width) { al_set_new_display_flags(ALLEGRO_FULLSCREEN); display = al_create_display(disp_data.width, disp_data.height); screenw = disp_data.width; screenh = disp_data.height; wall1_x = 2 * (screenw / 10); wall1_y = (screenh / screenh) + 50 * (screenh / screenh); } break; //Normal screen when n is pressed case ALLEGRO_KEY_N: if (screenw != 640) { al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); display = al_create_display(640, 480); screenw = 640; screenh = 480; } break; } } #pragma endregion #pragma region "Redraw Objects" if (redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_bitmap(wall1, wall1_x, wall1_y, 0); al_draw_bitmap(bouncer, game.getpacmanx(), game.getpacmany(), 0); al_draw_text(font2, al_map_rgb(255, 0, 0), screenw / 2, (screenh / screenh), ALLEGRO_ALIGN_CENTRE, "Moving a Square"); al_flip_display(); } #pragma endregion } #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Destroyers" al_destroy_bitmap(bouncer); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); #pragma endregion return 0; }
int main(int argc, char **argv) { //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Variable Initialization" const float FPS = 60; int screenw = 900; int screenh = 650; float bounce = 25; float bouncer_x = 604; float bouncer_y = 300; float wall1_x = 140.0; float wall1_y = 30.0; float wall1_w = 30.0; float wall1_h = 30.0; float wall2_x = 140.0; float wall2_y = 30.0; float wall2_w = 70.0; float wall2_h = 70.0; float food_x = 146; float food_y = 37; float food_h = 10; float food_w = 10; bool key[4] = { false, false, false, false }; bool redraw = true; bool exiting = false; bool menu = false; int maze[20][20]; int dir = 0; int coly[20][20] = {0}; #pragma region "Maze Init" for (int row = 0; row < 20; row++) { for (int col = 0; col < 20; col++) { maze[row][col] = true; } } int row = 1; while (1) { for (int col = 0; col < 3; col++) { maze[1][col] = false; } for (int col = 4; col < 16; col++) { maze[1][col] = false; } for (int col = 17; col < 20; col++) { maze[1][col] = false; } row = row + 14; if (row > 15) break; } int col = 4; while (1) { for (int row = 2; row < 16; row++) { maze[row][col] = false; } col = col + 11; if (col > 15) break; } for (int col = 2; col < 8; col++) { maze[3][col] = false; } for (int col = 12; col < 18; col++) { maze[3][col] = false; } col = 1; while (1) { for (int row = 3; row < 7; row++) { maze[row][col] = false; } col = col + 17; if (col > 18) break; } for (col = 7; col < 13; col++) { maze[4][col] = false; } row = 6; while (1) { for (int col = 2; col < 18; col++) { maze[row][col] = false; } row = row + 5; if (row > 11) break; } col = 2; while (1) { for (int row = 7; row < 16; row++) { maze[row][col] = false; } col = col + 15; if (col > 17) break; } maze[7][9] = false; maze[7][10] = false; col = 0; while (1) { for (int row = 10; row < 14; row++) { maze[row][col] = false; } col = col + 19; if (col > 19) break; } for (int col = 6; col < 14; col++) { maze[13][col] = false; } col = 1; while (1) { for (row = 16; row < 18; row++) { maze[row][col] = false; } col = col + 17; if (col > 18) break; } for (int col = 5; col < 8; col++) { maze[17][col] = false; } for (int col = 12; col < 15; col++) { maze[17][col] = false; } for (col = 1; col < 6; col++) { maze[18][col] = false; } for (col = 7; col < 13; col++) { maze[18][col] = false; } for (col = 14; col < 19; col++) { maze[18][col] = false; } maze[2][2] = false; maze[2][17] = false; maze[7][9] = false; maze[7][10] = false; maze[12][6] = false; maze[12][13] = false; maze[16][5] = false; maze[16][14] = false; maze[16][2] = false; maze[16][4] = false; maze[16][17] = false; maze[16][15] = false; maze[1][0] = true; maze[1][19] = true; maze[1][1] = true; maze[1][18] = true; maze[1][2] = true; maze[1][17] = true; maze[2][2] = true; maze[2][17] = true; for (int row = 8; row < 10; row++) { for (int col = 6; col < 14; col++) { maze[row][col] = false; } } #pragma endregion enum MYKEYS { KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT }; pac game; game.setpacmanx(bouncer_x); game.setpacmany(bouncer_y); game.initpacsize(); ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *bouncer = NULL; ALLEGRO_BITMAP *wall1 = NULL; ALLEGRO_BITMAP *food = NULL; ALLEGRO_BITMAP *wall2[20][20] = {NULL}; ALLEGRO_BITMAP *ghostb = NULL; ALLEGRO_BITMAP *ghostg = NULL; ALLEGRO_BITMAP *ghostr = NULL; ALLEGRO_BITMAP *ghostp = NULL; ALLEGRO_COLOR color; #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "System Protection" // Begining Initialization // Checks if Allegro is initialized if (!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } //Checks if keyboard is initialized if (!al_install_keyboard()) { fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; } //Checks if mouse is initialized if (!al_install_mouse()) { fprintf(stderr, "failed to initialize the mouse!\n"); return -1; } //Intializes timer to fps and then checks if there is a timer timer = al_create_timer(1.0 / FPS); if (!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } //checks if there is a display display = al_create_display(screenw , screenh); if (!display) { fprintf(stderr, "failed to create display!\n"); return -1; } al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); ALLEGRO_FONT *font = al_load_ttf_font("DroidSans.ttf", 23, 0); ALLEGRO_FONT *font2 = al_load_ttf_font("DroidSans.ttf", 12, 0); if (!font){ fprintf(stderr, "Could not load 'pirulen.ttf'.\n"); return -1; } bouncer = al_create_bitmap(bounce,bounce); if (!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } ghostb = al_create_bitmap(bounce,bounce); if (!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } ghostg = al_create_bitmap(bounce,bounce); if (!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } ghostr = al_create_bitmap(bounce,bounce); if (!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } ghostp = al_create_bitmap(bounce,bounce); if (!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } wall1 = al_create_bitmap(wall1_w, wall1_h); if (!wall1) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } food = al_create_bitmap(food_w, food_h); if (!food) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } for (int row = 0; row < 20; row++) //display { for (int col = 0; col < 20; col++) { wall2[col][row] = al_create_bitmap(wall1_w,wall1_w); al_set_target_bitmap(wall2[col][row]); al_clear_to_color(al_map_rgb(0, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); } } al_init_image_addon(); ALLEGRO_DISPLAY_MODE disp_data; al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); al_set_target_bitmap(food); al_clear_to_color(al_map_rgb(255, 255, 255)); al_set_target_bitmap(al_get_backbuffer(display)); al_set_target_bitmap(bouncer); al_clear_to_color(al_map_rgb(255, 255, 0)); al_set_target_bitmap(al_get_backbuffer(display)); al_set_target_bitmap(ghostb); al_clear_to_color(al_map_rgb(0, 150, 255)); al_set_target_bitmap(al_get_backbuffer(display)); al_set_target_bitmap(ghostr); al_clear_to_color(al_map_rgb(255, 0, 0)); al_set_target_bitmap(al_get_backbuffer(display)); al_set_target_bitmap(ghostg); al_clear_to_color(al_map_rgb(0, 255, 0)); al_set_target_bitmap(al_get_backbuffer(display)); al_set_target_bitmap(ghostp); al_clear_to_color(al_map_rgb(255, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); al_set_target_bitmap(wall1); al_clear_to_color(al_map_rgb(0, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if (!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Event Initialization" al_register_event_source(event_queue, al_get_display_event_source(display)); //display event handler al_register_event_source(event_queue, al_get_timer_event_source(timer)); //time envent handler al_register_event_source(event_queue, al_get_keyboard_event_source()); //keyboard event handler al_register_event_source(event_queue, al_get_mouse_event_source()); //mouse event handler al_clear_to_color(al_map_rgb(0, 0, 0)); al_flip_display(); al_start_timer(timer); #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Simple Menu" while (!menu) { ALLEGRO_EVENT ev; al_install_audio(); al_reserve_samples(1); ALLEGRO_SAMPLE *sample = al_load_sample("song.wav"); ALLEGRO_SAMPLE_INSTANCE *songinstance; //songinstance = al_create_sample_instance(sample); //al_set_sample_instance_playmode(songinstance, ALLEGRO_PLAYMODE_ONCE); //al_attach_sample_instance_to_mixer(songinstance, al_get_default_mixer()); al_wait_for_event(event_queue, &ev); al_install_mouse(); al_init_image_addon(); ALLEGRO_BITMAP *image = al_load_bitmap("Pacman.jpg"); //al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); ALLEGRO_FONT *font24 = al_load_font("PAC-FONT.ttf", 35, 0); //ALLEGRO_BITMAP *title = al_load_bitmap("title.jpg"); al_register_event_source(event_queue, al_get_mouse_event_source()); if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { if (ev.mouse.button == 1 && ev.mouse.x > 500 && ev.mouse.x < 700 && ev.mouse.y > 525) { menu = true; break; } else if(ev.mouse.button == 1 && ev.mouse.x > 500 && ev.mouse.y < 475 && ev.mouse.y > 425 ) { al_show_native_message_box(NULL, "Credits", "Created by:", "Anuraag Shankar, Ronit Sharma, and Sahana Premkumar", NULL, NULL); } else if(ev.mouse.button == 1 && ev.mouse.x > 500 && ev.mouse.y < 420 ) { al_show_native_message_box(NULL, "Controls", "Movement- Arrow Keys", "Objective: Avoid ghosts and collect maximum pellets", NULL, NULL); } } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } if (redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0, 0, 0)); al_init_primitives_addon(); al_init_acodec_addon(); //al_play_sample(sample, 255, 0, 2000, ALLEGRO_PLAYMODE_ONCE, 0); al_draw_text(font24, al_map_rgb(255, 255, 255), 420, 150, 0, "Pacman: The Game"); //al_draw_bitmap(title, 700, (50), NULL); al_draw_bitmap(image, 100, (100), NULL); al_draw_filled_rectangle(500,300,804,375, al_map_rgb(255, 0, 0)); al_draw_filled_rectangle(500,500,804,575, al_map_rgb(255, 0, 0)); al_draw_filled_rectangle(500,400,804,475, al_map_rgb(255, 0, 0)); al_draw_text(font, al_map_rgb(255, 255, 255), 650, 315, ALLEGRO_ALIGN_CENTRE, "Controls"); al_draw_text(font, al_map_rgb(255, 255, 255), 650, 525, ALLEGRO_ALIGN_CENTRE, "Click to play"); al_draw_text(font, al_map_rgb(255, 255, 255), 650, 415, ALLEGRO_ALIGN_CENTRE, "Credits"); al_flip_display(); } } #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Driver Program" while (!exiting) { #pragma region "Timer Events" ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_TIMER) { if(key[KEY_UP] && game.getpacmany() >= 4.0 ) { for (int row = 0; row < 20; row++) //display { for (int col = 0; col < 20; col++) { if(coly[row][col] !=2) { game.uppacman(); dir = 1; } } } } if (key[KEY_DOWN] && game.getpacmany() <= screenh - bounce - 4.0 ) { for (int row = 0; row < 20; row++) //display { for (int col = 0; col < 20; col++) { if(coly[row][col] != 4) { game.downpacman(); dir = 2; } } } } if (key[KEY_LEFT] && game.getpacmanx() >= 4.0) { for (int row = 0; row < 20; row++) //display { for (int col = 0; col < 20; col++) { if(coly[row][col] != 1) { game.leftpacman(); dir = 3; } } } } if (key[KEY_RIGHT] && game.getpacmanx() <= screenw - bounce - 4.0) { for (int row = 0; row < 20; row++) //display { for (int col = 0; col < 20; col++) { if(coly[row][col] != 3) { game.rightpacman(); dir = 4; } } } } redraw = true; } // "Closes the Window When Pressing X button" else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } #pragma endregion #pragma region "Checks for when key was pressed down" else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = true; break; } } #pragma endregion #pragma region "Checks for when key was released" else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = false; break; case ALLEGRO_KEY_ESCAPE: exiting = true; break; case ALLEGRO_KEY_Q: exiting = true; break; //Full screen when f is pressed case ALLEGRO_KEY_F: if (screenw != disp_data.width) { al_set_new_display_flags(ALLEGRO_FULLSCREEN); display = al_create_display(disp_data.width, disp_data.height); screenw = disp_data.width; screenh = disp_data.height; wall1_x = 2 * (screenw / 10); wall1_y = (screenh / screenh) + 50 * (screenh / screenh); } break; //Normal screen when n is pressed case ALLEGRO_KEY_N: al_show_native_message_box(al_get_current_display(), "THIS IS THE END", "YOU LOST", "Game Over", NULL, ALLEGRO_MESSAGEBOX_ERROR); break; case ALLEGRO_KEY_P: cout << "bx:" << (wall1_x + wall1_w + 13) - game.getpacmanx() << endl; cout << "by:" << (wall1_y + wall1_w + 13) - game.getpacmany() << endl; cout << "dx:" << (game.getpacmanx() + bounce) - wall1_x << endl; cout << "dy:" << (game.getpacmany() + bounce) - wall1_y << endl; cout << "dir:" << coly << endl; cout << "x:" << game.getpacmanx() << endl; cout << "y:" << game.getpacmany() << endl; } } #pragma endregion #pragma region "Redraw Objects" if (redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0, 0, 0)); for (int row = 0; row < 20; row++) //display { for (int col = 0; col < 20; col++) { if (maze[row][col] == 1) { al_draw_bitmap(wall1, wall1_x + col*30, wall1_y + row*30, 0); // al_draw_bitmap(wall2[row][col], wall1_x + col*30, wall1_y + row*30, 0); coly[row][col] = game.collision(wall1_x + col*30,wall1_y + row*30,wall1_w + 13); } else al_draw_bitmap(food, food_x + col*30, food_y + row*30, 0); } } int x = game.getpacmanx(); int y = game.getpacmany(); color = al_map_rgb(255,200,0); //al_draw_bitmap(bouncer, game.getpacmanx(), game.getpacmany(), 0); al_draw_filled_circle(game.getpacmanx(),game.getpacmany(),13.0,color); al_draw_bitmap(ghostb,screenw/2, 63, 0); al_draw_bitmap(ghostr,screenw/2 - 20, 213, 0); al_draw_bitmap(ghostg,screenw/2 - 20, 363, 0); al_draw_bitmap(ghostp, screenw/2 - 246,500, 0); #pragma region "TEST CODE" if(game.getpacmanx() - screenw/2 > 30 && game.getpacmanx() - screenw/2< 33) { al_show_native_message_box(al_get_current_display(), "THIS IS THE END", "YOU LOST", "Game Over", NULL, ALLEGRO_MESSAGEBOX_ERROR); } #pragma endregion al_draw_text(font2, al_map_rgb(255, 0, 0), screenw / 2, (screenh / screenh), ALLEGRO_ALIGN_CENTRE, "PAC-MAN VERSION 1.0"); al_flip_display(); } #pragma endregion } #pragma endregion //-------------------------------------------------------------------------------------------------------------------------------- #pragma region "Destroyers" al_destroy_bitmap(bouncer); al_destroy_bitmap(wall1); for (int row = 0; row < 20; row++) //display { for (int col = 0; col < 20; col++) { al_destroy_bitmap(wall2[row][col]); } } al_destroy_bitmap(food); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); #pragma endregion return 0; }
int main() { ALLEGRO_DISPLAY* display = NULL; ALLEGRO_DISPLAY_MODE disp; ALLEGRO_EVENT_QUEUE* eq = NULL; ALLEGRO_TIMER* timer = NULL; ALLEGRO_TIMER* ball_timer = NULL; bool run = true; bool draw = false; if (!al_init()) { return -1; } al_get_display_mode(al_get_num_display_modes() - 1, &disp); std::cout << disp.height << " " << height << std::endl; std::cout << disp.width << " " << width << std::endl; height = disp.height / 3 * 2; width = height / 9 * 16; std::cout << disp.height << " " << height << std::endl; std::cout << disp.width << " " << width << std::endl; al_set_new_display_flags(ALLEGRO_FULLSCREEN); display = al_create_display(width, height); al_set_window_title(display, "PONG V2"); if (display == NULL) { return -1; } init(&player1, &player2, &ball); eq = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); ball_timer = al_create_timer(15.0); al_install_keyboard(); al_init_primitives_addon(); al_init_font_addon(); al_init_ttf_addon(); font = al_load_ttf_font("./arial.ttf", 18, 0); scorefont = al_load_ttf_font("./arial.ttf", (height * width) / 36000, 0); al_register_event_source(eq, al_get_keyboard_event_source()); al_register_event_source(eq, al_get_display_event_source(display)); al_register_event_source(eq, al_get_timer_event_source(timer)); al_hide_mouse_cursor(display); al_start_timer(timer); while (run) { ALLEGRO_EVENT ev; al_wait_for_event(eq, &ev); if (ev.type == ALLEGRO_EVENT_TIMER) { draw = true; } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { run = false; } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_S: keys[P1][DOWN] = true; break; case ALLEGRO_KEY_W: keys[P1][UP] = true; break; case ALLEGRO_KEY_DOWN: keys[P2][DOWN] = true; break; case ALLEGRO_KEY_UP: keys[P2][UP] = true; break; case ALLEGRO_KEY_SPACE: start = true; firstGame = false; break; case ALLEGRO_KEY_ESCAPE: run = false; break; case ALLEGRO_KEY_P: if (pause == true) pause = false; else if (pause == false) pause = true; break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_S: keys[P1][DOWN] = false; break; case ALLEGRO_KEY_W: keys[P1][UP] = false; break; case ALLEGRO_KEY_DOWN: keys[P2][DOWN] = false; break; case ALLEGRO_KEY_UP: keys[P2][UP] = false; break; } } if (draw && al_event_queue_is_empty(eq)) { if (!pause) { if (keys[P1][UP])update(&player1, -1); if (keys[P1][DOWN])update(&player1, 1); if (keys[P2][UP])update(&player2, -1); if (keys[P2][DOWN])update(&player2, 1); update(&ball); updateGame(&player1, &player2, &ball); render(); draw = false; } } } al_destroy_display(display); al_destroy_event_queue(eq); return 0; }
/* starting game UI */ void start_game (bool Fullscreen) { /* creating display */ ALLEGRO_DISPLAY * display; if (Fullscreen) { /* creating disp_data struct to store supported resolutions */ ALLEGRO_DISPLAY_MODE disp_data; /* making it fullscreen */ al_set_new_display_flags(ALLEGRO_FULLSCREEN); /* storing info */ al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); CurrentScreenWidth = disp_data.width; CurrentScreenHeight = disp_data.height; /* creating display with different resolutions for different screens */ display = al_create_display(CurrentScreenWidth, CurrentScreenHeight); } else { al_set_new_display_flags(ALLEGRO_WINDOWED); display = al_create_display(CurrentScreenWidth, CurrentScreenHeight); } if (!display) { al_show_native_message_box(display, "Error", "Display Settings", "Couldn't create a display.", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } /* setting new window title */ al_set_window_title(display, "Snake"); // ----------------------- /* creating fonts */ ALLEGRO_FONT * font = al_load_font(MainFont, 36, ALLEGRO_ALIGN_CENTER); ALLEGRO_FONT * credits_font = al_load_font(CreditsFont, 20, NULL); if (!font) { al_show_native_message_box(display, "Error", "Could not load font file.", "Have you included the resources in the same directory of the program?", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } /* loading audio samples */ ALLEGRO_SAMPLE * over_button_sound = al_load_sample(OverButton); ALLEGRO_SAMPLE * pressed_button_sound = al_load_sample(PressedButton); ALLEGRO_SAMPLE * eating_apple_sound = al_load_sample(EatApple); ALLEGRO_SAMPLE * game_over_sound = al_load_sample(GameOverSound); if (!over_button_sound || !pressed_button_sound) { al_show_native_message_box(display, "Error", "Could not load one or more sound files.", "Your resources folder must be corrupt, please download it again.", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } al_reserve_samples(2); /* creating timer */ ALLEGRO_TIMER * timer = al_create_timer(1.0 / FPS); ALLEGRO_TIMER *frametimer = al_create_timer(1.0 / frameFPS); /* creating event queue */ ALLEGRO_EVENT_QUEUE * event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_timer_event_source(frametimer)); al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue, al_get_keyboard_event_source()); ALLEGRO_KEYBOARD_STATE keystate; /* loading BITMAPS */ ALLEGRO_BITMAP * SmallWallpaperBitmap = al_load_bitmap(SmallWallpaper); ALLEGRO_BITMAP * BigWallpaperBitmap = al_load_bitmap(BigWallpaper); ALLEGRO_BITMAP * mouse = al_load_bitmap(MouseCursor); ALLEGRO_BITMAP * applepng = al_load_bitmap(Apple_png); if (!mouse) { al_show_native_message_box(display, "Error", "Could not load one or more resource file.", "Your resources folder must be corrupt, please download it again.", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } al_hide_mouse_cursor(display); /* ---- VARIABLES ---- */ bool done = false, change_resolution = false, draw = true; int mouse_x = CurrentScreenWidth, mouse_y = CurrentScreenHeight; /* mouse */ bool left_mouse_button_down = false; bool left_mouse_button_up = false; /* MAIN MENU */ int button_displacement; if (!Fullscreen) { button_displacement = 20; } else { button_displacement = 15; } Button play_button(50, 20, Blue, 0, -button_displacement); Button options_button(40, 15, Blue); Button exit_button(40, 15, Blue, 0, button_displacement); /* PLAY */ string score; Direction new_direction; int speed_up, speed_up_anim_frame = 0; bool speed_up_anim = false; Snake snake; Apple apple; /* starting timers */ al_start_timer(timer); al_start_timer(frametimer); while (!done) { /* actually defining our events */ ALLEGRO_EVENT events; al_wait_for_event(event_queue, &events); al_get_keyboard_state(&keystate); switch (gameState) { case MainMenu: { /* WINDOW */ if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } /* MOUSE */ if (events.type == ALLEGRO_EVENT_MOUSE_AXES) { mouse_x = events.mouse.x; mouse_y = events.mouse.y; draw = true; } if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { /* left button */ if (events.mouse.button & 1) { left_mouse_button_down = true; draw = true; } } if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { /* left button */ if (events.mouse.button & 1) { left_mouse_button_down = false; left_mouse_button_up = true; draw = true; } } /* button conditions */ if (play_button.MouseOverButton(mouse_x, mouse_y) && !play_button.StillOverButton) { play_button.play_over_button_sound = true; } if (options_button.MouseOverButton(mouse_x, mouse_y) && !options_button.StillOverButton) { options_button.play_over_button_sound = true; } if (exit_button.MouseOverButton(mouse_x, mouse_y) && !exit_button.StillOverButton) { exit_button.play_over_button_sound = true; } /* KEYBOARD */ if (events.type == ALLEGRO_EVENT_KEY_UP) { switch (events.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: { done = true; break; } case ALLEGRO_KEY_SPACE: case ALLEGRO_KEY_ENTER: { /* STARTING GAME NOW */ new_direction = RIGHT; speed_up = 0; snake.ResetSnakeDetails(); apple.NewApple(snake.GetSnakeCells()); gameState = PlayGame; } } break; } /* ------------ NOW DRAWING ------------ */ if (draw) { /* drawing wallpaper */ if (Fullscreen) { al_draw_scaled_bitmap(BigWallpaperBitmap, 0, 0, al_get_bitmap_width(BigWallpaperBitmap), al_get_bitmap_height(BigWallpaperBitmap), 0, 0, CurrentScreenWidth, CurrentScreenHeight, NULL); } else { al_draw_bitmap(SmallWallpaperBitmap, 0, 0, 0); } /* -- play button -- */ if (play_button.MouseOverButton(mouse_x, mouse_y)) { play_button.SetButtonColor(LightBlue); /* button pressed */ if (left_mouse_button_down) { play_button.pressed_button = true; play_button.SetButtonColor(DarkBlue); } /* button released */ else if (left_mouse_button_up) { play_button.pressed_button = false; play_button.StillPressingButton = false; play_button.SetButtonColor(Blue); /* STARTING GAME NOW */ new_direction = RIGHT; speed_up = 0; snake.ResetSnakeDetails(); apple.NewApple(snake.GetSnakeCells()); gameState = PlayGame; } play_button.DisplayButton(); } else { play_button.pressed_button = false; play_button.StillPressingButton = false; play_button.StillOverButton = false; play_button.SetButtonColor(Blue); play_button.DisplayButton(); } /* -- options button -- */ if (options_button.MouseOverButton(mouse_x, mouse_y)) { options_button.SetButtonColor(LightBlue); /* button pressed */ if (left_mouse_button_down) { options_button.pressed_button = true; options_button.SetButtonColor(DarkBlue); } /* button released */ else if (left_mouse_button_up) { options_button.pressed_button = false; options_button.StillPressingButton = false; options_button.SetButtonColor(Blue); switch (Fullscreen) { case 0: { Fullscreen = 1; done = true; change_resolution = true; break; } case 1: { Fullscreen = 0; done = true; change_resolution = true; break; } } } options_button.DisplayButton(); } else { options_button.pressed_button = false; options_button.StillPressingButton = false; options_button.StillOverButton = false; options_button.SetButtonColor(Blue); options_button.DisplayButton(); } /* -- exit button -- */ if (exit_button.MouseOverButton(mouse_x, mouse_y)) { exit_button.SetButtonColor(LightBlue); /* button pressed */ if (left_mouse_button_down) { exit_button.pressed_button = true; exit_button.SetButtonColor(DarkBlue); } /* button released */ else if (left_mouse_button_up) { exit_button.pressed_button = false; exit_button.StillPressingButton = false; exit_button.SetButtonColor(Blue); done = true; } exit_button.DisplayButton(); } else { exit_button.pressed_button = false; exit_button.StillPressingButton = false; exit_button.StillOverButton = false; exit_button.SetButtonColor(Blue); exit_button.DisplayButton(); } /* -- sound -- */ /* mouse over button */ if (play_button.MouseOverButton(mouse_x, mouse_y) && !play_button.StillOverButton) { play_button.StillOverButton = true; al_play_sample(over_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } if (options_button.MouseOverButton(mouse_x, mouse_y) && !options_button.StillOverButton) { options_button.StillOverButton = true; al_play_sample(over_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } if (exit_button.MouseOverButton(mouse_x, mouse_y) && !exit_button.StillOverButton) { exit_button.StillOverButton = true; al_play_sample(over_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } /* button pressed */ if (play_button.pressed_button && !play_button.StillPressingButton) { play_button.StillPressingButton = true; al_play_sample(pressed_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } else if (options_button.pressed_button && !options_button.StillPressingButton) { options_button.StillPressingButton = true; al_play_sample(pressed_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } else if (exit_button.pressed_button && !exit_button.StillPressingButton) { exit_button.StillPressingButton = true; al_play_sample(pressed_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } /* -- text -- */ string fullscreenmode_string; if (Fullscreen) { fullscreenmode_string = "Fullscreen: On"; } else { fullscreenmode_string = "Fullscreen: Off"; } al_draw_text(font, White, CurrentScreenWidth / 2, play_button.GetButtonHeightCenter() - 23, ALLEGRO_ALIGN_CENTER, "New Game"); al_draw_text(font, White, CurrentScreenWidth / 2, options_button.GetButtonHeightCenter() - 23, ALLEGRO_ALIGN_CENTER, fullscreenmode_string.c_str()); al_draw_text(font, White, CurrentScreenWidth / 2, exit_button.GetButtonHeightCenter() - 23, ALLEGRO_ALIGN_CENTER, "Exit"); al_draw_text(credits_font, White, 3, CurrentScreenHeight - 20, NULL, "FEUP 2013 - Henrique Ferrolho"); /* -- mouse cursor -- */ al_draw_bitmap(mouse, mouse_x, mouse_y, NULL); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); left_mouse_button_up = false; draw = false; } break; } case PlayGame: { if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } /* KEYBOARD */ if (events.type == ALLEGRO_EVENT_KEY_UP) { switch (events.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: { /* going back to MAIN MENU */ draw = true; gameState = MainMenu; break; } case ALLEGRO_KEY_ENTER: case ALLEGRO_KEY_SPACE: { /* pausing game */ draw = true; gameState = PauseGame; break; } } break; } if (events.type == ALLEGRO_EVENT_TIMER) { if (events.timer.source == timer) { draw = true; /* navigation keys */ if (al_key_down(&keystate, ALLEGRO_KEY_DOWN) || al_key_down(&keystate, ALLEGRO_KEY_S)) { new_direction = DOWN; } else if (al_key_down(&keystate, ALLEGRO_KEY_UP) || al_key_down(&keystate, ALLEGRO_KEY_W)) { new_direction = UP; } else if (al_key_down(&keystate, ALLEGRO_KEY_RIGHT) || al_key_down(&keystate, ALLEGRO_KEY_D)) { new_direction = RIGHT; } else if (al_key_down(&keystate, ALLEGRO_KEY_LEFT) || al_key_down(&keystate, ALLEGRO_KEY_A)) { new_direction = LEFT; } /* checking boundaries */ if (!snake.IsInScreenBoundaries() || snake.EatedItself()) { draw = true; al_play_sample(game_over_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); gameState = GameOver; break; } } else if (events.timer.source == frametimer) { /* moving snake */ snake.SetSnakeDirection(new_direction); snake.MoveSnake(); if (snake.EatedApple(apple.GetAppleX(), apple.GetAppleY())) { al_play_sample(eating_apple_sound, 0.5, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); apple.NewApple(snake.GetSnakeCells()); snake.IncreaseSnakeLength(); draw = true; } } } /* ------------ NOW DRAWING ------------ */ if (draw) { /* game frame */ if (Fullscreen) { al_draw_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed, 20); //al_draw_rectangle(0, 0, 1360, 760, DarkRed, 20); } else { al_draw_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed, 20); } apple.DrawApple(applepng); snake.DrawSnake(); /* increasing speed */ if (snake.GetSnakeCells().size() < 5) { al_set_timer_speed(frametimer, 1.0 / frameFPS); } else if (snake.GetSnakeCells().size() < 10) { if (speed_up == 0) { speed_up++; speed_up_anim = true; } al_set_timer_speed(frametimer, 1.0 / 14); } else if (snake.GetSnakeCells().size() < 20) { if (speed_up == 1) { snake.SetColor(LightBlue); speed_up++; speed_up_anim = true; } al_set_timer_speed(frametimer, 1.0 / 16); } else if (snake.GetSnakeCells().size() < 30) { if (speed_up == 2) { speed_up++; speed_up_anim = true; } al_set_timer_speed(frametimer, 1.0 / 18); } else if (snake.GetSnakeCells().size() < 40) { if (speed_up == 3) { snake.SetColor(DarkRed); speed_up++; speed_up_anim = true; } al_set_timer_speed(frametimer, 1.0 / 20); } /* speed up animation */ if (speed_up_anim) { if (speed_up_anim_frame < 10) { al_draw_text(font, White, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 20) { al_draw_text(font, Yellow, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 30) { al_draw_text(font, White, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 40) { al_draw_text(font, Yellow, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 50) { al_draw_text(font, White, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 60) { speed_up_anim = false; speed_up_anim_frame = 0; } } /* printing score */ stringstream ss; ss << "Score: " << snake.GetSnakeCells().size(); score = ss.str(); al_draw_text(credits_font, Yellow, 60, 15, ALLEGRO_ALIGN_CENTER, score.c_str()); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); draw = false; } break; } case PauseGame: { if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } if (events.type == ALLEGRO_EVENT_KEY_UP) { switch (events.keyboard.keycode) { case ALLEGRO_KEY_SPACE: case ALLEGRO_KEY_ENTER: { /* resume game */ draw = true; gameState = PlayGame; break; } } break; } /* ------------ NOW DRAWING ------------ */ if (draw) { /* game frame */ if (Fullscreen) { al_draw_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed, 20); //al_draw_rectangle(0, 0, 1360, 760, DarkRed, 20); } else { al_draw_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed, 20); } apple.DrawApple(applepng); snake.DrawSnake(); /* printing score */ stringstream ss; ss << "Score: " << snake.GetSnakeCells().size(); score = ss.str(); al_draw_text(credits_font, Yellow, 60, 15, ALLEGRO_ALIGN_CENTER, score.c_str()); al_draw_text(font, Yellow, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "GAME PAUSED"); al_flip_display(); draw = false; } break; } case GameOver: { if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } /* MOUSE */ if (events.type == ALLEGRO_EVENT_MOUSE_AXES) { mouse_x = events.mouse.x; mouse_y = events.mouse.y; draw = true; } if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { /* left button */ if (events.mouse.button & 1) { left_mouse_button_down = true; draw = true; } } if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { /* left button */ if (events.mouse.button & 1) { left_mouse_button_down = false; left_mouse_button_up = true; draw = true; } } /* going to MAIN MENU */ if (events.type == ALLEGRO_EVENT_KEY_UP) { switch (events.keyboard.keycode) { case ALLEGRO_KEY_SPACE: case ALLEGRO_KEY_ENTER: { draw = true; gameState = MainMenu; break; } } break; } if (left_mouse_button_up) { left_mouse_button_up = false; draw = true; gameState = MainMenu; break; } if (draw) { al_draw_filled_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed); /* printing score */ stringstream ss; ss << "Score: " << snake.GetSnakeCells().size(); score = ss.str(); al_draw_text(font, Yellow, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 80, ALLEGRO_ALIGN_CENTER, score.c_str()); al_draw_text(font, White, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "Click to continue"); /* -- mouse cursor -- */ al_draw_bitmap(mouse, mouse_x, mouse_y, NULL); al_flip_display(); } break; } } } /* dealocating memory */ al_destroy_display(display); al_destroy_font(font); al_destroy_timer(timer); al_destroy_bitmap(mouse); al_destroy_sample(over_button_sound); al_destroy_sample(pressed_button_sound); al_destroy_event_queue(event_queue); if (change_resolution) { CurrentScreenWidth = DefaultScreenWidth; CurrentScreenHeight = DefaultScreenHeight; start_game(Fullscreen); } }
int main(){ // variables {{{1 int x=10; int y=10; int estado=2; ALLEGRO_DISPLAY *Screen; ALLEGRO_EVENT_QUEUE *qu; ALLEGRO_EVENT Event; ALLEGRO_TIMER *timer; ALLEGRO_BITMAP *Image = NULL; ALLEGRO_BITMAP *background= NULL; bool Exit = false; //}}}1 // Iniciando allegro {{{1 if(!al_init()){ cout<<"eror iniciando allegro"<<endl; return -1; } if(!al_init_image_addon()){ cout<<"error iniciando addon de imagenes"<<endl; return -1; } if(!al_init_primitives_addon()){ cout<<"Couldn't initialize primitives addon!\n"; return -1; } if(!al_install_keyboard()){ cout<<"error iniciando teclado"<<endl; return -1; } //}}}1 // pantalla y resoluciones {{{1 ALLEGRO_DISPLAY_MODE disp_data; int n=al_get_num_display_modes(); bool continuar_resolucion_w=false; bool continuar_resolucion_h=false; for(int i=0;i<n;i++){ al_get_display_mode(i, &disp_data); int w=disp_data.width; int h=disp_data.height; if(w==SWIDTH){ continuar_resolucion_w=true; } if(h==SHEIGHT){ continuar_resolucion_h=true; } } if(continuar_resolucion_w==false && continuar_resolucion_h==false){ cerr<<"resolucion no soportada"<<endl; return -1; } //al_set_new_display_flags(ALLEGRO_FULLSCREEN); Screen = al_create_display(SWIDTH,SHEIGHT); if(!Screen){ cout<<"no se pudo crear el display"<<endl; } //}}}1 // crear queue - timer screen keyboard {{{1 qu= al_create_event_queue(); if(!qu){ cout<<"error creando queue"<<endl; } al_register_event_source(qu, al_get_display_event_source(Screen)); timer = al_create_timer(1.0 / 35); if(!timer){ cout<<"error iniciando timer"<<endl; } al_register_event_source(qu, al_get_timer_event_source(timer)); al_register_event_source(qu, al_get_keyboard_event_source()); //}}}1 // iniciar clases y timer{{{1 // 0 inicio // 1 menu // 2 core Inicio inicio; Menu menu; Core core; inicio.Iniciar(); core.Iniciar_core(Screen); al_start_timer(timer); Image = al_load_bitmap("data/test.png"); ///load the bitmap from a file background= al_load_bitmap("data/background.png"); // ALLEGRO_COLOR clearcol=al_map_rgb(255,255,255); //}}}1 // main loop {{{1 while(Exit == false){ al_wait_for_event(qu, &Event); if(al_is_event_queue_empty(qu)){ if(estado==0){ inicio.Display(Screen); menu.Iniciar_menu(Screen); estado=1; } if(estado==1){ menu.Display(Screen); core.Iniciar_core(Screen); } if(estado==2){ x+=1; core.Animar(); al_draw_bitmap(background,0,0,0); core.Display(Screen); al_draw_bitmap(Image,x,y,0); } al_flip_display(); } if(Event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { Exit = true; } if(Event.type == ALLEGRO_EVENT_TIMER){ } if(Event.type==ALLEGRO_EVENT_KEY_DOWN){ Exit=true; } } return 0; }
int mainAdam(void) { //variables const int numSprites = 1; BaseMonster orbs[numSprites]; Player mario; Map map; Menu menu; Keyboard keyboard; bool done = false; bool render = false; settings.init(); //int xOff = 0; //int yOff = 0; //allegro variable ALLEGRO_DISPLAY *display = NULL; ALLEGRO_DISPLAY_MODE disp_data; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer; ALLEGRO_BITMAP *image; //program init if (!al_init()) //initialize Allegro return -1; int i = al_get_num_display_modes(); al_get_display_mode(1, &disp_data); //al_set_new_display_flags(ALLEGRO_FULLSCREEN); display = al_create_display(disp_data.width, disp_data.height); //create our display object if (!display) //test display object return -1; //addon init keyboard.init(display); al_init_image_addon(); al_install_audio(); al_init_acodec_addon(); al_reserve_samples(1); mario.Init(&map,1); globalText.init(); //menu.init(); if (map.init("Maps/test.fmp")) { return -5; } for (int i = 0; i < numSprites; i++) orbs[i].Init(); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / 60); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_start_timer(timer); while (!keyboard.keys[Keyboard::ESC]) { /*if (menu.state == END) break;*/ ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); keyboard.update(ev); if (ev.type == ALLEGRO_EVENT_TIMER) { /*if (menu.state == MENU) { menu.update(keyboard.keys); menu.updateBackgrounds(); } else {*/ if (!mario.win) { for (int i = 0; i < numSprites; i++){ orbs[i].Update(); mario.collisionWithOther(&orbs[i]); for (int v = 0; v < bumpingBlockAnimation.size(); v++) bumpingBlockAnimation[v].collisionWithOther(&orbs[i]); } mario.Update(keyboard.keys); for (int v = 0; v < bumpingBlockAnimation.size(); v++) bumpingBlockAnimation[v].Update(); for (int v = 0; v < destroyBrickAnimation.size(); v++) destroyBrickAnimation[v].Update(); for (int v = 0; v < jumpingCoinAnimation.size(); v++) jumpingCoinAnimation[v].Update(); if (map.item->live) { map.item->Update(); map.item->collisionWithOther(&mario); for (int v = 0; v < bumpingBlockAnimation.size(); v++) bumpingBlockAnimation[v].collisionWithOther(map.item); } //} } else { } render = true; } if (render && al_is_event_queue_empty(event_queue)) { render = false; /*if (menu.state == MENU) { menu.drawBackgrounds(); } else {*/ map.draw(); globalText.update(&mario); globalText.draw(); mario.Draw(); for (int v = 0; v < bumpingBlockAnimation.size(); v++) bumpingBlockAnimation[v].Draw(); for (int v = 0; v < destroyBrickAnimation.size(); v++) destroyBrickAnimation[v].Draw(); for (int v = 0; v < jumpingCoinAnimation.size(); v++) jumpingCoinAnimation[v].Draw(); if (map.item->live) map.item->Draw(); for (int i = 0; i < numSprites; i++) orbs[i].Draw(); //} al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); } } map.del(); keyboard.del(); al_destroy_event_queue(event_queue); al_destroy_display(display); //destroy our display object return 0; }
// Update loop void menu::update(){ //Menu animations if( animation_pos < 100 && !startClicked) animation_pos += 4; if( animation_pos > 0 && startClicked) animation_pos -= 4; // Start the game if( startClicked && animation_pos <= 0) set_next_state( STATE_GAME); // Open submenu or start game if( mini_screen == MINISTATE_MENU){ // Start game with controller if( joystickListener::buttonPressed[JOY_XBOX_START] || joystickListener::buttonPressed[JOY_XBOX_A]){ startClicked = true; } // Buttons if( mouseListener::mouse_pressed & 1){ // Start game if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 40, 40 + al_get_bitmap_width(start), mouseListener::mouse_y, mouseListener::mouse_y, 410, 410 + al_get_bitmap_height(start))){ startClicked = true; } // Scores else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 660, 660 + al_get_bitmap_width(highscores_button), mouseListener::mouse_y, mouseListener::mouse_y, 30, 30 + al_get_bitmap_height(highscores_button))){ updateScores( scores); mini_screen = MINISTATE_SCORES; } // Credits menu else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 542, 644, mouseListener::mouse_y, mouseListener::mouse_y, 548, 600)){ mini_screen = MINISTATE_CREDITS; } // Controls menu else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 644, 696, mouseListener::mouse_y, mouseListener::mouse_y, 548 ,600)){ mini_screen = MINISTATE_CONTROLS; } // Help screen else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 696, 749, mouseListener::mouse_y, mouseListener::mouse_y, 548, 600)){ mini_screen = MINISTATE_TUTORIAL; } // Options menu else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 749, 800, mouseListener::mouse_y, mouseListener::mouse_y, 548, 600)){ mini_screen = MINISTATE_OPTIONS; } } } // Exit menus else if( mini_screen == MINISTATE_TUTORIAL || mini_screen == MINISTATE_CREDITS || mini_screen == MINISTATE_CONTROLS || mini_screen == MINISTATE_SCORES ){ if( keyListener::lastKeyPressed != -1 || mouseListener::mouse_pressed & 1 || joystickListener::lastButtonPressed != -1){ mini_screen = MINISTATE_MENU; draw(); } } // Options else if( mini_screen == MINISTATE_OPTIONS && mouseListener::mouse_pressed & 1){ // Particles toggle if( collision( 280, 360, mouseListener::mouse_x, mouseListener::mouse_x, 400, 480, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_PARTICLE_TYPE] = (settings[SETTING_PARTICLE_TYPE] + 1) % 4; } // Sound button toggle else if( collision( 120, 200, mouseListener::mouse_x, mouseListener::mouse_x, 180, 260, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_SOUND] = (settings[SETTING_SOUND] + 1) % 2; } // Music button toggle else if( collision( 280, 360, mouseListener::mouse_x, mouseListener::mouse_x, 180, 260, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_MUSIC] = (settings[SETTING_MUSIC] + 1) % 2; if( settings[SETTING_MUSIC] == 0) al_stop_sample( ¤tMusic); else al_play_sample( music_mainmenu, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, ¤tMusic); } // Fullscreen toggle else if( collision( 120, 200, mouseListener::mouse_x, mouseListener::mouse_x, 400, 480, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_FULLSCREEN] = (settings[SETTING_FULLSCREEN] + 1) % 2; if( settings[SETTING_FULLSCREEN]){ // Fullscreen stuff al_destroy_display( display); al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); display = al_create_display( SCREEN_W, SCREEN_H); ALLEGRO_DISPLAY_MODE disp_data; al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); float sx = disp_data.width / (float)SCREEN_W; float sy = disp_data.height / (float)SCREEN_H; ALLEGRO_TRANSFORM trans; al_identity_transform(&trans); al_scale_transform(&trans, sx, sy); al_use_transform(&trans); al_hide_mouse_cursor( display); } else{ al_destroy_display( display); al_set_new_display_flags(ALLEGRO_WINDOWED); display = al_create_display( SCREEN_W, SCREEN_H); al_hide_mouse_cursor( display); } } //Screen shake else if( collision( 280, 360, mouseListener::mouse_x, mouseListener::mouse_x, 290, 370, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_SCREENSHAKE] = (settings[SETTING_SCREENSHAKE] + 1) % 4; } // Control Toggle else if( collision( 120, 200, mouseListener::mouse_x, mouseListener::mouse_x, 290, 370, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_CONTROLMODE] = ((settings[SETTING_CONTROLMODE] + 1) % 3); } // Power off else if( collision( 540, 620, mouseListener::mouse_x, mouseListener::mouse_x, 180, 260, mouseListener::mouse_y, mouseListener::mouse_y)){ write_settings(); set_next_state( STATE_EXIT); } // Exit menu else if( collision( 540, 620, mouseListener::mouse_x, mouseListener::mouse_x, 407, 487, mouseListener::mouse_y, mouseListener::mouse_y)){ mini_screen = MINISTATE_MENU; write_settings(); } } // Update mouse particles if( settings[SETTING_PARTICLE_TYPE] != 3 && mouse_rocket_up){ for( int i = 0; i < 500; i++){ if( random( 1, 10) == 1){ ALLEGRO_COLOR part_color = al_map_rgb( 255, random(0,255), 0); if( settings[SETTING_CHRISTMAS]){ int red_or_green = random( 0, 1) * 255; part_color = al_map_rgb( red_or_green, 255 - red_or_green, 0); } particle newParticle( mouseListener::mouse_x, mouseListener::mouse_y + 16, part_color, random( -2, 2), random( 8, 20), 1, settings[SETTING_PARTICLE_TYPE]); mousePart.push_back( newParticle); } } } for( unsigned int i = 0; i < mousePart.size(); i++){ mousePart.at(i).logic(); if( random( 0, 10) == 0) mousePart.erase( mousePart.begin() + i); } // Close game if( keyListener::key[ALLEGRO_KEY_ESCAPE]) set_next_state( STATE_EXIT); // Check if mouse is going up mouse_rocket_up = ( mouseListener::mouse_y < mouseMove); mouseMove = mouseListener::mouse_y; }
int main(int argc, char **argv) { bool done = false; bool render = false; float gameTime = 0; int frames = 0; int gameFPS = 0; float evTimer = 0; tractor = new Tractor(); Xml = new xml(); int state = -1; ALLEGRO_BITMAP *icon; ALLEGRO_BITMAP *map = NULL; ALLEGRO_BITMAP *panel = NULL; ALLEGRO_BITMAP *tractorImage = NULL; ALLEGRO_BITMAP *titleImage = NULL; ALLEGRO_BITMAP *lostImage = NULL; ALLEGRO_SAMPLE *titleSong = NULL; ALLEGRO_SAMPLE *gameSong = NULL; ALLEGRO_SAMPLE *lostSong = NULL; ALLEGRO_SAMPLE *cash = NULL; ALLEGRO_BITMAP *L1 = NULL; ALLEGRO_BITMAP *L2 = NULL; ALLEGRO_BITMAP *L3 = NULL; ALLEGRO_BITMAP *L4 = NULL; ALLEGRO_BITMAP *L5 = NULL; ALLEGRO_BITMAP *L6 = NULL; ALLEGRO_BITMAP *L7 = NULL; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_DISPLAY_MODE disp_data; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer; ALLEGRO_FONT *font; ALLEGRO_FONT *score; if (!al_init()) return -1; al_install_keyboard(); al_install_mouse(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); al_install_audio(); al_init_acodec_addon(); al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); //al_set_new_display_flags(ALLEGRO_FULLSCREEN); al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_REQUIRE); display = al_create_display(disp_data.width, disp_data.height); icon = al_load_bitmap("icon.png"); al_set_display_icon(display, icon); float sx = (float)disp_data.width / WIDTH; float sy = (float)disp_data.height / HEIGHT; ALLEGRO_TRANSFORM trans; al_identity_transform(&trans); al_scale_transform(&trans, sx, sy); al_use_transform(&trans); if (!display) return -1; font = al_load_font("arial.ttf", 20, 0); score = al_load_font("score.ttf", 45, 0); al_reserve_samples(15); map = al_load_bitmap("map2.png"); panel = al_load_bitmap("panel.png"); L1 = al_load_bitmap("l1.png"); L2 = al_load_bitmap("l2.png"); L3 = al_load_bitmap("l3.png"); L4 = al_load_bitmap("l4.png"); L5 = al_load_bitmap("l5.png"); L6 = al_load_bitmap("l6.png"); L7 = al_load_bitmap("l7.png"); Background *Map = new Background(map); objects.push_back(Map); TextBox *Task = new TextBox; Field *field1 = new Field(L1, L2, L3, L4, L5, L6, L7, 50, 50); objects.push_back(field1); Field *field2 = new Field(L1, L2, L3, L4, L5, L6, L7, 450, 50); objects.push_back(field2); Field *field3 = new Field(L1, L2, L3, L4, L5, L6, L7, 50, 450); objects.push_back(field3); Field *field4 = new Field(L1, L2, L3, L4, L5, L6, L7, 450, 450); objects.push_back(field4); tractorImage = al_load_bitmap("tractor.png"); cash = al_load_sample("cash.ogg"); tractor->Init(tractorImage, cash); objects.push_back(tractor); titleImage = al_load_bitmap("screen_Title.png"); lostImage = al_load_bitmap("screen_Lost.png"); titleScreen = new Background(titleImage); lostScreen = new Background(lostImage); titleSong = al_load_sample("title.ogg"); gameSong = al_load_sample("game.ogg"); lostSong = al_load_sample("lost.ogg"); songInstance = al_create_sample_instance(titleSong); al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP); songInstance2 = al_create_sample_instance(gameSong); al_set_sample_instance_playmode(songInstance2, ALLEGRO_PLAYMODE_LOOP); songInstance3 = al_create_sample_instance(lostSong); al_set_sample_instance_playmode(songInstance3, ALLEGRO_PLAYMODE_LOOP); al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer()); al_attach_sample_instance_to_mixer(songInstance2, al_get_default_mixer()); al_attach_sample_instance_to_mixer(songInstance3, al_get_default_mixer()); ChangeState(state, TITLE); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / 60); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); al_start_timer(timer); gameTime = al_current_time(); while (!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_ENTER: keys[ENTER] = true; if (state == TITLE) ChangeState(state, PLAYING); else if (state == PLAYING && Task->CheckText()) { TextBox *text = new TextBox(); text->SetText(Task->Send()); history.push_back(text); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 400) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } Xml->interpreter(Task->GetLast(), tractor); TextBox *txtxml = new TextBox(); txtxml->SetText(Xml->wyslij()); history.push_back(txtxml); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 300) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } } else if (state == LOST) ChangeState(state, PLAYING); break; case ALLEGRO_KEY_TAB: keys[TAB] = true; if (state == PLAYING) { Task->SetStatus(); if (Task->GetStatus()) { TextBox *text = new TextBox(); text->SetText("Konsola zostala wlaczona"); history.push_back(text); } else { TextBox *text = new TextBox(); text->SetText("Konsola zostala wylaczona"); history.push_back(text); } for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 300) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } setTimer(evTimer); } tractor->Sell(); break; case ALLEGRO_KEY_SPACE: keys[SPC] = true; if (state == PLAYING) Task->Add(" "); break; case ALLEGRO_KEY_BACKSPACE: if (state == PLAYING && Task->CheckText()) Task->Backspace(); break; case ALLEGRO_KEY_COMMA: keys[COM] = true; if (state == PLAYING && Task->GetStatus()) Task->Add(","); break; case ALLEGRO_KEY_0: numb[N0] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("0"); break; case ALLEGRO_KEY_1: numb[N1] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("1"); break; case ALLEGRO_KEY_2: numb[N2] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("2"); break; case ALLEGRO_KEY_3: numb[N3] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("3"); break; case ALLEGRO_KEY_4: numb[N4] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("4"); break; case ALLEGRO_KEY_5: numb[N5] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("5"); break; case ALLEGRO_KEY_6: numb[N6] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("6"); break; case ALLEGRO_KEY_7: numb[N7] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("7"); break; case ALLEGRO_KEY_8: numb[N8] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("8"); break; case ALLEGRO_KEY_9: numb[N9] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("9"); break; case ALLEGRO_KEY_A: letters[A] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("a"); break; case ALLEGRO_KEY_B: letters[B] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("b"); break; case ALLEGRO_KEY_C: letters[C] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("c"); break; case ALLEGRO_KEY_D: letters[D] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("d"); break; case ALLEGRO_KEY_E: letters[E] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("e"); break; case ALLEGRO_KEY_F: letters[F] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("f"); break; case ALLEGRO_KEY_G: letters[G] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("g"); break; case ALLEGRO_KEY_H: letters[H] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("h"); break; case ALLEGRO_KEY_I: letters[I] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("i"); break; case ALLEGRO_KEY_J: letters[J] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("j"); break; case ALLEGRO_KEY_K: letters[K] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("k"); break; case ALLEGRO_KEY_L: letters[L] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("l"); break; case ALLEGRO_KEY_M: letters[M] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("m"); break; case ALLEGRO_KEY_N: letters[N] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("n"); break; case ALLEGRO_KEY_O: letters[O] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("o"); break; case ALLEGRO_KEY_P: letters[P] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("p"); break; case ALLEGRO_KEY_Q: letters[Q] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("q"); break; case ALLEGRO_KEY_R: letters[R] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("r"); break; case ALLEGRO_KEY_S: letters[S] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("s"); break; case ALLEGRO_KEY_T: letters[T] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("t"); break; case ALLEGRO_KEY_U: letters[U] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("u"); break; case ALLEGRO_KEY_V: letters[V] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("v"); break; case ALLEGRO_KEY_W: letters[W] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("w"); break; case ALLEGRO_KEY_X: letters[X] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("x"); break; case ALLEGRO_KEY_Y: letters[Y] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("y"); break; case ALLEGRO_KEY_Z: letters[Z] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("z"); break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_ENTER: keys[ENTER] = false; break; case ALLEGRO_KEY_TAB: keys[TAB] = false; break; case ALLEGRO_KEY_BACKSPACE: keys[BSPC] = false; break; case ALLEGRO_KEY_COMMA: keys[COM] = false; break; case ALLEGRO_KEY_0: numb[N0] = false; break; case ALLEGRO_KEY_1: numb[N1] = false; break; case ALLEGRO_KEY_2: numb[N2] = false; break; case ALLEGRO_KEY_3: numb[N3] = false; break; case ALLEGRO_KEY_4: numb[N4] = false; break; case ALLEGRO_KEY_5: numb[N5] = false; break; case ALLEGRO_KEY_6: numb[N6] = false; break; case ALLEGRO_KEY_7: numb[N7] = false; break; case ALLEGRO_KEY_8: numb[N8] = false; break; case ALLEGRO_KEY_9: numb[N9] = false; break; case ALLEGRO_KEY_A: letters[A] = false; break; case ALLEGRO_KEY_B: letters[B] = false; break; case ALLEGRO_KEY_C: letters[C] = false; break; case ALLEGRO_KEY_D: letters[D] = false; break; case ALLEGRO_KEY_E: letters[E] = false; break; case ALLEGRO_KEY_F: letters[F] = false; break; case ALLEGRO_KEY_G: letters[G] = false; break; case ALLEGRO_KEY_H: letters[H] = false; break; case ALLEGRO_KEY_I: letters[I] = false; break; case ALLEGRO_KEY_J: letters[J] = false; break; case ALLEGRO_KEY_K: letters[K] = false; break; case ALLEGRO_KEY_L: letters[L] = false; break; case ALLEGRO_KEY_M: letters[M] = false; break; case ALLEGRO_KEY_N: letters[N] = false; break; case ALLEGRO_KEY_O: letters[O] = false; break; case ALLEGRO_KEY_P: letters[P] = false; break; case ALLEGRO_KEY_Q: letters[Q] = false; break; case ALLEGRO_KEY_R: letters[R] = false; break; case ALLEGRO_KEY_S: letters[S] = false; break; case ALLEGRO_KEY_T: letters[T] = false; break; case ALLEGRO_KEY_U: letters[U] = false; break; case ALLEGRO_KEY_V: letters[V] = false; break; case ALLEGRO_KEY_W: letters[W] = false; break; case ALLEGRO_KEY_X: letters[X] = false; break; case ALLEGRO_KEY_Y: letters[Y] = false; break; case ALLEGRO_KEY_Z: letters[Z] = false; break; } } else if (ev.type == ALLEGRO_EVENT_TIMER) { render = true; frames++; if (al_current_time() - gameTime >= 1) { gameTime = al_current_time(); gameFPS = frames; frames = 0; } if (state == PLAYING) { if (keys[UP]) { if (Map->GetY() + Map->frameHeight > disp_data.height) { for (iter = objects.begin(); iter != objects.end(); ++iter) { (*iter)->SetY((*iter)->GetY() - 10); } tractor->SetDistY((tractor->GetDistY() - 10)); } } else if (keys[DOWN]) { if (Map->GetY() < 0) { for (iter = objects.begin(); iter != objects.end(); ++iter) { (*iter)->SetY((*iter)->GetY() + 10); } tractor->SetDistY(tractor->GetDistY() + 10); } } if (keys[LEFT]) { if (Map->GetWidth() > (disp_data.width - al_get_bitmap_width(panel))) { for (iter = objects.begin(); iter != objects.end(); ++iter) { (*iter)->SetX((*iter)->GetX() - 10); } tractor->SetDistX(tractor->GetDistX() - 10); } } else if (keys[RIGHT]) { if (Map->GetX() < 0) { for (iter = objects.begin(); iter != objects.end(); ++iter) { (*iter)->SetX((*iter)->GetX() + 10); } tractor->SetDistX(tractor->GetDistX() + 10); } } for (iter = objects.begin(); iter != objects.end(); ++iter) (*iter)->Update(); if (tractor->GetStatus()) tractor->Move(); field1->Change_Field(); field1->Grow_Field(); field2->Change_Field(); field2->Grow_Field(); field3->Change_Field(); field3->Grow_Field(); field4->Change_Field(); field4->Grow_Field(); field1->Action_On_Field(tractor); field2->Action_On_Field(tractor); field3->Action_On_Field(tractor); field4->Action_On_Field(tractor); if (!tractor->Get_Iminwork()){ Xml->ZKolejki(field1, field2, field3, field4, tractor); if (Xml->wyslij() != ""){ TextBox *txtxml = new TextBox(); txtxml->SetText(Xml->wyslij()); history.push_back(txtxml); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 300) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } } } if (evTimer < 60) { evTimer += 0.1; } else { if (tractor->GetPodpowiedz() == 0) { Xml->podpowiedz(field1, field2, field3, field4, tractor); evTimer = 0; TextBox *txtxml = new TextBox(); txtxml->SetText(Xml->wyslij()); history.push_back(txtxml); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 300) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } } } } if (tractor->GetMoney() <= 0) ChangeState(state, LOST); } for (iter = objects.begin(); iter != objects.end();) { if (!(*iter)->GetAlive()) { delete (*iter); iter = objects.erase(iter); } else iter++; } if (render && al_is_event_queue_empty(event_queue)) { render = false; if (state == TITLE) { titleScreen->Render(); } else if (state == PLAYING) { for (iter = objects.begin(); iter != objects.end(); ++iter) (*iter)->Render(); al_draw_bitmap(panel, WIDTH - al_get_bitmap_width(panel), 0, 0); al_draw_textf(font, al_map_rgb(255, 255, 255), Task->GetX(), Task->GetY(), 0, Task->ShowText()); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { al_draw_textf(font, al_map_rgb(255, 255, 255), (*iter2)->GetX(), (*iter2)->GetY(), 0, (*iter2)->ShowText()); } if (tractor->GetHealth() < 20) al_draw_textf(score, RED, WIDTH - 430, 15, 0, "%i", tractor->GetHealth()); else al_draw_textf(score, BLACK, WIDTH - 430, 15, 0, "%i", tractor->GetHealth()); if (tractor->GetFuel() < 20) al_draw_textf(score, RED, WIDTH - 260, 15, 0, "%i", tractor->GetFuel()); else al_draw_textf(score, BLACK, WIDTH - 260, 15, 0, "%i", tractor->GetFuel()); if (tractor->GetMoney() < 200) al_draw_textf(score, RED, WIDTH - 400, 100, 0, "%i", tractor->GetMoney()); else al_draw_textf(score, BLACK, WIDTH - 400, 100, 0, "%i", tractor->GetMoney()); al_draw_textf(score, BLACK, WIDTH - 70, 15, 0, "%i", tractor->GetWater()); for (int j = 0; j < 5; j++) { al_draw_textf(font, BLACK, WIDTH - 170, 85 + j * 20, 0, "%i", tractor->GetSupply(0, j)); } for (int j = 0; j < 5; j++) { al_draw_textf(font, BLACK, WIDTH - 150, 85 + j * 20, 0, "%i", tractor->GetSupply(1, j)); } al_draw_textf(font, al_map_rgb(255, 0, 255), 5, 5, 0, "FPS: %i", WIDTH - al_get_bitmap_width(panel) /*gameFPS*/); } else if (state == LOST) lostScreen->Render(); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); } } for (iter = objects.begin(); iter != objects.end();) { (*iter)->Destroy(); delete (*iter); iter = objects.erase(iter); } for (iter2 = history.begin(); iter2 != history.end();) { (*iter2)->Destroy(); delete (*iter2); iter2 = history.erase(iter2); } //tractor->Destroy(); Task->Destroy(); titleScreen->Destroy(); lostScreen->Destroy(); delete titleScreen; delete lostScreen; al_destroy_sample(cash); al_destroy_sample_instance(songInstance); al_destroy_sample_instance(songInstance2); al_destroy_sample_instance(songInstance3); al_destroy_font(score); al_destroy_font(font); al_destroy_timer(timer); al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }
int main( int argc, char* argv[] ) { ALLEGRO_EVENT e; ALLEGRO_TIMER* t; int64_t framesToUpdate = 0; if( !al_init() ) { return -1; } al_init_font_addon(); if( !al_install_keyboard() || !al_install_mouse() || !al_init_primitives_addon() || !al_init_ttf_addon() || !al_init_image_addon() ) { return -1; } #if NETWORK_SUPPORT != 0 if( !install_network() ) { return -1; } #endif #if HTTP_SUPPORT if( !install_http() ) { return -1; } #ifdef PANDORA Downloads = new HttpManager(2); #else Downloads = new HttpManager(6); #endif #endif #if EXIT_IF_NO_AUDIO != 0 if( !al_install_audio() || !al_init_acodec_addon() ) { return -1; } voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2); if (!voice) return 1; mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); if (!mixer) return 1; if (!al_attach_mixer_to_voice(mixer, voice)) return 1; #else if( al_install_audio() ) { if( al_init_acodec_addon() ) { voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2); if( voice != 0 ) { mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); if( mixer != 0 ) al_attach_mixer_to_voice(mixer, voice); } } } #endif // EXIT_IF_NO_AUDIO // Random number is guarenteed to be random srand( 5 ); GameStack = new StageStack(); CurrentConfiguration = new Configuration(); if( CurrentConfiguration->FullScreen ) al_set_new_display_flags( ALLEGRO_FULLSCREEN_WINDOW ); al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); bool foundMode = false; int fallbackW = 640; int fallbackH = 480; if( CurrentConfiguration->ForceResolution ) { foundMode = true; } else { for( int modeIdx = 0; modeIdx < al_get_num_display_modes(); modeIdx++ ) { if( al_get_display_mode( modeIdx, &ScreenMode ) != NULL ) { if( ScreenMode.width == CurrentConfiguration->ScreenWidth && ScreenMode.height == CurrentConfiguration->ScreenHeight ) { foundMode = true; } else { fallbackW = ScreenMode.width; fallbackH = ScreenMode.height; } } if( foundMode ) break; } } if( foundMode ) { Screen = al_create_display( CurrentConfiguration->ScreenWidth, CurrentConfiguration->ScreenHeight ); } else { Screen = al_create_display( fallbackW, fallbackH ); CurrentConfiguration->ScreenWidth = fallbackW; CurrentConfiguration->ScreenHeight = fallbackH; } al_hide_mouse_cursor( Screen ); t = al_create_timer( 1.0 / SCREEN_FPS ); if( t == NULL ) Quit = true; al_start_timer( t ); EventQueue = al_create_event_queue(); al_register_event_source( EventQueue, al_get_display_event_source( Screen ) ); al_register_event_source( EventQueue, al_get_keyboard_event_source() ); al_register_event_source( EventQueue, al_get_mouse_event_source() ); al_register_event_source( EventQueue, al_get_timer_event_source( t ) ); #if NETWORK_SUPPORT != 0 al_register_event_source( EventQueue, get_network_event_source() ); #endif #if HTTP_SUPPORT Downloads->urlDownloads = CurrentConfiguration->MaxConcurrentDownloads; al_register_event_source( EventQueue, get_http_event_source() ); #endif Fonts = new FontManager(); Images = new ImageManager(); Audio = new SoundManager(); al_set_blender( ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA ); GameStack->Push( (Stage*)new BootUp() ); while( !Quit ) { if( GameStack->IsEmpty() ) { Quit = true; } else { while( al_get_next_event( EventQueue, &e ) ) { #if HTTP_SUPPORT Downloads->Event( &e ); #endif switch( e.type ) { case ALLEGRO_EVENT_DISPLAY_CLOSE: Quit = true; break; case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION: al_reconfigure_joysticks(); break; case ALLEGRO_EVENT_TIMER: if( e.timer.source == t ) framesToUpdate++; else if( !GameStack->IsEmpty() ) GameStack->Current()->Event( &e ); break; default: if( !GameStack->IsEmpty() ) GameStack->Current()->Event( &e ); switch( e.type ) { #if HTTP_SUPPORT case ALLEGRO_EVENT_HTTP: #endif #if NETWORK_SUPPORT case ALLEGRO_EVENT_NETWORK_CONNECTION: case ALLEGRO_EVENT_NETWORK_RECEIVEPACKET: case ALLEGRO_EVENT_NETWORK_DISCONNECTION: #endif case ALLEGRO_EVENT_BUTTON_CLICK: case ALLEGRO_EVENT_MOUSEEX_MOVE: case ALLEGRO_EVENT_MOUSEEX_DOWN: case ALLEGRO_EVENT_MOUSEEX_UP: case ALLEGRO_EVENT_MOUSEEX_CLICK: case ALLEGRO_EVENT_MOUSEEX_DOUBLECLICK: case ALLEGRO_EVENT_MOUSEEX_BOXED: case ALLEGRO_EVENT_MOUSEEX_WHEEL: al_unref_user_event( &e.user ); break; } break; } } if( framesToUpdate > 0 ) { for( int frmUp = 0; frmUp < framesToUpdate; frmUp++ ) { if( !GameStack->IsEmpty() ) GameStack->Current()->Update(); } framesToUpdate = 0; } al_clear_to_color( al_map_rgb( 128, 128, 128 ) ); if( !GameStack->IsEmpty() ) GameStack->Current()->Render(); al_flip_display(); Images->Tidy(); Fonts->Tidy(); Audio->Tidy(); } } while( !GameStack->IsEmpty() ) { GameStack->Pop(); } delete Downloads; delete Fonts; delete Images; delete Audio; al_destroy_event_queue( EventQueue ); al_destroy_display( Screen ); #if HTTP_SUPPORT uninstall_http(); #endif #if NETWORK_SUPPORT != 0 uninstall_network(); #endif al_uninstall_keyboard(); al_uninstall_mouse(); al_shutdown_primitives_addon(); al_shutdown_ttf_addon(); al_shutdown_image_addon(); al_uninstall_audio(); al_shutdown_font_addon(); return 0; }
int main(){ // variables {{{1 int fps=50; bool redraw=true; int x=10; int y=10; int mx=0; int my=0; int estado=INTRO; ALLEGRO_DISPLAY *Screen; ALLEGRO_EVENT_QUEUE *qu; ALLEGRO_EVENT Event; ALLEGRO_TIMER *timer; ALLEGRO_BITMAP *Image = NULL; ALLEGRO_BITMAP *background= NULL; ALLEGRO_FONT *font; ALLEGRO_FONT *bigfont; string registro; bool Exit = false; //}}}1 // Iniciando allegro {{{1 if(!al_init()){ cout<<"error iniciando allegro"<<endl; return -1; } if(!al_init_image_addon()){ cout<<"error iniciando addon de imagenes"<<endl; return -1; } if(!al_init_primitives_addon()){ cout<<"Couldn't initialize primitives addon!\n"; return -1; } if(!al_install_keyboard()){ cout<<"error iniciando teclado"<<endl; return -1; } if(!al_install_mouse()){ cout<<"error iniciando raton"<<endl; return -1; } al_init_font_addon(); al_init_ttf_addon(); font=al_load_font("data/helvetica.ttf",24,0); bigfont=al_load_font("data/helvetica.ttf", 48,0); if(!font){ cout<<"font esta vacio"<<endl; return -1; } if(!bigfont){ cout<<"big font esta vacio"<<endl; return -1; } //}}}1 // pantalla y resoluciones {{{1 ALLEGRO_DISPLAY_MODE disp_data; int n=al_get_num_display_modes(); bool continuar_resolucion_w=false; bool continuar_resolucion_h=false; for(int i=0;i<n;i++){ al_get_display_mode(i, &disp_data); int w=disp_data.width; int h=disp_data.height; if(w==SWIDTH){ continuar_resolucion_w=true; } if(h==SHEIGHT){ continuar_resolucion_h=true; } } if(continuar_resolucion_w==false && continuar_resolucion_h==false){ cerr<<"resolucion no soportada"<<endl; return -1; } //al_set_new_display_flags(ALLEGRO_FULLSCREEN); Screen = al_create_display(SWIDTH,SHEIGHT); if(!Screen){ cout<<"no se pudo crear el display"<<endl; } //}}}1 // crear queue - timer screen keyboard {{{1 qu= al_create_event_queue(); if(!qu){ cout<<"error creando queue"<<endl; } al_register_event_source(qu, al_get_display_event_source(Screen)); timer = al_create_timer(1.0 /fps); if(!timer){ cout<<"error iniciando timer"<<endl; } al_register_event_source(qu, al_get_timer_event_source(timer)); al_register_event_source(qu, al_get_keyboard_event_source()); al_register_event_source(qu, al_get_mouse_event_source()); //}}}1 // iniciar clases y timer{{{1 // 0 inicio // 1 menu // 2 core Inicio inicio; Menu menu_principal; Menu_juego menu_juego; Menu_carreras menu_carreras; Core core; Tienda tienda; Registros registros; inicio.Iniciar(); // core.Iniciar(Screen); al_start_timer(timer); Image = al_load_bitmap("data/test.png"); ///load the bitmap from a file background= al_load_bitmap("data/background.png"); // ALLEGRO_COLOR clearcol=al_map_rgb(255,255,255); //}}}1 // main loop {{{1 while(Exit == false){ al_wait_for_event(qu, &Event); //if(al_is_event_queue_empty(qu)){ if(Event.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ Exit = true; } if(Event.type==ALLEGRO_EVENT_KEY_DOWN){ cout<<Event.keyboard.keycode<<endl; if(Event.keyboard.keycode==59){ Exit=true; } if(Event.keyboard.keycode==1){ // a fps+=10; cout<<"fps actual "<<fps<<endl; } if(Event.keyboard.keycode==2){ // b fps-=10; cout<<"fps actual "<<fps<<endl; } if(Event.keyboard.keycode==16){ // core.runner.animando=true; } //cout<<fps<<endl; } if(Event.type==ALLEGRO_EVENT_MOUSE_AXES){ mx=Event.mouse.x; my=Event.mouse.y; } if(Event.type==ALLEGRO_EVENT_MOUSE_BUTTON_DOWN){ //cout<<Event.mouse.x<<endl; } if(Event.type == ALLEGRO_EVENT_TIMER){ if(estado==MENU_PRINCIPAL){menu_principal.Display(Screen, font,mx,my); }; if(estado==MENU_JUEGO){menu_juego.Display(Screen, font, mx,my); }; if(estado==MENU_CARRERAS){menu_carreras.Display(Screen, font, mx,my); }; if(estado==TIENDA){ tienda.Display();}; if(estado==REGISTROS){ registros.Display(); } ; if(estado==MAIN){ x+=1; core.Animar(); core.Mover(); core.Gestion(Event); al_draw_bitmap(background,0,0,0); core.Display(Screen, bigfont); al_draw_bitmap(Image,x,y,0); }; redraw=true; } if(estado==INTRO){ inicio.Display(Screen); // en este estado, cargar todo // // intentar hacer un thread para que que vea la // barra de progreso de carga menu_juego.Iniciar(Screen); menu_carreras.Iniciar(Screen); core.Iniciar(Screen); tienda.Iniciar(); registros.Iniciar(); int n=menu_principal.Iniciar_menu(Screen); if(n==1){ estado=MENU_PRINCIPAL; } } if(estado==MENU_PRINCIPAL){ //menu_principal.Display(Screen, font,mx,my); int next=menu_principal.Gestion(Event); switch(next){ case 0: // estado=CONFIGURACION break; case 1: estado=MENU_JUEGO; break; case 2: // estado=CONTINUAR break; case 3: // estado=GUARDAR break; case 4: Exit=true; break; default: break; } } // en menu juego estan la tienda, las carreras, los registros, etc. if(estado==MENU_JUEGO){ //menu_juego.Display(Screen, font, mx,my); int next=menu_juego.Gestion(Event); switch(next){ case 0: // tienda break; case 1: estado=MENU_CARRERAS; break; case 2: // records break; case 3: estado=MENU_PRINCIPAL; break; default: break; } } if(estado==MENU_CARRERAS){ //menu_carreras.Display(Screen, font, mx,my); int next=menu_carreras.Gestion(Event); if(next>-1 && next<99){ estado=MAIN; } if(next==99){ estado=MENU_JUEGO; } } if(estado==TIENDA){ //tienda.Display(); } if(estado==REGISTROS){ //registros.Display(); } if(estado==MAIN){ core.Gestion(Event); //x+=1; //core.Animar(); //al_draw_bitmap(background,0,0,0); //core.Display(Screen); //al_draw_bitmap(Image,x,y,0); } /* ALLEGRO_COLOR c=al_map_rgb( 25, 25, 25); string s= static_cast<ostringstream*>( &(ostringstream() << fps) )->str(); cout<<"antes del texto"<<endl; al_draw_text(font,c,20,20,0,s.c_str()); cout<<"despues"<<endl; */ //} if(redraw && al_is_event_queue_empty(qu)) { redraw=false; al_flip_display(); } // } } return 0; }