void init_video (void) { if (! al_init_image_addon ()) error (-1, 0, "%s (void): failed to initialize image addon", __func__); al_set_new_display_flags (al_get_new_display_flags () | (display_mode < 0 ? ALLEGRO_WINDOWED : ALLEGRO_FULLSCREEN) | ALLEGRO_RESIZABLE | ALLEGRO_GENERATE_EXPOSE_EVENTS); display_width = display_width ? display_width : DISPLAY_WIDTH; display_height = display_height ? display_height : DISPLAY_HEIGHT; if (display_mode >= 0) { ALLEGRO_DISPLAY_MODE d; get_display_mode (display_mode, &d); display_width = d.width; display_height = d.height; al_set_new_display_refresh_rate (d.refresh_rate); al_set_new_display_flags (al_get_new_display_flags () & ~ALLEGRO_FULLSCREEN_WINDOW); } al_set_new_display_option (ALLEGRO_SINGLE_BUFFER, 1, ALLEGRO_SUGGEST); display = al_create_display (display_width, display_height); if (! display) error (-1, 0, "%s (void): failed to initialize display", __func__); set_target_backbuffer (display); al_set_new_bitmap_flags (ALLEGRO_VIDEO_BITMAP); al_set_window_title (display, WINDOW_TITLE); icon = load_bitmap (ICON); al_set_display_icon (display, icon); cutscene = true; if (mr.fit_w == 0 && mr.fit_h == 0) { mr.fit_w = 2; mr.fit_h = 2; } set_multi_room (1, 1); effect_buffer = create_bitmap (CUTSCENE_WIDTH, CUTSCENE_HEIGHT); black_screen = create_bitmap (CUTSCENE_WIDTH, CUTSCENE_HEIGHT); uscreen = create_bitmap (CUTSCENE_WIDTH, CUTSCENE_HEIGHT); iscreen = create_bitmap (display_width, display_height); clear_bitmap (uscreen, TRANSPARENT_COLOR); video_timer = create_timer (1.0 / EFFECT_HZ); al_init_font_addon (); builtin_font = al_create_builtin_font (); if (! builtin_font) error (-1, 0, "%s (void): cannot create builtin font", __func__); if (! al_init_primitives_addon ()) error (-1, 0, "%s (void): failed to initialize primitives addon", __func__); }
// Initializes the the GDATA struct _Bool data_init(void) { // Create a window to display things on: 800x600 pixels al_set_new_display_flags(ALLEGRO_RESIZABLE | ALLEGRO_OPENGL); al_set_new_display_refresh_rate(60); data->display = al_create_display(res_width, res_height); if(data->display == NULL) { al_show_native_message_box(data->display, "Error!", "Display Error:", "Failed to create window!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return false; } // Sets the physfs file interface to be used for loading files al_set_physfs_file_interface(); PHYSFS_mount("images", NULL, 1); PHYSFS_mount("fonts", NULL, 1); PHYSFS_mount("buttons", NULL, 1); // Well, looks like we're gonna have to draw our own mouse *SIGH* al_hide_mouse_cursor(data->display); data->cursor = al_load_bitmap("cursor_darkgrey.png"); if(data->cursor == NULL) { al_show_native_message_box(data->display, "Error!", "Cursor Error:", "Failed to locate cursor!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return false; } // Set the title of the window al_set_window_title(data->display, "SpeedRun!"); // Sets the window's icon data->icon = al_load_bitmap("speedrundisplayicon.png"); if(data->icon == NULL) { al_show_native_message_box(data->display, "Error!", "Icon Error:", "Failed to locate icon!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return false; } al_set_display_icon(data->display, data->icon); // Load a font data->font = al_load_ttf_font("times.ttf", 25, 0); data->b_font = al_load_ttf_font("impact.ttf", 25, 0); if(data->font == NULL || data->b_font == NULL) { al_show_native_message_box(data->display, "Error!", "Font Error:", "Failed to load font!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return false; } // Make and set a color to draw with data->text_color = al_map_rgba(80, 112, 255, 255); // Set background color data->background_color = al_map_rgba(0, 0, 0, 0); // Install the keyboard handler if(!al_install_keyboard()) { al_show_native_message_box(data->display, "Error!", "Keyboard Error:", "Failed to install keyboard handler!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return false; } // Install the mouse handler if(!al_install_mouse()) { al_show_native_message_box(data->display, "Error!", "Mouse Error:", "Failed to install mouse handler!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return false; } // Detect and set refresh rate int refresh_rate = al_get_display_refresh_rate(data->display); if(refresh_rate == 0) { refresh_rate = 60; } // Install timer with refresh rate data->timer = al_create_timer(1.0/refresh_rate); if(data->timer == NULL) { al_show_native_message_box(data->display, "Error!", "Timer Error:", "Failed to initialize timer!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return 1; } al_start_timer(data->timer); // Start the event queues to handle keyboard input, mouse input, display input and timer input data->queue = al_create_event_queue(); al_register_event_source(data->queue, (ALLEGRO_EVENT_SOURCE*)data->timer); data->queue2 = al_create_event_queue(); al_register_event_source(data->queue2, al_get_keyboard_event_source()); al_register_event_source(data->queue2, (ALLEGRO_EVENT_SOURCE*)data->display); al_register_event_source(data->queue2, al_get_mouse_event_source()); // Set the quit and gamestarted flags to false initially data->exit = false; data->gamestarted = false; data->options = false; data->highscores = false; data->howtoplay = false; al_start_timer(data->timer); return true; }
int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_FONT *font; ALLEGRO_CONFIG *config; ALLEGRO_EVENT_QUEUE *queue; bool write = false; bool flip = false; bool quit; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_init_font_addon(); al_init_image_addon(); al_install_keyboard(); al_install_mouse(); /* Read parameters from ex_vsync.ini. */ config = al_load_config_file("ex_vsync.ini"); if (!config) { config = al_create_config(); write = true; } /* 0 -> Driver chooses. * 1 -> Force vsync on. * 2 -> Force vsync off. */ vsync = option(config, "vsync", 0); fullscreen = option(config, "fullscreen", 0); frequency = option(config, "frequency", 0); /* Write the file back (so a template is generated on first run). */ if (write) { al_save_config_file("ex_vsync.ini", config); } al_destroy_config(config); /* Vsync 1 means force on, 2 means forced off. */ if (vsync) al_set_new_display_option(ALLEGRO_VSYNC, vsync, ALLEGRO_SUGGEST); /* Force fullscreen mode. */ if (fullscreen) { al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); /* Set a monitor frequency. */ if (frequency) al_set_new_display_refresh_rate(frequency); } display = al_create_display(640, 480); if (!display) { abort_example("Error creating display.\n"); } font = al_load_font("data/a4_font.tga", 0, 0); if (!font) { abort_example("Failed to load a4_font.tga\n"); } queue = al_create_event_queue(); 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(display)); quit = display_warning(queue, font); al_flush_event_queue(queue); while (!quit) { ALLEGRO_EVENT event; /* With vsync, this will appear as a 50% gray screen (maybe * flickering a bit depending on monitor frequency). * Without vsync, there will be black/white shearing all over. */ if (flip) al_clear_to_color(al_map_rgb_f(1, 1, 1)); else al_clear_to_color(al_map_rgb_f(0, 0, 0)); al_flip_display(); flip = !flip; while (al_get_next_event(queue, &event)) { switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: quit = true; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) quit = true; } } /* Let's not go overboard and limit flipping at 1000 Hz. Without * this my system locks up and requires a hard reboot :P */ al_rest(0.001); } al_destroy_font(font); al_destroy_event_queue(queue); return 0; }
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); } /*/ }
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; }