int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_install_keyboard(); al_install_mouse(); al_init_image_addon(); al_init_font_addon(); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); return 1; } init(); timer = al_create_timer(1.0 / ex.FPS); ex.queue = al_create_event_queue(); al_register_event_source(ex.queue, al_get_keyboard_event_source()); al_register_event_source(ex.queue, al_get_mouse_event_source()); al_register_event_source(ex.queue, al_get_display_event_source(display)); al_register_event_source(ex.queue, al_get_timer_event_source(timer)); al_start_timer(timer); run(); al_destroy_event_queue(ex.queue); return 0; }
// TODO: remove init_core_sprites, put instead ini_base_images. // Game entry point, calls all initialization functions before calling main_loop int main(int argc, char** argv) { // Declare main struct containing the important data data = (GDATAPTR)malloc(sizeof(GDATA)); // Initialize Allegro 5 and addon routines al_init(); al_init_primitives_addon(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); PHYSFS_init(argv[0]); // Initialize the core game data structure if(!data_init()) exit(EXIT_FAILURE); // Creates a player sprite if(!init_core_sprites()) exit(EXIT_FAILURE); // Initializes the buttons if(!init_buttons()) exit(EXIT_FAILURE); // Initializes the physics and collision detection if(!init_chipmunk()) exit(EXIT_FAILURE); // Create the progress bar loader(); // Enters the main loop, where everything happens main_loop(); // Cleanup before exit cleanup(data); // Return 0 before exit return 0; }
int main(void) { if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_init_primitives_addon(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); } queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); myfont = al_load_font("data/font.tga", 0, 0); if (!myfont) { abort_example("font.tga not found\n"); } while (!quit) { if (al_get_new_bitmap_flags() & ALLEGRO_FORCE_LOCKING) al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); else al_set_new_bitmap_flags(ALLEGRO_FORCE_LOCKING); run(); } al_destroy_event_queue(queue); return 0; }
int main() { al_init(); al_init_image_addon(); al_install_mouse(); al_init_primitives_addon(); al_init_font_addon(); al_init_ttf_addon(); al_install_keyboard(); equipAligner *editorWindow = new equipAligner(); //Initialize essential resource management resourceManager::resourcePack::initializeAllegroInterface(); gameEngine::resources::graphics.load("../../../Assets/compiledAssets/Graphics.gtd"); //Load the graphics package gameEngine::resources::data.load("../../../Assets/compiledAssets/Data.gtd"); //Load the graphics package editorWindow->initialize(); float timeLastFrame = al_get_time(); while (1) { double delta = 60/(1/(al_get_time()-timeLastFrame)); timeLastFrame = al_get_time(); static double accumulatedTime = 0; accumulatedTime += delta; const double fixedFrameTime = (double)1/(double)60; while( accumulatedTime > fixedFrameTime ) { accumulatedTime -= fixedFrameTime; editorWindow->update(fixedFrameTime); //update the world } editorWindow->render(); al_rest(0.0); } }
int main(int argc, char *argv[]) { ALLEGRO_DISPLAY *display; ALLEGRO_FONT *font; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro\n"); } al_init_primitives_addon(); al_install_keyboard(); al_install_mouse(); al_init_font_addon(); al_init_ttf_addon(); al_set_new_display_flags(ALLEGRO_GENERATE_EXPOSE_EVENTS); display = al_create_display(640, 480); if (!display) { abort_example("Unable to create display\n"); } font = al_load_font("data/DejaVuSans.ttf", 12, 0); if (!font) { abort_example("Failed to load data/DejaVuSans.ttf\n"); } Theme theme(font); Prog prog(theme, display); prog.run(); al_destroy_font(font); return 0; }
void GameEngine::Init() { running = true; al_init(); //Initialises the allegro library al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); display = al_create_display(scrx, scry); al_set_window_title(display, "Knights"); al_install_keyboard(); //Installs keyboard driver eventQueue = al_create_event_queue(); redrawQueue = al_create_event_queue(); al_register_event_source(eventQueue, al_get_display_event_source(display)); al_register_event_source(eventQueue, al_get_keyboard_event_source()); redrawTimer = al_create_timer(1.0f / FPS); al_register_event_source(redrawQueue, al_get_timer_event_source(redrawTimer)); al_start_timer(redrawTimer); srand((int)time(NULL)); //Randomises the numbers used in program so that it's not the same each time. }
void inic_biblioteca_allegro_font (void) { al_init_font_addon(); }
int main() { //shell vars bool render = false; //allegro vars ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; //allegro init functions printf ("Initializing allegro\n"); if (!al_init()) { al_show_native_message_box(NULL, NULL, NULL, "failed", NULL, 0); return -1; } printf("Creating display\n"); display = al_create_display(WIDTH, HEIGHT); if (!display) { al_show_native_message_box(NULL, NULL, NULL, "failed", NULL, 0); return -1; } printf("Installing addons\n"); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); al_init_image_addon(); al_install_keyboard(); al_install_mouse(); al_install_audio(); al_init_acodec_addon(); al_reserve_samples(10); //project inits srand(time(NULL)); printf("Initializing timer\n"); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); printf("Registering event sources\n"); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); printf("Init mouse and keyboard\n"); init_keyboard(); init_mouse(); printf("Loading assets\n"); load_bitmaps(); load_fonts(); load_samples(); printf ("Creating manager\n"); push_state(new TitleMenu()); printf("Beginning game\n"); while (!is_game_over()) { //declare an event ALLEGRO_EVENT event; //monitor event sources al_wait_for_event(event_queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { end_game(); } else if (event.type == ALLEGRO_EVENT_TIMER) { render = true; update_mouse(); update_keyboard(); handle_key(); update_game(); } // Render screen if (render && al_event_queue_is_empty(event_queue)) { render = false; render_game(); al_flip_display(); } } unload_assets(); al_destroy_event_queue(event_queue); al_destroy_display(display); al_destroy_timer(timer); return 0; }
int main(void) { srand(static_cast<int>(time(nullptr) % 100 )); bool keys[10] = {false, false, false, false, false, false, false, false, false, false}; bool done = false; bool redraw = true; int FPS = 60; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; //initialize allegro if(!al_init()) { al_show_native_message_box(NULL, NULL, NULL, "failed to initialize allegro!", NULL, NULL); return -1; } //create our display object display = al_create_display(app_width, app_height); //test display object if(!display) { al_show_native_message_box(NULL, NULL, NULL, "failed to initialize display!", NULL, NULL); return -1; } al_init_font_addon(); al_init_ttf_addon(); ALLEGRO_FONT *font12 = al_load_font("arial.ttf", 12, 0); ALLEGRO_FONT *font24 = al_load_font("arial.ttf", 24, 0); ALLEGRO_FONT *font36 = al_load_font("arial.ttf", 36, 0); ALLEGRO_FONT *font18 = al_load_font("arial.ttf", 18, 0); al_init_primitives_addon(); al_install_keyboard(); al_install_mouse(); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); 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_hide_mouse_cursor(display); al_start_timer(timer); Tilemap tilemap; /*int mountain_index = rand() % (tilemap_size_num_tiles_x * tilemap_size_num_tiles_y); tilemap.tile_[300].height_ = 10; tilemap.tile_[300].erosion_givingness = 0; int deep_index = rand() % (tilemap_size_num_tiles_x * tilemap_size_num_tiles_y); tilemap.tile_[301].height_ = -10; tilemap.tile_[301].erosion_givingness = 0;*/ std::vector<Sprite> tree; Sprite p1(tile_render_size*1.5,tile_render_size*1.5,color_p1); Sprite p2(tile_render_size*2.5,tile_render_size*1.5,color_p2); Timer tilemap_time; int num_natural_erosions = 0; int sec_per_natural_erosion = 1; int num_tree_periods = 0; int sec_per_tree_period = 0.3; while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { if(ev.mouse.button & 1) { p1.x = ev.mouse.x; p1.y = ev.mouse.y; } else if (ev.mouse.button & 2) { p2.x = ev.mouse.x; p2.y = ev.mouse.y; } } else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES) { } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_W: keys[W] = true; break; case ALLEGRO_KEY_A: keys[A] = true; break; case ALLEGRO_KEY_S: keys[S] = true; break; case ALLEGRO_KEY_D: keys[D] = true; break; case ALLEGRO_KEY_E: keys[E] = true; std::cout << "E:\n"; //std::cout << "\n" << tilemap.tile[1][1].height() << "\n"; ErodeTilemap(tilemap); //std::cout << "\n" << tilemap.tile[1][1].height() << "\n"; break; case ALLEGRO_KEY_N: keys[N] = true; Tilemap new_tilemap; //for ( int i = 0; i < 10; i++ ) new_tilemap = ErodeTilemap(new_tilemap); tilemap = new_tilemap; tree.clear(); break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_W: keys[W] = false; break; case ALLEGRO_KEY_A: keys[A] = false; break; case ALLEGRO_KEY_S: keys[S] = false; break; case ALLEGRO_KEY_D: keys[D] = false; break; case ALLEGRO_KEY_E: keys[E] = false; break; case ALLEGRO_KEY_N: keys[N] = false; break; case ALLEGRO_KEY_ESCAPE: done = true; break; } } else if(ev.type == ALLEGRO_EVENT_TIMER) { if ( keys[UP] ) p1.y -= 1; if ( keys[DOWN] ) p1.y += 1; if ( keys[RIGHT] ) p1.x += 1; if ( keys[LEFT] ) p1.x -= 1; if ( keys[W] ) p2.y -= 1; if ( keys[S] ) p2.y += 1; if ( keys[D] ) p2.x += 1; if ( keys[A] ) p2.x -= 1; /*ALLEGRO_MOUSE_STATE MouseState; al_get_mouse_state(&MouseState); if ( MouseState.buttons & 1 ) { p1.x = ev.mouse.x; }*/ if ( tilemap_time.now() > num_natural_erosions * sec_per_natural_erosion ) { ErodeTilemap(tilemap); num_natural_erosions++; //kill underwater and beach trees: for( int i = 0; i < tree.size(); i++ ) { if ( 0.2 > tilemap.tile_[(int)tree[i].x%tile_render_size,(int)tree[i].y/tile_render_size].height() ) { tree.erase(tree.begin()+i); } } } if ( tilemap_time.now() > num_tree_periods * sec_per_tree_period ) { for ( int i = 0; i < tilemap_size_num_tiles_x * tilemap_size_num_tiles_y; i++) { if ( tilemap.tile_[i].height_ > 1 ) { if ( 0.05 > (float)(rand() % 10000) / 100 ) { int x = i_t_x(i)*tile_render_size + rand()%tile_render_size; int y = i_t_y(i)*tile_render_size + rand()%tile_render_size; int g = 102+rand()%102; tree.push_back(Sprite(x,y,Color(0,g,0))); } } } } if ( collision(p1,p2) ) std::cout << "collision!\n"; redraw = true; } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; // Draw tilemap tiles: // i and j changes are hack that will cause problems later for ( int i = 1; i < view_tile_count_x+1; i++ ) { for ( int j = 1; j < view_tile_count_y+1; j++ ) { al_draw_filled_rectangle(i*tile_render_size,j*tile_render_size,i*tile_render_size+tile_render_size,j*tile_render_size+tile_render_size, al_map_rgb(tilemap.tile_[xy_t_i(i,j)].color_.r,tilemap.tile_[xy_t_i(i,j)].color_.g,tilemap.tile_[xy_t_i(i,j)].color_.b)); //al_draw_textf(font12, al_map_rgb(255,255,255), i*tile_render_size + tile_render_size/2,j*tile_render_size, ALLEGRO_ALIGN_CENTRE, "%.1fm", tilemap.tile_[xy_t_i(i,j)].height() ); //al_draw_textf(font12, al_map_rgb(255,255,255), i*tile_render_size + tile_render_size/2,(j+.5)*tile_render_size, ALLEGRO_ALIGN_CENTRE, "%d", i + j * tilemap_size_num_tiles_x ); } } // Draw player 1 //al_draw_filled_rectangle(p1.x-p1.radius, p1.y-p1.radius, p1.x+p1.radius, p1.y+p1.radius, al_map_rgb(204, 0, 0)); al_draw_filled_circle(p1.x, p1.y, p1.radius, al_map_rgb(p1.color.r, p1.color.g, p1.color.b)); al_draw_text(font24, al_map_rgb(0,0,0), p1.x, p1.y-p1.radius, ALLEGRO_ALIGN_CENTRE, "1"); // Draw player 2 //al_draw_filled_rectangle(p2.x-p2.radius, p2.y-p2.radius, p2.x+p2.radius, p2.y+p2.radius, al_map_rgb(0, 102, 204)); al_draw_filled_circle(p2.x, p2.y, p2.radius, al_map_rgb(p2.color.r, p2.color.g, p2.color.b)); al_draw_text(font24, al_map_rgb(0,0,0), p2.x, p2.y-p2.radius, ALLEGRO_ALIGN_CENTRE, "2"); // Draw trees for( int i = 0; i < tree.size(); i++ ) { al_draw_filled_circle(tree[i].x, tree[i].y, tree[i].radius, al_map_rgb(tree[i].color.r, tree[i].color.g, tree[i].color.b)); } // Draw info al_draw_textf(font12, al_map_rgb(255,255,255), tilemap_size_num_tiles_x*tile_render_size,0,0, "tilemap_time.now(): %.1f --- ErodeTilemap occurs every 10s!", tilemap_time.now() ); al_draw_textf(font12, al_map_rgb(255,255,255), tilemap_size_num_tiles_x*tile_render_size,12,0, "ErodeTilemap occurs every %ds!", sec_per_natural_erosion ); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } } al_destroy_font(font12); al_destroy_font(font18); al_destroy_font(font24); al_destroy_font(font36); al_destroy_event_queue(event_queue); al_destroy_timer(timer); al_destroy_display(display); //destroy our display object return 0; }
int inicializadores(int *LARGURA_TELA, int *ALTURA_TELA, float *FPS, ALLEGRO_DISPLAY *janela , ALLEGRO_EVENT_QUEUE *fila_eventos, ALLEGRO_BITMAP *poderes, ALLEGRO_FONT *fonte_equacao, ALLEGRO_FONT *fonte_pontos, ALLEGRO_TIMER *timer){ if (!al_init()){ fprintf(stderr, "Falha ao inicializar a Allegro.\n"); return -1; } al_init_image_addon(); // Inicialização do add-on para uso de fontes al_init_font_addon(); // Inicialização do add-on para uso de fontes True Type if (!al_init_ttf_addon()){ fprintf(stderr, "Falha ao inicializar add-on allegro_ttf.\n"); return -1; } janela = al_create_display(LARGURA_TELA, ALTURA_TELA); if (!janela){ fprintf(stderr, "Falha ao criar janela.\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer){ fprintf(stderr, "failed to create timer!\n"); return -1; } // Configura o título da janela al_set_window_title(janela, "PENAS - Telas de Poderes -"); fonte_equacao = al_load_font("fontes/letra_equacao.ttf", 36, 0); if (!fonte_equacao){ al_destroy_display(janela); fprintf(stderr, "Falha ao carregar fonte.\n"); return -1; } fonte_pontos = al_load_font("fontes/letra_equacao.ttf", 26, 0); if (!fonte_pontos){ al_destroy_display(janela); fprintf(stderr, "Falha ao carregar fonte.\n"); return -1; } // Torna apto o uso de mouse na aplicação if (!al_install_mouse()){ fprintf(stderr, "Falha ao inicializar o mouse.\n"); al_destroy_display(janela); return -1; } // Atribui o cursor padrão do sistema para ser usado if (!al_set_system_mouse_cursor(janela, ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT)){ fprintf(stderr, "Falha ao atribuir ponteiro do mouse.\n"); al_destroy_display(janela); return -1; } fila_eventos = al_create_event_queue(); if (!fila_eventos){ fprintf(stderr, "Falha ao inicializar o fila de eventos.\n"); al_destroy_display(janela); return -1; } if (!al_init_primitives_addon()){ fprintf(stderr, "Falha ao inicializar add-on de primitivas.\n"); return false; } // Dizemos que vamos tratar os eventos vindos do mouse al_register_event_source(fila_eventos, al_get_mouse_event_source()); al_register_event_source(fila_eventos, al_get_display_event_source(janela)); al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); }
int main() { bool done = false; bool redraw = true; bool is_game_over = false; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *events = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font20 = NULL; ALLEGRO_FONT *font40 = NULL; if (!al_init()) return 1; display = al_create_display(DISPLAY_WIDTH, DISPLAY_HEIGHT); if (!display) return 1; // Initialize add-ons al_init_primitives_addon(); al_install_keyboard(); al_init_font_addon(); al_init_ttf_addon(); timer = al_create_timer(1.0 / FPS); events = al_create_event_queue(); al_register_event_source(events, al_get_keyboard_event_source()); al_register_event_source(events, al_get_timer_event_source(timer)); font20 = al_load_font("arial.ttf", 20, 0); font40 = al_load_font("arial.ttf", 40, 0); al_start_timer(timer); while (!done) { // Events handling ALLEGRO_EVENT ev; al_wait_for_event(events, &ev); if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_J: keys[J] = false; break; case ALLEGRO_KEY_K: keys[K] = false; break; case ALLEGRO_KEY_S: keys[S] = false; break; case ALLEGRO_KEY_D: keys[D] = false; break; case ALLEGRO_KEY_ESCAPE: done = true; break; } } else if (ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; if (!is_game_over) { updateBall(*ball); updateLeftBar(*bar1); updateRightBar(*bar2); handleCollision(*bar1, *bar2, *ball); if (score1 == WINNING_SCORE || score2 == WINNING_SCORE) is_game_over = true; } } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_J: keys[J] = true; break; case ALLEGRO_KEY_K: keys[K] = true; break; case ALLEGRO_KEY_S: keys[S] = true; break; case ALLEGRO_KEY_D: keys[D] = true; break; } } if (redraw && al_is_event_queue_empty(events)) { redraw = false; if (!is_game_over) { // Rendering al_clear_to_color(al_map_rgb(0, 0, 0)); ball->render(); bar1->render(); bar2->render(); al_draw_textf(font20, al_map_rgb(255, 255, 255), DISPLAY_WIDTH/2, DISPLAY_HEIGHT - 40, ALLEGRO_ALIGN_CENTRE, "%d : %d", score1, score2); al_flip_display(); } else { al_clear_to_color(al_map_rgb(0, 0, 0)); if (score1 == WINNING_SCORE) al_draw_text(font40, al_map_rgb(255, 255, 255), DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2 - 20, ALLEGRO_ALIGN_CENTRE, "PLAYER 1 WON"); else if (score2 == WINNING_SCORE) al_draw_text(font40, al_map_rgb(255, 255, 255), DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2 - 20, ALLEGRO_ALIGN_CENTRE, "PLAYER 2 WON"); al_draw_text(font20, al_map_rgb(255, 255, 255), DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2 + 40, ALLEGRO_ALIGN_CENTRE, "Press 'r' to start a new game"); al_flip_display(); } } } // House cleaning delete ball; delete bar1; delete bar2; al_destroy_font(font20); al_destroy_font(font40); al_destroy_display(display); al_destroy_event_queue(events); return 0; }
int main(void) { char* error = ""; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_FONT *font = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; HANDLE mutex = CreateMutex(NULL, FALSE, "Chapter3_CloseMutex-Mutex"); if (GetLastError() == ERROR_ALREADY_EXISTS) { MessageBoxA(GetForegroundWindow(), "Only one instance of the game can run at a time.", "Chapter3_CloseMutex", MB_OK); return 0; } do { loadHighscore(); if(!al_init()) { error = "failed to initialize allegro!"; break; } if(!al_init_primitives_addon()) { error = "failed to initialize primitives!"; break; } al_init_font_addon(); if(!al_init_ttf_addon()) { error = "failed to initialize ttfs!"; break; } if(!al_install_keyboard()) { error = "failed to initialize keyboard!"; break; } if(!(font = al_load_ttf_font("arial.ttf", 18, 0))) { error = "failed to initialize font!"; break; } if(!(timer = al_create_timer(1.0 / 20))) { error = "failed to initialize timer!"; break; } if(!(display = al_create_display(640, 480))) { error = "failed to initialize display!"; break; } if(!(event_queue = al_create_event_queue())) { error = "failed to initialize event_queue!"; break; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); while(1) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) break; else if (ev.keyboard.keycode <= 255) keys[ev.keyboard.keycode] = true; } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { if (ev.keyboard.keycode <= 255) keys[ev.keyboard.keycode] = false; } else if (ev.type == ALLEGRO_EVENT_KEY_CHAR) keyboardEvent(); if (al_is_event_queue_empty(event_queue) && ev.type == ALLEGRO_EVENT_TIMER) drawFrame(font); } } while (0); if (mutex) CloseHandle(mutex); if (strlen(error) > 0) { al_show_native_message_box(NULL, NULL, "error", error, NULL, NULL); return -1; } al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); saveHighscore(); return 0; }
/* the main program body */ int main(int argc, char *argv[]) { ALLEGRO_PATH *font_path; int w = 0, h = 0; int www = FALSE; int i, n; int display_flags = ALLEGRO_GENERATE_EXPOSE_EVENTS; srand(time(NULL)); al_set_org_name("liballeg.org"); al_set_app_name("SPEED"); if (!al_init()) { fprintf(stderr, "Could not initialise Allegro.\n"); return 1; } al_init_primitives_addon(); /* parse the commandline */ for (i=1; i<argc; i++) { if (strcmp(argv[i], "-cheat") == 0) { cheat = TRUE; } else if (strcmp(argv[i], "-simple") == 0) { low_detail = TRUE; } else if (strcmp(argv[i], "-nogrid") == 0) { no_grid = TRUE; } else if (strcmp(argv[i], "-nomusic") == 0) { no_music = TRUE; } else if (strcmp(argv[i], "-www") == 0) { www = TRUE; } else if (strcmp(argv[i], "-fullscreen") == 0) { /* if no width is specified, assume fullscreen_window */ display_flags |= w ? ALLEGRO_FULLSCREEN : ALLEGRO_FULLSCREEN_WINDOW; } else { n = atoi(argv[i]); if (!n) { usage(); return 1; } if (!w) { w = n; if (display_flags & ALLEGRO_FULLSCREEN_WINDOW) { /* toggle from fullscreen_window to fullscreen */ display_flags &= ~ALLEGRO_FULLSCREEN_WINDOW; display_flags |= ALLEGRO_FULLSCREEN; } } else if (!h) { h = n; } else { usage(); return 1; } } } /* it's a real shame that I had to take this out! */ if (www) { printf( "\n" "Unfortunately the built-in web browser feature had to be removed.\n" "\n" "I did get it more or less working as of Saturday evening (forms and\n" "Java were unsupported, but tables and images were mostly rendering ok),\n" "but the US Department of Justice felt that this was an unacceptable\n" "monopolistic attempt to tie in web browsing functionality to an\n" "unrelated product, so they threatened me with being sniped at from\n" "the top of tall buildings by guys with high powered rifles unless I\n" "agreed to disable this code.\n" "\n" "We apologise for any inconvenience that this may cause you.\n" ); return 1; } if (!w || !h) { if (argc == 1 || (display_flags & ALLEGRO_FULLSCREEN_WINDOW)) { w = 640; h = 480; } else { usage(); return 1; } } /* set the screen mode */ 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_flags(display_flags); screen = al_create_display(w, h); if (!screen) { fprintf(stderr, "Error setting %dx%d display mode\n", w, h); return 1; } al_init_image_addon(); /* The Allegro 5 port introduced an external data dependency, sorry. * To avoid performance problems on graphics drivers that don't support * drawing to textures, we build up transition screens on memory bitmaps. * We need a font loaded into a memory bitmap for those, then a font * loaded into a video bitmap for the game view. Blech! */ font_path = get_resources_path(); al_set_path_filename(font_path, "a4_font.tga"); al_init_font_addon(); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); font = al_load_bitmap_font(al_path_cstr(font_path, '/')); if (!font) { fprintf(stderr, "Error loading %s\n", al_path_cstr(font_path, '/')); return 1; } al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); font_video = al_load_bitmap_font(al_path_cstr(font_path, '/')); if (!font_video) { fprintf(stderr, "Error loading %s\n", al_path_cstr(font_path, '/')); return 1; } al_destroy_path(font_path); /* set up everything else */ al_install_keyboard(); al_install_joystick(); if (al_install_audio()) { if (!al_reserve_samples(8)) al_uninstall_audio(); } init_input(); init_sound(); init_hiscore(); /* the main program body */ while (title_screen()) { if (play_game()) { show_results(); score_table(); } } /* time to go away now */ shutdown_hiscore(); shutdown_sound(); goodbye(); shutdown_input(); al_destroy_font(font); al_destroy_font(font_video); return 0; }
int main(int argc, char *argv[]) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *evqueue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_KEYBOARD_STATE keyboard_state; ALLEGRO_EVENT event; ALLEGRO_FONT *font; ALLEGRO_BITMAP *clock_hand, *clock_quadrant, *bow, *sword; float clock_ray = 0, clock_angle = 0; int clock_ray_alpha; float soul_interval = SOUL_TIME_INTERVAL; FuzzyPlayer *player, *cpu; FuzzyGame * game; bool running = true; bool redraw = true; int map_x = 13*16, map_y = 5*16; int screen_width = WINDOW_WIDTH; int screen_height = WINDOW_HEIGHT; double curtime; /* Initialization */ fuzzy_iz_error(al_init(), "Failed to initialize allegro"); fuzzy_load_addon("image", al_init_image_addon()); fuzzy_load_addon("primitives", al_init_primitives_addon()); fuzzy_load_addon("keyboard", al_install_keyboard()); fuzzy_load_addon("mouse", al_install_mouse()); al_init_font_addon(); fuzzy_iz_error(timer = al_create_timer(1.0 / FPS), "Cannot create FPS timer"); fuzzy_iz_error(evqueue = al_create_event_queue(), "Cannot create event queue"); fuzzy_iz_error(display = al_create_display(screen_width, screen_height), "Cannot initialize display"); al_set_window_title(display, WINDOW_TITLE); fuzzy_iz_error(font = al_load_font(fuzzy_res(FONT_FOLDER, "fixed_font.tga"), 0, 0), "Cannot load 'fixed_font.tga'"); clock_hand = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "clock_hand.png")); fuzzy_iz_error(clock_hand, "Cannot load clock handle"); clock_quadrant = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "clock_quadrant.png")); fuzzy_iz_error(clock_hand, "Cannot load clock quadrant"); bow = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "bow.png")); fuzzy_iz_error(clock_hand, "Cannot load bow image"); sword = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "sword.png")); fuzzy_iz_error(clock_hand, "Cannot load sword image"); /* Queue setup */ al_register_event_source(evqueue, al_get_display_event_source(display)); al_register_event_source(evqueue, al_get_timer_event_source(timer)); al_register_event_source(evqueue, al_get_keyboard_event_source()); al_register_event_source(evqueue, al_get_mouse_event_source()); /* Game setup */ game = fuzzy_game_new("level000.tmx"); player = fuzzy_player_new(game, FUZZY_PLAYER_LOCAL, "Dolly"); cpu = fuzzy_player_new(game, FUZZY_PLAYER_CPU, "CPU_0"); fuzzy_chess_add(game, player, FUZZY_FOO_LINK, 34, 30); fuzzy_chess_add(game, player, FUZZY_FOO_LINK, 33, 30); fuzzy_chess_add(game, cpu, FUZZY_FOO_LINK, 40, 30); bool showing_area = false; FuzzyChess *chess, *focus = NULL; al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_bitmap(game->map->bitmap, -map_x, -map_y, 0); al_flip_display(); #if DEBUG ALLEGRO_BITMAP *icon; int fps, fps_accum; double fps_time; icon = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "icon.tga")); if (icon) al_set_display_icon(display, icon); fps_accum = fps_time = 0; fps = FPS; #endif /* Server connection */ int svsock; //~ FuzzyMessage * sendmsg = fuzzy_message_new(); svsock = fuzzy_server_connect(FUZZY_DEFAULT_SERVER_ADDRESS, FUZZY_DEFAULT_SERVER_PORT); _aaa_menu(game, svsock); /* MAIN loop */ player->soul_time = al_get_time(); al_start_timer(timer); while (running) { /* wait until an event happens */ al_wait_for_event(evqueue, &event); switch (event.type) { case ALLEGRO_EVENT_TIMER: /* check soul ticks */ curtime = al_get_time(); while (curtime - player->soul_time >= soul_interval) { //~ fuzzy_debug("Soul tick!"); player->soul_time += soul_interval; player->soul_points += SOUL_POINTS_BOOST; clock_ray = 1; } clock_angle = (curtime - player->soul_time)/soul_interval * FUZZY_2PI; if (clock_ray) { clock_ray = (curtime - player->soul_time)/RAY_TIME_INTERVAL * 50 + 40; clock_ray_alpha = (curtime - player->soul_time)/RAY_TIME_INTERVAL*(55) + 200; if (clock_ray >= 90) clock_ray = 0; } al_get_keyboard_state(&keyboard_state); if (al_key_down(&keyboard_state, ALLEGRO_KEY_RIGHT)) { map_x += 5; if (map_x > (game->map->tot_width - screen_width)) map_x = game->map->tot_width - screen_width; } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_LEFT)) { map_x -= 5; if (map_x < 0) map_x = 0; } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_UP)) { map_y -= 5; if (map_y < 0) map_y = 0; } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_DOWN)) { map_y += 5; if (map_y > (game->map->tot_height - screen_height)) map_y = game->map->tot_height - screen_height; } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_O)) { soul_interval = fuzzy_max(0.1, soul_interval - 0.05); } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_P)) { soul_interval += 0.05; } redraw = true; break; case ALLEGRO_EVENT_KEY_DOWN: if(! focus) break; _attack_area_off(); switch(event.keyboard.keycode) { case ALLEGRO_KEY_W: _chess_move(game, player, focus, focus->x, focus->y-1); break; case ALLEGRO_KEY_A: _chess_move(game, player, focus, focus->x-1, focus->y); break; case ALLEGRO_KEY_S: _chess_move(game, player, focus, focus->x, focus->y+1); break; case ALLEGRO_KEY_D: _chess_move(game, player, focus, focus->x+1, focus->y); break; case ALLEGRO_KEY_K: _attack_area_on(); break; case ALLEGRO_KEY_SPACE: /* switch attack type */ if (! focus) break; if (focus->atkarea == &FuzzyMeleeMan) focus->atkarea = &FuzzyRangedMan; else focus->atkarea = &FuzzyMeleeMan; break; } break; case ALLEGRO_EVENT_DISPLAY_CLOSE: running = false; break; case ALLEGRO_EVENT_KEY_UP: break; case ALLEGRO_EVENT_KEY_CHAR: break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: if(event.mouse.button == RIGHT_BUTTON) { _attack_area_on(); } else if(event.mouse.button == LEFT_BUTTON) { /* world to tile coords */ int tx = (event.mouse.x+map_x) / game->map->tile_width; int ty = (event.mouse.y+map_y) / game->map->tile_height; #ifdef DEBUG printf("SELECT %d %d\n", tx, ty); #endif if(showing_area && fuzzy_chess_inside_target_area(game, focus, tx, ty)) { /* select attack target */ if (fuzzy_map_spy(game->map, FUZZY_LAYER_SPRITES, tx, ty) == FUZZY_CELL_SPRITE) { if (fuzzy_chess_local_attack(game, player, focus, tx, ty)) _attack_area_off(); } } else { /* select chess */ chess = fuzzy_chess_at(game, player, tx, ty); if (chess && focus != chess) { _attack_area_off(); if (focus != NULL) { // already has a focus effect, just move it fuzzy_sprite_move(game->map, FUZZY_LAYER_BELOW, focus->x, focus->y, tx, ty); } else { fuzzy_sprite_create(game->map, FUZZY_LAYER_BELOW, GID_TARGET, tx, ty); } focus = chess; } else if (! chess) { if (showing_area) { // just hide the attack area _attack_area_off(); } else if(focus) { // remove the focus fuzzy_sprite_destroy(game->map, FUZZY_LAYER_BELOW, focus->x, focus->y); focus = NULL; } } } } break; default: #ifdef DEBUG //~ fprintf(stderr, "Unknown event received: %d\n", event.type); #endif break; } if (redraw && al_is_event_queue_empty(evqueue)) { curtime = al_get_time(); fuzzy_map_update(game->map, curtime); // Clear the screen al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_bitmap(game->map->bitmap, -map_x, -map_y, 0); #ifdef GRID_ON /* Draw the grid */ int tw = game->map->tile_width; int ty = game->map->tile_height; int x, y; for (x=(tw-map_x)%tw; x<screen_width; x+=tw) al_draw_line(x, 0, x, screen_height, al_map_rgba(7,7,7,100), 1); for (y=(ty-map_y)%ty; y<screen_height; y+=ty) al_draw_line(0, y, screen_width, y, al_map_rgba(7,7,7,100), 1); #endif #if DEBUG al_draw_filled_rounded_rectangle(screen_width-100, 4, screen_width, 30, 8, 8, al_map_rgba(0, 0, 0, 200)); al_draw_textf(font, al_map_rgb(255, 255, 255), screen_width-50, 8, ALLEGRO_ALIGN_CENTRE, "FPS: %d", fps); #endif /* draw SP count */ al_draw_filled_rounded_rectangle(4, screen_height-170, 175, screen_height-4, 8, 8, al_map_rgba(0, 0, 0, 200)); al_draw_textf(font, al_map_rgb(255, 255, 255), 15, screen_height-163, ALLEGRO_ALIGN_LEFT, "SP: %d", player->soul_points); /* draw Soul Clock */ al_draw_scaled_bitmap(clock_quadrant, 0, 0, 301, 301, 20, screen_height-80-139/2, 139, 139, 0); al_draw_scaled_rotated_bitmap(clock_hand, 160, 607, 90, screen_height-80, 0.11, 0.11, clock_angle, 0); al_draw_circle(90, screen_height-80, clock_ray, al_map_rgb(80, clock_ray_alpha, 80), 2.0); /* draw weapon */ if (focus) { ALLEGRO_BITMAP * weapon; if (focus->atkarea == &FuzzyMeleeMan) weapon = sword; else weapon = bow; al_draw_scaled_bitmap(weapon, 0, 0, 90, 90, 20, 20, 60, 60, 0); } al_flip_display(); #if DEBUG fps_accum++; if (curtime - fps_time >= 1) { fps = fps_accum; fps_accum = 0; fps_time = curtime; } #endif redraw = false; } } /* Cleanup */ //~ void * retval; //~ char srvkey[FUZZY_SERVERKEY_LEN]; //~ fuzzy_protocol_server_shutdown(svsock, sendmsg, srvkey); //~ fuzzy_message_del(sendmsg); fuzzy_game_free(game); al_destroy_event_queue(evqueue); al_destroy_display(display); al_destroy_timer(timer); return 0; }
int main (int argc, char *argv[]) { al_init(); ALLEGRO_DISPLAY *display = al_create_display(640, 480); al_init_primitives_addon(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); srand(time(NULL)); char* disappointed[] = {" Razocharovana sum!", " Tolkova losho kontrolno ne sum vijdala!", "Golqm murzel vi e nalegnal...", " Potresavashto!!"}; ALLEGRO_BITMAP *image = al_load_bitmap("pic.bmp"); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); ALLEGRO_TIMER *timer = al_create_timer(1/60.0); al_register_event_source(event_queue, al_get_timer_event_source(timer)); ALLEGRO_FONT *font = al_load_font("Arial.ttf", 20, 0); int done = 0; int render = 0; int x = 375; int y = 340; int traveled_y = 0; int delay = 120; int time_elapsed = 0; int dir = -1; int move_left = 0; int traveled_x = 0; int time_elapsed2 = 0; int draw_text = 0; int random = 0; int should_draw_text = 0; al_start_timer(timer); while(!done) { ALLEGRO_EVENT event; al_wait_for_event(event_queue, &event); if(event.type == ALLEGRO_EVENT_TIMER) { if(x < 50) done = 1; if(traveled_y >= 75) { if(!draw_text) { random = rand() % 4; if(y < 350) should_draw_text = 1; } draw_text = 1; if(++time_elapsed >= delay) { traveled_y = 0; time_elapsed = 0; if(y <= 60 || y >= 375) { if(!move_left) { if(y <= 60) { if(traveled_x >= 50) { if(++time_elapsed2 >= delay) { traveled_x = 0; time_elapsed2 = 0; move_left = 1; } } else { x -= 3; traveled_x += 3; } } else if(y >= 375) { should_draw_text = 0; if(traveled_x >= 140) { if(++time_elapsed2 >= delay) { traveled_x = 0; time_elapsed2 = 0; move_left = 1; } } else { x -= 3; traveled_x += 3; } } time_elapsed = 120; traveled_y = 75; } else { dir *= -1; move_left = 0; } } } } else { draw_text = 0; should_draw_text = 0; y += 3 * dir; traveled_y += 3; } render = 1; } if(render) { al_draw_bitmap(image, 0, 0, 0); al_draw_pixel(x, y, al_map_rgb(255, 0, 0)); if(should_draw_text) { al_draw_text(font, al_map_rgb(255, 255, 255), 10, 450, 0, disappointed[random]); } al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); render = 0; } } al_destroy_display(display); al_destroy_bitmap(image); al_destroy_event_queue(event_queue); al_destroy_timer(timer); return 0; }
//MAIN// int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_BITMAP *image = NULL, *image1 = NULL, *image2 = NULL, *image3 = NULL; ALLEGRO_FONT *font1 = NULL, *font2 = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; if(!al_init()) { return -1; } if(!al_install_keyboard()) { return -1; } if(!al_install_mouse()) { return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { return -1; } event_queue = al_create_event_queue(); if(!event_queue) { return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); al_init_primitives_addon(); al_init_font_addon(); al_init_ttf_addon(); if(!al_init_image_addon()) { printf("Problema com inicialização image_addon"); return -1; } font1 = al_load_ttf_font("pirulen.ttf",12,0 ); if (!font1) { printf("Problema ao carregar pirulen.ttf"); return -1; } image = al_load_bitmap("iceblue2.png"); if(!image) { printf("Problema ao carregar iceblue2.png"); return -1; } image1 = al_load_bitmap("IceBlue1.png"); if(!image1) { printf("Problema ao carregar IceBlue1.png"); return -1; } image2 = al_load_bitmap("NG.png"); if(!image2) { printf("Problema ao carregar NG.png"); return -1; } image3 = al_load_bitmap("retanrosa.png"); if(!image3) { printf("Problema ao carregar retanrosa.png"); return -1; } al_draw_bitmap(image,0,0,0); al_draw_bitmap(image1,10,10,10); al_draw_bitmap(image2,180,45,15); al_draw_bitmap(image3,310,45,15); //FUNÇÃO GERA RETANGULO// x1_ini=30.0, y1_ini=170.0, x2_ini=100.0, y2_ini=240; x1 = x1_ini,y1= y1_ini,x2 = x2_ini,y2 = y2_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 211, 193)); y1=y1+80; y2=y2+80; } y1=y1_ini; y2=y2_ini; x1=x1+80; x2=x2+80; } font2 = al_load_ttf_font("pirulen.ttf",23,0 ); if (!font2) { return -1; } al_flip_display(); //LOGICA// while(!doexit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: for (coluna=0; coluna<5; coluna++) { for (i=3; i>=0; i--) { for (linha=4; linha>=1; linha--) { if ((matriz[linha][coluna]!=0) && (matriz[linha-1][coluna]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha-1][coluna]; matriz[linha-1][coluna]=nt; } } } } for (coluna=0; coluna<5; coluna++) { for (linha=0; linha<4; linha++) { if ((matriz[linha][coluna]==matriz[linha+1][coluna])&&(matriz[linha][coluna]!=0)) { matriz[linha+1][coluna]+=matriz[linha][coluna]; matriz[linha][coluna]=0; linha++; } } } for (coluna=0; coluna<5; coluna++) { for (i=3; i>=0; i--) { for (linha=4; linha>=1; linha--) { if ((matriz[linha][coluna]!=0) && (matriz[linha-1][coluna]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha-1][coluna]; matriz[linha-1][coluna]=nt; } } } } break; case ALLEGRO_KEY_DOWN: for (coluna=0; coluna<5; coluna++) { for (i=3; i>=0; i--) { for (linha=0; linha<4; linha++) { if ((matriz[linha][coluna]!=0) && (matriz[linha+1][coluna]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha+1][coluna]; matriz[linha+1][coluna]=nt; } } } } for (coluna=0; coluna<5; coluna++) { for (linha=4; linha>=1; linha--) { if ((matriz[linha][coluna]==matriz[linha-1][coluna])&&(matriz[linha][coluna]!=0)) { matriz[linha][coluna]+=matriz[linha-1][coluna]; matriz[linha-1][coluna]=0; linha--; } } } for (coluna=0; coluna<5; coluna++) { for (i=3; i>=0; i--) { for (linha=0; linha<4; linha++) { if ((matriz[linha][coluna]!=0) && (matriz[linha+1][coluna]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha+1][coluna]; matriz[linha+1][coluna]=nt; } } } } break; case ALLEGRO_KEY_LEFT: for (linha=0; linha<5; linha++) { for (i=3; i>=0; i--) { for (coluna=4; coluna>=1; coluna--) { if ((matriz[linha][coluna]!=0) && (matriz[linha][coluna-1]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha][coluna-1]; matriz[linha][coluna-1]=nt; } } } } for (linha=0; linha<5; linha++) { for (coluna=0; coluna<4; coluna++) { if ((matriz[linha][coluna]==matriz[linha][coluna+1])&&(matriz[linha][coluna]!=0)) { matriz[linha][coluna+1]+=matriz[linha][coluna]; matriz[linha][coluna]=0; coluna++; } } } for (linha=0; linha<5; linha++) { for (i=3; i>=0; i--) { for (coluna=4; coluna>=1; coluna--) { if ((matriz[linha][coluna]!=0) && (matriz[linha][coluna-1]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha][coluna-1]; matriz[linha][coluna-1]=nt; } } } } break; case ALLEGRO_KEY_RIGHT: for (linha=0; linha<5; linha++) { for (i=3; i>=0; i--) { for (coluna=0; coluna<4; coluna++) { if ((matriz[linha][coluna]!=0) && (matriz[linha][coluna+1]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha][coluna+1]; matriz[linha][coluna+1]=nt; } } } } for (linha=0; linha<5; linha++) { for (coluna=4; coluna>=1; coluna--) { if ((matriz[linha][coluna]==matriz[linha][coluna-1])&&(matriz[linha][coluna]!=0)) { matriz[linha][coluna-1]+=matriz[linha][coluna]; matriz[linha][coluna]=0; coluna--; } } } for (linha=0; linha<5; linha++) { for (i=3; i>=0; i--) { for (coluna=0; coluna<4; coluna++) { if ((matriz[linha][coluna]!=0) && (matriz[linha][coluna+1]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha][coluna+1]; matriz[linha][coluna+1]=nt; } } } } break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; } //GERADOR RANDOM// random(&rcoluna,&rlinha); while (matriz[rlinha][rcoluna]!=0) { random(&rcoluna,&rlinha); } matriz[rlinha][rcoluna]= (rand()%4 == 3)? 4 : 2; for (linha=0; linha<=4; linha++) { for (coluna=0; coluna<=4; coluna++) { if (matriz[linha][coluna]!=0) i++; } } if (i==25) break; //SCORE// al_draw_bitmap(image3,310,45,15); score=0; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { score+=matriz[ql][qc]; } } al_draw_textf(font1, al_map_rgb(255,255,255),365,68,ALLEGRO_ALIGN_CENTRE,"%i",score); } //FUNÇÃO GERA RETANGULO// x1_ini=30.0, y1_ini=170.0, x2_ini=100.0, y2_ini=240; x1 = x1_ini,y1= y1_ini,x2 = x2_ini,y2 = y2_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { if(matriz[ql][qc]==0) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 211, 193)); } else if(matriz[ql][qc]==2) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 182, 151)); } else if(matriz[ql][qc]==4) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 141, 132)); } else if(matriz[ql][qc]==8) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 211, 100)); } else if(matriz[ql][qc]==16) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 128, 1)); } else if(matriz[ql][qc]==32) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 0, 128)); } else if(matriz[ql][qc]==64) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 0, 30)); } else if(matriz[ql][qc]==128) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(108, 128, 255)); } else if(matriz[ql][qc]==256) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(108, 188, 255)); } else if(matriz[ql][qc]==512) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(15, 203, 80)); } else if(matriz[ql][qc]==1024) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(140, 255, 198)); } else if(matriz[ql][qc]==2048) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 255, 26)); } else if(matriz[ql][qc]==4069) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 0, 0)); } else { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 225, 255)); } y1=y1+80; y2=y2+80; } y1=y1_ini; y2=y2_ini; x1=x1+80; x2=x2+80; } //FUNÇÃO GERA MATRIZ// text2x_ini=64, text2y_ini=192; text2x=text2x_ini, text2y=text2y_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { if (matriz[ql][qc]==0) { c1=255, c2=211, c3=193; } else { c1=255, c2=255, c3=255; } al_draw_textf(font2, al_map_rgb(c1,c2,c3),text2x,text2y,ALLEGRO_ALIGN_CENTRE, "%i",matriz[ql][qc]); text2y=text2y+80; } text2y=text2y_ini; text2x=text2x+80; al_flip_display(); } //MOUSE EVENT// if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { if(ev.mouse.x >= al_get_bitmap_width(image2) && ev.mouse.x <= SCREEN_W -10 && ev.mouse.y <= SCREEN_H - 10 && ev.mouse.y >= al_get_bitmap_height(image2) - 10) { al_draw_bitmap(image3,310,45,15); score=0; text2x_ini=64, text2y_ini=192; text2x=text2x_ini, text2y=text2y_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { c1=255, c2=211, c3=193; matriz[ql][qc]=0; al_draw_textf(font2, al_map_rgb(c1,c2,c3),text2x,text2y,ALLEGRO_ALIGN_CENTRE, "%i",matriz[ql][qc]); text2y=text2y+80; } text2y=text2y_ini; text2x=text2x+80; } x1_ini=30.0, y1_ini=170.0, x2_ini=100.0, y2_ini=240; x1 = x1_ini,y1= y1_ini,x2 = x2_ini,y2 = y2_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 211, 193)); y1=y1+80; y2=y2+80; } y1=y1_ini; y2=y2_ini; x1=x1+80; x2=x2+80; } al_flip_display(); } } } system("cls"); printf("GAME OVER!"); al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }
int init::init_all() { //this->display; srand (time(NULL)); if(!al_init()) { fprintf(stderr, "failed to initialize randomness!\n"); return -1; } if(!al_init_primitives_addon()) { fprintf(stderr, "failed to initialize primitives addon!\n"); return -1; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize keyboard!\n"); return -1; } if(!al_install_mouse()) { fprintf(stderr, "failed to initialize mouse!\n"); return -1; } if(FULLSCREEN) { ALLEGRO_DISPLAY_MODE disp_data; al_get_display_mode(0, &disp_data); al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW | ALLEGRO_OPENGL); fprintf(stderr,"%i %i",disp_data.width,disp_data.height); this->screenWidth = disp_data.width; this->screenHeight = disp_data.height; this->display = al_create_display(disp_data.width, disp_data.height); } else { this->display = al_create_display(screenWidth,screenHeight); } if(!display) { fprintf(stderr, "failed to create display!\n"); return -1; } al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR | ALLEGRO_MIN_LINEAR); al_init_font_addon(); if(!al_init_ttf_addon()) { fprintf(stderr, "failed to initialize font addon!\n"); return -1; } this->timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } this->font = al_load_ttf_font("resources/pirulen.ttf",14,0); if (!font) { fprintf(stderr, "Could not load 'pirulen.ttf'.\n"); return -1; } if (!al_init_image_addon()) { fprintf(stderr, "Could not initialize image addon.\n"); return -1; } this->event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); } al_register_event_source(this->event_queue, al_get_timer_event_source(this->timer)); al_register_event_source(this->event_queue, al_get_keyboard_event_source()); al_register_event_source(this->event_queue, al_get_display_event_source(this->display)); al_register_event_source(this->event_queue, al_get_mouse_event_source()); al_start_timer(timer); return 0; }
int main(short int argc, char** argv) { al_init(); al_install_keyboard(); al_install_mouse(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); ALLEGRO_MONITOR_INFO oMonitorInfo; al_get_monitor_info(0, &oMonitorInfo); short int iDisplayWidth = oMonitorInfo.x2 * 0.70f; short int iDisplayHeight = oMonitorInfo.y2 * 0.70f; short int iAppWidth = iDisplayWidth * 0.95f; short int iAppHeight = iDisplayHeight * 0.95f; short int iMarginHorizontal = (iDisplayWidth - iAppWidth) / 2; short int iMarginVertical = ((iDisplayHeight - iAppHeight) / 2); int iGenerations = 0; short int iFPS = 30; float iLifeMin = 3.0f; float iLifeMax = 37.0f; short int iFontSize = (iDisplayWidth > 1024) ? 12 : 10; if (iDisplayWidth < 800) { iFontSize = 8; } long int iSimulations = 1; std::random_device rd; std::mt19937 mt(rd()); std::uniform_real_distribution<double> dist(iLifeMin, iLifeMax); int iLifeScarcity = std::round(dist(mt)); bool** pCells = new bool*[iAppWidth]; bool** pNextGenCells = new bool*[iAppWidth]; initCells(pCells, pNextGenCells, iAppWidth, iAppHeight, iLifeScarcity); ALLEGRO_DISPLAY* pDisplay = al_create_display(iDisplayWidth, iDisplayHeight); ALLEGRO_EVENT_QUEUE* pQueue = al_create_event_queue(); ALLEGRO_TIMER* pTimer = al_create_timer(1.0f / iFPS); ALLEGRO_TIMER* pSecondBySecondTimer = al_create_timer(1.0f); ALLEGRO_BITMAP* pBuffer = al_create_bitmap(iAppWidth, iAppHeight); ALLEGRO_COLOR oBackgroundColor = al_map_rgb(0, 0, 0); ALLEGRO_COLOR oCellColor = al_map_rgb(randr(150, 255), randr(150, 255), randr(150, 255)); ALLEGRO_FONT* oFont = al_load_ttf_font("VeraMono.ttf", iFontSize, 0); ALLEGRO_FONT* oFontLarge = al_load_ttf_font("VeraMono.ttf", (iFontSize * 3), 0); al_inhibit_screensaver(true); al_register_event_source(pQueue, al_get_keyboard_event_source()); al_register_event_source(pQueue, al_get_mouse_event_source()); al_register_event_source(pQueue, al_get_timer_event_source(pTimer)); al_register_event_source(pQueue, al_get_timer_event_source(pSecondBySecondTimer)); al_set_target_backbuffer(pDisplay); al_clear_to_color(oBackgroundColor); al_flip_display(); al_start_timer(pTimer); al_start_timer(pSecondBySecondTimer); ALLEGRO_EVENT oEvent; short int iBufferUsed = 0; short int iBufferDrawn = 0; bool bRedraw = false; std::string sHeaderStatistics = "GEN [GENXXXXX] FPS [FPSXXXXX] CELLS [CELLSXXXXX] GENS/S [GENSSXXXXX] SCARCTY [SCARXXXXX] TIME [TIMEXXXXX]"; std::string sHeaderStats = ""; /* std::string sHeaderText_2 = ""; std::string sHeaderText_3 = ""; std::string sHeaderText_4 = ""; std::string sHeaderText_5 = ""; std::string sHeaderText_6 = ""; */ std::string sCountdownText = ""; std::string sSimulations = ""; std::string sStats = "CELLS: "; sStats.append(std::to_string((iAppWidth * iAppHeight))); sStats.append(", MAP SIZE (KB): "); sStats.append(std::to_string((iAppWidth * iAppHeight * sizeof(bool)) / 1024)); sStats.append(" (SPACE) Pause (C)olor, (R)eload, (S)carcity, (F) +1 FPS, (G) -1 FPS, (ESC) Exit"); long int iTotalAlive = 0; int iPatternStableBuffer = (iFPS * 4); long int* iTotalPatternStable = new long int[iPatternStableBuffer]; short int iTotalPatternCounter = 0; long int iSecondsRunning = 0; float fPosText2 = (iAppWidth * 0.15); float fPosText3 = (iAppWidth * 0.30); float fPosText4 = (iAppWidth * 0.50); float fPosText5 = (iAppWidth * 0.70); float fPosText6 = (iAppWidth * 0.85); float fPosTextSim = (iAppWidth * 0.75); bool bPatternIsStable = false; int iCountdownSeconds = 10; bool bDrawingOn = false; bool bTimerStopped = false; ALLEGRO_COLOR oRandColor = al_map_rgb(randr(0, 255), randr(0, 255), randr(0, 255)); while (true) { al_wait_for_event(pQueue, &oEvent); if (oEvent.type == ALLEGRO_EVENT_TIMER) { if (!bTimerStopped) { if (oEvent.timer.source == pTimer) { iTotalAlive = 0; redrawCells(pBuffer, pCells, pNextGenCells, iAppWidth, iAppHeight, oCellColor, oBackgroundColor); nextGeneration(pCells, pNextGenCells, iAppWidth, iAppHeight, iTotalAlive); al_set_target_backbuffer(pDisplay); al_clear_to_color(oBackgroundColor); al_draw_bitmap(pBuffer, iMarginHorizontal, iMarginVertical, 0); sHeaderStats = ReplaceString(sHeaderStatistics, "[GENXXXXX]", std::to_string(iGenerations)); sHeaderStats = ReplaceString(sHeaderStats, "[FPSXXXXX]", std::to_string(iFPS)); sHeaderStats = ReplaceString(sHeaderStats, "[CELLSXXXXX]", std::to_string(iTotalAlive)); sHeaderStats = ReplaceString(sHeaderStats, "[SCARXXXXX]", std::to_string(iLifeScarcity)); sHeaderStats = ReplaceString(sHeaderStats, "[TIMEXXXXX]", std::to_string(iSecondsRunning)); if (iGenerations > 0 && iSecondsRunning > 0) { sHeaderStats = ReplaceString(sHeaderStats, "[GENSSXXXXX]", std::to_string(iGenerations / iSecondsRunning)); } else { sHeaderStats = ReplaceString(sHeaderStats, "[GENSSXXXXX]", "0"); } sSimulations = "SIMS "; sSimulations.append(std::to_string(iSimulations)); int iLengthSims = al_get_text_width(oFont, sSimulations.c_str()); int iLengthStats = al_get_text_width(oFont, sHeaderStats.c_str()); al_draw_text(oFont, oCellColor, ((iAppWidth - iLengthStats) / 2), 1.0f, 0, sHeaderStats.c_str()); al_draw_text(oFont, oCellColor, (iDisplayWidth - (iLengthSims + 25.0f)), (iAppHeight + iMarginVertical + 5.0f), 0, sSimulations.c_str()); al_draw_text(oFont, oCellColor, 25.0f, (iAppHeight + iMarginVertical + 5.0f), 0, sStats.c_str()); if (bPatternIsStable == true) { sCountdownText.clear(); sCountdownText.append("PATTERN STABILIZED, RESTARTING IN... "); int iLengthStr = al_get_text_width(oFontLarge, sCountdownText.c_str()); sCountdownText.append(std::to_string(iCountdownSeconds)); al_draw_text(oFontLarge, oRandColor, ((iAppWidth - iLengthStr) / 2), (iAppHeight * 0.45f), 0, sCountdownText.c_str()); } al_flip_display(); ++iGenerations; copyCells(pCells, pNextGenCells, iAppWidth, iAppHeight); if (iTotalPatternCounter == iPatternStableBuffer) { bPatternIsStable = isPatternStable(iTotalPatternStable, iPatternStableBuffer); delete iTotalPatternStable; iTotalPatternStable = new long int[iPatternStableBuffer]; iTotalPatternCounter = 0; } iTotalPatternStable[iTotalPatternCounter] = iTotalAlive; ++iTotalPatternCounter; } if (oEvent.timer.source == pSecondBySecondTimer) { if (bPatternIsStable == true) { if (iCountdownSeconds > 1) { --iCountdownSeconds; } else { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } } else { iCountdownSeconds = 10; } ++iSecondsRunning; oRandColor = al_map_rgb(randr(0, 255), randr(0, 255), randr(0, 255)); } } } if (oEvent.type == ALLEGRO_EVENT_KEY_DOWN) { if (oEvent.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } if (oEvent.keyboard.keycode == ALLEGRO_KEY_SPACE) { if (!bTimerStopped) { bTimerStopped = true; al_stop_timer(pTimer); } else { bTimerStopped = false; al_start_timer(pTimer); } } if (oEvent.keyboard.keycode == ALLEGRO_KEY_R) { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_S) { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; iLifeScarcity = randr(iLifeMin, iLifeMax); ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_M) { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; iLifeScarcity = iLifeMin; ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_N) { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; iLifeScarcity = iLifeMax; ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_F) { ++iFPS; al_set_timer_speed(pTimer, (1.0f / iFPS)); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_G) { if(iFPS > 3) { --iFPS; } al_set_timer_speed(pTimer, (1.0f / iFPS)); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_C) { int iRCell = randr(0, 255); int iGCell = randr(0, 255); int iBCell = randr(0, 255); oCellColor = al_map_rgb(iRCell, iGCell, iBCell); } } } // End main loop al_destroy_event_queue(pQueue); al_destroy_display(pDisplay); delete iTotalPatternStable; for (short int i = 0; i < iAppWidth; i++) { delete pCells[i]; delete pNextGenCells[i]; } delete[] pCells; delete[] pNextGenCells; return 0; }
int main() { al_init(); // Start Allegro ALLEGRO_DISPLAY *screen = NULL; // Main display ALLEGRO_EVENT_QUEUE *event_queue = NULL; // Event queue to maintain events ALLEGRO_TIMER *timer = NULL; // Timer to control game fps ALLEGRO_FONT *fontA = NULL; // Initializing screen, event queue and timer screen = al_create_display(SCREEN_W, SCREEN_H); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); // Timer set to tick at 60 frames per second al_init_image_addon(); // Initialize Allegro image addon al_install_keyboard(); // Initialize Allegro keyboard al_start_timer(timer); // Start timer al_init_font_addon(); al_init_ttf_addon(); // Register event sources to be handled in the event queue al_register_event_source(event_queue, al_get_display_event_source(screen)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); fontA = al_load_font("arial.ttf", 18, 0); GameManager gameManager; // Object creation gameManager.level1(); // Function call //Main game loop while (!keyboard[KEY_ESCAPE]) // Run while escape key is not pressed { ALLEGRO_EVENT event; // ALLEGRO_EVENT to hold events al_wait_for_event(event_queue, &event); // Wait for events to occur // Display handler // Draw if timer counter has incremented and there are no events to handle // and event queue is empty if (event.type == ALLEGRO_EVENT_TIMER && al_is_event_queue_empty(event_queue)) { gameManager.DrawLevel(screen); al_flip_display(); // Display backbuffer on to the main screen al_clear_to_color(al_map_rgb(255, 0, 255)); // Clear display to black } //Event handlers if (event.type == ALLEGRO_EVENT_TIMER) { gameManager.UpdateLogic(); // Function call } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; // End game loop on closing display window } else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { gameManager.KeyboardEventUpdate(event, true); // Update routine on key press } else if (event.type == ALLEGRO_EVENT_KEY_UP) { gameManager.KeyboardEventUpdate(event, false); // Update routine on key release } al_draw_textf(fontA, al_map_rgb(238, 105, 29), SCREEN_W / 2, SCREEN_H / 2, ALLEGRO_ALIGN_CENTRE, "GAME OVER Final score %i", score); } // End of Game Loop al_destroy_display(screen); // Destroy display return 0; }
int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *cursor; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; ALLEGRO_FONT *font; int mx = 0; int my = 0; int mz = 0; int mw = 0; int mmx = 0; int mmy = 0; int mmz = 0; int mmw = 0; bool in = true; bool buttons[NUM_BUTTONS] = {false}; int i; float p = 0.0; ALLEGRO_COLOR black; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_init_primitives_addon(); al_install_mouse(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); actual_buttons = al_get_mouse_num_buttons(); if (actual_buttons > NUM_BUTTONS) actual_buttons = NUM_BUTTONS; al_set_new_display_flags(ALLEGRO_RESIZABLE); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); } al_hide_mouse_cursor(display); cursor = al_load_bitmap("data/cursor.tga"); if (!cursor) { abort_example("Error loading cursor.tga\n"); } font = al_load_font("data/fixed_font.tga", 1, 0); if (!font) { abort_example("data/fixed_font.tga not found\n"); } black = al_map_rgb_f(0, 0, 0); queue = al_create_event_queue(); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); while (1) { if (al_is_event_queue_empty(queue)) { al_clear_to_color(al_map_rgb(0xff, 0xff, 0xc0)); for (i = 0; i < actual_buttons; i++) { draw_mouse_button(i, buttons[i]); } al_draw_bitmap(cursor, mx, my, 0); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); al_draw_textf(font, black, 5, 5, 0, "dx %i, dy %i, dz %i, dw %i", mmx, mmy, mmz, mmw); al_draw_textf(font, black, 5, 25, 0, "x %i, y %i, z %i, w %i", mx, my, mz, mw); al_draw_textf(font, black, 5, 45, 0, "p = %g", p); al_draw_textf(font, black, 5, 65, 0, "%s", in ? "in" : "out"); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); mmx = mmy = mmz = 0; al_flip_display(); } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_MOUSE_AXES: mx = event.mouse.x; my = event.mouse.y; mz = event.mouse.z; mw = event.mouse.w; mmx = event.mouse.dx; mmy = event.mouse.dy; mmz = event.mouse.dz; mmw = event.mouse.dw; p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: if (event.mouse.button-1 < NUM_BUTTONS) { buttons[event.mouse.button-1] = true; } p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: if (event.mouse.button-1 < NUM_BUTTONS) { buttons[event.mouse.button-1] = false; } p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY: in = true; break; case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY: in = false; break; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { goto done; } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_CLOSE: goto done; } } done: al_destroy_event_queue(queue); return 0; }
int main(){ //variaveis que tem alguma coisa a ver com o jogados, como exp recebida e level int game_status = 0, option = 0, school = 0; //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- //Variaveis internas relacionadas as bibliotecas usadas e erros do jogo em si. int status_allegro = 0, tecla = 0; //A variavel teclado tem 5 estados /* 0 - Neutro 1 - Tecla para cima 2 - Tecla para Baixo 3 - Tecla para Direita 4 - Tecla para Esquerda */ //iniciando o Allegro e todos as suas bibliotecas e funções usadas. al_init(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_install_keyboard(); //Iniciando o jogo. display = al_create_display(SCREEN_W, SCREEN_H); background = al_load_bitmap("img/background.jpg"); start_menu_img = al_load_bitmap("img/start_screen.jpg"); font_text = al_load_font("font/font.ttf", 50, 0); timer = al_create_timer(0.1); tcont = al_create_timer(1.0); sections_event = al_create_event_queue(); al_set_window_title(display, "M.I.P. - Matemática, Português e Inglês"); al_start_timer(timer); al_start_timer(tcont); //Qualquer valor que seja colocado apos a imagem, ira interferir na sua posição em relação a tela. fadeout(7, SCREEN_W, SCREEN_H, display); al_draw_bitmap(background, 0,0,0); al_flip_display(); //Verificando erros no allegro e suas bibliotecas. status_allegro = check_allegro(background, start_menu_img, sections_event, display, font_text, timer, tcont); if(status_allegro == -1){ close_game(background, start_menu_img, sections_event, display, timer, tcont); return 0; } al_register_event_source(sections_event, al_get_keyboard_event_source()); al_register_event_source(sections_event, al_get_display_event_source(display)); //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- //menu_background(); fadeout(1, SCREEN_W, SCREEN_H, display); al_draw_bitmap(start_menu_img, 0,0,0); printf("Menu Principal\n"); //capture_event(); al_flip_display(); al_draw_textf(font_text, al_map_rgb(0, 255, 255), SCREEN_H / 4, SCREEN_W / 4, ALLEGRO_ALIGN_LEFT, "Novo Jogo"); al_draw_textf(font_text, al_map_rgb(0, 255, 255), SCREEN_H / (3.7), SCREEN_W / 3, ALLEGRO_ALIGN_LEFT, "Continuar"); al_draw_textf(font_text, al_map_rgb(0, 255, 255), SCREEN_H / (3.4), SCREEN_W / (2.5), ALLEGRO_ALIGN_LEFT, "Sair do jogo"); al_flip_display(); option = capture_event_queue(SCREEN_H, SCREEN_W, keyboard, display, sections_event, tecla, font_text); if(option == -1){ close_game(background, start_menu_img, sections_event, display, timer, tcont); return 0; } switch(option){ case 0: game_start(); printf("game_start\n"); break; case 1: game_load(); printf("game_load\n"); break; case 2: printf("Finalizando Jogo\n"); close_game(background, start_menu_img, sections_event, display, timer, tcont); return 0; break; } //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- //Apos a escolha entre continuar ou começar novamente o usuário estará na tela de seleção de escolas printf("Selecione a escola\n"); scanf("%d", &school); switch(school){ case 0: open_math(); break; case 1: open_ing(); break; case 2: open_port(); break; } //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- return 0; }
bool Game::init() { mShouldExit = false; //create Timers mpLoopTimer = new Timer; mpMasterTimer = new Timer; //startup allegro if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return false; } //create and init GraphicsSystem mpGraphicsSystem = new GraphicsSystem(); bool goodGraphics = mpGraphicsSystem->init( WIDTH, HEIGHT ); if(!goodGraphics) { fprintf(stderr, "failed to initialize GraphicsSystem object!\n"); return false; } mpGraphicsBufferManager = new GraphicsBufferManager(); mpSpriteManager = new SpriteManager(); //startup a lot of allegro stuff //load image loader addon if( !al_init_image_addon() ) { fprintf(stderr, "image addon failed to load!\n"); return false; } //install audio stuff if( !al_install_audio() ) { fprintf(stderr, "failed to initialize sound!\n"); return false; } if(!al_init_acodec_addon()) { fprintf(stderr, "failed to initialize audio codecs!\n"); return false; } if (!al_reserve_samples(1)) { fprintf(stderr, "failed to reserve samples!\n"); return false; } //should probably be done in the InputSystem! if( !al_install_keyboard() ) { printf( "Keyboard not installed!\n" ); return false; } //should probably be done in the InputSystem! if( !al_install_mouse() ) { printf( "Mouse not installed!\n" ); return false; } //should be somewhere else! al_init_font_addon(); if( !al_init_ttf_addon() ) { printf( "ttf font addon not initted properly!\n" ); return false; } //actually load the font mpFont = al_load_ttf_font( "cour.ttf", 20, 0 ); if( mpFont == NULL ) { printf( "ttf font file not loaded properly!\n" ); return false; } //show the mouse if( !al_hide_mouse_cursor( mpGraphicsSystem->getDisplay() ) ) { printf( "Mouse cursor not able to be hidden!\n" ); return false; } if( !al_init_primitives_addon() ) { printf( "Primitives addon not added!\n" ); return false; } //load the sample mpSample = al_load_sample( "clapping.wav" ); if (!mpSample) { printf( "Audio clip sample not loaded!\n" ); return false; } mpMessageManager = new GameMessageManager(); //load buffers mBackgroundBufferID = mpGraphicsBufferManager->loadBuffer("wallpaper.bmp"); mPlayerIconBufferID = mpGraphicsBufferManager->loadBuffer("arrow.bmp"); mEnemyIconBufferID = mpGraphicsBufferManager->loadBuffer("enemy-arrow.bmp"); //setup sprites GraphicsBuffer* pBackGroundBuffer = mpGraphicsBufferManager->getBuffer( mBackgroundBufferID ); if( pBackGroundBuffer != NULL ) { mpSpriteManager->createAndManageSprite( BACKGROUND_SPRITE_ID, pBackGroundBuffer, 0, 0, pBackGroundBuffer->getWidth(), pBackGroundBuffer->getHeight() ); } GraphicsBuffer* pPlayerBuffer = mpGraphicsBufferManager->getBuffer( mPlayerIconBufferID ); Sprite* pArrowSprite = NULL; if( pPlayerBuffer != NULL ) { pArrowSprite = mpSpriteManager->createAndManageSprite( PLAYER_ICON_SPRITE_ID, pPlayerBuffer, 0, 0, pPlayerBuffer->getWidth(), pPlayerBuffer->getHeight() ); } GraphicsBuffer* pAIBuffer = mpGraphicsBufferManager->getBuffer( mEnemyIconBufferID ); Sprite* pEnemyArrow = NULL; if( pAIBuffer != NULL ) { pEnemyArrow = mpSpriteManager->createAndManageSprite( AI_ICON_SPRITE_ID, pAIBuffer, 0, 0, pAIBuffer->getWidth(), pAIBuffer->getHeight() ); } //setup units Vector2D pos( 0.0f, 0.0f ); Vector2D vel( 0.0f, 0.0f ); mpUnit = new KinematicUnit( pArrowSprite, pos, 1, vel, 0.0f, 200.0f, 10.0f ); Vector2D pos2( 1000.0f, 500.0f ); Vector2D vel2( 0.0f, 0.0f ); mpAIUnit = new KinematicUnit( pEnemyArrow, pos2, 1, vel2, 0.0f, 180.0f, 100.0f ); //give steering behavior mpAIUnit->dynamicArrive( mpUnit ); Vector2D pos3( 500.0f, 500.0f ); mpAIUnit2 = new KinematicUnit( pEnemyArrow, pos3, 1, vel2, 0.0f, 180.0f, 100.0f ); //give steering behavior mpAIUnit2->dynamicSeek( mpUnit ); return true; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; int redraw = 0; double t = 0; if (!al_init()) { abort_example("Could not init Allegro.\n"); } open_log_monospace(); al_init_primitives_addon(); al_install_mouse(); al_init_font_addon(); al_init_ttf_addon(); al_init_image_addon(); init_platform_specific(); #ifdef ALLEGRO_IPHONE al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); #endif display = al_create_display(640, 480); if (!display) { abort_example("Could not create display.\n"); } al_install_keyboard(); if (argc >= 2) { font_file = argv[1]; } ex.f1 = al_load_font(font_file, 48, 0); ex.f2 = al_load_font(font_file, 48, ALLEGRO_TTF_NO_KERNING); ex.f3 = al_load_font(font_file, 12, 0); /* Specifying negative values means we specify the glyph height * in pixels, not the usual font size. */ ex.f4 = al_load_font(font_file, -140, 0); ex.f5 = al_load_font(font_file, 12, ALLEGRO_TTF_MONOCHROME); { int ranges[] = {0x1F40A, 0x1F40A}; ALLEGRO_BITMAP *icon = al_load_bitmap("data/icon.png"); if (!icon) { abort_example("Couldn't load data/icon.png.\n"); } ALLEGRO_BITMAP *glyph = al_create_bitmap(50, 50); al_set_target_bitmap(glyph); al_clear_to_color(al_map_rgba_f(0, 0, 0, 0)); al_draw_rectangle(0.5, 0.5, 49.5, 49.5, al_map_rgb_f(1, 1, 0), 1); al_draw_bitmap(icon, 1, 1, 0); al_set_target_backbuffer(display); ex.f_alex = al_grab_font_from_bitmap(glyph, 1, ranges); } if (!ex.f1 || !ex.f2 || !ex.f3 || !ex.f4 || !ex.f_alex) { abort_example("Could not load font: %s\n", font_file); } al_set_fallback_font(ex.f3, ex.f_alex); ex.ranges_count = al_get_font_ranges(ex.f1, 0, NULL); print_ranges(ex.f1); ex.config = al_load_config_file("data/ex_ttf.ini"); if (!ex.config) { abort_example("Could not data/ex_ttf.ini\n"); } timer = al_create_timer(1.0 / 60); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (true) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } if (event.type == ALLEGRO_EVENT_TIMER) redraw++; while (redraw > 0 && al_is_event_queue_empty(queue)) { double dt; redraw--; dt = al_get_time(); render(); dt = al_get_time() - dt; t = 0.99 * t + 0.01 * dt; ex.fps = 1.0 / t; al_flip_display(); } } al_destroy_font(ex.f1); al_destroy_font(ex.f2); al_destroy_font(ex.f3); al_destroy_font(ex.f4); al_destroy_font(ex.f5); al_destroy_config(ex.config); close_log(false); return 0; }
int main() { // don't forget to put allegro-5.0.10-monolith-md-debug.lib const float FPS = 60.0f; ALLEGRO_DISPLAY *display; if(!al_init()) { al_show_native_message_box(NULL,"Error","Error", "Cannot initialize Allegro", NULL, NULL); return -1; } display = al_create_display(ScreenWidth, ScreenHeight); if(!display) { al_show_native_message_box(NULL,"Error","Error", "Cannot create dsiplay", NULL, NULL); return -1; } al_set_window_position(display, 100, 100); al_install_keyboard(); al_install_mouse(); al_init_image_addon(); //al_init_acodec_addon(); al_init_font_addon(); al_init_ttf_addon(); ALLEGRO_TIMER *timer = al_create_timer(1.0f / FPS); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); ALLEGRO_KEYBOARD_STATE keyState; al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_display_event_source(display)); bool done = false; InputManager input; ScreenManager::GetInstance().Initialize(); ScreenManager::GetInstance().LoadContent(); std::vector<int> keys; //these two below plus the IsKeyReleased are example how to use simultaneous keys keys.push_back(ALLEGRO_KEY_DOWN); keys.push_back(ALLEGRO_KEY_ESCAPE); float fade = 0.0f; al_start_timer(timer); while (!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); al_get_keyboard_state(&keyState); if(input.IsKeyReleased(ev,keys)) done=true; //closes the game //barnhen to check begin if(input.IsKeyPressed(ev, ALLEGRO_KEY_RIGHT)) fade++; else if(input.IsKeyPressed(ev, ALLEGRO_KEY_LEFT)) fade--; //barnhen to check end ScreenManager::GetInstance().Update(ev); ScreenManager::GetInstance().Draw(display); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } ScreenManager::GetInstance().UnloadContent(); //Destroyers al_destroy_display(display); al_destroy_timer(timer); al_destroy_event_queue(event_queue); //std::cin.get(); return 0; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *bmp; ALLEGRO_FONT *f; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; bool redraw; int min_w, min_h, max_w, max_h; int ret_min_w, ret_min_h, ret_max_w, ret_max_h; bool constr_min_w, constr_min_h, constr_max_w, constr_max_h; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_set_new_display_flags(ALLEGRO_RESIZABLE | ALLEGRO_GENERATE_EXPOSE_EVENTS); display = al_create_display(640, 480); if (!display) { abort_example("Unable to set any graphic mode\n"); } bmp = al_load_bitmap("data/mysha.pcx"); if (!bmp) { abort_example("Unable to load image\n"); } f = al_load_font("data/a4_font.tga", 0, 0); if (!f) { abort_example("Failed to load a4_font.tga\n"); } min_w = 640; min_h = 480; max_w = 800; max_h = 600; constr_min_w = constr_min_h = constr_max_w = constr_max_h = true; if (!al_set_window_constraints( display, constr_min_w ? min_w : 0, constr_min_h ? min_h : 0, constr_max_w ? max_w : 0, constr_max_h ? max_h : 0)) { abort_example("Unable to set window constraints.\n"); } al_apply_window_constraints(display, true); queue = al_create_event_queue(); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_keyboard_event_source()); redraw = true; while (true) { if (redraw && al_is_event_queue_empty(queue)) { al_clear_to_color(al_map_rgb(255, 0, 0)); al_draw_scaled_bitmap(bmp, 0, 0, al_get_bitmap_width(bmp), al_get_bitmap_height(bmp), 0, 0, al_get_display_width(display), al_get_display_height(display), 0); /* Display screen resolution */ al_draw_textf(f, al_map_rgb(255, 255, 255), 0, 0, 0, "Resolution: %dx%d", al_get_display_width(display), al_get_display_height(display)); if (!al_get_window_constraints(display, &ret_min_w, &ret_min_h, &ret_max_w, &ret_max_h)) { abort_example("Unable to get window constraints\n"); } al_draw_textf(f, al_map_rgb(255, 255, 255), 0, al_get_font_line_height(f), 0, "Min Width: %d, Min Height: %d, Max Width: %d, Max Height: %d", ret_min_w, ret_min_h, ret_max_w, ret_max_h); al_draw_textf(f, al_map_rgb(255, 255, 255), 0, al_get_font_line_height(f) * 2,0, "Toggle Restriction: Min Width: Z, Min Height: X, Max Width: C, Max Height: V"); al_flip_display(); redraw = false; } al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { al_acknowledge_resize(event.display.source); redraw = true; } if (event.type == ALLEGRO_EVENT_DISPLAY_EXPOSE) { redraw = true; } if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.keyboard.keycode == ALLEGRO_KEY_Z) { constr_min_w = ! constr_min_w; } else if (event.keyboard.keycode == ALLEGRO_KEY_X) { constr_min_h = ! constr_min_h; } else if (event.keyboard.keycode == ALLEGRO_KEY_C) { constr_max_w = ! constr_max_w; } else if (event.keyboard.keycode == ALLEGRO_KEY_V) { constr_max_h = ! constr_max_h; } redraw = true; if (!al_set_window_constraints(display, constr_min_w ? min_w : 0, constr_min_h ? min_h : 0, constr_max_w ? max_w : 0, constr_max_h ? max_h : 0)) { abort_example("Unable to set window constraints.\n"); } al_apply_window_constraints(display, true); } if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } } al_destroy_bitmap(bmp); al_destroy_display(display); return 0; }
int main(void) { int vMapa = 0; int vMatriz_Mapa[21][27],i=0,j=0; FILE *fMapa = fopen("dormitorio_masculino.txt", "r"); // Carrega o arquivo de texto da fase em questão; char vLe_Char; while((vLe_Char = getc(fMapa) ) != EOF ) // Grava Caracter enquanto não chegar ao final do arquivo; { if ( i < 27 ) // Enquanto estivar na linha; { vMatriz_Mapa[j][i] = atoi(&vLe_Char); // Carrega a matriz com os caracteres que representam as imagens; i++; } else // senao passa para a proxima linha; { j++; i=0; } } fclose(fMapa); for (j=0;j<19;j++){ for (i=0;i<27;i++){ // if(vMatriz_Mapa[j/32][i/32]==2){ printf("%d",vMatriz_Mapa[j][i]); } printf("\n"); } bool sair = false; int tecla = 0,top = 0, left = 0, right = 0, down=0; int x=192,y=64; if (!al_init()) { fprintf(stderr, "Falha ao inicializar a Allegro.\n"); return false; } al_init_font_addon(); if (!al_init_ttf_addon()) { fprintf(stderr, "Falha ao inicializar add-on allegro_ttf.\n"); return false; } if (!al_init_image_addon()) { fprintf(stderr, "Falha ao inicializar add-on allegro_image.\n"); return false; } if (!al_install_keyboard()) { fprintf(stderr, "Falha ao inicializar o teclado.\n"); return false; } janela = al_create_display(LARGURA_TELA, ALTURA_TELA); if (!janela) { fprintf(stderr, "Falha ao criar janela.\n"); return false; } al_set_window_title(janela, "Utilizando o Teclado"); fila_eventos = al_create_event_queue(); if (!fila_eventos) { fprintf(stderr, "Falha ao criar fila de eventos.\n"); al_destroy_display(janela); return false; } fundo = al_load_bitmap("dormitorio_masculino.bmp"); if (!fundo) { fprintf(stderr, "Falha ao carregar imagem de fundo.\n"); al_destroy_display(janela); al_destroy_event_queue(fila_eventos); return false; } timer = al_create_timer(6/60.0); al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); al_register_event_source(fila_eventos, al_get_keyboard_event_source()); al_register_event_source(fila_eventos, al_get_display_event_source(janela)); al_draw_bitmap(fundo, 0, 0, 0); ALLEGRO_BITMAP *peterSET = al_load_bitmap("sabrina3.bmp"); al_convert_mask_to_alpha(peterSET,al_map_rgb(255,0,255)); ALLEGRO_BITMAP *peterCHAR[12]; peterCHAR[0] = al_create_bitmap(32,32); peterCHAR[0] = al_create_sub_bitmap(peterSET, 0, 0, 32, 32); peterCHAR[1] = al_create_bitmap(32,32); peterCHAR[1] = al_create_sub_bitmap(peterSET, 32, 0, 32, 32); peterCHAR[2] = al_create_bitmap(32,32); peterCHAR[2] = al_create_sub_bitmap(peterSET, 64, 0, 32, 32); peterCHAR[3] = al_create_bitmap(32,32); peterCHAR[3] = al_create_sub_bitmap(peterSET, 0, 32, 32, 32); peterCHAR[4] = al_create_bitmap(32,32); peterCHAR[4] = al_create_sub_bitmap(peterSET, 32, 32, 32, 32); peterCHAR[5] = al_create_bitmap(32,32); peterCHAR[5] = al_create_sub_bitmap(peterSET, 64, 32, 32, 32); peterCHAR[6] = al_create_bitmap(32,32); peterCHAR[6] = al_create_sub_bitmap(peterSET, 0, 64, 32, 32); peterCHAR[7] = al_create_bitmap(32,32); peterCHAR[7] = al_create_sub_bitmap(peterSET, 32, 64, 32, 32); peterCHAR[8] = al_create_bitmap(32,32); peterCHAR[8] = al_create_sub_bitmap(peterSET, 64, 64, 32, 32); peterCHAR[9] = al_create_bitmap(32,32); peterCHAR[9] = al_create_sub_bitmap(peterSET, 0, 96, 32, 32); peterCHAR[10] = al_create_bitmap(32,32); peterCHAR[10] = al_create_sub_bitmap(peterSET, 32, 96, 32, 32); peterCHAR[11] = al_create_bitmap(32,32); peterCHAR[11] = al_create_sub_bitmap(peterSET, 64, 96, 32, 32); al_draw_bitmap(fundo, 0, 0, 0); al_draw_bitmap(peterCHAR[10],x,y,0); al_flip_display(); al_start_timer(timer); while (!sair) { while(!al_is_event_queue_empty(fila_eventos)) { ALLEGRO_EVENT evento; al_wait_for_event(fila_eventos, &evento); if (evento.type == ALLEGRO_EVENT_KEY_DOWN) { switch(evento.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: sair = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_SPACE: keys[SELECT]=true; break; } }else if (evento.type == ALLEGRO_EVENT_KEY_UP) { switch(evento.keyboard.keycode) { case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_SPACE: keys[SELECT]=false; break; } } else if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { sair = true; } else if (evento.type == ALLEGRO_EVENT_TIMER){ if(keys[SELECT]){ if(vMatriz_Mapa[y/32][(x+32)/32]==4){ return 0; } } if (keys[UP]){ if(vMatriz_Mapa[(y-32)/32][x/32]==0){ al_draw_bitmap(fundo, 0, 0, 0); if(top == 0) { y=y-32; al_draw_bitmap(peterCHAR[10],x,y,0); top = 1; } else if(top == 1) { y=y-32; al_draw_bitmap(peterCHAR[9],x,y,0); top=2; } else if (top == 2) { y=y-32; al_draw_bitmap(peterCHAR[11],x,y,0); top =0; } printf("x = %d e y = %d\n",x,y); al_flip_display(); } } if (keys[DOWN]){ if(vMatriz_Mapa[(y+32)/32][x/32]==0){ al_draw_bitmap(fundo, 0, 0, 0); if(down == 0) { y=y+32; al_draw_bitmap(peterCHAR[1],x,y,0); down = 1; } else if(down == 1) { y=y+32; al_draw_bitmap(peterCHAR[0],x,y,0); down = 2; } else if (down == 2) { y=y+32; al_draw_bitmap(peterCHAR[2],x,y,0); down = 0; } } } printf("x = %d e y = %d\n",x,y); al_flip_display(); } if (keys[LEFT]){ if(vMatriz_Mapa[y/32][(x-32)/32]==0){ al_draw_bitmap(fundo, 0, 0, 0); if(left == 0) { x=x-32; al_draw_bitmap(peterCHAR[4],x,y,0); left = 1; } else if(left == 1) { x=x-32; al_draw_bitmap(peterCHAR[3],x,y,0); left=2; } else if (left == 2) { x=x-32; al_draw_bitmap(peterCHAR[5],x,y,0); left =0; } al_flip_display(); printf("x = %d e y = %d\n",x,y); } } if (keys[RIGHT]){ if(vMatriz_Mapa[y/32][(x+32)/32]==0){ al_draw_bitmap(fundo, 0, 0, 0); if(right == 0) { x=x+32; al_draw_bitmap(peterCHAR[7],x,y,0); right = 1; } else if(right == 1) { x=x+32; al_draw_bitmap(peterCHAR[6],x,y,0); right=2; } else if (right == 2) { x=x+32; al_draw_bitmap(peterCHAR[8],x,y,0); right =0; } printf("x = %d e y = %d\n",x,y); al_flip_display(); } } } } al_destroy_display(janela); al_destroy_event_queue(fila_eventos); return 0; }
//prototype int main(void){ bool done = false; bool redraw = true; const int FPS = 60; bool isGameOver = false; //object variables spaceShip ship; Bullet bullets[NUM_BULLETS]; Comet comets[NUM_COMETS]; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *eventQueue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font18 = NULL; //check if allegro is initialised <allegro_native_dialog.h> if(!al_init()){ return -1; } display = al_create_display(WIDTH,HEIGHT); //check if display was created if(!display){ return -1; } al_init_primitives_addon(); al_install_keyboard(); al_init_font_addon(); al_init_ttf_addon(); eventQueue = al_create_event_queue(); timer= al_create_timer(1.0/FPS); srand(time(NULL)); // gives the rand() some value initShip(ship); initBullet(bullets, NUM_BULLETS); initComet(comets, NUM_COMETS); font18 = al_load_font("arial.ttf",18,0); al_register_event_source(eventQueue, al_get_keyboard_event_source()); al_register_event_source(eventQueue, al_get_timer_event_source(timer)); al_register_event_source(eventQueue, al_get_display_event_source(display)); //event os display resize closing, moving al_start_timer(timer); while(!done){ ALLEGRO_EVENT ev; al_wait_for_event(eventQueue, &ev); if (ev.type==ALLEGRO_EVENT_TIMER){ redraw=true; if(keys[UP]){ moveShipUp(ship); } if(keys[DOWN]){ moveShipDown(ship); } if(keys[LEFT]){ moveShipLeft(ship); } if(keys[RIGHT]){ moveShipRight(ship); } if(!isGameOver){ updateBullet(bullets,NUM_BULLETS); startComet(comets, NUM_COMETS); updateComet(comets, NUM_COMETS); collideBullet(bullets,NUM_BULLETS, comets, NUM_BULLETS, ship); collideComet(comets, NUM_COMETS, ship); if (ship.lives <= 0){ isGameOver = true; } } } // when clicling on red X icon to close the game else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ done=true; } else if (ev.type==ALLEGRO_EVENT_KEY_DOWN){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_ESCAPE: done=true; break; case ALLEGRO_KEY_UP: keys[UP]=true; break; case ALLEGRO_KEY_DOWN: keys[DOWN]=true; break; case ALLEGRO_KEY_LEFT: keys[LEFT]=true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT]=true; break; case ALLEGRO_KEY_SPACE: keys[SPACE]=true; fireBullet(bullets, NUM_BULLETS, ship); break; } } else if (ev.type==ALLEGRO_EVENT_KEY_UP){ switch(ev.keyboard.keycode){ //make sure when exiting the program exit with esc button case ALLEGRO_KEY_ESCAPE: done=true; break; case ALLEGRO_KEY_UP: keys[UP]=false; break; case ALLEGRO_KEY_DOWN: keys[DOWN]=false; break; case ALLEGRO_KEY_LEFT: keys[LEFT]=false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT]=false; break; case ALLEGRO_KEY_SPACE: keys[SPACE]=false; break; } } if (redraw && al_is_event_queue_empty(eventQueue)){ // I only draw the screen if there no more event redraw=false; if(!isGameOver){ drawShip(ship); drawBullet(bullets,NUM_BULLETS); drawComet(comets, NUM_COMETS); al_draw_textf(font18, al_map_rgb(255,0,255), 5, 5, 0, "Player has %i lives left. Player has destroyed %i objects", ship.lives, ship.score); } else{ al_draw_textf(font18, al_map_rgb(0,255,255),WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over, Pal. Final Score: %i", ship.score); } al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); //that prevents the filled rectangle draw imga just like snake style } //make sure when exiting the program exit with esc button // when clicling on red X icon to close the game } //al_rest(5.0); al_destroy_display(display); //hadouken return 0; }
int main(int argc, char **argv) { // Initialize Allegro if (!al_init()) { fprintf(stderr, "Fatal Error: Allegro initialization failed!\n"); return -1; } // Initialize the Allegro Image addon, used to load sprites and maps if (!al_init_image_addon()) { fprintf(stderr, "Fatal Error: Allegro Image Addon initialization failed!\n"); return -1; } // Initialize primitives for drawing if (!al_init_primitives_addon()) { fprintf(stderr, "Fatal Error: Could not initialize primitives module!"); throw -1; } // Initialize keyboard modules if (!al_install_keyboard()) { fprintf(stderr, "Fatal Error: Could not initialize keyboard module!"); throw -1; } // Initialize mouse if (!al_install_mouse()) { fprintf(stderr, "Fatal Error: Could not initialize mouse module!"); throw -1; } // Initialize networking system if (enet_initialize()) { fprintf(stderr, "Fatal Error: Could not initialize enet!"); throw -1; } // Create a display ALLEGRO_DISPLAY *display; al_set_new_display_option(ALLEGRO_VSYNC, 2, ALLEGRO_REQUIRE); al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_RESIZABLE); display = al_create_display(1280, 720); if(!display) { // FIXME: Make the error argument mean anything? fprintf(stderr, "Fatal Error: Could not create display\n"); throw -1; } //load font //gg2 font as placeholder for now i guess al_init_font_addon(); al_init_ttf_addon(); ALLEGRO_FONT *font = al_load_font("Vanguard Main Font.ttf", 12, ALLEGRO_TTF_MONOCHROME); if (!font) { fprintf(stderr, "Could not load 'gg2bold.ttf'.\n"); throw -1; } // MainMenu *mainmenu = new MainMenu(display); bool isserver; if (argc >= 2) { // If there are any arguments isserver = false; } else { isserver = true; } // double lasttimeupdated = al_get_time(); // bool run = true; // while (run) // { // if (al_get_time() - lasttimeupdated >= MENU_TIMESTEP) // { // run = mainmenu->run(display, &gametype); // lasttimeupdated = al_get_time(); // } // } // delete mainmenu; Engine engine(isserver); Renderer renderer; InputCatcher inputcatcher(display); Gamestate renderingstate(&engine); std::unique_ptr<Networker> networker; if (isserver) { networker = std::unique_ptr<Networker>(new ServerNetworker()); } else { networker = std::unique_ptr<Networker>(new ClientNetworker()); } engine.loadmap("lijiang"); // FIXME: Hack to make sure the oldstate is properly initialized engine.update(&(networker->sendbuffer), 0); EntityPtr myself(0); if (isserver) { myself = engine.currentstate->addplayer(); engine.currentstate->get<Player>(myself)->spawntimer.active = true; } else { ClientNetworker *n = reinterpret_cast<ClientNetworker*>(networker.get()); while (not n->isconnected()) { n->receive(engine.currentstate.get()); } myself = engine.currentstate->playerlist[engine.currentstate->playerlist.size()-1]; } InputContainer held_keys; double mouse_x; double mouse_y; double enginetime = al_get_time(); double networkertime = al_get_time(); while (true) { try { while (al_get_time() - enginetime >= ENGINE_TIMESTEP) { networker->receive(engine.currentstate.get()); inputcatcher.run(display, &held_keys, &mouse_x, &mouse_y); if (not isserver) { Character *c = engine.currentstate->get<Player>(myself)->getcharacter(engine.currentstate.get()); if (c != 0) { ClientNetworker *n = reinterpret_cast<ClientNetworker*>(networker.get()); n->sendinput(held_keys, mouse_x/renderer.zoom+renderer.cam_x, mouse_y/renderer.zoom+renderer.cam_y); } } engine.setinput(myself, held_keys, mouse_x/renderer.zoom+renderer.cam_x, mouse_y/renderer.zoom+renderer.cam_y); engine.update(&(networker->sendbuffer), ENGINE_TIMESTEP); networker->sendeventdata(engine.currentstate.get()); enginetime += ENGINE_TIMESTEP; } if (isserver) { if (al_get_time() - networkertime >= NETWORKING_TIMESTEP) { ServerNetworker *n = reinterpret_cast<ServerNetworker*>(networker.get()); n->sendframedata(engine.currentstate.get()); networkertime = al_get_time(); } } renderingstate.interpolate(engine.oldstate.get(), engine.currentstate.get(), (al_get_time()-enginetime)/ENGINE_TIMESTEP); renderer.render(display, &renderingstate, myself, networker.get()); } catch (int e) { if (e != 0) { fprintf(stderr, "\nError during regular loop."); fprintf(stderr, "\nExiting.."); } al_destroy_display(display); return 0; } } al_shutdown_font_addon(); al_shutdown_ttf_addon(); al_destroy_display(display); enet_deinitialize(); return 0; }
bool Allegro::init(XkontiConsoleColors* _con) { #if(DEBUG_ALLEGROHELPER) _con->print(debug, "- Allegro::init - Start\n"); #endif // Arguments pass con = _con; // Initialization of Allegro if (!al_init()) { con->print(fatal, "Failed to initialize allegro!\n"); return false; } #if(DEBUG_ALLEGROHELPER) con->print(debug, "-- Allegro::init - al_init() successful\n"); #endif // Initialization of Image Addon if (!al_init_image_addon()) { con->print(fatal, "Failed to initialize al_init_image_addon!\n"); return false; } #if(DEBUG_ALLEGROHELPER) con->print(debug, "-- Allegro::init - al_init_image_addon() successful\n"); #endif // Initialization of Primitives Addon if (!al_init_primitives_addon()) { con->print(fatal, "Failed to initialize al_init_primitives_addon!\n"); return false; } #if(DEBUG_ALLEGROHELPER) con->print(debug, "-- Allegro::init - al_init_primitives_addon() successful\n"); #endif // Initialization of Font Addon al_init_font_addon(); if (!al_init_ttf_addon()) { con->print(fatal, "Failed to initialize al_init_font_addon() and al_init_ttf_addon!\n"); return false; } #if(DEBUG_ALLEGROHELPER) con->print(debug, "-- Allegro::init - al_init_font_addon() successful\n"); #endif if (!al_install_keyboard()) { fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; } if (!al_install_mouse()) { fprintf(stderr, "failed to initialize the mouse!\n"); return -1; } #if(DEBUG_ALLEGROHELPER) con->print(debug, "-- Allegro::init - al_install_keyboard() and al_install_mouse() successful\n"); #endif lastTime = al_get_time(); #if(DEBUG_ALLEGROHELPER) con->print(debug, "- Allegro::init - Success\n"); #endif return true; }
int main() { al_init(); al_install_mouse(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); ALLEGRO_DISPLAY *display; al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_OPENGL); al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE); display = al_create_display(800, 600); if(!display) { std::cout<<"Failed to create display"<<std::endl; return 0; } ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); al_register_event_source(event_queue, (ALLEGRO_EVENT_SOURCE *)display); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); if(!Init()) return 0; double last_time = al_current_time(); bool quit = false; while(1) { ALLEGRO_EVENT event; while (al_get_next_event(event_queue, &event)) { if (ALLEGRO_EVENT_KEY_DOWN == event.type) { if (ALLEGRO_KEY_ESCAPE == event.keyboard.keycode) { quit = true; } } if (ALLEGRO_EVENT_DISPLAY_CLOSE == event.type) { quit = true; } Event(event); } if (quit) break; double current_time = al_current_time(); double dt = current_time - last_time; last_time = current_time; Update(dt); al_clear_to_color(al_map_rgb(0, 0, 0)); Render(); al_flip_display(); al_rest(0.001); } al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }