#include #include #include #include #include #include #include "xpms/smile.xpm" #include "xpms/waves1.xpm" #include "xpms/waves2.xpm" static HexDisplaySDL* setup_test_hex_display (SDL_Surface *surface, HexWalker **walker); static gboolean got_a_keypress (SDL_KeyboardEvent *event, HexWalker *walker); static void walker_moved (HexWalker *walker, HexDisplaySDL *disp); int main(int argc, char *argv[]) { SDL_Surface *screen; HexDisplaySDL *display; HexWalker *walker; Uint8 depth = 16; if(hex_sdl_init(SDL_INIT_VIDEO, NULL) < 0) return -1; /* depth = SDL_GetVideoSurface()->format->BitsPerPixel; */ screen = SDL_SetVideoMode(500, 500, depth, SDL_ANYFORMAT | SDL_SRCALPHA); display = setup_test_hex_display(screen, &walker); hex_display_sdl_center_hex(display, walker->x, walker->y); hex_poll_sdl_add_object(SDL_KEYDOWNMASK, (HexPollSDLSourceFunc) got_a_keypress, G_OBJECT(walker)); hex_sdl_run(); return 0; } static HexDisplaySDL* setup_test_hex_display (SDL_Surface *surface, HexWalker **walker) { HexGrid *grid; HexDisplaySDL *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_sdl_new(grid, surface, NULL); g_signal_connect_data(*walker, "moved", (GCallback) walker_moved, disp, NULL, (GConnectFlags) 0); g_object_unref(grid); return disp; } static gboolean got_a_keypress (SDL_KeyboardEvent *event, HexWalker *walker) { HexDirection direction; g_assert(event->type == SDL_KEYDOWN); switch(event->keysym.sym) { case SDLK_KP7: case SDLK_HOME: direction = HEX_DIRECTION_NW; break; case SDLK_KP8: case SDLK_UP: direction = HEX_DIRECTION_N; break; case SDLK_KP9: case SDLK_PAGEUP: direction = HEX_DIRECTION_NE; break; case SDLK_KP1: case SDLK_END: direction = HEX_DIRECTION_SW; break; case SDLK_KP2: case SDLK_DOWN: direction = HEX_DIRECTION_S; break; case SDLK_KP3: case SDLK_PAGEDOWN: direction = HEX_DIRECTION_SE; break; case SDLK_ESCAPE: hex_sdl_quit(); return TRUE; default: return FALSE; } hex_walker_move(walker, direction, 1); return TRUE; } static void walker_moved (HexWalker *walker, HexDisplaySDL *disp) { hex_display_sdl_center_hex(disp, walker->x, walker->y); }