int main(int argc, char* argv[]) { gtk_init (&argc, &argv); MainWindow* main_window = main_window_new(); // Declaring Level and cpSpace structures Level* l; cpSpace* physics_world; // function that returns a filename depending on the level number selected char* file_name = level_file_name(main_window->level_number); // These functions allocate space and initializes the Level structure // and passes the structure to the World for the world to be initialized l = level_new(file_name); physics_world = world_physics_new(l); World* world = game_view_get_world(main_window_get_game_view(main_window)); world_physics_add_box(physics_world, .5, .5, .1, .1, 0); world_load_space(world, physics_world); main_window_begin(main_window); // This function frees memory allocated for the Level structure and the sub-structures // contained within it level_destroy(l); return EXIT_SUCCESS; }
/* Ready a game structure for actual use: */ void game_finalize(GameData *gd) { Level *lvl; TankList *tl; DrawBuffer *b; PList *pl; Screen *s; ASSERT_CONFIG(); /* Initialize most of the structures: */ s = screen_new (gd->data.config.is_fullscreen); pl = plist_new (); b = drawbuffer_new(gd->data.config.w, gd->data.config.h); lvl = level_new (b, gd->data.config.w, gd->data.config.h); tl = tanklist_new (lvl, pl); /* Generate our random level: */ generate_level(lvl, gd->data.config.gen); level_decorate(lvl); level_make_bases(lvl); /* Debug the starting data, if we're debugging: */ if(gd->is_debug) level_dump_bmp(lvl, "debug_start.bmp"); /* Start drawing! */ drawbuffer_set_default(b, color_rock); level_draw_all(lvl, b); screen_set_mode_level(s, b); /* Set up the players/GUI: */ if (gd->data.config.player_count == 1) init_single_player(s, tl, lvl); else if(gd->data.config.player_count == 2) init_double_player(s, tl, lvl); else { ERR_OUT("Don't know how to draw more than 2 players at once..."); exit(1); } /* Copy all of our variables into the GameData struct: */ gd->is_active = 1; gd->data.active.s = s; gd->data.active.pl = pl; gd->data.active.b = b; gd->data.active.lvl = lvl; gd->data.active.tl = tl; }
world_t world_create(const char *dir) { struct _world *w; char *path; w = calloc(1, sizeof(*w)); if ( NULL == w ) goto out; w->path = strdup(dir); if ( NULL == w->path ) goto out_free; w->level_dat = level_new(); if ( NULL == w->level_dat ) goto out_free; /* create the dir */ if ( !have_dir(w->path) ) goto out_free_level; /* Open regular world */ if ( asprintf(&path, "%s/region", dir) < 0 ) goto out_free_level; w->overworld = dim_create(path); free(path); if ( NULL == w->overworld ) { fprintf(stderr, "world: open: overworld create failed\n"); goto out_free_level; } goto out; out_free_level: level_put(w->level_dat); out_free: level_put(w->level_dat); dim_close(w->overworld); dim_close(w->nether); free(w->path); free(w); w = NULL; out: return w; }
static level *level_load(char *filename) { FILE *fp; char buf[FILENAME_MAX+10]; char fname[FILENAME_MAX]; level *level; int nlines; fname[sizeof(fname)-1] = '\0'; strncpy(fname, LEVELDIR, sizeof(fname)); strncpy(fname + strlen(fname), filename, sizeof(fname)-strlen(fname)); if (fname[sizeof(fname)-1] != '\0') { fatal("File name length overflow"); } fp = fopen(fname, "r"); if (!fp) { fatal("Unable to read level file"); } level = level_new(); level->title = NULL; level->width = level->height = -1; nlines = 0; while (fgets(buf, sizeof(buf), fp)) { if (buf[strlen(buf)-1] != '\n') { fatal("Line length overflow in level set file"); } buf[strcspn(buf, "\r\n")] = '\0'; if (ishdr(buf, "Title: ")) { if (level->title) { fatal("Multiple titles in level file"); } level->title = dupstr(buf + 7); } else if (ishdr(buf, "Width: ")) { level->width = atoi(buf + 7); } else if (ishdr(buf, "Height: ")) { level->height = atoi(buf + 8); } else if (ishdr(buf, "Map: ")) { if (level->leveldata == NULL) { fatal("Map before size in level file"); } if ((int)strlen(buf + 5) != level->width) { fatal("Wrong length map line in level file"); } if (nlines >= level->height) { fatal("Too many map lines in level file"); } memcpy(level->leveldata + level->width * nlines, buf + 5, level->width); nlines++; } else { fatal("Unrecognised keyword in level file"); } if (level->width > 0 && level->height > 0 && level->leveldata == NULL) level_setsize(level, level->width, level->height); } if (nlines < level->height) { fatal("Not enough map lines in level file"); } fclose(fp); return level; }
/* * Create the wid_game_map_client */ void wid_game_map_client_wid_create (void) { if (sdl_is_exiting()) { return; } if (wid_game_map_client_window) { return; } wid_notify_flush(); LOG("Client: Create map"); { fpoint tl = {0.0f, 0.0f}; fpoint br = {1.0f, 1.0f}; wid_game_map_client_window = wid_new_square_window("wid_game_map_client"); wid_set_movable(wid_game_map_client_window, false); wid_set_do_not_raise(wid_game_map_client_window, true); wid_set_no_shape(wid_game_map_client_window); wid_set_mode(wid_game_map_client_window, WID_MODE_NORMAL); wid_set_text_advance(wid_game_map_client_window, 0.9f); wid_set_text_scaling(wid_game_map_client_window, 2.0f); wid_set_text_pos(wid_game_map_client_window, true, 0.5f, 0.10f); wid_set_text_bot(wid_game_map_client_window, true); wid_set_text_lhs(wid_game_map_client_window, true); wid_set_tl_br_pct(wid_game_map_client_window, tl, br); fsize sz = {0.0f, 0.0f}; wid_set_tex_tl(wid_game_map_client_window, sz); fsize sz2 = {1.0f, 1.0f}; wid_set_tex_br(wid_game_map_client_window, sz2); wid_set_on_key_down(wid_game_map_client_window, wid_game_map_key_event); wid_set_on_joy_down(wid_game_map_client_window, wid_game_map_joy_event); } { fpoint tl = {0.00f, 0.00f}; fpoint br = {1.00f, 1.00f}; wid_game_map_client_grid_container = wid_new_container(wid_game_map_client_window, "wid game client grid container"); wid_set_no_shape(wid_game_map_client_grid_container); wid_set_color(wid_game_map_client_grid_container, WID_COLOR_TL, BLACK); wid_set_color(wid_game_map_client_grid_container, WID_COLOR_BG, BLACK); wid_set_color(wid_game_map_client_grid_container, WID_COLOR_BR, BLACK); wid_set_on_mouse_motion(wid_game_map_client_grid_container, wid_game_map_client_receive_mouse_motion); wid_set_tl_br_pct(wid_game_map_client_grid_container, tl, br); wid_set_tex(wid_game_map_client_grid_container, 0, 0); wid_set_on_key_down(wid_game_map_client_grid_container, wid_game_map_key_event); wid_set_on_joy_down(wid_game_map_client_grid_container, wid_game_map_joy_event); LOG("Client: Created map container window"); } { double base_tile_width = ((1.0f / ((double)TILES_SCREEN_WIDTH)) * (double)global_config.video_gl_width); double base_tile_height = ((1.0f / ((double)TILES_SCREEN_HEIGHT)) * (double)global_config.video_gl_height); fpoint tl = { 0, 0 }; fpoint br = { 0, 0 }; br.x += base_tile_width; br.y += base_tile_height; client_tile_width = br.x - tl.x; client_tile_height = br.y - tl.y; client_tile_width = br.x - tl.x; client_tile_height = br.y - tl.y; if (!client_tile_width) { client_tile_width = TILE_WIDTH; } if (!client_tile_height) { client_tile_height = TILE_HEIGHT; } wid_new_grid(wid_game_map_client_grid_container, MAP_WIDTH, MAP_HEIGHT, client_tile_width, client_tile_height); LOG("Client: Created map container window grid"); } /* * Mark that we want to learn the starting stats so we can use those * when starting again with this player type. */ global_config.starting_stats_inited = false; level_pos_t level_pos = global_config.stats.level_pos; if (!level_pos.x && !level_pos.y) { level_pos.x = (myrand() % LEVEL_INITIAL_RANDOM) + 1; level_pos.y = 1; } client_level = level_new(wid_game_map_client_grid_container, level_pos, false /* is_editor */, false /* is_map_editor */, false /* on_server */); LOG("Client: Created level %d.%d", level_pos.y, level_pos.x); if (!client_level) { WARN("failed to load level %u.%u", level_pos.y, level_pos.x); } wid_game_map_client_vert_scroll = wid_new_vert_scroll_bar(wid_game_map_client_window, wid_game_map_client_grid_container); wid_game_map_client_horiz_scroll = wid_new_horiz_scroll_bar(wid_game_map_client_window, wid_game_map_client_grid_container); wid_visible(wid_get_parent(wid_game_map_client_vert_scroll), 0); wid_visible(wid_get_parent(wid_game_map_client_horiz_scroll), 0); wid_visible(wid_game_map_client_vert_scroll, 0); wid_visible(wid_game_map_client_horiz_scroll, 0); wid_update(wid_game_map_client_vert_scroll); wid_update(wid_game_map_client_horiz_scroll); wid_game_map_client_score_update(client_level, true /* redo */); wid_hide(wid_game_map_client_window, 0); if (global_config.server_current_players > 1) { wid_visible(wid_chat_window, 0); } }