Freeing Map Textures On Map Load
Freeing map textures on map load clears the previously loaded map textures -- which in GLQuake are never freed. A benefit of this is never hitting the max texture limit. Additionally, on alternative platform engines with very limited memory (like the PSP, for instance) clearing world textures might be beneficial [since the PSP isn't using OpenGL, whatever the PSP equivalent of glDeleteTextures would need to be used].
Now deleting all the world textures on isn't instantaneous. And R00k says this about doubles map loading time.Most Quake engines have a defined "texture upload limit" -- where a texture is uploaded via an OpenGL API to the video card. [in the engine check out GL_Upload8 and GL_Upload32 and the mechanics of GL_LoadTexture, etc.]
This texture upload limit might be 512 or 1024 textures or some such number. Loading several maps back to back causes a Quake "system error" where you get "TEXTURES > MAX_TEXTURES" or some such message.
The reason you only want to delete "world" textures (textures associated with maps and such) and not all textures is that you really don't want to have to reload all the 2D graphics like the HUD and the menu, of course.
Quicky and largely complete tutorial skeleton:
1. Create a cvar gl_free_world_textures somewhere and then register it (gl_draw.c or gl_rmisc.c would be one of the better places).
2. When loading a map, mark the textures with a TEX_WORLD flag to have a way to know which are world textures vs. textures for weapons or 2D graphics and such.
3. And when loading a map, check the cvar and call the following if gl_free_world_textures has a non-zero value.
Code: Select all
/*
================
GL_FreeTextures -- BPJ
================
*/
void GL_FreeTextures (void)
{
int i, j;
if (gl_free_world_textures.value == 0)
{
Con_DPrintf("GL_FreeTextures: Not Clearing old Map Textures.\n");
return;
}
for (i = j = 0; i < numgltextures; ++i, ++j)
{
if (gltextures[i].texmode & TEX_WORLD)//Only clear out world textures... for now.
{
Con_DPrintf("GL_FreeTextures: Clearing texture %s\n", gltextures[i].identifier);
glDeleteTextures(1, &gltextures[i].texnum);
--j;
}
else if (j < i)
gltextures[j] = gltextures[i];
}
numgltextures = j;
}
And on a limited memory platform where memory is precious, adapting this to the platform 3D API should help preserve memory resources.