Ejemplo n.º 1
0
int main(void)
{
    ALLEGRO_COLOR background, active, inactive, info;
    old_dialog = NULL;
    cur_dialog = NULL;
    bool redraw = false;
    bool close_log = false;
    bool message_log = true;

    if (!al_init()) {
        abort_example("Could not init Allegro.\n");
    }

    //open_log();
    log_printf("Starting up log window.\n");

    al_init_image_addon();
    al_init_font_addon();
    al_init_ttf_addon();
    al_init_primitives_addon();

    background = al_map_rgb(0,0,98);
    active = al_color_name("white");
    inactive = al_color_name("gray");
    info = al_color_name("red");

    user_config.save_values();
    user_config.load_file();
    user_config.retrieve_values();

    if((!user_config.map_path.empty()) && user_config.map_autoload)
    {
        populate_filenames(user_config.map_path, &path_list);
        load_bitmaps(&path_list, &map_list);
    }

    al_install_mouse();
    al_install_keyboard();

    al_set_new_display_flags(ALLEGRO_RESIZABLE);
    display = al_create_display(user_config.res_x, user_config.res_y);
    if (!display) {
        log_printf("failure.\n");
        abort_example("Error creating display\n");
        return 1;
    }
    ALLEGRO_BITMAP * isoicon = al_load_bitmap("isoworld/isoWorld.png");
    if(isoicon) {
        al_set_display_icon(display, isoicon);
    }
    al_set_window_title(display, "IsoWorld");

    log_printf("success.\n");

    log_printf("Loading font '%s'...", "isoworld/DejaVuSans.ttf");
    user_config.font = al_load_font("isoworld/DejaVuSans.ttf", 14, 0);
    if (!user_config.font) {
        log_printf("failure.\n");
        abort_example("Error loading isoworld/DejaVuSans.ttf\n");
        return 1;
    }
    log_printf("success.\n");

    timer = al_create_timer(1.0 / 30);
    network_timer = al_create_timer(1.0);
    log_printf("Starting main loop.\n");
    allegro_queue = al_create_event_queue();
    al_register_event_source(allegro_queue, al_get_keyboard_event_source());
    al_register_event_source(allegro_queue, al_get_mouse_event_source());
    al_register_event_source(allegro_queue, al_get_display_event_source(display));
    al_register_event_source(allegro_queue, al_get_timer_event_source(timer));
    al_register_event_source(allegro_queue, al_get_timer_event_source(network_timer));
    if (textlog) {
        al_register_event_source(allegro_queue, al_get_native_text_log_event_source(
            textlog));
    }
    al_start_timer(timer);
    al_start_timer(network_timer);

    initializeAgui();
    add_widgets();

    MapSection test_map;

    test_map.set_size(user_config.map_width, user_config.map_height);

    minimap.reload();

    test_map.load_tilesets("isoworld/tilesets.ini");
    main_screen->UpdateTilesetList(&test_map);

    test_map.board_center_x = 0;
    test_map.board_top_y = 0;

    load_detailed_tiles(path_list.elevation_map, &test_map);

    int selection = 0;

    bool mapmove = 0;
    bool rightmove = 0;
    int mousemove_start_x = 0;
    int mousemove_start_y = 0;
    int mapmove_start_x = 0;
    int mapmove_start_y = 0;

    ALLEGRO_COLOR unselected = al_map_rgb(255,255,255);
    ALLEGRO_COLOR selected = al_map_rgb(128,128,128);

    ALLEGRO_KEYBOARD_STATE keys;
    while (1) {
        float h = al_get_display_height(display);
        float w = al_get_display_width(display);
        ALLEGRO_EVENT event;
        al_wait_for_event(allegro_queue, &event);

        if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE && !cur_dialog)
            break;

        if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
            al_get_keyboard_state(&keys);
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE && !cur_dialog)
                break;
            else if (event.keyboard.keycode == ALLEGRO_KEY_UP && !cur_dialog)
            {
                if(al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT))
                    user_config.map_y-=10;
                else
                    user_config.map_y--;
                continue;
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_DOWN && !cur_dialog)
            {
                if(al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT))
                    user_config.map_y+=10;
                else
                    user_config.map_y++;
                continue;
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT && !cur_dialog)
            {
                if(al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT))
                    user_config.map_x+=10;
                else
                    user_config.map_x++;
                continue;
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_LEFT && !cur_dialog)
            {
                if(al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT))
                    user_config.map_x-=10;
                else
                    user_config.map_x--;
                continue;
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_F5 && !cur_dialog)
            {
                saveScreenshot();
                continue;
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_G && !cur_dialog)
            {
                user_config.showgrid = !user_config.showgrid;
                continue;
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_Q && !cur_dialog)
            {
                if(user_config.ray_distance > 1)
                    user_config.ray_distance --;
                continue;
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_W && !cur_dialog)
            {
                user_config.ray_distance++;
                continue;
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_D && !cur_dialog)
            {
                user_config.debugmode = !user_config.debugmode;
                continue;
            }
        }

        /* When a mouse button is pressed, and no native dialog is
        * shown already, we show a new one.
        */
        if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
        {
            mapmove = false;
            rightmove = false;
        }
        if(event.type == ALLEGRO_EVENT_MOUSE_AXES)
        {
            if(mapmove)
            {
                if ((event.mouse.x >= w - user_config.minimap_size) && (event.mouse.y >= user_config.minimap_size))
                {
                    mapmove = true;
                    int relx = event.mouse.x - w + user_config.minimap_size -1;
                    int rely = event.mouse.y - h + user_config.minimap_size -1;
                    user_config.map_x = (float)al_get_bitmap_width(map_list.biome_map)/user_config.minimap_size * relx - user_config.map_width/2;
                    user_config.map_y = (float)al_get_bitmap_height(map_list.biome_map)/user_config.minimap_size * rely - user_config.map_height/2;
                }
                else
                    mapmove = false;
            }
            if(rightmove)
            {
                user_config.map_x = mapmove_start_x - ((event.mouse.x - mousemove_start_x) / test_map.tileset_list[test_map.current_tileset].tile_width) - ((event.mouse.y - mousemove_start_y) / test_map.tileset_list[test_map.current_tileset].tile_height);
                user_config.map_y = mapmove_start_y + ((event.mouse.x - mousemove_start_x) / test_map.tileset_list[test_map.current_tileset].tile_width) - ((event.mouse.y - mousemove_start_y) / test_map.tileset_list[test_map.current_tileset].tile_height);
            }
        }
        if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
            //log_printf("Mouse clicked at %d,%d.\n", event.mouse.x, event.mouse.y);
            if(event.mouse.button == 2)
            {
                rightmove = true;
                mousemove_start_x = event.mouse.x;
                mousemove_start_y = event.mouse.y;
                mapmove_start_x = user_config.map_x;
                mapmove_start_y = user_config.map_y;
            }
            else if ((event.mouse.x >= w - user_config.minimap_size) && (event.mouse.y >= user_config.minimap_size))
            {
                mapmove = true;
                int relx = event.mouse.x - w + user_config.minimap_size -1;
                int rely = event.mouse.y - h + user_config.minimap_size -1;
                user_config.map_x = (float)al_get_bitmap_width(map_list.biome_map)/user_config.minimap_size * relx - user_config.map_width/2;
                user_config.map_y = (float)al_get_bitmap_height(map_list.biome_map)/user_config.minimap_size * rely - user_config.map_height/2;
            }
            else if (event.mouse.y > 30) {
                if (event.mouse.y > h - 30) {
                    message_log = !message_log;
                    if (message_log) {
                        textlog = al_open_native_text_log("Log", 0);
                        if (textlog) {
                            al_register_event_source(allegro_queue,
                                al_get_native_text_log_event_source(textlog));
                        }
                    }
                    else {
                        close_log = true;
                    }
                }
            }
        }
        /* We receive this event from the other thread when the dialog is
        * closed.
        */
        if (event.type == ASYNC_DIALOG_EVENT1) {
            al_unregister_event_source(allegro_queue, &cur_dialog->event_source);

            /* If files were selected, we replace the old files list.
            * Otherwise the dialog was cancelled, and we keep the old results.
            */
            if (al_get_native_file_dialog_count(cur_dialog->file_dialog) > 0) {
                if (old_dialog)
                    stop_async_dialog(old_dialog);
                old_dialog = cur_dialog;
            }
            else {
                stop_async_dialog(cur_dialog);
            }
            cur_dialog = NULL;
        }

        if (event.type == ALLEGRO_EVENT_NATIVE_DIALOG_CLOSE) {
            close_log = true;
        }

        if (event.type == ALLEGRO_EVENT_TIMER) {
            if(event.timer.source == timer)
                redraw = true;
            else if(event.timer.source == network_timer) {
                if(connection_state) {
                    al_stop_timer(network_timer); //wait at least a second between the end of load and the start of the next, not between the start and start.
                    connection_state->net_request.set_save_folder(current_save);
                    if(!color_list.has_names){
                        if(connection_state->MaterialInfoCall(&connection_state->net_request, &connection_state->net_material_names) == DFHack::command_result::CR_OK) {
                            if(connection_state->net_material_names.available()) {
                                color_list.import_names(&connection_state->net_material_names);
                                if(!color_list.has_colors) {
                                    color_list.import_colors("isoworld/material_colors.ini");
                                }
                            }
                        }
                        else {
                            delete connection_state;
                            connection_state = NULL;
                            goto EXIT_LOOP;
                        }
                    }
                    if(connection_state->EmbarkInfoCall(&connection_state->net_request, &connection_state->net_reply) == DFHack::command_result::CR_OK) {
                        if(connection_state->net_reply.available()) {
                            if(center_on_loaded_map) {
                                user_config.map_x = connection_state->net_reply.region_x() + (connection_state->net_reply.region_size_x() / 2) - (user_config.map_width / 2);
                                user_config.map_y = connection_state->net_reply.region_y() + (connection_state->net_reply.region_size_y() / 2) - (user_config.map_height / 2);
                            }
                            for(int yy = 0; yy < connection_state->net_reply.region_size_y(); yy++) {
                                for(int xx = 0; xx < connection_state->net_reply.region_size_x(); xx++) {
                                    if(!test_map.query_tile(&connection_state->net_reply, xx, yy))
                                        continue;
                                    connection_state->net_tile_request.set_want_x(xx);
                                    connection_state->net_tile_request.set_want_y(yy);
                                    if(connection_state->EmbarkTileCall(&connection_state->net_tile_request, &connection_state->net_embark_tile) == DFHack::command_result::CR_OK) {
                                        if(!connection_state->net_embark_tile.is_valid())
                                            continue;
                                        test_map.make_tile(&connection_state->net_embark_tile, &connection_state->net_reply);
                                        goto EXIT_LOOP;
                                    }
                                    else {
                                        delete connection_state;
                                        connection_state = NULL;
                                        goto EXIT_LOOP;
                                    }
                                }
                            }
                        }
                    }
                    else {
                        delete connection_state;
                        connection_state = NULL;
                        goto EXIT_LOOP;
                    }
EXIT_LOOP: ;
                    al_start_timer(network_timer);
                }
            }
        }
        if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
            al_acknowledge_resize(event.display.source);
            user_config.res_x = al_get_display_width(display);
            user_config.res_y = al_get_display_height(display);
            //Resize Agui
            gui->resizeToDisplay();
            redraw = true;
        }
        if (redraw && al_is_event_queue_empty(allegro_queue)) {
            float x = al_get_display_width(display) / 2;
            float y = 0;
            redraw = false;
            al_clear_to_color(background);
            al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
            if (old_dialog)
            {
                if(old_dialog->newimages)
                {
                    show_files_list(old_dialog->file_dialog, user_config.font, info);
                    old_dialog->newimages = 0;
                    for(int i=0;i<test_map.tileset_list.size();i++){
                        delete test_map.tileset_list[test_map.current_tileset].rendered_map;
                        test_map.tileset_list[test_map.current_tileset].rendered_map = NULL;
                    }
                    load_detailed_tiles(path_list.elevation_map, &test_map);
                }
            }
            test_map.propogate_tiles(&map_list);
            test_map.draw(al_get_display_width(display) / 2, (al_get_display_height(display) / 2) + user_config.map_shift);
            minimap.draw();
            if(user_config.debugmode)
            {
                test_map.draw_debug_info();
                al_draw_textf(user_config.font, cur_dialog ? inactive : active, 0, y, ALLEGRO_ALIGN_LEFT, "Drawtime: %dms", test_map.draw_time);
                al_draw_textf(user_config.font, cur_dialog ? inactive : active, 0, y + al_get_font_line_height(user_config.font), ALLEGRO_ALIGN_LEFT, "Load Time: %dms", test_map.load_time);
                al_draw_textf(user_config.font, cur_dialog ? inactive : active, 0, y + al_get_font_line_height(user_config.font)*2, ALLEGRO_ALIGN_LEFT, "Fetch Time: %dms", test_map.tile_fetch_time);
            }
            gui->logic();
            render_gui();
            al_flip_display();
        }

        if (close_log && textlog) {
            close_log = false;
            message_log = false;
            al_unregister_event_source(allegro_queue,
                al_get_native_text_log_event_source(textlog));
            al_close_native_text_log(textlog);
            textlog = NULL;
        }


        //Let Agui process the event last, because f**k Agui
        inputHandler->processEvent(event);

    }

    log_printf("Exiting.\n");
    if(connection_state) {
        delete connection_state;
        connection_state = NULL;
    }

    imagelist.unload_bitmaps();

    user_config.save_values();
    user_config.save_file();

    al_destroy_event_queue(allegro_queue);

    stop_async_dialog(old_dialog);
    stop_async_dialog(cur_dialog);

    return 0;
}