void init() { int depth, res; allegro_init(); alpng_init(); alfont_init(); depth = desktop_color_depth(); if (depth == 0) depth = 32; set_color_depth(depth); res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, SCREEN_WIDTH ,SCREEN_HEIGHT, 0, 0); if (res != 0) { allegro_message(allegro_error); exit(-1); } /* set the window title */ set_window_title("Title"); install_timer(); install_keyboard(); install_mouse(); /* add other initializations here */ }
// Sets up game (bitmaps, sounds, ect.) void setup(bool first) { //Assigns variables x = 0; y = 0; gridimage = 0; //Refreshes game board for(int i = 0; i < 4; i++) { for(int t = 0; t < 4; t++) { gridarray[i][t] = 0; } } if(first) { // Allegro Stuff allegro_init(); alpng_init(); install_keyboard(); install_mouse(); install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,"."); set_window_title("A.D.S. Games - Tic Tac Toe"); // Graphics set_color_depth(32); set_gfx_mode(GFX_AUTODETECT_WINDOWED, 300, 300, 0, 0); // Init variables gameRunning = true; turn = 0; selector = 0; soundfx = true; difficulty = 1; gameScreen = 1; // Sets button images one_player.set_images("images/buttons/one_player.png", "images/buttons/one_player_hover.png"); two_player.set_images("images/buttons/two_player.png", "images/buttons/two_player_hover.png"); quit.set_images("images/buttons/quit.png", "images/buttons/quit_hover.png"); menu.set_images("images/buttons/menu.png", "images/buttons/menu_hover.png"); sound.set_images("images/buttons/sound_on.png", "images/buttons/sound_on_hover.png"); difficulty_b.set_images("images/buttons/medium.png", "images/buttons/medium_hover.png"); // Assigns bitmaps buffer = create_bitmap(300, 300); grid = load_bitmap("images/grid.png", NULL); img_x = load_bitmap("images/x.png", NULL); img_o = load_bitmap("images/o.png", NULL); blank = load_bitmap("images/none.png", NULL); xwin = load_bitmap("images/xwin.png", NULL); owin = load_bitmap("images/owin.png", NULL); catsgame = load_bitmap("images/catsgame.png", NULL); main_menu = load_bitmap("images/main_menu.png", NULL); cursor[0] = load_bitmap("images/cursor.png", NULL); cursor[1] = load_bitmap("images/cursor_x.png", NULL); cursor[2] = load_bitmap("images/cursor_o.png", NULL); selected[0] = load_bitmap("images/selected.png", NULL); selected[1] = load_bitmap("images/selected2.png", NULL); selected[2] = load_bitmap("images/selected3.png", NULL); selected[3] = load_bitmap("images/selected4.png", NULL); // Assigns Sounds win = load_sample("sfx/win.wav"); lose = load_sample("sfx/lose.wav"); cat = load_sample("sfx/catsgame.wav"); place = load_sample("sfx/place.wav"); // Seeds random number generator srand (time(NULL)); // Sets button positions one_player.set_position(50, 70); two_player.set_position(50, 130); quit.set_position(50, 190); menu.set_position(0, 270); sound.set_position(110, 250); difficulty_b.set_position(150, 250); } }
//Main function int main(){ allegro_init(); alpng_init(); install_timer(); install_keyboard(); install_mouse(); install_joystick(JOY_TYPE_AUTODETECT); install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,"."); set_color_depth(32); // Setup basic functionality setup(); //Set the current state ID stateID = STATE_INIT; //Set the current game state object currentState = new Init(); while(!game_closed_esc && !close_button_pressed && stateID != STATE_EXIT){ while(ticks == 0){ rest(1); } while(ticks > 0){ //Esc key handler if(key[KEY_ESC]){ if(alert(NULL, "Are you sure you want to exit the game?",NULL,"&Resume Game", "&Exit", 'r','e' )==2) game_closed_esc=true; } int old_ticks = ticks; //Do state logic currentState -> update(); //Change state if needed change_state(); // Counter for FPS frames_done++; ticks--; if(old_ticks <= ticks){ break; } } if(game_time - old_time >= 10){ fps = frames_done; frames_done = 0; old_time = game_time; } //Do state rendering currentState -> draw( true); } //Clean up clean_up(); return 0; }