Ejemplo n.º 1
0
// The main Allegro loop, all input handling, animation and drawing is done here
_Bool main_loop(void)
{
    // Flag for drawing
    _Bool needredraw = true;
    // Declare primitive data types
    float old_time = 0.0, current_time = 0, dt = 0;

    while(!data->exit)
    {
        if(needredraw && al_event_queue_is_empty(data->queue) && al_event_queue_is_empty(data->queue2))
        {
            // Clear, draw, flip
            al_clear_to_color(data->background_color);
            render();
            if(get_fps_status())
                al_draw_textf(data->font, data->text_color, 0, res_height-30, 0, "fps: %.2f", 1/dt);
            al_flip_display();
            needredraw = false;
        }

        // Block until an event enters the queue
        al_wait_for_event(data->queue, &(data->event));

        while(!al_event_queue_is_empty(data->queue2))
        {
            al_get_next_event(data->queue2, &(data->event2));
            switch(data->event2.type)
            {
                case ALLEGRO_EVENT_DISPLAY_CLOSE:
                {
                    // If x button is pressed on window
                    data->exit = true;
                    data->gamestarted = false;
                }
                break;
                case ALLEGRO_EVENT_DISPLAY_RESIZE:
                {
                    al_acknowledge_resize(data->event2.display.source);
                    scale(res_width, res_height);
                }
                break;
                case ALLEGRO_EVENT_MOUSE_AXES:
                {
                    // Stores the mouse's new position and change in position
                    mouseaxes(&(data->event2.mouse));
                }
                case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
                {
                    // Stores the mouse button pressed
                    mousedown(&(data->event2.mouse));
                }
                break;
                case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
                {
                    // Stores the mouse button released
                    mouseup(&(data->event2.mouse));
                }
                break;
                case ALLEGRO_EVENT_KEY_DOWN:
                {
                    // Stores keydown keycode into keycode array for processing
                    keydown(&(data->event2.keyboard));
                }
                break;
                case ALLEGRO_EVENT_KEY_UP:
                {
                    // Stores keycode into keycode array for processing
                    keyup(&(data->event2.keyboard));
                }
                break;
                default:
                break;
            }
        }

        switch (data->event.type)
        {
            case ALLEGRO_EVENT_TIMER:
            {
                // Determine the change in time between frames, in seconds
				current_time = al_current_time();
				dt = current_time-old_time;

				// If the computer lags for some reason, don't penalize the player
				// Cap dt at 0.5 seconds
				if(dt > 0.25)
				{
                    dt = 0.25;
				}

				// Handle Mouse and Keyboard events and Button Events
				buttoncheck(&buttonhandler, data);
				keycheck(&keyhandler, data);
				mousecheck(&mousehandler, data);
				keyupdate();
                mouseupdate();

                // Check if data->quit has been set before updating and drawing
                if(data->exit) break;

                // Update the game, always
                update();

                // Skip drawing frames if computer lags
                if(current_time - data->event.timer.timestamp <= 1.0/al_get_display_refresh_rate(data->display))
                {
                    needredraw = true;
                }

				// Make the time at this frame the old time, for the next frame
				old_time = current_time;
            }
            break;
            default:
            break;
        }
    }
    return true;
}
Ejemplo n.º 2
0
// 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;
}
Ejemplo n.º 3
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;
}