#include #include #include #include #include "xpms/smile.xpm" #include "xpms/waves1.xpm" #include "xpms/waves2.xpm" static void mainwin_destroy (GtkWidget *widget, gpointer dummy); static GtkWidget* create_mainwin (void); static GtkWidget* setup_test_hex_display (HexWalker **walker); static gboolean got_a_keypress (HexDisplayGtk *disp, GdkEventKey *event, HexWalker *walker); static void walker_moved (HexWalker *walker, HexDisplayGtk *disp); int main(int argc, char *argv[]) { GtkWidget *window, *scrolled_window, *display; HexWalker *walker; gtk_init(&argc, &argv); window = create_mainwin(); gtk_window_set_default_size(GTK_WINDOW(window), 500, 500); scrolled_window = gtk_scrolled_window_new(GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 0, 0, 0)), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 0, 0, 0))); gtk_container_add(GTK_CONTAINER(window), scrolled_window); display = setup_test_hex_display(&walker); g_assert (HEX_IS_DISPLAY_GTK(display)); g_object_ref (display); gtk_container_add(GTK_CONTAINER(scrolled_window), display); g_object_unref (display); gtk_widget_add_events(window, GDK_KEY_PRESS_MASK); gtk_signal_connect_full(GTK_OBJECT(window), "key-press-event", (GtkSignalFunc) got_a_keypress, NULL, walker, g_object_unref, FALSE, FALSE); gtk_widget_show(display); gtk_widget_show(scrolled_window); gtk_widget_show(window); hex_display_gtk_center_hex(HEX_DISPLAY_GTK(display), walker->x, walker->y); gtk_main(); return 0; } static void mainwin_destroy (GtkWidget *widget, gpointer dummy) { gtk_main_quit(); } static GtkWidget* create_mainwin (void) { GtkWidget *window; window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (mainwin_destroy), NULL); return window; } static GtkWidget* setup_test_hex_display (HexWalker **walker) { HexGrid *grid; GtkWidget *disp; const gint width = 49, height = 33, hex_width = 25; HexData *background, *water, *light_forest, *heavy_forest, *swamp, *smile, *waves[3]; HexTile *water_tile, *lt_for_tile, *hv_for_tile, *swamp_tile, *waves_tile, *next_tile; const gchar map[33][50] = { /* Allow space for terminating nul, some compilers care */ "ffffffffffffffff.................................", "fffffffffffffffff................................", "ffffffffffffffffff...............................", "fffffffffffffffffff..............................", "fffffffffffffffffff..............................", "fffffffffffffffffff..............................", "fffffffffffffffffff..............................", "ffffffffffffffffffffff...........................", "ffffffffffffffffffffffff.........................", "ffffffffffffffffffffffffff.......................", "ffffffffffffffffffffffffffssss...................", "fffffffffffffffffffffffffsssss...................", "fffffffffffffffffffffffffsssww...................", "ffffffffffffffffffffffffssswwww..................", "fffffffffffffffffffffffssswwwwwww................", "fffffffffffffffffffffffsswwwwwwwwwwwwwwwwwwwwwwww", "fffffffffffffffffffffFFFswwww...www..............", "fffffffffffffffffffffFFFsswww...www..............", "fffffffffffffffffffffFFFFwwwww.ww................", "ffffffffffffffffffffFFFwwwwwwwwww................", "ffffffffffffffffffffFFFwwwwwwwww.................", "ffffffffffffffffffffFFwwwwwwww...................", "fffffffffffffffffffffFwwwwwww....................", "fffffffffffffffffffff............................", "fffffffffffffffffffff............................", "fffffffffffffffffffff............................", "fffffffffffffffffffff............................", "ffffffffffffffffffffff...........................", "ffffffffffffffffffffff...........................", "ffffffffffffffffffffff...........................", "ffffffffffffffffffffffff.........................", "fffffffffffffffffffffffff........................", "ffffffffffffffffffffffffff.......................", }; gint x, y; HexColor green = {0, 0xffff, 0}, lt_green = {0x7fff, 0xffff, 0x7fff}, dk_green = {0, 0x7fff, 0}, blue = {0, 0, 0xffff}, dk_blue = {0, 0, 0x7fff}, black = {0, 0, 0}; water = hex_data_new_from_color(&blue, hex_width); background = hex_data_new_from_color(<_green, hex_width); swamp = hex_data_new_from_color(&dk_blue, hex_width); light_forest = hex_data_new_from_color(&green, hex_width); heavy_forest = hex_data_new_from_color(&dk_green, hex_width); waves[0] = hex_data_new_from_xpm_d((const gchar**)waves1_xpm); waves[1] = hex_data_new_from_xpm_d((const gchar**)waves2_xpm); waves[2] = hex_data_new_empty_cached(hex_width); grid = HEX_GRID(hex_grid_new(background, &black, width, height)); water_tile = HEX_TILE(hex_obstacle_new(water, HEX_DIRECTION_SE)); swamp_tile = hex_tile_new(swamp); lt_for_tile = hex_tile_new(light_forest); hv_for_tile = hex_tile_new(heavy_forest); waves_tile = HEX_TILE(hex_animation_new(waves, 3, 300, TRUE)); hex_data_unref(water); hex_data_unref(background); hex_data_unref(swamp); hex_data_unref(light_forest); hex_data_unref(heavy_forest); hex_data_unref(waves[0]); hex_data_unref(waves[1]); hex_data_unref(waves[2]); for(x = 0; x < width; x++) { for(y = 0; y < height; y++) { switch(map[y][x]) { case '.': continue; case 'w': next_tile = water_tile; hex_grid_set_tile(grid, x, y, 10, waves_tile); break; case 's': next_tile = swamp_tile; break; case 'f': next_tile = lt_for_tile; break; case 'F': next_tile = hv_for_tile; break; default: g_warning("Invalid hex type %c.\n", map[y][x]); continue; } hex_grid_set_tile(grid, x, y, 0, next_tile); } } /* Just hardcode the border */ for(x = -1; x < 14; x += 2) hex_grid_set_tile(grid, x, -1, 0, lt_for_tile); for(x = 0; x < 28; x += 2) hex_grid_set_tile(grid, x, height, 0, lt_for_tile); for(y = 0; y < height; y++) hex_grid_set_tile(grid, -1, y, 0, lt_for_tile); hex_grid_set_tile(grid, width, 15, 0, water_tile); hex_grid_set_tile(grid, width, 15, 10, waves_tile); g_object_unref(water_tile); g_object_unref(swamp_tile); g_object_unref(lt_for_tile); g_object_unref(hv_for_tile); g_object_unref(waves_tile); /* Test the resize algorithm */ hex_grid_set_size(grid, width + 5, height + 5); hex_grid_set_size(grid, width, height); /* Add a walker */ smile = hex_data_new_from_xpm_d((const gchar**)smile_xpm); *walker = hex_walker_new(smile); hex_walker_set_position(*walker, 30, 17, 1000); hex_walker_connect_grid(*walker, grid); hex_data_unref(smile); /* Set up the display */ disp = hex_display_gtk_new(grid); g_signal_connect_data(*walker, "moved", (GCallback) walker_moved, disp, NULL, (GConnectFlags) 0); g_object_unref(grid); return disp; } static gboolean got_a_keypress (HexDisplayGtk *disp, GdkEventKey *event, HexWalker *walker) { HexDirection direction; switch(event->keyval) { case GDK_KP_7: case GDK_KP_Home: direction = HEX_DIRECTION_NW; break; case GDK_KP_8: case GDK_KP_Up: direction = HEX_DIRECTION_N; break; case GDK_KP_9: case GDK_KP_Page_Up: direction = HEX_DIRECTION_NE; break; case GDK_KP_1: case GDK_KP_End: direction = HEX_DIRECTION_SW; break; case GDK_KP_2: case GDK_KP_Down: direction = HEX_DIRECTION_S; break; case GDK_KP_3: case GDK_KP_Page_Down: direction = HEX_DIRECTION_SE; break; case GDK_Escape: gtk_main_quit(); return TRUE; default: return FALSE; } hex_walker_move(walker, direction, 1); return TRUE; } static void walker_moved (HexWalker *walker, HexDisplayGtk *disp) { hex_display_gtk_center_hex(disp, walker->x, walker->y); }