int main(int argc, char **argv) { int num_touches = 0; TOUCH touches[MAX_TOUCHES]; ALLEGRO_DISPLAY *display; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_init_primitives_addon(); if (!al_install_touch_input()) { abort_example("Could not init touch input.\n"); } display = al_create_display(800, 600); if (!display) { abort_example("Error creating display\n"); } queue = al_create_event_queue(); al_register_event_source(queue, al_get_touch_input_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); while (true) { if (al_is_event_queue_empty(queue)) { al_clear_to_color(al_map_rgb(255, 255, 255)); draw_touches(num_touches, touches); al_flip_display(); } al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_TOUCH_BEGIN) { int i = num_touches; if (num_touches < MAX_TOUCHES) { touches[i].id = event.touch.id; touches[i].x = event.touch.x; touches[i].y = event.touch.y; num_touches++; } } else if (event.type == ALLEGRO_EVENT_TOUCH_END) { int i = find_index(event.touch.id, num_touches, touches); if (i >= 0 && i < num_touches) { touches[i] = touches[num_touches - 1]; num_touches--; } } else if (event.type == ALLEGRO_EVENT_TOUCH_MOVE) { int i = find_index(event.touch.id, num_touches, touches); if (i >= 0) { touches[i].x = event.touch.x; touches[i].y = event.touch.y; } } } return 0; }
int main(int argc, char **argv) { const char *filename = "../demos/cosmic_protector/data/sfx/title_music.ogg"; ALLEGRO_VOICE *voice; ALLEGRO_MIXER *mixer; ALLEGRO_AUDIO_STREAM *stream; if (argc > 1) { filename = argv[1]; } if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_init_primitives_addon(); al_init_image_addon(); al_init_acodec_addon(); al_install_keyboard(); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display.\n"); return 1; } dbuf = al_create_bitmap(640, 480); bmp = al_load_bitmap("data/mysha.pcx"); if (!bmp) { abort_example("Could not load data/mysha.pcx\n"); return 1; } if (!al_install_audio()) { abort_example("Could not init sound.\n"); return 1; } voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2); if (!voice) { abort_example("Could not create voice.\n"); return 1; } mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); if (!mixer) { abort_example("Could not create mixer.\n"); return 1; } if (!al_attach_mixer_to_voice(mixer, voice)) { abort_example("al_attach_mixer_to_voice failed.\n"); return 1; } stream = al_load_audio_stream(filename, 4, 2048); if (!stream) { abort_example("Could not load '%s'\n", filename); return 1; } al_set_audio_stream_playmode(stream, ALLEGRO_PLAYMODE_LOOP); al_attach_audio_stream_to_mixer(stream, mixer); al_set_mixer_postprocess_callback(mixer, update_meter, NULL); main_loop(); al_destroy_audio_stream(stream); al_destroy_mixer(mixer); al_destroy_voice(voice); al_uninstall_audio(); al_destroy_bitmap(dbuf); al_destroy_bitmap(bmp); return 0; }
void mapEditor::initialize() { this->display = al_create_display(1000,800); eventQueue = al_create_event_queue(); al_register_event_source(eventQueue,al_get_mouse_event_source()); al_register_event_source(eventQueue,al_get_keyboard_event_source()); }
bool Renderer::init(Minecraft *mc, const char *argv0) { NBT_Debug("begin"); al_set_org_name("mctools"); al_set_app_name("viewer"); if(!al_init()) { NBT_Debug("al_init failed???"); return false; } ALLEGRO_TIMER *tmr = nullptr; ALLEGRO_EVENT_QUEUE *queue = nullptr; ALLEGRO_DISPLAY *dpy = nullptr; ALLEGRO_BITMAP *bmp = nullptr; ALLEGRO_TRANSFORM *def_trans = nullptr; ALLEGRO_FONT *fnt = nullptr; if(!al_install_keyboard()) goto init_failed; if(!al_install_mouse()) goto init_failed; if(!al_init_primitives_addon()) goto init_failed; if(!al_init_image_addon()) goto init_failed; if(!al_init_font_addon()) goto init_failed; tmr = al_create_timer(1.0/60.0); if(!tmr) goto init_failed; queue = al_create_event_queue(); if(!queue) goto init_failed; // do display creation last so a display isn't created and instantly destroyed if any of the // preceeding initializations fail. al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_PROGRAMMABLE_PIPELINE | ALLEGRO_OPENGL_3_0); //al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_REQUIRE); //al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_REQUIRE); al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE); dpy = al_create_display(800, 600); if(!dpy) { NBT_Debug("display creation failed"); goto init_failed; } if(!al_get_opengl_extension_list()->ALLEGRO_GL_EXT_framebuffer_object) { NBT_Debug("FBO GL extension is missing. bail"); goto init_failed; } glGenVertexArrays(1, &vao_); glBindVertexArray(vao_); NBT_Debug("load shaders"); if(!loadShaders("shaders/default.vtx", "shaders/default.pxl")) { NBT_Debug("shader init failed"); goto init_failed; } NBT_Debug("load allegro shaders"); if(!loadAllegroShaders()) { NBT_Debug("allegro shader init failed"); goto init_failed; } glBindVertexArray(0); NBT_Debug("create resource manager"); resManager_ = new ResourceManager(this); if(!resManager_->init(mc, argv0)) { NBT_Debug("failed to init resource manager"); goto init_failed; } fnt = al_create_builtin_font(); if(!fnt) { NBT_Debug("failed to create builtin font"); goto init_failed; } al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_display_event_source(dpy)); al_register_event_source(queue, al_get_timer_event_source(tmr)); def_trans = al_get_projection_transform(dpy); al_copy_transform(&al_proj_transform_, def_trans); al_identity_transform(&camera_transform_); rx_look = 0.0; queue_ = queue; tmr_ = tmr; dpy_ = dpy; bmp_ = bmp; fnt_ = fnt; grab_mouse_ = false; // initial clear display // make things look purdy al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); NBT_Debug("end"); return true; init_failed: delete resManager_; resManager_ = nullptr; if(fnt) al_destroy_font(fnt); if(dpy) al_destroy_display(dpy); if(queue) al_destroy_event_queue(queue); al_uninstall_system(); NBT_Debug("end"); return false; }
int MonopolyGame::init() { // Now begin initializing the Allegro library. if(!al_init()) { fprintf(stderr, "Failed to initialize Allegro.\n"); return -1; } if(!al_install_keyboard()) { fprintf(stderr, "Failed to initialize the Keyboard!\n"); return -1; } m_alTimer = al_create_timer(1.0 / MAX_FPS); if(!m_alTimer) { fprintf(stderr, "Failed to create Timer!\n"); return -1; } m_alFrameTimer = al_create_timer(1.0 / MAX_FRAME_FPS); if(!m_alFrameTimer) { fprintf(stderr, "Failed to create Timer!\n"); al_destroy_timer(m_alTimer); return -1; } al_init_image_addon(); // Initialize the image addon. al_install_audio(); al_init_acodec_addon(); al_init_font_addon(); // Initialize the font addon. al_init_ttf_addon(); // Initialize the ttf (True Type Font) addon. // Setup the display device in windowed mode. al_set_new_display_flags(ALLEGRO_WINDOWED); m_alDisplayDevice = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT); if(!m_alDisplayDevice) { fprintf(stderr, "Failed to create Display: %i x %i\n", WINDOW_WIDTH, WINDOW_HEIGHT); al_destroy_timer(m_alTimer); al_destroy_timer(m_alFrameTimer); return -1; } // Create an event queue. This is where timed events, etc are pulled from. m_alEventQueue = al_create_event_queue(); if(!m_alEventQueue) { fprintf(stderr, "Failed to create Event Queue!\n"); al_destroy_display(m_alDisplayDevice); al_destroy_timer(m_alTimer); al_destroy_timer(m_alFrameTimer); return -1; } if( !al_reserve_samples( MAX_NUM_SAMPLES ) ) { fprintf(stderr, "Failed to reserve the required sound instances!\n"); al_destroy_display(m_alDisplayDevice); al_destroy_timer(m_alTimer); al_destroy_timer(m_alFrameTimer); return -1; } // Register an event source per input. al_register_event_source(m_alEventQueue, al_get_display_event_source(m_alDisplayDevice)); al_register_event_source(m_alEventQueue, al_get_timer_event_source(m_alTimer)); al_register_event_source(m_alEventQueue, al_get_timer_event_source(m_alFrameTimer)); al_register_event_source(m_alEventQueue, al_get_keyboard_event_source()); // Ensure the mouse cursor is not visible. al_hide_mouse_cursor(m_alDisplayDevice); // Set some default values for the camera. m_alCamera.cameraPosition[Positions::X_POS] = 0; m_alCamera.cameraPosition[Positions::Y_POS] = 0; return 0; }
int main(int, char**) { // Setup Allegro al_init(); al_install_keyboard(); al_install_mouse(); al_init_primitives_addon(); al_set_new_display_flags(ALLEGRO_RESIZABLE); ALLEGRO_DISPLAY* display = al_create_display(1280, 720); al_set_window_title(display, "ImGui Allegro 5 example"); ALLEGRO_EVENT_QUEUE* 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()); al_register_event_source(queue, al_get_mouse_event_source()); // Setup ImGui binding ImGui_ImplA5_Init(display); // Load Fonts // (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details) //ImGuiIO& io = ImGui::GetIO(); //io.Fonts->AddFontDefault(); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f); //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); bool show_test_window = true; bool show_another_window = false; ImVec4 clear_color = ImColor(114, 144, 154); // Main loop bool running = true; while (running) { ALLEGRO_EVENT ev; while (al_get_next_event(queue, &ev)) { ImGui_ImplA5_ProcessEvent(&ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) running = false; if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { ImGui_ImplA5_InvalidateDeviceObjects(); al_acknowledge_resize(display); Imgui_ImplA5_CreateDeviceObjects(); } } ImGui_ImplA5_NewFrame(); // 1. Show a simple window // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" { static float f; ImGui::Text("Hello, world!"); ImGui::SliderFloat("float", &f, 0.0f, 1.0f); ImGui::ColorEdit3("clear color", (float*)&clear_color); if (ImGui::Button("Test Window")) show_test_window ^= 1; if (ImGui::Button("Another Window")) show_another_window ^= 1; ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f/ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window, this time using an explicit Begin/End pair if (show_another_window) { ImGui::Begin("Another Window", &show_another_window); ImGui::Text("Hello from another window!"); ImGui::End(); } // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow() if (show_test_window) { ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver); ImGui::ShowTestWindow(&show_test_window); } // Rendering al_clear_to_color(al_map_rgba_f(clear_color.x, clear_color.y, clear_color.z, clear_color.w)); ImGui::Render(); al_flip_display(); } // Cleanup ImGui_ImplA5_Shutdown(); al_destroy_event_queue(queue); al_destroy_display(display); return 0; }
GAME * game_init () { if (!al_init ()) { fprintf (stderr, "Failed to initialize Allegro.\n"); return NULL; } if (!al_init_image_addon ()) { fprintf (stderr, "Failed to initialize image addon.\n"); return NULL; } if (!al_install_keyboard ()) { fprintf (stderr, "Failed to install keyboard.\n"); return NULL; } al_init_font_addon (); if (!al_init_ttf_addon ()) { fprintf (stderr, "Failed to initialize ttf addon.\n"); return NULL; } if (!al_init_primitives_addon ()) { fprintf (stderr, "Failed to initialize primitives addon.\n"); return NULL; } GAME *game = al_malloc (sizeof (GAME)); if (!game) return NULL; srand (time (NULL)); game->running = true; game->paused = false; game->fullscreen = 1; game->windowed = 1; game->rrate = 60; game->suggest_vsync = 1; game->force_vsync = 0; game->current_npc = NULL; game->screen = screen_new (); char *filename; const char *str; filename = get_resource_path_str ("data/game.ini"); ALLEGRO_CONFIG *game_config = al_load_config_file (filename); al_free (filename); str = al_get_config_value (game_config, "", "org"); al_set_org_name (str); str = al_get_config_value (game_config, "", "app"); al_set_app_name (str); ALLEGRO_PATH *settpath = al_get_standard_path (ALLEGRO_USER_SETTINGS_PATH); ALLEGRO_PATH *gcpath = al_clone_path (settpath); al_set_path_filename (gcpath, "general.ini"); const char * gcpath_str = al_path_cstr (gcpath, ALLEGRO_NATIVE_PATH_SEP); ALLEGRO_CONFIG *gconfig = al_load_config_file (gcpath_str); if (!gconfig) { gconfig = al_create_config (); al_make_directory (al_path_cstr (settpath, ALLEGRO_NATIVE_PATH_SEP)); set_config_i (gconfig, "display", "width", game->screen.width); set_config_i (gconfig, "display", "height", game->screen.height); set_config_i (gconfig, "display", "fullscreen", game->fullscreen); set_config_i (gconfig, "display", "windowed", game->windowed); set_config_i (gconfig, "display", "refreshrate", game->rrate); set_config_i (gconfig, "display", "suggest_vsync", game->suggest_vsync); set_config_i (gconfig, "display", "force_vsync", game->force_vsync); } else { get_config_i (gconfig, "display", "width", &game->screen.width); get_config_i (gconfig, "display", "height", &game->screen.height); get_config_i (gconfig, "display", "fullscreen", &game->fullscreen); get_config_i (gconfig, "display", "windowed", &game->windowed); get_config_i (gconfig, "display", "refreshrate", &game->rrate); get_config_i (gconfig, "display", "suggest_vsync", &game->suggest_vsync); get_config_i (gconfig, "display", "force_vsync", &game->force_vsync); } al_save_config_file (gcpath_str, gconfig); al_destroy_path (settpath); al_destroy_path (gcpath); al_destroy_config (gconfig); int flags = 0; if (game->fullscreen == game->windowed) flags |= ALLEGRO_FULLSCREEN_WINDOW; else if (game->fullscreen) flags |= ALLEGRO_FULLSCREEN; else flags |= ALLEGRO_WINDOWED; al_set_new_display_option (ALLEGRO_VSYNC, game->suggest_vsync, ALLEGRO_SUGGEST); al_set_new_display_option (ALLEGRO_DEPTH_SIZE, 8, ALLEGRO_SUGGEST); al_set_new_display_flags (flags); al_set_new_display_refresh_rate (game->rrate); game->display = al_create_display (game->screen.width, game->screen.height); if (!game->display) { fprintf (stderr, "Failed to create display.\n"); al_free (game); return NULL; } al_set_new_bitmap_flags (ALLEGRO_VIDEO_BITMAP); game->timer = al_create_timer (1.0 / FPS); if (!game->timer) { fprintf (stderr, "Failed to create timer.\n"); al_free (game); return NULL; } game->screen.width = al_get_display_width (game->display); game->screen.height = al_get_display_height (game->display); screen_update_size (&game->screen, game->screen.width, game->screen.height); game->rrate = al_get_display_refresh_rate (game->display); game->event_queue = al_create_event_queue (); if (!game->event_queue) { fprintf (stderr, "Failed to create event queue.\n"); al_free (game); return NULL; } al_register_event_source (game->event_queue, al_get_display_event_source (game->display)); al_register_event_source (game->event_queue, al_get_timer_event_source (game->timer)); al_set_render_state (ALLEGRO_ALPHA_FUNCTION, ALLEGRO_RENDER_EQUAL); al_set_render_state (ALLEGRO_ALPHA_TEST_VALUE, 1); filename = get_resource_path_str ("data/sprites.ini"); game->sprites = sprite_load_sprites (filename); al_free (filename); filename = get_resource_path_str ("data/scenes.ini"); game->scenes = scene_load_file (filename); scene_load_scenes (game->scenes, game->sprites); al_free (filename); str = al_get_config_value (game_config, "", "scene"); game->current_scene = scene_get (game->scenes, str); str = al_get_config_value (game_config, "", "actor"); game->current_actor = sprite_new_actor (game->sprites, str); str = al_get_config_value (game_config, "", "portal"); SCENE_PORTAL *portal = scene_get_portal (game->scenes, str); al_destroy_config (game_config); filename = get_resource_path_str ("data/ui.ini"); game->ui = ui_load_file (filename); al_free (filename); sprite_center (game->current_actor, &portal->position); screen_center (&game->screen, portal->position, game->current_scene->map); return game; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *bitmap; ALLEGRO_EVENT_QUEUE *events; ALLEGRO_EVENT event; bool down = false; int down_x = 0, down_y = 0; ALLEGRO_TIMER *timer; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_install_mouse(); al_install_keyboard(); al_init_image_addon(); init_platform_specific(); al_set_new_display_flags(ALLEGRO_FRAMELESS); display = al_create_display(300, 200); if (!display) { abort_example("Error creating display\n"); } bitmap = al_load_bitmap("data/fakeamp.bmp"); if (!bitmap) { abort_example("Error loading fakeamp.bmp\n"); } timer = al_create_timer(1.0f/30.0f); events = al_create_event_queue(); al_register_event_source(events, al_get_mouse_event_source()); al_register_event_source(events, al_get_keyboard_event_source()); al_register_event_source(events, al_get_display_event_source(display)); al_register_event_source(events, al_get_timer_event_source(timer)); al_start_timer(timer); for (;;) { al_wait_for_event(events, &event); if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { if (event.mouse.button == 1 && event.mouse.x) { down = true; down_x = event.mouse.x; down_y = event.mouse.y; } if (event.mouse.button == 2) { al_set_display_flag(display, ALLEGRO_FRAMELESS, !(al_get_display_flags(display) & ALLEGRO_FRAMELESS)); } } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { if (event.mouse.button == 1) { down = false; } } else if (event.type == ALLEGRO_EVENT_MOUSE_AXES) { if (down) { int cx, cy; if (al_get_mouse_cursor_position(&cx, &cy)) { al_set_window_position(display, cx - down_x, cy - down_y); } } } else if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.type == ALLEGRO_EVENT_TIMER) { al_draw_bitmap(bitmap, 0, 0, 0); al_flip_display(); } } al_destroy_timer(timer); al_destroy_event_queue(events); al_destroy_display(display); return 0; }
extern int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_FONT *font; bool done = false; bool redraw = true; use_constraints = true; (void)argc; (void)argv; if (!al_init()) { abort_example("Failed to init Allegro.\n"); } if (!al_init_primitives_addon()) { abort_example("Failed to init primitives addon.\n"); } if (!al_init_image_addon()) { abort_example("Failed to init image addon.\n"); } if (!al_init_font_addon()) { abort_example("Failed to init font addon.\n"); } if (!al_init_native_dialog_addon()) { abort_example("Failed to init native dialog addon.\n"); } al_set_new_display_flags(ALLEGRO_WINDOWED #if defined(USE_GTK) && !defined(_WIN32) | ALLEGRO_GTK_TOPLEVEL #endif | ALLEGRO_RESIZABLE | ALLEGRO_MAXIMIZED | ALLEGRO_GENERATE_EXPOSE_EVENTS); /* creating really small display */ display = al_create_display(DISPLAY_W / 3, DISPLAY_H / 3); if (!display) { abort_example("Error creating display.\n"); } /* set lower limits for constraints only */ if(!al_set_window_constraints(display, DISPLAY_W / 2, DISPLAY_H / 2, 0, 0)) abort_example("Unable to set window constraints.\n"); al_apply_window_constraints(display, use_constraints); if (!al_install_keyboard()) { abort_example("Error installing keyboard.\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)); ALLEGRO_COLOR color_1 = al_map_rgb(255, 127, 0); ALLEGRO_COLOR color_2 = al_map_rgb(0, 255, 0); ALLEGRO_COLOR *color = &color_1; ALLEGRO_COLOR color_text = al_map_rgb(0, 0, 0); font = al_create_builtin_font(); while (!done) { ALLEGRO_EVENT event; if (redraw && al_is_event_queue_empty(queue)) { redraw = false; int x2 = al_get_display_width(display) - 10; int y2 = al_get_display_height(display) - 10; al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_filled_rectangle(10, 10, x2, y2, *color); draw_information(display, font, color_text); al_flip_display(); } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; break; case ALLEGRO_EVENT_KEY_UP: if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) { redraw = true; if (color == &color_1) { if (!al_set_window_constraints(display, 0, 0, DISPLAY_W, DISPLAY_H)) { abort_example("Unable to set window constraints.\n"); } color = &color_2; } else { if (!al_set_window_constraints(display, DISPLAY_W / 2, DISPLAY_H / 2, 0, 0)) { abort_example("Unable to set window constraints.\n"); } color = &color_1; } al_apply_window_constraints(display, use_constraints); } else if (event.keyboard.keycode == ALLEGRO_KEY_ENTER) { redraw = true; use_constraints = !use_constraints; al_apply_window_constraints(display, use_constraints); } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); redraw = true; break; case ALLEGRO_EVENT_DISPLAY_EXPOSE: redraw = true; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; } /* switch (event.type) { */ } al_destroy_font(font); return 0; }
// METODO PARA CRIAR O DISPLAY void GerenciadorGrafico::CriaDisplay() { display = al_create_display(LarguraTela, AlturaTela); }
int main(void) { bool done =false; bool redraw = true; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_TIMER *timer =NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; SpaceShip ship; Bullet bullet[num_bullets]; Commet commet[num_commets]; if(!al_init()) { fprintf(stderr,"allegro is not intialized"); return -1; } display = al_create_display(SCREEN_W,SCREEN_H); if(!display) { fprintf(stderr,"failed to intialize the display"); return -1; } timer = al_create_timer(1.0/FPS); if(!timer) { fprintf(stderr,"failed to intialize timer"); al_destroy_display(display); return -1; } event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr,"failed to create event queue"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_install_keyboard(); al_register_event_source(event_queue,al_get_timer_event_source(timer)); al_register_event_source(event_queue,al_get_keyboard_event_source()); al_register_event_source(event_queue,al_get_display_event_source(display)); init_ship(&ship); init_bullet(&bullet); init_commet(&commet); al_start_timer(timer); while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue,&ev); if(ev.type == ALLEGRO_EVENT_TIMER) { if(keys[KEY_UP]) { move_ship_up(&ship); } if(keys[KEY_DOWN]) { move_ship_down(&ship); }if(keys[KEY_LEFT]) { move_ship_left(&ship); } if(keys[KEY_RIGHT]) { move_ship_right(&ship); } if(keys[KEY_SPACEBAR]) { fire_bullet(&bullet,&ship); } if(!GameOver) { update_bullet(&bullet); update_commet(&commet); bullet_collision(&bullet,&commet); } redraw = true; } 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_UP: { keys[KEY_UP]=true; break; } case ALLEGRO_KEY_DOWN: { keys[KEY_DOWN]=true; break; } case ALLEGRO_KEY_LEFT: { keys[KEY_LEFT]=true; break; } case ALLEGRO_KEY_RIGHT : { keys[KEY_RIGHT]=true; break; } case ALLEGRO_KEY_SPACE: { keys[KEY_SPACEBAR] = true; break; } } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: { keys[KEY_UP]=false; break; } case ALLEGRO_KEY_DOWN: { keys[KEY_DOWN]=false; break; } case ALLEGRO_KEY_LEFT: { keys[KEY_LEFT]=false; break; } case ALLEGRO_KEY_RIGHT : { keys[KEY_RIGHT]=false; break; } case ALLEGRO_KEY_SPACE: { keys[KEY_SPACEBAR]=false; break; } } } if(redraw && al_event_queue_is_empty(event_queue)) { redraw = false ; al_clear_to_color(al_map_rgb(255,100,100)); draw_ship(&ship); draw_bullet(bullet); draw_commet(commet); al_flip_display(); al_clear_to_color(al_map_rgb(255,100,100)); } } al_destroy_display(display); al_destroy_timer(timer); al_destroy_event_queue(event_queue); return 0; }
int main(void) { ALLEGRO_TRANSFORM trans; ALLEGRO_EVENT event; int bps = 4; bool redraw = false; unsigned int last_timer = 0; if (!al_init()) { abort_example("Could not init Allegro.\n"); } open_log(); al_install_keyboard(); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display\n"); } font = al_create_builtin_font(); if (!font) { abort_example("Could not create font\n"); } if (!al_install_audio()) { abort_example("Could not init sound\n"); } if (!al_reserve_samples(RESERVED_SAMPLES)) { abort_example("Could not set up voice and mixer\n"); } ping = generate_ping(); if (!ping) { abort_example("Could not generate sample\n"); } timer = al_create_timer(1.0 / bps); al_set_timer_count(timer, -1); event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_identity_transform(&trans); al_scale_transform(&trans, 16.0, 16.0); al_use_transform(&trans); al_start_timer(timer); while (true) { al_wait_for_event(event_queue, &event); if (event.type == ALLEGRO_EVENT_TIMER) { const float speed = pow(21.0/20.0, (event.timer.count % PERIOD)); if (!al_play_sample(ping, 1.0, 0.0, speed, ALLEGRO_PLAYMODE_ONCE, NULL)) { log_printf("Not enough reserved samples.\n"); } redraw = true; last_timer = event.timer.count; } else if (event.type == ALLEGRO_EVENT_KEY_CHAR) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } if (event.keyboard.unichar == '+' || event.keyboard.unichar == '=') { if (bps < 32) { bps++; al_set_timer_speed(timer, 1.0 / bps); } } else if (event.keyboard.unichar == '-') { if (bps > 1) { bps--; al_set_timer_speed(timer, 1.0 / bps); } } } if (redraw && al_is_event_queue_empty(event_queue)) { ALLEGRO_COLOR c; if (last_timer % PERIOD == 0) c = al_map_rgb_f(1, 1, 1); else c = al_map_rgb_f(0.5, 0.5, 1.0); al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_textf(font, c, 640/32, 480/32 - 4, ALLEGRO_ALIGN_CENTRE, "%u", last_timer); al_flip_display(); } } close_log(false); return 0; }
int main(int argc, char** argv) { int defw = 640; int defh = 480; if (argc == 3) { /*the program was run using 3 arguments argv[0] = name of the executable file argv[1] = screen width (as a string) argv[2] = screen height (as a string) */ defw = atoi(argv[1]); defh = atoi(argv[2]); } if (!al_init()) return 1; //exit if can't initialize allegro //initialize most stuff al_install_keyboard(); al_init_primitives_addon(); //run in a window al_set_new_display_flags(ALLEGRO_WINDOWED); //initialize our display and event queue ALLEGRO_DISPLAY* display = al_create_display(defw, defh); ALLEGRO_EVENT_QUEUE* eventq = al_create_event_queue(); //this timer will tick once per frame ALLEGRO_TIMER* framet = al_create_timer(1.0 / 30.0); //30 frames per second al_start_timer(framet); //register the event sources so they send events to our queue al_register_event_source(eventq, al_get_display_event_source(display)); //display/window al_register_event_source(eventq, al_get_keyboard_event_source()); //keyboard al_register_event_source(eventq, al_get_timer_event_source(framet)); //fps timer //this box is our play field (covers the whole screen) CBox fieldbox(0, 0, defw, defh); //we setup the ball at the center of the screen with white color CBall ball(CBox(defw / 2 - 10, defh / 2 - 10, 20, 20), al_map_rgb(255, 255, 255)); //we tell it to move to the left ball.setXYMovement(-5.0, 0.0); //we initialize our both players in an array CPlayer players[2] = { //red player on the left CPlayer(CBox(10, defh / 2 - 80 / 2, 20, 80), al_map_rgb(255, 0, 0)), //blue player on the right CPlayer(CBox(defw - 10 - 20, defh / 2 - 80 / 2, 20, 80), al_map_rgb(0, 0, 255)), }; /*when this variable is set to true the program will quit the main loop and free the allocated resources before quitting */ bool exit = false; while (!exit) { al_wait_for_event(eventq, NULL); ALLEGRO_EVENT ev; while (al_get_next_event(eventq, &ev)) { if (ev.type == ALLEGRO_EVENT_TIMER) { if (ev.timer.source == framet) { //fill the screen with black al_clear_to_color(al_map_rgb(0, 0, 0)); //move and draw our two players for (int i = 0; i<2; i++) { players[i].move(fieldbox); players[i].draw(); } //move, collide and draw the ball switch (ball.move(fieldbox, players)) { case 0: break; case 1: players[0].setScore(players[0].getScore() + 1); ball.setXYMovement(5.0, 0.0); break; case 2: players[1].setScore(players[1].getScore() + 1); ball.setXYMovement(-5.0, 0.0); break; } ball.draw(); //show what we've drawn al_flip_display(); } } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { //quit if the user tries to close the window if (ev.display.source == display) exit = true; } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { //handle key presses switch (ev.keyboard.keycode) { case ALLEGRO_KEY_W: players[0].setYMovement(-3.0); break; case ALLEGRO_KEY_S: players[0].setYMovement(3.0); break; case ALLEGRO_KEY_UP: players[1].setYMovement(-3.0); break; case ALLEGRO_KEY_DOWN: players[1].setYMovement(3.0); break; case ALLEGRO_KEY_ESCAPE: exit = true; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: exit = true; break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { int code = ev.keyboard.keycode; /*avoid clumsy movement making sure the released key corresponds with the moving direction*/ if (code == ALLEGRO_KEY_W && players[0].getYMovement() < 0) players[0].setYMovement(0.0); else if (code == ALLEGRO_KEY_S && players[0].getYMovement() > 0) players[0].setYMovement(0.0); else if (code == ALLEGRO_KEY_UP && players[1].getYMovement() < 0) players[1].setYMovement(0.0); else if (code == ALLEGRO_KEY_DOWN && players[1].getYMovement() > 0) players[1].setYMovement(0.0); } } } al_destroy_event_queue(eventq); al_destroy_timer(framet); al_destroy_display(display); }
int main(int argc, const char *argv[]) { ALLEGRO_SAMPLE *sample_data[MAX_SAMPLE_DATA] = {NULL}; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue; ALLEGRO_EVENT event; int i; if (!al_init()) { abort_example("Could not init Allegro.\n"); } open_log(); if (argc < 2) { log_printf("This example needs to be run from the command line.\nUsage: %s {audio_files}\n", argv[0]); goto done; } argc--; argv++; al_install_keyboard(); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display\n"); } event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_init_acodec_addon(); Restart: if (!al_install_audio()) { abort_example("Could not init sound!\n"); } if (!al_reserve_samples(RESERVED_SAMPLES)) { abort_example("Could not set up voice and mixer.\n"); } memset(sample_data, 0, sizeof(sample_data)); for (i = 0; i < argc && i < MAX_SAMPLE_DATA; i++) { const char *filename = argv[i]; /* Load the entire sound file from disk. */ sample_data[i] = al_load_sample(argv[i]); if (!sample_data[i]) { log_printf("Could not load sample from '%s'!\n", filename); continue; } } log_printf("Press digits to play sounds, space to stop sounds, " "Escape to quit.\n"); while (true) { al_wait_for_event(event_queue, &event); if (event.type == ALLEGRO_EVENT_KEY_CHAR) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } if (event.keyboard.unichar == ' ') { al_stop_samples(); } if (event.keyboard.unichar >= '0' && event.keyboard.unichar <= '9') { i = (event.keyboard.unichar - '0' + 9) % 10; if (sample_data[i]) { log_printf("Playing %d\n",i); if (!al_play_sample(sample_data[i], 1.0, 0.5, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL)) { log_printf( "al_play_sample_data failed, perhaps too many sounds\n"); } } } /* Hidden feature: restart audio subsystem. * For debugging race conditions on shutting down the audio. */ if (event.keyboard.unichar == 'r') { al_uninstall_audio(); goto Restart; } } } /* Sample data and other objects will be automatically freed. */ al_uninstall_audio(); done: al_destroy_display(display); close_log(true); return 0; }
int main() { bool quit = false; int gamestate = 0; srand(time(NULL)); int change_bkg = 0; int stopwatch = 120; bool iddle = false; int bulletID=0; int bulletCount=0; ALLEGRO_DISPLAY* display; ALLEGRO_TIMER* timer_0p2; ALLEGRO_TIMER* timer_1; ALLEGRO_TIMER* timer_60; ALLEGRO_EVENT_QUEUE* event_queue; ALLEGRO_EVENT ev; ALLEGRO_BITMAP* img_home_screen; ALLEGRO_BITMAP* img_dica_h1n1; ALLEGRO_BITMAP* img_background0; ALLEGRO_BITMAP* img_game_over; ALLEGRO_BITMAP* img_you_win; ALLEGRO_BITMAP* img_heart; ALLEGRO_BITMAP* img_medal; ALLEGRO_BITMAP* img_clock; ALLEGRO_BITMAP* img_block1; ALLEGRO_BITMAP* img_block2; ALLEGRO_BITMAP* img_player_walking; ALLEGRO_BITMAP* img_player_walking_shoot; ALLEGRO_BITMAP* img_player_immobile; ALLEGRO_BITMAP* img_player_immobile_shoot; ALLEGRO_BITMAP* img_player_jump; ALLEGRO_BITMAP* img_player_jump_shoot; ALLEGRO_BITMAP* img_player_bullet; ALLEGRO_BITMAP* img_enemy1; ALLEGRO_BITMAP* img_boss1; ALLEGRO_BITMAP* img_enemy_bullet; ALLEGRO_SAMPLE* spl_theme; ALLEGRO_SAMPLE* spl_playerShoot; ALLEGRO_SAMPLE* spl_mlk; ALLEGRO_SAMPLE_INSTANCE* instance_theme; ALLEGRO_SAMPLE_INSTANCE* instance_playerShoot; ALLEGRO_SAMPLE_INSTANCE* instance_mlk; ALLEGRO_FONT* fonte16; /* Estruturas */ s_object player; s_object block[LINHA_MAX][COLUNA_MAX]; s_object enemy1[LINHA_MAX][COLUNA_MAX]; for (i=0; i<LINHA_MAX; i++) { for(j=0; j<COLUNA_MAX; j++) { /* Cria o player */ if(mapa[i][j] == 1) { player.y = i*16 - 24; player.x = j*64 + 24; player.speed = 3; player.direction = 1; player.live = true; player.life = 100; block[i][j].live = false; } /* Cria os Blocos */ if(mapa[i][j] == 2) { block[i][j].y = i*16; block[i][j].x = j*64; block[i][j].live = true; } if(mapa[i][j] == 3) { block[i][j].y = i*16; block[i][j].x = j*64; block[i][j].live = true; } if(mapa[i][j] == 4) { block[i][j].y = i*16; block[i][j].x = j*64; block[i][j].live = true; } if(mapa[i][j] == 5) { block[i][j].y = i*16; block[i][j].x = j*64; block[i][j].live = false; } /* Cria os Inimigos */ if(mapa[i][j] == 6) { enemy1[i][j].y = i*16 - 24; enemy1[i][j].x = j*64; enemy1[i][j].speed = 2; enemy1[i][j].direction = -1; enemy1[i][j].life = 3; enemy1[i][j].live = true; block[i][j].live = false; } if(mapa[i][j] == 7 || mapa[i][j] == 8) { enemy1[i][j].y = i*16 - 24; enemy1[i][j].x = j*64; enemy1[i][j].speed = 2; enemy1[i][j].direction = 1; enemy1[i][j].life = 3; enemy1[i][j].live = false; block[i][j].live = false; } if(mapa[i][j] == 9) { enemy1[i][j].y = i*16 - 24; enemy1[i][j].x = j*64; enemy1[i][j].speed = 2; enemy1[i][j].direction = -1; enemy1[i][j].life = 25; enemy1[i][j].live = false; block[i][j].live = false; } } } s_bullet playerBullet[NUM_BULLET]; s_bullet enemyBullet[NUM_BULLET]; for(i=0; i<NUM_BULLET; i++) { playerBullet[i].x = 0; playerBullet[i].y = 0; playerBullet[i].speed = 5; playerBullet[i].direction = 1; playerBullet[i].live = false; enemyBullet[i].x = 0; enemyBullet[i].y = 0; enemyBullet[i].speed = 5; enemyBullet[i].direction = 0; enemyBullet[i].live = false; } s_animation walking; walking.maxFrame = 8; walking.frameDelay = 5; walking.frameCount = 0; walking.curFrame = 0; walking.frameHeight = 40; walking.frameWidth = 40; s_animation jumping; jumping.maxFrame = 7; jumping.frameDelay = 5; jumping.frameCount = 0; jumping.curFrame = 0; jumping.frameHeight = 52; jumping.frameWidth = 40; s_animation immobile; immobile.maxFrame = 7; immobile.frameDelay = 15; immobile.frameCount = 0; immobile.curFrame = 0; immobile.frameHeight = 40; immobile.frameWidth = 40; s_animation anim_enemy1; anim_enemy1.maxFrame = 3; anim_enemy1.frameDelay = 15; anim_enemy1.frameCount = 0; anim_enemy1.curFrame = 0; anim_enemy1.frameHeight = 40; anim_enemy1.frameWidth = 40; /* Faz com que as teclas comecem em false */ for(i=0; i<KEY_MAX; i++) { keys[i] = false; } /* Carrega as configuracoes (teclado, audio, etc) */ al_init(); al_install_keyboard(); al_init_image_addon(); al_install_audio(); al_init_acodec_addon(); al_init_font_addon(); al_init_ttf_addon(); /* Erros ao criar algo */ display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { printf("Erro ao criar o display"); exit(-1); } timer_0p2 = al_create_timer(5.0); timer_1 = al_create_timer(1.0); timer_60 = al_create_timer(1/60.0); if(!timer_0p2) { printf("Erro ao criar o timer de 0.2 FPS"); exit(-1); } if(!timer_1) { printf("Erro ao criar o timer de 1 FPS"); exit(-1); } if(!timer_60) { printf("Erro ao criar o timer de 60 FPS"); exit(-1); } event_queue = al_create_event_queue(); if(!event_queue) { printf("Erro ao criar o event_queue"); exit(-1); } /* Carregando as Imagens */ img_home_screen = al_load_bitmap("Sprites/Background/home_screen.png"); img_dica_h1n1 = al_load_bitmap("Sprites/Background/dica_h1n1.png"); img_background0 = al_load_bitmap("Sprites/Background/background_h1n1.png"); img_you_win = al_load_bitmap("Sprites/Background/you_win.png"); img_game_over = al_load_bitmap("Sprites/Background/game_over.png"); img_heart = al_load_bitmap("Sprites/heart.png"); img_medal = al_load_bitmap("Sprites/medal.png"); img_clock = al_load_bitmap("Sprites/clock.png"); img_block1 = al_load_bitmap("Sprites/block1.png"); img_block2 = al_load_bitmap("Sprites/block2.png"); img_player_walking = al_load_bitmap("Sprites/Player/player_walking.png"); img_player_walking_shoot = al_load_bitmap("Sprites/Player/player_walking_shoot.png"); img_player_immobile = al_load_bitmap("Sprites/Player/player_immobile.png"); img_player_immobile_shoot = al_load_bitmap("Sprites/Player/player_immobile_shoot.png"); img_player_jump = al_load_bitmap("Sprites/Player/player_jump.png"); img_player_jump_shoot = al_load_bitmap("Sprites/Player/player_jump_shoot.png"); img_player_bullet = al_load_bitmap("Sprites/Player/player_bullet.png"); img_enemy1 = al_load_bitmap("Sprites/Enemies/enemy_h1n1.png"); img_boss1 = al_load_bitmap("Sprites/Enemies/boss_h1n1.png"); img_enemy_bullet = al_load_bitmap("Sprites/Enemies/enemy_bullet.png"); /* Carregando os Samples */ al_reserve_samples(10); spl_theme = al_load_sample("Sounds/theme.wav"); spl_playerShoot = al_load_sample("Sounds/shoot.wav"); spl_mlk = al_load_sample("Sounds/mlk.wav"); instance_theme = al_create_sample_instance(spl_theme); instance_playerShoot = al_create_sample_instance(spl_playerShoot); instance_mlk = al_create_sample_instance(spl_mlk); al_set_sample_instance_gain(instance_playerShoot, 0.5); al_attach_sample_instance_to_mixer(instance_theme, al_get_default_mixer()); al_attach_sample_instance_to_mixer(instance_playerShoot, al_get_default_mixer()); al_attach_sample_instance_to_mixer(instance_mlk, al_get_default_mixer()); /* Registra os Eventos */ al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer_0p2)); al_register_event_source(event_queue, al_get_timer_event_source(timer_1)); al_register_event_source(event_queue, al_get_timer_event_source(timer_60)); al_register_event_source(event_queue, al_get_keyboard_event_source()); /* Carregando os timers */ al_start_timer(timer_0p2); al_start_timer(timer_1); al_start_timer(timer_60); /* Carregando a fonte */ fonte16 = al_load_ttf_font("Joystix.TTF", 16, 0); if (!fonte16) { printf("Erro ao carregar Joystix.TTF\n"); exit(1); } while(!quit) { switch(gamestate) { case 0: al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) /* Faz com que o jogo feche ao clicar no botao "X" do display */ { quit = true; } if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) /* Faz com que o jogo feche ao pressionar esc */ { quit = true; } if(ev.keyboard.keycode == ALLEGRO_KEY_SPACE) /* Faz com que o jogo inicie ao pressionar space */ { change_bkg++; if(change_bkg >= 2) { gamestate = 1; } } } if(ev.type == ALLEGRO_EVENT_TIMER) /* Mostrar mensagens na tela */ { if(ev.timer.source == timer_60) { al_clear_to_color(al_map_rgb(0,0,0)); if(change_bkg == 0) { al_draw_bitmap(img_home_screen, 0, 0, 0); } if(change_bkg == 1) { al_draw_bitmap(img_dica_h1n1, 0, 0, 0); if(++anim_enemy1.frameCount >= anim_enemy1.frameDelay) { if(++anim_enemy1.curFrame >= anim_enemy1.maxFrame) { anim_enemy1.curFrame = 0; } anim_enemy1.frameCount = 0; } al_draw_bitmap_region(img_enemy1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, 330, 320, 0); al_draw_bitmap_region(img_boss1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, 450, 320, 0); } al_flip_display(); } } break; case 1: if(force>= -7.5) { force-=0.5; /* Queda */ } /* Toca a música de fundo */ if(!al_get_sample_instance_playing(instance_theme) && !al_get_sample_instance_playing(instance_mlk)) { al_play_sample_instance(instance_theme); } /* Fechar o display */ al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { quit = true; } /* Evento de quando a tecla eh pressionada */ if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: quit = true; break; case ALLEGRO_KEY_SPACE: keys[KEY_SPACE]=true; break; case ALLEGRO_KEY_UP: keys[KEY_UP] = true; if(jump == false) { jump = true; force = gravity; } break; case ALLEGRO_KEY_LEFT: keys[KEY_RIGHT]=false; keys[KEY_LEFT]=true; break; case ALLEGRO_KEY_RIGHT: keys[KEY_LEFT]=false; keys[KEY_RIGHT]=true; break; case ALLEGRO_KEY_F1: if(!al_get_sample_instance_playing(instance_mlk)) { al_play_sample_instance(instance_mlk); al_stop_sample_instance(instance_theme); } break; case ALLEGRO_KEY_M: if(al_get_sample_instance_playing(instance_mlk)) { al_stop_sample_instance(instance_mlk); } break; } } /* Evento de quando a tecla eh solta */ if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_SPACE: keys[KEY_SPACE]=false; break; case ALLEGRO_KEY_UP: keys[KEY_UP] = false; break; case ALLEGRO_KEY_LEFT: keys[KEY_LEFT]=false; break; case ALLEGRO_KEY_RIGHT: keys[KEY_RIGHT]=false; break; } } if(ev.type == ALLEGRO_EVENT_TIMER) { if(ev.timer.source == timer_0p2) { if((iddle == false) && (keys[KEY_RIGHT] == false) && (keys[KEY_LEFT] == false) && (jump == false)) { iddle = true; } } if(ev.timer.source == timer_1) { stopwatch--; } if(ev.timer.source == timer_60) { /* Posicionamento do player*/ player.y-=force; if(keys[KEY_RIGHT]) { player.direction = 1; player.x+=player.speed; } if(keys[KEY_LEFT]) { player.direction = -1; player.x-=player.speed; } if(keys[KEY_SPACE]) { for(i=0; i<NUM_BULLET; i++) { if(!al_get_sample_instance_playing(instance_playerShoot)) { playerShoot(&player, &playerBullet[i], instance_playerShoot); } } } /*Posicionamento do Inimigo */ for (i=0; i<LINHA_MAX; i++) { for(j=0; j<COLUNA_MAX; j++) { if(player.x > enemy1[i][j].x + 40) { enemy1[i][j].direction = 1; } else if(player.x + 40 <= enemy1[i][j].x) { enemy1[i][j].direction = -1; } } } /* ~~Posicionamento do projetil~~ */ /* Chance do Inimigo Atirar */ chance_enemy_shoot = rand() % 40; for (i=0; i<LINHA_MAX; i++) { for(j=0; j<COLUNA_MAX; j++) { enemyShoot(&player, &enemy1[i][j], enemyBullet, &bulletID, &bulletCount); } } for(i=0; i<NUM_BULLET; i++) { if(!playerBullet[i].live) { playerBullet[i].direction = player.direction; } if(playerBullet[i].live) { if(playerBullet[i].direction == -1) { playerBullet[i].x-=playerBullet[i].speed; } else if(playerBullet[i].direction == 1) { playerBullet[i].x+=playerBullet[i].speed; } } if(enemyBullet[i].live) { if(enemyBullet[i].direction == -1) { enemyBullet[i].x-=enemyBullet[i].speed; } if(enemyBullet[i].direction == 1) { enemyBullet[i].x+=enemyBullet[i].speed; } } } /* Prende a Camera no Personagem */ cameraX = player.x-(SCREEN_W/2); cameraY = player.y-(SCREEN_H/2); /* Fazer com que a camera nao passe dos limites do mapa */ if (cameraX < 0) cameraX = 0; if (cameraY < 0) cameraY = 0; if (cameraX > WORLD_W - SCREEN_W) cameraX = WORLD_W - SCREEN_W; if (cameraY > WORLD_H - SCREEN_H) cameraY = WORLD_H - SCREEN_H; /* Colisoes + check trap */ for (i = 0; i<LINHA_MAX; i++) { for(j = 0; j<COLUNA_MAX; j++) { for(k=0; k<NUM_BULLET; k++) { collision_bullet_player(&player, &enemyBullet[k], img_enemy_bullet, &bulletCount, 40, 40); } if(block[i][j].live == true) { if(mapa[i][j] == 2) { collision_player_tiles(&player, &block[i][j], &jumping, img_block2); } if(mapa[i][j] == 3) { collision_player_tiles(&player, &block[i][j], &jumping, img_block2); } if(mapa[i][j] == 4) { check_trap(&player, &block[i][j], &enemy1[i][j], 4); collision_player_tiles(&player, &block[i][j], &jumping, img_block2); } if(mapa[i][j] == 5) { collision_player_tiles(&player, &block[i][j], &jumping, img_block2); } for(k=0; k<NUM_BULLET; k++) { collision_bullet_tiles(&playerBullet[k], &block[i][j], img_player_bullet, img_block2, 0, &bulletCount); collision_bullet_tiles(&enemyBullet[k], &block[i][j], img_player_bullet, img_block2, 1, &bulletCount); } } if(block[i][j].live == false) { if(mapa[i][j] == 5) { check_trap(&player, &block[i][j], &enemy1[i][j], 5); } if(mapa[i][j] == 6) { collision_player_enemy(&player, &enemy1[i][j], 40, 40); for(k=0; k<NUM_BULLET; k++) { collision_bullet_enemy(&playerBullet[k], &enemy1[i][j], img_player_bullet, 40, 40); } } if(mapa[i][j] == 7) { check_trap(&player, &block[i][j], &enemy1[i][j], 7); collision_player_enemy(&player, &enemy1[i][j], 40, 40); for(k=0; k<NUM_BULLET; k++) { collision_bullet_enemy(&playerBullet[k], &enemy1[i][j], img_player_bullet, 40, 40); } } if(mapa[i][j] == 8) { check_trap(&player, &block[i][j], &enemy1[i][j], 8); collision_player_enemy(&player, &enemy1[i][j], 40, 40); for(k=0; k<NUM_BULLET; k++) { collision_bullet_enemy(&playerBullet[k], &enemy1[i][j], img_player_bullet, 40, 40); } } if(mapa[i][j] == 9) { check_trap(&player, &block[i][j], &enemy1[i][j], 9); collision_player_enemy(&player, &enemy1[i][j], 40, 40); for(k=0; k<NUM_BULLET; k++) { collision_bullet_enemy(&playerBullet[k], &enemy1[i][j], img_player_bullet, 40, 40); } } } } } collision_player_wall(&player, &jumping, img_block1); /* ~~Desenha o Background~~ */ al_draw_bitmap(img_background0, 0 - cameraX, 0 - cameraY, 0); /* ~~Animação dos inimigos~~ */ if(++anim_enemy1.frameCount >= anim_enemy1.frameDelay) { if(++anim_enemy1.curFrame >= anim_enemy1.maxFrame) { anim_enemy1.curFrame = 0; } anim_enemy1.frameCount = 0; } /* ~~Desenha os Blocos/Inimigos~~ */ for (i = 0; i<LINHA_MAX; i++) { for(j = 0; j<COLUNA_MAX; j++) { if(mapa[i][j] == 2 && block[i][j].live == true) { al_draw_bitmap(img_block1, block[i][j].x - cameraX, block[i][j].y - cameraY, 0); } if(mapa[i][j] == 3 && block[i][j].live == true) { al_draw_bitmap(img_block2, block[i][j].x - cameraX, block[i][j].y - cameraY, 0); } if(mapa[i][j] == 4 && block[i][j].live == true) { al_draw_bitmap(img_block2, block[i][j].x - cameraX, block[i][j].y - cameraY, 0); } if(mapa[i][j] == 5 && block[i][j].live == true) { al_draw_bitmap(img_block2, block[i][j].x - cameraX, block[i][j].y - cameraY, 0); } if((mapa[i][j] == 6 || mapa[i][j] == 7 || mapa[i][j] == 8) && (enemy1[i][j].direction == -1) && (enemy1[i][j].live == true)) { al_draw_bitmap_region(img_enemy1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, enemy1[i][j].x - cameraX, enemy1[i][j].y - cameraY, 0); } if((mapa[i][j] == 6 || mapa[i][j] == 7 || mapa[i][j] == 8) && (enemy1[i][j].direction == 1) && (enemy1[i][j].live == true)) { al_draw_bitmap_region(img_enemy1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, enemy1[i][j].x - cameraX, enemy1[i][j].y - cameraY, ALLEGRO_FLIP_HORIZONTAL); } if(mapa[i][j] == 9 && enemy1[i][j].live == true) { al_draw_bitmap_region(img_boss1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, enemy1[i][j].x - cameraX, enemy1[i][j].y - cameraY, 0); } } } /* ~~Desenho do player~~ */ /* Player parado */ if(iddle == true) { if(++immobile.frameCount >= immobile.frameDelay) { if(++immobile.curFrame >= immobile.maxFrame) { immobile.curFrame = 0; iddle = false; } immobile.frameCount = 0; } } if((keys[KEY_SPACE] == false) && (jump == false) && ((!keys[KEY_LEFT] && player.direction == -1) || player.x == 64)) { al_draw_bitmap_region(img_player_immobile, immobile.curFrame * immobile.frameWidth, 0, immobile.frameWidth, immobile.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL); } if((keys[KEY_SPACE] == false) && (jump == false) && ((!keys[KEY_RIGHT] && player.direction == 1) || player.x == WORLD_W - (64-immobile.frameWidth))) { al_draw_bitmap_region(img_player_immobile, immobile.curFrame * immobile.frameWidth, 0, immobile.frameWidth, immobile.frameHeight, player.x - cameraX, player.y - cameraY, 0); } if((keys[KEY_SPACE] == true) && (jump == false) && ((!keys[KEY_LEFT] && player.direction == -1))) { al_draw_bitmap(img_player_immobile_shoot, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL); } if((keys[KEY_SPACE] == true) && (jump == false) && ((!keys[KEY_RIGHT] && player.direction == 1))) { al_draw_bitmap(img_player_immobile_shoot, player.x - cameraX, player.y - cameraY, 0); } /* Player andando */ if(++walking.frameCount >= walking.frameDelay) { if(++walking.curFrame >= walking.maxFrame) { walking.curFrame = 0; } walking.frameCount = 0; } if(keys[KEY_SPACE] == false) { if(jump == false && keys[KEY_LEFT] && player.direction == -1) { al_draw_bitmap_region(img_player_walking, walking.curFrame * walking.frameWidth, 0, walking.frameWidth, walking.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL); } if(jump == false && keys[KEY_RIGHT] && player.direction == 1) { al_draw_bitmap_region(img_player_walking, walking.curFrame * walking.frameWidth, 0, walking.frameWidth, walking.frameHeight, player.x - cameraX, player.y - cameraY, 0); } } if(keys[KEY_SPACE] == true) { if(jump == false && keys[KEY_LEFT] && player.direction == -1) { al_draw_bitmap_region(img_player_walking_shoot, walking.curFrame * walking.frameWidth, 0, walking.frameWidth, walking.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL); } if(jump == false && keys[KEY_RIGHT] && player.direction == 1) { al_draw_bitmap_region(img_player_walking_shoot, walking.curFrame * walking.frameWidth, 0, walking.frameWidth, walking.frameHeight, player.x - cameraX, player.y - cameraY, 0); } } /* Player pulando */ if(jump == true) { if(++jumping.frameCount >= jumping.frameDelay) { if(++jumping.curFrame >= jumping.maxFrame) { jumping.curFrame = 0; } jumping.frameCount = 0; } if(keys[KEY_SPACE] == false) { if(player.direction == -1) { al_draw_bitmap_region(img_player_jump, jumping.curFrame * jumping.frameWidth, 0, jumping.frameWidth, jumping.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL); } if(player.direction == 1) { al_draw_bitmap_region(img_player_jump, jumping.curFrame * jumping.frameWidth, 0, jumping.frameWidth, jumping.frameHeight, player.x - cameraX, player.y - cameraY, 0); } } else if(keys[KEY_SPACE] == true) { if(player.direction == -1) { al_draw_bitmap_region(img_player_jump_shoot, jumping.curFrame * jumping.frameWidth, 0, jumping.frameWidth, jumping.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL); } if(player.direction == 1) { al_draw_bitmap_region(img_player_jump_shoot, jumping.curFrame * jumping.frameWidth, 0, jumping.frameWidth, jumping.frameHeight, player.x - cameraX, player.y - cameraY, 0); } } } /* ~~Desenho dos projeteis~~ */ for(i=0; i<NUM_BULLET; i++) { if(playerBullet[i].live) { al_draw_bitmap(img_player_bullet, playerBullet[i].x - cameraX, playerBullet[i].y - cameraY, 0); } if(enemyBullet[i].live) { al_draw_bitmap(img_enemy_bullet, enemyBullet[i].x - cameraX, enemyBullet[i].y - cameraY, 0); } } /* Pontuacao e Porcentagem de Vida */ al_draw_bitmap(img_heart, 0, 0, 0); al_draw_textf(fonte16, al_map_rgb(255, 255, 255), 20, 0, ALLEGRO_ALIGN_LEFT, ("%03d"), player.life); al_draw_bitmap(img_medal, 64, 0, 0); al_draw_textf(fonte16, al_map_rgb(255, 255, 255), 80, 0, ALLEGRO_ALIGN_LEFT, ("%04d"), scores); al_draw_bitmap(img_clock, 144, 0, 0); al_draw_textf(fonte16, al_map_rgb(255, 255, 255), 160, 0, ALLEGRO_ALIGN_LEFT, ("%03d"), stopwatch); } /* Termino da Fase */ if(enemyKilled == ENEMY_MAX) /* You Win! */ { scores = scores + (25 * stopwatch) + (10*player.life); gamestate = 2; } if(player.life <= 0 || stopwatch == 0) /* Game Over! */ { gamestate = 3; } /* Troca o display */ al_flip_display(); } break; case 2: al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) /* Faz com que o jogo feche ao clicar no botao "X" do display */ { quit = true; } if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) /* Faz com que o jogo feche ao pressionar esc */ { quit = true; } } if(ev.type == ALLEGRO_EVENT_TIMER) /* Mostrar mensagens na tela */ { if(ev.timer.source == timer_60) { al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(img_you_win, 0, 0, 0); al_draw_textf(fonte16, al_map_rgb(0, 0, 0), SCREEN_W/2, SCREEN_H - 48, ALLEGRO_ALIGN_CENTRE, "Seus pontos: %d", scores); al_draw_textf(fonte16, al_map_rgb(0, 0, 0), SCREEN_W/2, SCREEN_H - 32, ALLEGRO_ALIGN_CENTRE, "Pressione Esc para sair"); al_flip_display(); } } break; case 3: al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) /* Faz com que o jogo feche ao clicar no botao "X" do display */ { quit = true; } if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) /* Faz com que o jogo feche ao pressionar esc */ { quit = true; } } if(ev.type == ALLEGRO_EVENT_TIMER) /* Mostrar mensagens na tela */ { if(ev.timer.source == timer_60) { al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(img_game_over, 0, 0, 0); al_draw_textf(fonte16, al_map_rgb(255, 0, 0), SCREEN_W/2, SCREEN_H - 48, ALLEGRO_ALIGN_CENTRE, "Seus pontos: %d", scores); al_draw_textf(fonte16, al_map_rgb(255, 0, 0), SCREEN_W/2, SCREEN_H - 32, ALLEGRO_ALIGN_CENTRE, "Pressione Esc para sair"); al_flip_display(); } } break; } } /* Destruindo as variaveis */ al_destroy_display(display); al_destroy_event_queue(event_queue); al_destroy_timer(timer_0p2); al_destroy_timer(timer_1); al_destroy_timer(timer_60); al_destroy_bitmap(img_home_screen); al_destroy_bitmap(img_dica_h1n1); al_destroy_bitmap(img_background0); al_destroy_bitmap(img_heart); al_destroy_bitmap(img_medal); al_destroy_bitmap(img_clock); al_destroy_bitmap(img_block1); al_destroy_bitmap(img_block2); al_destroy_bitmap(img_player_walking); al_destroy_bitmap(img_player_walking_shoot); al_destroy_bitmap(img_player_immobile); al_destroy_bitmap(img_player_immobile_shoot); al_destroy_bitmap(img_player_jump); al_destroy_bitmap(img_player_jump_shoot); al_destroy_bitmap(img_player_bullet); al_destroy_bitmap(img_enemy1); al_destroy_bitmap(img_boss1); al_destroy_bitmap(img_enemy_bullet); al_destroy_sample(spl_theme); al_destroy_sample(spl_playerShoot); al_destroy_sample(spl_mlk); return 0; }
int main(int argc, char **argv){ ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *bouncer = NULL; float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0; float bouncer_dx = -4.0, bouncer_dy = 4.0; bool redraw = true; if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE); if(!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_set_target_bitmap(bouncer); al_clear_to_color(al_map_rgb(255, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } 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_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); while(1) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) { if(bouncer_x < 0 || bouncer_x > SCREEN_W - BOUNCER_SIZE) { bouncer_dx = -bouncer_dx; } if(bouncer_y < 0 || bouncer_y > SCREEN_H - BOUNCER_SIZE) { bouncer_dy = -bouncer_dy; } bouncer_x += bouncer_dx; bouncer_y += bouncer_dy; redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0); al_flip_display(); } } al_destroy_bitmap(bouncer); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int main(void) { ALLEGRO_FONT *font; ALLEGRO_DISPLAY *display; ALLEGRO_EVENT_QUEUE *event_queue; ALLEGRO_EVENT event; bool right_button_down = false; bool redraw = true; int fake_x = 0, fake_y = 0; ALLEGRO_COLOR white; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_init_primitives_addon(); al_init_font_addon(); al_init_image_addon(); al_install_mouse(); al_install_keyboard(); al_set_new_display_flags(ALLEGRO_WINDOWED); display = al_create_display(width, height); if (!display) { abort_example("Could not create display.\n"); return 1; } memset(&event, 0, sizeof(event)); event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue, al_get_keyboard_event_source()); font = al_load_font("data/fixed_font.tga", 0, 0); white = al_map_rgb_f(1, 1, 1); while (1) { if (redraw && al_is_event_queue_empty(event_queue)) { int th = al_get_font_line_height(font); al_clear_to_color(al_map_rgb_f(0, 0, 0)); if (right_button_down) { al_draw_line(width / 2, height / 2, fake_x, fake_y, al_map_rgb_f(1, 0, 0), 1); al_draw_line(fake_x - 5, fake_y, fake_x + 5, fake_y, al_map_rgb_f(1, 1, 1), 2); al_draw_line(fake_x, fake_y - 5, fake_x, fake_y + 5, al_map_rgb_f(1, 1, 1), 2); } al_draw_textf(font, white, 0, 0, 0, "x: %i y: %i dx: %i dy %i", event.mouse.x, event.mouse.y, event.mouse.dx, event.mouse.dy); al_draw_textf(font, white, width / 2, height / 2 - th, ALLEGRO_ALIGN_CENTRE, "Left-Click to warp pointer to the middle once."); al_draw_textf(font, white, width / 2, height / 2, ALLEGRO_ALIGN_CENTRE, "Hold right mouse button to constantly move pointer to the middle."); al_flip_display(); redraw = false; } al_wait_for_event(event_queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) break; } if (event.type == ALLEGRO_EVENT_MOUSE_WARPED) { printf("Warp\n"); } if (event.type == ALLEGRO_EVENT_MOUSE_AXES) { if (right_button_down) { al_set_mouse_xy(display, width / 2, height / 2); fake_x += event.mouse.dx; fake_y += event.mouse.dy; } redraw = true; } if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { if (event.mouse.button == 1) al_set_mouse_xy(display, width / 2, height / 2); if (event.mouse.button == 2) { right_button_down = true; fake_x = width / 2; fake_y = height / 2; } } if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { if (event.mouse.button == 2) { right_button_down = false; } } } al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }
int main(void) { ALLEGRO_DISPLAY *display, *ms_display; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_TIMER *timer; ALLEGRO_BITMAP *memory; char title[1024]; bool quit = false; bool redraw = true; int wx, wy; if (!al_init()) { abort_example("Couldn't initialise Allegro.\n"); } al_init_primitives_addon(); al_install_keyboard(); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); memory = create_bitmap(); /* Create the normal display. */ al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 0, ALLEGRO_REQUIRE); al_set_new_display_option(ALLEGRO_SAMPLES, 0, ALLEGRO_SUGGEST); display = al_create_display(300, 450); if (!display) { abort_example("Error creating display\n"); } al_set_window_title(display, "Normal"); /* Create bitmaps for the normal display. */ al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); bitmap_filter = al_clone_bitmap(memory); al_set_new_bitmap_flags(0); bitmap_normal = al_clone_bitmap(memory); font = al_create_builtin_font(); al_get_window_position(display, &wx, &wy); if (wx < 160) wx = 160; /* Create the multi-sampling display. */ al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_REQUIRE); al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); ms_display = al_create_display(300, 450); if (!ms_display) { abort_example("Multisampling not available.\n"); } sprintf(title, "Multisampling (%dx)", al_get_display_option( ms_display, ALLEGRO_SAMPLES)); al_set_window_title(ms_display, title); /* Create bitmaps for the multi-sampling display. */ al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); bitmap_filter_ms = al_clone_bitmap(memory); al_set_new_bitmap_flags(0); bitmap_normal_ms = al_clone_bitmap(memory); font_ms = al_create_builtin_font(); /* Move the windows next to each other, because some window manager * would put them on top of each other otherwise. */ al_set_window_position(display, wx - 160, wy); al_set_window_position(ms_display, wx + 160, wy); timer = al_create_timer(1.0 / 30.0); 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_display_event_source(ms_display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (!quit) { ALLEGRO_EVENT event; /* Check for ESC key or close button event and quit in either case. */ al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: quit = true; break; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) quit = true; break; case ALLEGRO_EVENT_TIMER: bitmap_move(); redraw = true; break; } if (redraw && al_is_event_queue_empty(queue)) { /* Draw the multi-sampled version into the first window. */ al_set_target_backbuffer(ms_display); al_clear_to_color(al_map_rgb_f(1, 1, 1)); draw(bitmap_filter_ms, 0, "filtered, multi-sample"); draw(bitmap_normal_ms, 250, "no filter, multi-sample"); al_flip_display(); /* Draw the normal version into the second window. */ al_set_target_backbuffer(display); al_clear_to_color(al_map_rgb_f(1, 1, 1)); draw(bitmap_filter, 0, "filtered"); draw(bitmap_normal, 250, "no filter"); al_flip_display(); redraw = false; } } return 0; }
int main(void){ if (!al_init()) { //abort_example("Could not init Allegro.\n"); return 1; } display = al_create_display(600, 480); if (!display) { //abort_example("al_create_display failed\n"); return 1; } if (!al_install_keyboard()) { //abort_example("al_install_keyboard failed\n"); return 1; } event_queue = al_create_event_queue(); if (!event_queue) { //abort_example("al_create_event_queue failed\n"); return 1; } al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_display_event_source(display)); while (true) { /* Take the next event out of the event queue, and store it in `event'. */ al_wait_for_event(event_queue, &event); /* Check what type of event we got and act accordingly. ALLEGRO_EVENT * is a union type and interpretation of its contents is dependent on * the event type, which is given by the 'type' field. * * Each event also comes from an event source and has a timestamp. * These are accessible through the 'any.source' and 'any.timestamp' * fields respectively, e.g. 'event.any.timestamp' */ switch (event.type) { /* ALLEGRO_EVENT_KEY_DOWN - a keyboard key was pressed. */ case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { printf("\nEsc Pressed"); return 0; } if (event.keyboard.keycode == ALLEGRO_KEY_UP || event.keyboard.keycode == ALLEGRO_KEY_W){ printf("\nUp/W Pressed"); } if (event.keyboard.keycode == ALLEGRO_KEY_DOWN || event.keyboard.keycode == ALLEGRO_KEY_S){ printf("\nDown/S Pressed"); } if (event.keyboard.keycode == ALLEGRO_KEY_LEFT || event.keyboard.keycode == ALLEGRO_KEY_A){ printf("\nLeft/A Pressed"); } if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT || event.keyboard.keycode == ALLEGRO_KEY_D){ printf("\nRight/D Pressed"); } //log_key("KEY_DOWN", event.keyboard.keycode, 0, 0); break; /* ALLEGRO_EVENT_KEY_UP - a keyboard key was released. */ case ALLEGRO_EVENT_KEY_UP: //log_key("KEY_UP", event.keyboard.keycode, 0, 0); break; /* ALLEGRO_EVENT_KEY_CHAR - a character was typed or repeated. */ // case ALLEGRO_EVENT_KEY_CHAR: { // char const *label = (event.keyboard.repeat ? "repeat" : "KEY_CHAR"); // log_key(label, // event.keyboard.keycode, // event.keyboard.unichar, // event.keyboard.modifiers); // break; // } /* ALLEGRO_EVENT_DISPLAY_CLOSE - the window close button was pressed. */ case ALLEGRO_EVENT_DISPLAY_CLOSE: return 1; /* We received an event of some type we don't know about. * Just ignore it. */ default: break; } } //close_log(false); return 0; }
int main(void) { //primitive variable 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 variables ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font18 = NULL; //Initialization Functions if(!al_init()) //initialize Allegro return -1; display = al_create_display(WIDTH, HEIGHT); //create our display object if(!display) //test display object return -1; al_init_primitives_addon(); al_install_keyboard(); al_init_font_addon(); al_init_ttf_addon(); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); srand(time(NULL)); InitShip(ship); InitBullet(bullets, NUM_BULLETS); InitComet(comets, NUM_COMETS); font18 = al_load_font("arial.ttf", 18, 0); 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)); al_start_timer(timer); while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &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_COMETS, ship); CollideComet(comets, NUM_COMETS, ship); if(ship.lives <= 0) isGameOver = true; } } 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) { 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(event_queue)) { 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. Final Score: %i", ship.score); } al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } } al_destroy_event_queue(event_queue); al_destroy_timer(timer); al_destroy_font(font18); al_destroy_display(display); //destroy our display object return 0; }
int main(void) { ALLEGRO_DISPLAY *displays[2]; ALLEGRO_MONITOR_INFO *info; int adapter_count; int x, y; ALLEGRO_FONT *myfont; ALLEGRO_EVENT_QUEUE *events; ALLEGRO_EVENT event; int i; srand(time(NULL)); if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_install_mouse(); al_init_font_addon(); al_init_image_addon(); adapter_count = al_get_num_video_adapters(); info = malloc(adapter_count * sizeof(ALLEGRO_MONITOR_INFO)); for (i = 0; i < adapter_count; i++) { al_get_monitor_info(i, &info[i]); } x = ((info[0].x2 - info[0].x1) / 3) - (W / 2); y = ((info[0].y2 - info[0].y1) / 2) - (H / 2); al_set_new_window_position(x, y); displays[0] = al_create_display(W, H); x *= 2; al_set_new_window_position(x, y); displays[1] = al_create_display(W, H); if (!displays[0] || !displays[1]) { abort_example("Could not create displays.\n"); } al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); myfont = al_load_font("data/fixed_font.tga", 0, 0); if (!myfont) { abort_example("Could not load font.\n"); } events = al_create_event_queue(); al_register_event_source(events, al_get_mouse_event_source()); al_register_event_source(events, al_get_display_event_source(displays[0])); al_register_event_source(events, al_get_display_event_source(displays[1])); for (;;) { for (i = 0; i < 2; i++) { al_set_target_backbuffer(displays[i]); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); if (i == 0) al_clear_to_color(al_map_rgb(255, 0, 255)); else al_clear_to_color(al_map_rgb(155, 255, 0)); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); al_draw_textf(myfont, al_map_rgb(0, 0, 0), 50, 50, ALLEGRO_ALIGN_CENTRE, "Click me.."); al_flip_display(); } if (al_wait_for_event_timed(events, &event, 1)) { if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { int a = rand() % adapter_count; int w = info[a].x2 - info[a].x1; int h = info[a].y2 - info[a].y1; int margin = 20; x = margin + info[a].x1 + (rand() % (w - W - margin)); y = margin + info[a].y1 + (rand() % (h - H - margin)); al_set_window_position(event.mouse.display, x, y); } } } al_destroy_event_queue(events); al_destroy_display(displays[0]); al_destroy_display(displays[1]); free(info); return 0; }
int main() { const float FPS = 60.0; if(!al_init()) { al_show_native_message_box(NULL, "Fatal Error", NULL, "No se pudo inicializar Allegro", NULL, ALLEGRO_MESSAGEBOX_ERROR); return -1; } al_set_new_display_flags(ALLEGRO_WINDOWED); // Pone la ventana en modo Windowed ALLEGRO_DISPLAY *display = al_create_display(ScreenWidth, ScreenHeight); // Pone la posición en la que debe salir la ventana //al_set_window_position(display, 0, 30); // Pone el título de la ventana al_set_window_title(display, "Rabbit Kills Robots"); if(!display) // Si no se pudo crear la ventana, entonces pone un mensaje de error { al_show_native_message_box(NULL, "Error", NULL, "No se pudo crear la pantalla", NULL, ALLEGRO_MESSAGEBOX_ERROR); return -1; } al_install_keyboard(); al_install_mouse(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); // ----------------------------------------------------------------- ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS); ALLEGRO_KEYBOARD_STATE keyState; // Utilizado para debugging ALLEGRO_FONT *font = al_load_font("sprites/DroidSans.ttf", 10, 0); ALLEGRO_BITMAP *fondo1 = al_load_bitmap("sprites/fondo1.png"); bool done = false; // ---------Estructuras del juego----------------------------------- //~ struct Player //~ { //~ ALLEGRO_BITMAP *image; //~ float x; //~ float y; //~ float moveSpeed; //~ float degrees; //~ int clip; //~ bool alive; //~ float xmouse; //~ float ymouse; //~ }player; struct Player player; player.image = al_load_bitmap("sprites/player.png"); al_convert_mask_to_alpha(player.image, al_map_rgb(255,255,255)); player.x = ScreenWidth / 2; player.y = ScreenHeight / 2; player.w = al_get_bitmap_width(player.image); player.h = al_get_bitmap_height(player.image); player.moveSpeed = 3; player.degrees = -ALLEGRO_PI/2; player.alive = true; player.clip = 6; //~ struct Bala //~ { //~ ALLEGRO_BITMAP *image; //~ float x; //~ float y; //~ float dx; //~ float dy; //~ bool shot; //~ void update(); //~ }bala; struct Bala bala; //~ void Bala::update() //~ { //~ bala.dx = cosf(player.xmouse - player.x); //~ bala.dy = senf(player.ymouse - player.y); //~ //~ if(bala.shot){ //~ //~ bala.x += bala.dx; //~ bala.y += bala.dy; //~ } //~ //~ } bala.image = al_load_bitmap("sprites/bullet.png"); bala.x = player.x+50; bala.y = player.y+25; bala.shot = false; //~ struct Enemigo //~ { //~ ALLEGRO_BITMAP *image; //~ float x; //~ float y; //~ float velocidad_x; //~ float velocidad_y; //~ //float degrees; //~ }robot; struct Enemigo robot; robot.image = al_load_bitmap("sprites/Robot_sprites.png"); //al_convert_mask_to_alpha(robot.image, al_map_rgb(255,255,255)); robot.x = 50; robot.y = 50; robot.w = al_get_bitmap_width(robot.image); robot.h = al_get_bitmap_height(robot.image); robot.velocidad_x = 0.23; robot.velocidad_y = 0.23; //~ void Weapon::recargar() //~ { //~ for(int i = 0; i < 6; i++) //~ { //~ bullets[i] = bala; //~ } //~ } //~ struct Weapon //~ { //~ struct Bala bullets[player.clip]; //~ void recargar(); //~ }arma; //~ //~ void Weapon::recargar() //~ { //~ for(int i = 0; i < player.clip; i++) //~ { //~ bullets[i] = bala; //~ } //~ } //~ // ----------------------------------------------------------------- // Esta variable guardará los eventos del mouse ALLEGRO_MOUSE_STATE mouseState; // Registro varias fuentes de eventos al_register_event_source(event_queue, al_get_keyboard_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_register_event_source(event_queue, al_get_mouse_event_source()); // Inicializo el temporizador principal al_start_timer(timer); while(!done) { // La variable de los eventos ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); // Y aquí espero por los eventos if(ev.type == ALLEGRO_EVENT_TIMER) { // Dos funciones para pasar eventos del mouse y del teclado al_get_keyboard_state(&keyState); al_get_mouse_state(&mouseState); // Esto detecta la posición del mouse y lo guarda a un par de variables player.xmouse = al_get_mouse_state_axis(&mouseState, 0); player.ymouse = al_get_mouse_state_axis(&mouseState, 1); // Si presiono Esc entonces me saca del juego if(al_key_down(&keyState, ALLEGRO_KEY_ESCAPE)) { done = true; } // Si presiono A entonces el valor x se reduce, osea, se va a la izquierda if(al_key_down(&keyState, ALLEGRO_KEY_A)) { player.x -= player.moveSpeed; } // Si... meh, ya sabes lo que sigue if(al_key_down(&keyState, ALLEGRO_KEY_D)) { player.x += player.moveSpeed; } // ... if(al_key_down(&keyState, ALLEGRO_KEY_W)) { player.y -= player.moveSpeed; } // ... if(al_key_down(&keyState, ALLEGRO_KEY_S)) { player.y += player.moveSpeed; } } // Esto permite que el jugador se mueva con el mouse player.degrees = atan2((player.ymouse-player.y),(player.xmouse-player.x)); // La Inteligencia Artificial del enemigo if(robot.x < player.x-25) robot.x += robot.velocidad_x; if(robot.x > player.x-25) robot.x -= robot.velocidad_x; if(robot.y > player.y-25) robot.y -= robot.velocidad_y; if(robot.y < player.y-25) robot.y += robot.velocidad_y; // Uso de las funciones para las colisiones //if(Collision(player.x, player.y, 50, 50, robot.x, robot.y, 34, 34)) player.alive = false; if(PixelCol(player.image, robot.image, player.x-(player.w/2), player.y-(player.h/2), player.w, player.h, robot.x, robot.y, robot.w/7, robot.h)) player.alive = false; al_clear_to_color(al_map_rgb(255, 255, 255)); // Se pinta todo a negro al_draw_scaled_bitmap(fondo1,0, 0, 256, 256, 0, 0, ScreenWidth, ScreenHeight, 0); // Se dibuja el fondo if(player.alive){ // Si el jugador está vivo al_draw_rotated_bitmap(player.image, 25, 25, player.x, player.y, player.degrees, 0); // Dibujo el jugador al_draw_rotated_bitmap(bala.image, 0, 0, player.x+5, player.y+5, player.degrees, 0); // Dibujo la bala (esto hay que quitarlo) } al_draw_bitmap_region(robot.image, 0, 0, 60, 52, robot.x, robot.y, 0); // Dibujo el robot // Esto es para el debugging // Dibujo rectángulos para las colisiones al_draw_rectangle(player.x-(player.w/2), player.y-(player.h/2), (player.x+player.w)-(player.w/2), (player.y+player.h)-(player.h/2), al_map_rgb(0, 255, 0), 1.0); al_draw_rectangle(robot.x, robot.y, robot.x+(robot.w/7), robot.y+robot.h, al_map_rgb(0, 255, 0), 1.0); // Escribo en una esquina de la pantalla, información respecto al personaje al_draw_textf(font, al_map_rgb(255,255,255), ScreenWidth-10, 2, ALLEGRO_ALIGN_RIGHT, "Player x, y : %.1f %.1f", player.x, player.y); al_draw_textf(font, al_map_rgb(255,255,255), ScreenWidth-10, 12, ALLEGRO_ALIGN_RIGHT, "Rotation (rad): %.5f", player.degrees); al_draw_textf(font, al_map_rgb(255,255,255), ScreenWidth-10, 22, ALLEGRO_ALIGN_RIGHT, "Rotation (degrees): %.2f", (player.degrees*180)/ALLEGRO_PI); // Actualizo la pantalla (flip) al_flip_display(); } //-----After party (hay que limpiar)-------------------------------- // A destruirlo todo!! BAM BAM BAM, KABOOM!! al_destroy_font(font); al_destroy_bitmap(fondo1); al_destroy_bitmap(robot.image); al_destroy_display(display); al_destroy_event_queue(event_queue); al_destroy_bitmap(player.image); al_destroy_timer(timer); return 0; }
int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_BITMAP *bmp; ALLEGRO_BITMAP *mem_bmp; ALLEGRO_BITMAP *disp_bmp; ALLEGRO_FONT *font; char *text; bool done = false; bool redraw = true; if (!al_init()) { abort_example("Failed to init Allegro.\n"); return 1; } if (!al_init_image_addon()) { abort_example("Failed to init IIO addon.\n"); return 1; } al_init_font_addon(); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display.\n"); return 1; } if (!al_install_keyboard()) { abort_example("Error installing keyboard.\n"); return 1; } font = al_load_font("data/fixed_font.tga", 0, 0); if (!font) { abort_example("Error loading data/fixed_font.tga\n"); return 1; } bmp = disp_bmp = al_load_bitmap("data/mysha.pcx"); if (!bmp) { abort_example("Error loading data/mysha.pcx\n"); return 1; } text = "Display bitmap (space to change)"; al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); mem_bmp = al_load_bitmap("data/mysha.pcx"); if (!bmp) { abort_example("Error loading data/mysha.pcx\n"); return 1; } timer = al_create_timer(INTERVAL); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); al_register_event_source(queue, al_get_display_event_source(display)); al_start_timer(timer); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); while (!done) { ALLEGRO_EVENT event; if (redraw && al_is_event_queue_empty(queue)) { update(bmp); al_clear_to_color(al_map_rgb_f(0, 0, 0)); al_draw_tinted_bitmap(bmp, al_map_rgba_f(1, 1, 1, 0.5), bmp_x, bmp_y, bmp_flag); al_draw_text(font, al_map_rgba_f(1, 1, 1, 0.5), 0, 0, 0, text); al_flip_display(); redraw = false; } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; else if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) { if (bmp == mem_bmp) { bmp = disp_bmp; text = "Display bitmap (space to change)"; } else { bmp = mem_bmp; text = "Memory bitmap (space to change)"; } } break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; case ALLEGRO_EVENT_TIMER: redraw = true; break; } } al_destroy_bitmap(bmp); return 0; }
int main(int argc, char *argv[]) { int c; while((c = getopt(argc, argv, "hv")) != -1) { switch(c) { case 'h': puts(HELP); return 0; case 'v': printf("version %.1f\n", PROGRAM_VERSION); return 0; } } if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(argc == 2 && !strcmp(argv[1], "-v")) { printf("version %.1f\n", PROGRAM_VERSION); return 0; } bool key[5] = {false, false, false, false, false}; bool doexit = false; bool redraw = true; timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "couldn't initialize timer.\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { fprintf(stderr, "failed to create the display.\n"); al_destroy_timer(timer); return -1; } al_set_window_title(display, "blasteroids"); al_init_primitives_addon(); if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize keyboard.\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } /* Making the sky look like sky */ colors[0] = al_map_rgba(255, 100, 255, 128); colors[1] = al_map_rgba(255, 100, 100, 255); colors[2] = al_map_rgba(100, 100, 255, 255); for (layer = 0; layer < 3; layer++) { for (star = 0; star < NUM_STARS/3; star++) { Point *p = &stars[layer][star]; p->x = rand() % SCREEN_W; p->y = rand() % SCREEN_H; } } start = al_get_time() * 1000; now = start; elapsed = 0; frame_count = 0; program_start = al_get_time(); /* done with the sky. Now making the ship. */ Spaceship *ship = init_ship(); List *a = (List *)summon_asteroids(NUMBER_ASTEROIDS); ListElmt *astElmt = list_head(a); if(!ship) { fprintf(stderr, "couldn't create bitmap.\n"); al_destroy_timer(timer); al_destroy_display(display); return -1; } Blast *blast = init_blast(ship->sx, ship->sy, ship->heading); event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event queue.\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_keyboard_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_start_timer(timer); while(!doexit) { /* animate the sky, just sky, starts, but NOT objects. */ ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) { if(key[UP]) ship->speed += 0.04; if(key[RIGHT]) ship->heading += 1.0f; if(key[DOWN]) if(ship->speed > 0.05) ship->speed -= 0.04; if(key[LEFT]) ship->heading -= 1.0f; fly_ship(ship); // fire ze missile if(key[SPACE]) { blast->sx = ship->sx; blast->sy = ship->sy; blast->heading = ship->heading; blast->gone = 0; } float theta = head2theta(blast->heading); blast->sx += blast->speed * cos(theta); if(blast->sx <= 0 || blast->sx >= SCREEN_W) { blast->gone = 1; } blast->sy += blast->speed * sin(theta); if(blast->sy <= 0 || blast->sy >= SCREEN_H) { blast->gone = 1; } /* loop through the list of asteroids */ astElmt = (astElmt->next)?astElmt->next : list_head(a); Asteroid *aster = astElmt->aster; /* asteroid eternity */ if(aster->sx < 0 || aster->sx > SCREEN_W - 33) aster->sx = 0; if(aster->sy < 0 || aster->sy > SCREEN_H) aster->sy = 0; aster->twist += aster->rot_velocity; /* Fuzzy movement */ if((int)aster->sx % 3 == 0) aster->sx += aster->speed; aster->sx += 0.9; if((int)aster->sy % 5 == 3) aster->sy += aster->speed; aster->sy += 0.9; aster->twist += 0.4; /* detect alteroid collision, but only if 5 seconds have passed since the last Death */ Box s = {{ship->sx, ship->sy}, 16.0f, 20.0f}; Box a = {{aster->sx, aster->sy}, 45.0f, 40.0f}; if(ship->heading != -90.0f) { if(!aster->gone && !ship->gone && is_collision(&s, &a)) { ship->color = al_map_rgb(0, 0, 255); } } /* detect asteroid being shot */ Box b = {{blast->sx, blast->sy}, 120.0f, 3.0f}; if(!(blast->gone) && !(aster->gone) && is_collision(&b, &a)) { aster->gone = 1; } redraw = true; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[UP] = true; break; case ALLEGRO_KEY_RIGHT: key[RIGHT] = true; break; case ALLEGRO_KEY_DOWN: key[DOWN] = true; break; case ALLEGRO_KEY_LEFT: key[LEFT] = true; break; case ALLEGRO_KEY_SPACE: key[SPACE] = true; break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[UP] = false; break; case ALLEGRO_KEY_RIGHT: key[RIGHT] = false; break; case ALLEGRO_KEY_DOWN: key[DOWN] = false; break; case ALLEGRO_KEY_LEFT: key[LEFT] = false; break; case ALLEGRO_KEY_SPACE: key[SPACE] = false; break; case ALLEGRO_KEY_Q: case ALLEGRO_KEY_ESCAPE: doexit = true; break; } } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0, 0, 0)); if (frame_count < (1000/TARGET_FPS)) { frame_count += elapsed; } else { int X, Y; frame_count -= (1000/TARGET_FPS); for (star = 0; star < NUM_STARS/3; star++) { Point *p = &stars[0][star]; al_draw_pixel(p->x, p->y, colors[0]); } /* al_lock_bitmap(al_get_backbuffer(display), ALLEGRO_PIXEL_FORMAT_ANY, 0); */ for (layer = 1; layer < 3; layer++) { for (star = 0; star < NUM_STARS/3; star++) { Point *p = &stars[layer][star]; // put_pixel ignores blending al_put_pixel(p->x, p->y, colors[layer]); } } /* Check that dots appear at the window extremes. */ X = SCREEN_W - 1; Y = SCREEN_H - 1; al_put_pixel(0, 0, al_map_rgb_f(1, 1, 1)); al_put_pixel(X, 0, al_map_rgb_f(1, 1, 1)); al_put_pixel(0, Y, al_map_rgb_f(1, 1, 1)); al_put_pixel(X, Y, al_map_rgb_f(1, 1, 1)); /* al_unlock_bitmap(al_get_backbuffer(display)); */ total_frames++; } now = al_get_time() * 1000; elapsed = now - start; start = now; for (layer = 0; layer < 3; layer++) { for (star = 0; star < NUM_STARS/3; star++) { Point *p = &stars[layer][star]; p->y -= speeds[layer] * elapsed; if (p->y < 0) { p->x = rand() % SCREEN_W; p->y = SCREEN_H; } } } draw_ship(ship); if(!blast->gone) { draw_blast(blast); } draw_asteroids(a); al_flip_display(); } } length = al_get_time() - program_start; /* printf("Length = %f\n", length); */ al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int main(int argc, char **argv){ /*declaramos nave como una estructura tipo móvil, q está definida * en el archivo "physics.h"*/ struct Movil nave; /*creamos las variables de allegro, que son punteros * apuntando a NULL, de momento*/ ALLEGRO_DISPLAY *display = NULL; ALLEGRO_BITMAP *sprites = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; bool key[4] = {false, false, false,false};//Array para las teclas, todos a false bool redraw = true; bool do_exit = false; /* Inicializamos allegro */ if(!al_init()) { al_show_native_message_box ( display, "Error", "Error", "Failed to initialize allegro!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return 0; } /* Inicializamos añadido de imágenes */ if(!al_init_image_addon()) { al_show_native_message_box ( display, "Error", "Error", "Failed to initialize al_init_image_addon!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return 0; } ni /* Inicializamos el teclado */ if(!al_install_keyboard()) { al_show_native_message_box ( display, "Error", "Error", "Failed to initialize al_init_image_addon!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return 0; }; /* Alegro artifact creation * del display, timer, cola eventos y sprites*/ display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { al_show_native_message_box( display, "Error", "Error", "Failed to initialize display!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return 0; } timer = al_create_timer(1.0 / FPS); if(!timer) { al_show_native_message_box( display, "Error", "Error", "failed to create timer!\n", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_display(display); return 0; } event_queue = al_create_event_queue(); if(!event_queue) { al_show_native_message_box( display, "Error", "Error", "failed to create event_queue!\n", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_display(display); al_destroy_timer(timer); return 0; } sprites = al_load_bitmap("images/xenon2_sprites.png"); al_convert_mask_to_alpha(sprites, al_map_rgb(255,0,255)); if(!sprites) { al_show_native_message_box( display, "Error", "Error", "Failed to load sprites!", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_display(display); al_destroy_timer(timer); al_destroy_event_queue(event_queue); return 0; } /* registramos todos los eventos provenientes de la pantalla * el keyboard y el temporizador*/ 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)); /* Init es una funcion definida como extern el "physics.h" , que le meto * los sprites, que es el bitmap de la nave, y &nave, que es una dirección * de memoria donde hemos declarado nave como estructura*/ init(sprites, &nave); al_start_timer(timer); while(!doexit){ ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) redraw = true; else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; /* éste bloque funciona con el evento KEY_DOWN, y cuando apretamos * cualquier tecla, se registra el avento KEY_DOWN q corresponda */ else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_THROTTLE] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_BRAKE] = true; break; case ALLEGRO_KEY_LEFT: key[KEY_ROTATE_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: key[KEY_ROTATE_RIGHT] = true; break; } /* Éste bloque funciona con el evento KEY_UP, cuando soltamos * la tecla se registra el evento KEY_DOWN correspondiente.*/ } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_THROTTLE] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_BRAKE] = false; break; case ALLEGRO_KEY_LEFT: key[KEY_ROTATE_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: key[KEY_ROTATE_RIGHT] = false; break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; } } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; update_physics(key, &nave); al_clear_to_color(al_map_rgb(0,0,0)); // al_draw_bitmap(sprites,200,200,0); al_draw_bitmap(nave.img[2 + (int) (nave.v.x / ROLL) ], SCREEN_W / 2 + nave.r.x, SCREEN_H - 50 - nave.r.y, 0); al_flip_display(); } } /* DEstrucción de los artefactos */ al_destroy_display(display); al_destroy_timer(timer); al_destroy_event_queue(event_queue); al_destroy_bitmap(sprites); return 0; }
bool initialize () { ALLEGRO_BITMAP *temp_color_bitmap; ALLEGRO_BITMAP *temp_mask_bitmap; global.quit = false; al_set_app_name ("Super Battle Thor II"); if (!al_init ()) { fputs ("Error: Could not start allegro.\n", stderr); return false; } al_set_new_display_option (ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); al_set_new_display_flags (ALLEGRO_GENERATE_EXPOSE_EVENTS); global.display = al_create_display (256, 192); if (!global.display) { fputs ("Error: Allegro could not create a display of 256x192 pixels.\n", stderr); return false; } if (!al_install_keyboard ()) { fputs ("Allegro could not initialize the keyboard system.\n", stderr); return false; } global.timer = al_create_timer (ALLEGRO_BPS_TO_SECS (60)); if (!global.timer) { fputs ("Allegro could not create a 60hrz timer.\n", stderr); return false; } global.queue = al_create_event_queue (); if (!global.queue) { fputs ("Allegro could not create an event queue.\n", stderr); return false; } al_register_event_source (global.queue, al_get_keyboard_event_source ()); al_register_event_source (global.queue, al_get_display_event_source (global.display)); al_register_event_source (global.queue, al_get_timer_event_source (global.timer)); if (!al_init_image_addon ()) { fputs ("Allegro could not initialize the image loading system.\n", stderr); return false; } if (!al_install_audio ()) { fputs ("Allegro could not initialize the audio system.\n", stderr); return false; } al_reserve_samples (16); if (!al_init_acodec_addon ()) { fputs ("Allegro could not initialize the audio file loading system.\n", stderr); return false; } al_init_font_addon (); /* load files */ global.path = al_get_standard_path (ALLEGRO_PROGRAM_PATH); al_append_path_component (global.path, "media"); al_set_path_filename (global.path, "proggy_tiny.png"); global.programing_font = al_load_bitmap_font (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.programing_font) { fputs ("Allegro could not load proggy_tiny.png.\n", stderr); return false; } al_set_path_filename (global.path, "mjollnir.png"); global.sprite_bitmap[SPRITE_THOR] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sprite_bitmap[SPRITE_THOR]) { fputs ("Allegro could not load mjollnir.png.\n", stderr); return false; } al_set_path_filename (global.path, "arrows.png"); global.arrow_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.arrow_bitmap) { fputs ("Allegro could not load arrows.png.\n", stderr); return false; } al_set_path_filename (global.path, "background_1_2_3_4.jpg"); global.backgrounds[BACKGROUND_1] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_1]) { fputs ("Allegro could not load background_1_2_3_4.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "background_5.jpg"); global.backgrounds[BACKGROUND_5] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_5]) { fputs ("Allegro could not load background_5.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "background_6.jpg"); global.backgrounds[BACKGROUND_6] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_6]) { fputs ("Allegro could not load background_6.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "title_screen.jpg"); global.backgrounds[BACKGROUND_TITLE] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_TITLE]) { fputs ("Allegro could not load title_screen.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "game_over.jpg"); global.backgrounds[BACKGROUND_GAME_OVER] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_GAME_OVER]) { fputs ("Allegro could not load game_over.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "you_win.jpg"); global.backgrounds[BACKGROUND_YOU_WIN] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_YOU_WIN]) { fputs ("Allegro could not load you_win.jpg.\n", stderr); return false; } global.backgrounds[BACKGROUND_BLACK] = al_create_bitmap (256, 192); if (!global.backgrounds[BACKGROUND_BLACK]) { fputs ("Allegro could not make a 256x192 bitmap :o", stderr); return false; } al_set_target_bitmap (global.backgrounds[BACKGROUND_BLACK]); al_clear_to_color (al_map_rgb (0, 0, 0)); al_set_target_backbuffer (global.display); /* start copy paste code here becasue I'm lazy */ /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "0-thor.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 0-thor.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "0-thor_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 0-thor_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_THOR] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "1-giant.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 1-giant.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "1-giant_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 1-giant_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_GIANT] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "2-nidhogg.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 2-nidhogg.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "2-nidhogg_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 2-nidhogg_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_NIDHOGG] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "3-ran.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 3-ran.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "3-ran_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 3-ran_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_RAN] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "4-grendel.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 4-grendel.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "4-grendel_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 4-grendel_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_GRENDEL] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "5-fafnir.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 5-fafnir.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "5-fafnir_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 5-fafnir_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_FAFNIR] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "6-loki.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 6-loki.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "6-loki_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 6-loki_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_LOKI] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "7-fenrir.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 7-fenrir.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "7-fenrir_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 7-fenrir_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_FENRIR] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "fight.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load fight.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "fight_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load fight_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_FIGHT] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ /* loading sounds here (still copy paste) */ al_set_path_filename (global.path, "titlescreen_loop.ogg"); global.sounds[SOUND_TITLESCREEN_LOOP] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_TITLESCREEN_LOOP]) { fputs ("Allegro could not load titlescreen_loop.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "begin_battle.ogg"); global.sounds[SOUND_BEGIN_BATTLE] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_BEGIN_BATTLE]) { fputs ("Allegro could not load begin_battle.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "battle_loop.ogg"); global.sounds[SOUND_BATTLE_LOOP] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_BATTLE_LOOP]) { fputs ("Allegro could not load battle_loop.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "dragon_loop.ogg"); global.sounds[SOUND_DRAGON_LOOP] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_DRAGON_LOOP]) { fputs ("Allegro could not load dragon_loop.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "hit_1.ogg"); global.sounds[SOUND_HIT_1] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_HIT_1]) { fputs ("Allegro could not load hit_1.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "hit_2.ogg"); global.sounds[SOUND_HIT_2] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_HIT_2]) { fputs ("Allegro could not load hit_2.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_1.ogg"); global.sounds[SOUND_LAUGH_1] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_1]) { fputs ("Allegro could not load laugh_1.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_2.ogg"); global.sounds[SOUND_LAUGH_2] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_2]) { fputs ("Allegro could not load laugh_2.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_3.ogg"); global.sounds[SOUND_LAUGH_3] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_3]) { fputs ("Allegro could not load laugh_3.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_4.ogg"); global.sounds[SOUND_LAUGH_4] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_4]) { fputs ("Allegro could not load laugh_4.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_5.ogg"); global.sounds[SOUND_LAUGH_5] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_5]) { fputs ("Allegro could not load laugh_5.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_6.ogg"); global.sounds[SOUND_LAUGH_6] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_6]) { fputs ("Allegro could not load laugh_6.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "killed_baddie.ogg"); global.sounds[SOUND_KILLED_BADDIE] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_KILLED_BADDIE]) { fputs ("Allegro could not load killed_baddie.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "got_hit.ogg"); global.sounds[SOUND_GOT_HIT] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_GOT_HIT]) { fputs ("Allegro could not load got_hit.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "you_are_dead.ogg"); global.sounds[SOUND_YOU_ARE_DEAD] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_YOU_ARE_DEAD]) { fputs ("Allegro could not you_are_dead.ogg.\n", stderr); return false; } global.music = NULL; al_destroy_path (global.path); return true; }
int main() { //===Set ship Color===================================== ALLEGRO_COLOR shipColor = al_map_rgb(50,50,50); //===Set up Display===================================== al_init(); al_create_display(640, 480); al_clear_to_color(al_map_rgb(84,151,252)); al_set_new_display_refresh_rate(1); //===Set up ship struct typedef struct { float sx; float sy; float heading; float speed; int gone; ALLEGRO_COLOR color; } Spaceship; Spaceship myShip = {320, 240, 0, 0, 1, al_map_rgb(50,50,50)}; //===Draw ship========================================== //al_draw_line(42, 59, 50, 39,al_map_rgb(50,50,50) , 3.0f); //al_draw_line(50, 39, 58, 59,al_map_rgb(50,50,50), 3.0f); //al_draw_line(44, 54, 49, 54, al_map_rgb(50,50,50), 3.0f); //al_draw_line(56, 54, 51, 54, al_map_rgb(50,50,50), 3.0f); al_draw_line(myShip.sx-8, myShip.sy+9, myShip.sx, myShip.sy-11,myShip.color, 3.0f); al_draw_line(myShip.sx, myShip.sy-11, myShip.sx+8, myShip.sy+9,myShip.color, 3.0f); al_draw_line(myShip.sx-6, myShip.sy+4, myShip.sx-1, myShip.sy+4,myShip.color, 3.0f); al_draw_line(myShip.sx+6, myShip.sy+4, myShip.sx+1, myShip.sy+4,myShip.color, 3.0f); al_flip_display(); al_rest(1.0); //===Wait for event from event queue===================== ALLEGRO_EVENT_QUEUE *queue; queue = al_create_event_queue(); ALLEGRO_EVENT event; al_wait_for_event(queue, &event); //while(1); /*/{ if (event.type == ALLEGRO_EVENT_KEY_DOWN) { switch (event.keyboard.keycode) { case ALLEGRO_KEY_LEFT: myShip.sx = myShip.sx-10; break; case ALLEGRO_KEY_RIGHT: myShip.sx = myShip.sx+10; break; case ALLEGRO_KEY_SPACE: //fire break; } } al_flip_display(); al_rest(1.0); } /*/ }
int main(int argc, char **argv) { int num_joysticks; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_JOYSTICK *curr_joy; ALLEGRO_DISPLAY *display; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } if (!al_install_joystick()) { abort_example("Could not init joysticks.\n"); } al_install_keyboard(); al_init_primitives_addon(); open_log(); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display.\n"); } queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_joystick_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); num_joysticks = al_get_num_joysticks(); log_printf("Num joysticks: %d\n", num_joysticks); if (num_joysticks > 0) { curr_joy = al_get_joystick(0); print_joystick_info(curr_joy); } else { curr_joy = NULL; } draw(curr_joy); while (1) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_KEY_CHAR) { int n = event.keyboard.unichar - '0'; if (n >= 0 && n < num_joysticks) { curr_joy = al_get_joystick(n); log_printf("switching to joystick %d\n", n); print_joystick_info(curr_joy); } } else if (event.type == ALLEGRO_EVENT_JOYSTICK_CONFIGURATION) { al_reconfigure_joysticks(); num_joysticks = al_get_num_joysticks(); log_printf("after reconfiguration num joysticks = %d\n", num_joysticks); if (curr_joy) { log_printf("current joystick is: %s\n", al_get_joystick_active(curr_joy) ? "active" : "inactive"); } curr_joy = al_get_joystick(0); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_AXIS) { log_printf("axis event from %p, stick %d, axis %d\n", event.joystick.id, event.joystick.stick, event.joystick.axis); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) { log_printf("button down event %d from %p\n", event.joystick.button, event.joystick.id); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_UP) { log_printf("button up event %d from %p\n", event.joystick.button, event.joystick.id); } draw(curr_joy); } close_log(false); return 0; }
int main(void){ //allegro variables ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_BITMAP *bg = NULL; ALLEGRO_TIMER *timer; //program init if(!al_init()) //initialize Allegro return -1; display = al_create_display(WIDTH, HEIGHT); //create our display object if(!display) //test display object return -1; //============================================== //ADDON INSTALL //============================================== al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); // FONT DEL PROGRAMA. ALLEGRO_FONT *font = al_load_ttf_font("Sarpanch-SemiBold.ttf",30,0 ); // VARIABLES DEL JUEGO ======================== bg = al_load_bitmap("img/sp1.jpg"); SpaceShip nave_jugador; Enemy enemies1[10]; Enemy enemies2[10]; Enemy enemies3[10]; Enemy enemies4[10]; Enemy jefe1[10]; Bullet bullets[5]; char vidas_char[2]; // INICIALIZAR OBJETOS==================== InitShip(nave_jugador); InitBullet(bullets, NUM_BULLETS); InitEnemies(enemies1,ENM1,10,0,220,WIDTH/2); InitEnemies(enemies2,ENM2,8,50,180,WIDTH/2+100); InitEnemies(enemies3,ENM3,6,100,140,WIDTH); InitEnemies(enemies4,ENM4,4,150,100,(WIDTH/2)+100); InitEnemies(jefe1,JEFE,2,200,60); //DrawEnemies(enemies,NUM_ENEMIES); //============================================== //TIMER INIT AND STARTUP //============================================== event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / 60); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_start_timer(timer); //AnimacionEntrada(enemies,NUM_ENEMIES); printf("ya"); int animacion=1; int movimientos=0; while(!done){ ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); // INFORMACION DEL JUEGO Y BG // DrawWindowStatus(); al_draw_bitmap(bg, 0, 0, 0); al_draw_bitmap(nave_jugador.image, 5,440,0); al_draw_text(font, al_map_rgb(255,255,255), 40,430, 0, "X"); sprintf(vidas_char,"%d",vidas); al_draw_text(font, al_map_rgb(255,255,255), 60,430, 0, vidas_char); al_draw_text(font, al_map_rgb(255,255,255), WIDTH/2 - 30, 0,ALLEGRO_ALIGN_CENTRE, "Score"); char vartext[10]; sprintf(vartext,"%d",score); al_draw_text(font, al_map_rgb(255,255,255), WIDTH/2 + 40, 0, 0, vartext); //============================================== //INPUT //============================================== 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_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; FireBullet(bullets, NUM_BULLETS, nave_jugador); break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } //============================================== //GAME UPDATE //============================================== else if(ev.type == ALLEGRO_EVENT_TIMER){ render = true; if(keys[LEFT]) MoveShipLeft(nave_jugador); else if(keys[RIGHT]) MoveShipRight(nave_jugador); UpdateBullet(bullets, NUM_BULLETS); CollideBullet(bullets,NUM_BULLETS,enemies1,NUM_ENEMIES); CollideBullet(bullets,NUM_BULLETS,enemies2,8); CollideBullet(bullets,NUM_BULLETS,enemies3,6); CollideBullet(bullets,NUM_BULLETS,enemies4,4); CollideBullet(bullets,NUM_BULLETS,jefe1,2); } //============================================== //RENDER //============================================== if(render && al_is_event_queue_empty(event_queue)) { if (animacion==1){ printf("si primero"); movEnemies(enemies1,10,1); movEnemies(enemies2, 8,1); movEnemies(enemies3,6,1); movEnemies(enemies4,4,1); movEnemies(jefe1,2,1); animacion=0; movimientos=1; } render = false; // Dibujar nave al_draw_bitmap(nave_jugador.image, nave_jugador.x - nave_jugador.w / 2, nave_jugador.y - nave_jugador.h / 2, 0); // Dibujar Balas DrawBullet(bullets, NUM_BULLETS); // Dibuja los enemigos. DrawEnemies(enemies1,10); DrawEnemies(enemies2,8); DrawEnemies(enemies3,6); DrawEnemies(enemies4,4); DrawEnemies(jefe1,2); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); if (movimientos){ movimientos=0; movEnemies(enemies1,10,2); movEnemies(enemies2,8,3); } } } for (int i =0;i<10;i++) al_destroy_bitmap(enemies1[i].image); al_destroy_event_queue(event_queue); al_destroy_timer(timer); al_destroy_display(display); //destroy our display object return 0; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; bool redraw = true; bool doexit = false; bool key[4] = { false, false, false, false }; if (!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if (!al_init_image_addon()) { al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return 0; } if (!al_install_keyboard()) { fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; } collider collide; std::vector<bouncer *> balls; std::vector<tile*> tiles; std::vector<move *> moveVector; ALLEGRO_BITMAP *image = NULL; image = al_load_bitmap("megamanss.png"); if (!image) { al_show_native_message_box(display, "Error", "Error", "Failed to load image!", NULL, NULL); al_destroy_display(display); return 0; } for (int i = 0; i < NUM_BALLS; i++) { bouncer* a; a = new bouncer(30 * i, 30 * i, 10, i / 5.0, i); balls.push_back(a); // collide.add(a); } for (int i = 0; i < NUM_TILES; i++) { tile * a; // std::cout << i%16 << "\t" << i/12 << "\n"; a = new tile(TILE_SIZE * (i % 16), TILE_SIZE * (i / 16), TILE_SIZE); tiles.push_back(a); // collide.add(a); } animator *anim = new animator("megamanss.png", "Source/megamanss.txt"); //ballGenerator *bg = new ballGenerator(100,100,&collide,&balls); //PokemonFactory pf; pokemon * p = new pokemon("treeko", 1, 2, 3, 4); MoveFactory *mf = new MoveFactory(); move *m = mf->Create(SWAG, "swag", p, p, 0, 400, 200); moveVector.push_back(m); collide.add(p); collide.add(m); // bouncer b(20,20,32,3,4); // bouncer c(30,30,32,4,4); timer = al_create_timer(1.0 / FPS); if (!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if (!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if (!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_clear_to_color(al_map_rgb(0, 0, 0)); al_flip_display(); al_start_timer(timer); std::cout << "\nDONE WITH INIT\n"; while (!doexit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_TIMER) { collide.update(); anim->update(); for (int i = 0; i < moveVector.size(); i++) { std::cout << "moveVector.size()" << moveVector.size() << "\n"; if (moveVector[i]->markForDeath == true) { delete moveVector[i]; moveVector.erase(moveVector.begin() + i); i--; } else { moveVector[i]->update(); } } /* if (m != NULL && m->markForDeath == true) { std::cout << "deleting object"; delete m; m = NULL; } else if (m!= NULL) m->update(); */ for (int i = 0; i < balls.size(); i++) { if (balls[i]->markForDeath == true) { collide.remove(balls[i]); delete balls[i]; balls.erase(balls.begin() + i); i--; } else { balls[i]->update(); } } if (key[KEY_UP]) { anim->switchAnimations(1); } if (key[KEY_DOWN]) { anim->switchAnimations(2); } if (key[KEY_LEFT]) { anim->switchAnimations(3); } if (key[KEY_RIGHT]) { anim->switchAnimations(4); } // for (int i = 0; i < tiles.size(); i++) // tiles[i]->update(); p->update(); //bg->update(); redraw = true; } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = true; break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = false; break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; } } if (redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_set_target_bitmap(al_get_backbuffer(display)); al_clear_to_color(al_map_rgb(0, 0, 0)); // al_draw_bitmap(image,200,200,0); for (int i = 0; i < moveVector.size(); i++) moveVector[i]->draw(); /* for (int i = 0; i < tiles.size(); i++) tiles[i]->draw(); for (int i = 0; i < balls.size(); i++) balls[i]->draw();*/ anim->draw(100, 100); p->draw(); al_flip_display(); } } for (int i = 0; i < tiles.size(); i++) delete tiles[i]; for (int i = 0; i < balls.size(); i++) delete balls[i]; al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }