void dxModel::Load(const char * filename, bool fullpath) { Free(); FS_Change(FS_LOADING); loading_percent = 0; StrPool_Clear(); DX_SAFE_FREE(mod_base); //free old map char fullfilename[MAX_PATH]; if (fullpath) { static char temp[MAX_PATH]; static char dirname[MAX_PATH]; Str_ExtractDirName(filename, temp); Str_ExtractDirName(temp, dirname); Str_ExtractDirName(dirname, temp); strcat_s(temp, "\\valve"); sysvar.basedir = dirname; sysvar.basedir_valve = temp; strcpy_s(fullfilename, filename); } else { sysvar.basedir = default_basedir; sysvar.basedir = default_basedir; sprintf_s(fullfilename, MAX_PATH, "%s\\maps\\%s.bsp", sysvar.basedir, filename); } FILE * f = NULL; fopen_s(&f, fullfilename, "rb"); if (!f) { Sys_Error("could not open file %s", fullfilename); } fseek(f, 0, SEEK_END); long filesize = ftell(f); fseek(f, 0, SEEK_SET); mod_base = (char*)DX_MEM_ALLOC(filesize); if (!mod_base) { fclose(f); Sys_Error("could not alloc memory"); } fread(mod_base, 1, filesize, f); fclose(f); dheader_s * header = (dheader_s*)mod_base; int v = Swap_LittleLong(header->version); if (v != BSPVERSION) { Sys_Error("bad bsp version"); } for (int i = 0; i < HEADER_LUMPS; i++) { header->lumps[i].fileofs = Swap_LittleLong(header->lumps[i].fileofs); header->lumps[i].filelen = Swap_LittleLong(header->lumps[i].filelen); } UpdateLoadingPercent(6); //load entities first, we need wad file list LoadEntities(&header->lumps[LUMP_ENTITIES]); UpdateLoadingPercent(12); LoadVertexes(&header->lumps[LUMP_VERTEXES]); UpdateLoadingPercent(18); LoadEdges(&header->lumps[LUMP_EDGES]); UpdateLoadingPercent(24); LoadSurfedges(&header->lumps[LUMP_SURFEDGES]); UpdateLoadingPercent(30); LoadTextures(&header->lumps[LUMP_TEXTURES]); UpdateLoadingPercent(36); LoadLighting(&header->lumps[LUMP_LIGHTING]); UpdateLoadingPercent(42); LoadPlanes(&header->lumps[LUMP_PLANES]); UpdateLoadingPercent(48); LoadTexinfo(&header->lumps[LUMP_TEXINFO]); UpdateLoadingPercent(54); LoadFaces(&header->lumps[LUMP_FACES]); UpdateLoadingPercent(60); LoadMarksurfaces(&header->lumps[LUMP_MARKSURFACES]); UpdateLoadingPercent(66); LoadVisibility(&header->lumps[LUMP_VISIBILITY]); UpdateLoadingPercent(72); LoadLeafs(&header->lumps[LUMP_LEAFS]); UpdateLoadingPercent(78); LoadNodes(&header->lumps[LUMP_NODES]); UpdateLoadingPercent(84); LoadClipnodes(&header->lumps[LUMP_CLIPNODES]); UpdateLoadingPercent(90); LoadSubmodels(&header->lumps[LUMP_MODELS]); UpdateLoadingPercent(100); LoadSky(); DX_SAFE_FREE(mod_base); visleafs = numleafs; if (numsubmodels > 0) { visleafs = submodels[0].visleafs; } InitViewer(); loaded = true; FS_Change(FS_DEMO); }
int Map_Load (const char *name) { int direxists; map_error = NULL; memset (&loadmap, 0, sizeof(loadmap)); if (Data_IsDir(name, &direxists) && direxists) { loaddir = name; Get = GetFromDir; } else { /* no directory with the map name; try a pak */ const char *ext = ".pak"; char path[2048]; if (strlen(name) > (sizeof(path) - strlen(ext) - 1)) return Error("map name too long"); strcpy (path, name); strcat (path, ext); if ((loadpak = Pak_Open(path)) == NULL) return Error("unable to open \"%s\"", path); Get = GetFromPak; } if (!LoadPlanes()) goto failed; if (!LoadVertices()) goto failed; if (!LoadEdges()) goto failed; if (!LoadEdgeLoops()) goto failed; if (!LoadSurfaces()) goto failed; if (!LoadPortals()) goto failed; if (!LoadNodes()) goto failed; if (!LoadLeafs()) goto failed; if (!LoadTextures()) goto failed; /* success; get rid of current map & switch over */ Map_Unload (); map = loadmap; memset (&loadmap, 0, sizeof(loadmap)); Get = NULL; loadpak = Pak_Close (loadpak); loaddir = NULL; return 1; failed: /* failure; keep the currently-loaded map running */ FreeMap (&loadmap); Get = NULL; loadpak = Pak_Close (loadpak); loaddir = NULL; return 0; }