- Got rid of most TexMan.AddPatch calls because they are no longer needed.

- Got rid of R_InitPatches because the new texture init code needs to preload
  everything to work correctly.
- Rewrote texture manager initialization to order textures primarily by WAD 
  rather than by type. This way later textures will always override earlier 
  ones. The only exception is that TEX_MiscPatch are only used as a fallback
  if nothing else can be found.
- Optimized the tryany case of FTextureManager::CheckForTexture. It is not
  necessary to scan the hash chain twice. The required information can be
  retrieved during the first pass as easily and even offers a little more
  control.
- Made FFont destructor virtual.
- Added 'Ice' translation to DECORATE.

(Caution: Not fully tested yet!)

SVN r715 (trunk)
This commit is contained in:
Christoph Oelckers 2008-01-26 23:20:34 +00:00
commit 1b28557341
20 changed files with 1430 additions and 1364 deletions

View file

@ -60,7 +60,6 @@ static int R_CountGroup (const char *start, const char *end);
static int R_CountTexturesX ();
static int R_CountLumpTextures (int lumpnum);
extern void R_InitBuildTiles();
extern void R_DeinitBuildTiles();
extern int R_CountBuildTiles();
@ -80,539 +79,6 @@ BYTE** warpedflats;
int* flatwarpedwhen;
FTextureManager TexMan;
FTextureManager::FTextureManager ()
{
memset (HashFirst, -1, sizeof(HashFirst));
// Texture 0 is a dummy texture used to indicate "no texture"
AddTexture (new FDummyTexture);
}
FTextureManager::~FTextureManager ()
{
for (unsigned int i = 0; i < Textures.Size(); ++i)
{
delete Textures[i].Texture;
}
}
int FTextureManager::CheckForTexture (const char *name, int usetype, BITFIELD flags)
{
int i;
if (name == NULL || name[0] == '\0')
{
return -1;
}
// [RH] Doom counted anything beginning with '-' as "no texture".
// Hopefully nobody made use of that and had textures like "-EMPTY",
// because -NOFLAT- is a valid graphic for ZDoom.
if (name[0] == '-' && name[1] == '\0')
{
return 0;
}
i = HashFirst[MakeKey (name) % HASH_SIZE];
while (i != HASH_END)
{
const FTexture *tex = Textures[i].Texture;
if (stricmp (tex->Name, name) == 0)
{
// The name matches, so check the texture type
if (usetype == FTexture::TEX_Any)
{
// All NULL textures should actually return 0
return tex->UseType==FTexture::TEX_Null? 0 : i;
}
else if ((flags & TEXMAN_Overridable) && tex->UseType == FTexture::TEX_Override)
{
return i;
}
else if (tex->UseType == usetype)
{
return i;
}
}
i = Textures[i].HashNext;
}
if ((flags & TEXMAN_TryAny) && usetype != FTexture::TEX_Any)
{
return CheckForTexture (name, FTexture::TEX_Any, flags & ~TEXMAN_TryAny);
}
return -1;
}
int FTextureManager::ListTextures (const char *name, TArray<int> &list)
{
int i;
if (name == NULL || name[0] == '\0')
{
return 0;
}
// [RH] Doom counted anything beginning with '-' as "no texture".
// Hopefully nobody made use of that and had textures like "-EMPTY",
// because -NOFLAT- is a valid graphic for ZDoom.
if (name[0] == '-' && name[1] == '\0')
{
return 0;
}
i = HashFirst[MakeKey (name) % HASH_SIZE];
while (i != HASH_END)
{
const FTexture *tex = Textures[i].Texture;
if (stricmp (tex->Name, name) == 0)
{
// NULL textures must be ignored.
if (tex->UseType!=FTexture::TEX_Null)
{
unsigned int j;
for(j = 0; j < list.Size(); j++)
{
// Check for overriding definitions from newer WADs
if (Textures[list[j]].Texture->UseType == tex->UseType) break;
}
if (j==list.Size()) list.Push(i);
}
}
i = Textures[i].HashNext;
}
return list.Size();
}
int FTextureManager::GetTexture (const char *name, int usetype, BITFIELD flags)
{
int i;
if (name == NULL || name[0] == 0)
{
return 0;
}
else
{
i = CheckForTexture (name, usetype, flags | TEXMAN_TryAny);
}
if (i == -1)
{
// Use a default texture instead of aborting like Doom did
Printf ("Unknown texture: \"%s\"\n", name);
i = DefaultTexture;
}
return i;
}
void FTextureManager::WriteTexture (FArchive &arc, int picnum)
{
FTexture *pic;
if ((size_t)picnum >= Textures.Size())
{
pic = Textures[0].Texture;
}
else
{
pic = Textures[picnum].Texture;
}
arc.WriteCount (pic->UseType);
arc.WriteName (pic->Name);
}
int FTextureManager::ReadTexture (FArchive &arc)
{
int usetype;
const char *name;
usetype = arc.ReadCount ();
name = arc.ReadName ();
return GetTexture (name, usetype);
}
void FTextureManager::UnloadAll ()
{
for (unsigned int i = 0; i < Textures.Size(); ++i)
{
Textures[i].Texture->Unload ();
}
}
int FTextureManager::AddTexture (FTexture *texture)
{
// Later textures take precedence over earlier ones
size_t bucket = MakeKey (texture->Name) % HASH_SIZE;
TextureHash hasher = { texture, HashFirst[bucket] };
WORD trans = Textures.Push (hasher);
Translation.Push (trans);
HashFirst[bucket] = trans;
return trans;
}
// Calls FTexture::CreateTexture and adds the texture to the manager.
int FTextureManager::CreateTexture (int lumpnum, int usetype)
{
if (lumpnum != -1)
{
FTexture *out = FTexture::CreateTexture(lumpnum, usetype);
if (out != NULL) return AddTexture (out);
else
{
Printf (TEXTCOLOR_ORANGE "Invalid data encountered for texture %s\n", Wads.GetLumpFullName(lumpnum));
return -1;
}
}
return -1;
}
void FTextureManager::ReplaceTexture (int picnum, FTexture *newtexture, bool free)
{
if ((size_t)picnum >= Textures.Size())
return;
FTexture *oldtexture = Textures[picnum].Texture;
strcpy (newtexture->Name, oldtexture->Name);
newtexture->UseType = oldtexture->UseType;
Textures[picnum].Texture = newtexture;
if (free)
{
delete oldtexture;
}
}
int FTextureManager::AddPatch (const char *patchname, int namespc, bool tryany)
{
if (patchname == NULL)
{
return -1;
}
int lumpnum = CheckForTexture (patchname, FTexture::TEX_MiscPatch, tryany);
if (lumpnum >= 0)
{
return lumpnum;
}
lumpnum = Wads.CheckNumForName (patchname, namespc==ns_global? ns_graphics:namespc);
if (lumpnum < 0)
{
return -1;
}
return CreateTexture (lumpnum, FTexture::TEX_MiscPatch);
}
void FTextureManager::AddGroup(const char * startlump, const char * endlump, int ns, int usetype)
{
int firsttx = Wads.CheckNumForName (startlump);
int lasttx = Wads.CheckNumForName (endlump);
char name[9];
if (firsttx == -1 || lasttx == -1)
{
return;
}
name[8] = 0;
// Go from first to last so that ANIMDEFS work as expected. However,
// to avoid duplicates (and to keep earlier entries from overriding
// later ones), the texture is only inserted if it is the one returned
// by doing a check by name in the list of wads.
for (firsttx += 1; firsttx < lasttx; ++firsttx)
{
Wads.GetLumpName (name, firsttx);
if (Wads.CheckNumForName (name, ns) == firsttx)
{
CreateTexture (firsttx, usetype);
}
StartScreen->Progress();
}
}
//==========================================================================
//
// Adds all hires texture definitions.
//
//==========================================================================
void FTextureManager::AddHiresTextures ()
{
int firsttx = Wads.CheckNumForName ("HI_START");
int lasttx = Wads.CheckNumForName ("HI_END");
char name[9];
TArray<int> tlist;
if (firsttx == -1 || lasttx == -1)
{
return;
}
name[8] = 0;
for (firsttx += 1; firsttx < lasttx; ++firsttx)
{
tlist.Clear();
Wads.GetLumpName (name, firsttx);
if (Wads.CheckNumForName (name, ns_hires) == firsttx)
{
int amount = ListTextures(name, tlist);
if (amount == 0)
{
int oldtex = AddPatch(name);
if (oldtex >= 0) tlist.Push(oldtex);
}
if (tlist.Size() == 0)
{
// A texture with this name does not yet exist
FTexture * newtex = FTexture::CreateTexture (firsttx, FTexture::TEX_Any);
newtex->UseType=FTexture::TEX_Override;
AddTexture(newtex);
}
else
{
for(unsigned int i = 0; i < tlist.Size(); i++)
{
FTexture * newtex = FTexture::CreateTexture (firsttx, FTexture::TEX_Any);
if (newtex != NULL)
{
int oldtexno = tlist[i];
FTexture * oldtex = Textures[oldtexno].Texture;
// Replace the entire texture and adjust the scaling and offset factors.
newtex->bWorldPanning = true;
newtex->SetScaledSize(oldtex->GetScaledWidth(), oldtex->GetScaledHeight());
newtex->LeftOffset = FixedMul(oldtex->GetScaledLeftOffset(), newtex->xScale);
newtex->TopOffset = FixedMul(oldtex->GetScaledTopOffset(), newtex->yScale);
ReplaceTexture(oldtexno, newtex, true);
}
}
}
StartScreen->Progress();
}
}
}
//==========================================================================
//
// Loads the HIRESTEX lumps
//
//==========================================================================
void FTextureManager::LoadHiresTex()
{
int remapLump, lastLump;
char src[9];
bool is32bit;
int width, height;
int type, mode;
TArray<int> tlist;
lastLump = 0;
src[8] = '\0';
while ((remapLump = Wads.FindLump("HIRESTEX", &lastLump)) != -1)
{
FScanner sc(remapLump, "HIRESTEX");
while (sc.GetString())
{
if (sc.Compare("remap")) // remap an existing texture
{
sc.MustGetString();
// allow selection by type
if (sc.Compare("wall")) type=FTexture::TEX_Wall, mode=FTextureManager::TEXMAN_Overridable;
else if (sc.Compare("flat")) type=FTexture::TEX_Flat, mode=FTextureManager::TEXMAN_Overridable;
else if (sc.Compare("sprite")) type=FTexture::TEX_Sprite, mode=0;
else type = FTexture::TEX_Any, mode = 0;
sc.String[8]=0;
tlist.Clear();
int amount = ListTextures(sc.String, tlist);
if (amount == 0)
{
int oldtex = AddPatch(sc.String);
if (oldtex >= 0) tlist.Push(oldtex);
}
FName texname = sc.String;
sc.MustGetString();
int lumpnum = Wads.CheckNumForFullName(sc.String);
if (lumpnum < 0) lumpnum = Wads.CheckNumForName(sc.String, ns_graphics);
if (tlist.Size() == 0)
{
Printf("Attempting to remap non-existent texture %s to %s\n",
texname.GetChars(), sc.String);
}
else
{
for(unsigned int i = 0; i < tlist.Size(); i++)
{
FTexture * oldtex = Textures[tlist[i]].Texture;
int sl;
// only replace matching types. For sprites also replace any MiscPatches
// based on the same lump. These can be created for icons.
if (oldtex->UseType == type || type == FTexture::TEX_Any ||
(mode == TEXMAN_Overridable && oldtex->UseType == FTexture::TEX_Override) ||
(type == FTexture::TEX_Sprite && oldtex->UseType == FTexture::TEX_MiscPatch &&
(sl=oldtex->GetSourceLump()) >= 0 && Wads.GetLumpNamespace(sl) == ns_sprites)
)
{
FTexture * newtex = FTexture::CreateTexture (lumpnum, FTexture::TEX_Any);
if (newtex != NULL)
{
// Replace the entire texture and adjust the scaling and offset factors.
newtex->bWorldPanning = true;
newtex->SetScaledSize(oldtex->GetScaledWidth(), oldtex->GetScaledHeight());
newtex->LeftOffset = FixedMul(oldtex->GetScaledLeftOffset(), newtex->xScale);
newtex->TopOffset = FixedMul(oldtex->GetScaledTopOffset(), newtex->yScale);
ReplaceTexture(tlist[i], newtex, true);
}
}
}
}
}
else if (sc.Compare("define")) // define a new "fake" texture
{
sc.GetString();
memcpy(src, sc.String, 8);
int lumpnum = Wads.CheckNumForFullName(sc.String);
if (lumpnum < 0) lumpnum = Wads.CheckNumForName(sc.String, ns_graphics);
sc.GetString();
is32bit = !!sc.Compare("force32bit");
if (!is32bit) sc.UnGet();
sc.GetNumber();
width = sc.Number;
sc.GetNumber();
height = sc.Number;
if (lumpnum>=0)
{
FTexture *newtex = FTexture::CreateTexture(lumpnum, FTexture::TEX_Override);
if (newtex != NULL)
{
// Replace the entire texture and adjust the scaling and offset factors.
newtex->bWorldPanning = true;
newtex->SetScaledSize(width, height);
memcpy(newtex->Name, src, sizeof(newtex->Name));
int oldtex = TexMan.CheckForTexture(src, FTexture::TEX_Override);
if (oldtex>=0) TexMan.ReplaceTexture(oldtex, newtex, true);
else TexMan.AddTexture(newtex);
}
}
//else Printf("Unable to define hires texture '%s'\n", tex->Name);
}
}
}
}
void FTextureManager::AddPatches (int lumpnum)
{
FWadLump *file = Wads.ReopenLumpNum (lumpnum);
DWORD numpatches, i;
char name[9];
*file >> numpatches;
name[8] = 0;
for (i = 0; i < numpatches; ++i)
{
file->Read (name, 8);
if (CheckForTexture (name, FTexture::TEX_WallPatch, false) == -1)
{
CreateTexture (Wads.CheckNumForName (name, ns_patches), FTexture::TEX_WallPatch);
}
StartScreen->Progress();
}
delete file;
}
//
// R_InitTextures
// Initializes the texture list with the textures from the world map.
//
void R_InitTextures (void)
{
int lastlump = 0, lump;
int texlump1 = -1, texlump2 = -1, texlump1a, texlump2a;
int i;
int pfile = -1;
// For each PNAMES lump, load the TEXTURE1 and/or TEXTURE2 lumps from the same wad.
while ((lump = Wads.FindLump ("PNAMES", &lastlump)) != -1)
{
pfile = Wads.GetLumpFile (lump);
TexMan.AddPatches (lump);
texlump1 = Wads.CheckNumForName ("TEXTURE1", ns_global, pfile);
texlump2 = Wads.CheckNumForName ("TEXTURE2", ns_global, pfile);
TexMan.AddTexturesLumps (texlump1, texlump2, lump);
}
// If the final TEXTURE1 and/or TEXTURE2 lumps are in a wad without a PNAMES lump,
// they have not been loaded yet, so load them now.
texlump1a = Wads.CheckNumForName ("TEXTURE1");
texlump2a = Wads.CheckNumForName ("TEXTURE2");
if (texlump1a != -1 && (texlump1a == texlump1 || Wads.GetLumpFile (texlump1a) <= pfile))
{
texlump1a = -1;
}
if (texlump2a != -1 && (texlump2a == texlump2 || Wads.GetLumpFile (texlump2a) <= pfile))
{
texlump2a = -1;
}
TexMan.AddTexturesLumps (texlump1a, texlump2a, Wads.GetNumForName ("PNAMES"));
// The Hexen scripts use BLANK as a blank texture, even though it's really not.
// I guess the Doom renderer must have clipped away the line at the bottom of
// the texture so it wasn't visible. I'll just map it to 0, so it really is blank.
if (gameinfo.gametype == GAME_Hexen &&
0 <= (i = TexMan.CheckForTexture ("BLANK", FTexture::TEX_Wall, false)))
{
TexMan.SetTranslation (i, 0);
}
// Hexen parallax skies use color 0 to indicate transparency on the front
// layer, so we must not remap color 0 on these textures. Unfortunately,
// the only way to identify these textures is to check the MAPINFO.
for (unsigned int i = 0; i < wadlevelinfos.Size(); ++i)
{
if (wadlevelinfos[i].flags & LEVEL_DOUBLESKY)
{
int picnum = TexMan.CheckForTexture (wadlevelinfos[i].skypic1, FTexture::TEX_Wall, false);
if (picnum > 0)
{
TexMan[picnum]->SetFrontSkyLayer ();
}
}
}
}
static struct FakeCmap {
char name[8];
PalEntry blend;
@ -795,16 +261,8 @@ void R_InitData ()
{
FTexture::InitGrayMap();
StartScreen->Progress();
TexMan.AddGroup("S_START", "S_END", ns_sprites, FTexture::TEX_Sprite);
R_InitPatches (); // Initializes "special" textures that have no external references
StartScreen->Progress();
R_InitTextures ();
TexMan.AddGroup("F_START", "F_END", ns_flats, FTexture::TEX_Flat);
R_InitBuildTiles ();
TexMan.AddGroup("TX_START", "TX_END", ns_newtextures, FTexture::TEX_Override);
TexMan.AddHiresTextures ();
TexMan.LoadHiresTex ();
TexMan.DefaultTexture = TexMan.CheckForTexture ("-NOFLAT-", FTexture::TEX_Override, 0);
TexMan.Init();
V_InitFonts();
StartScreen->Progress();
R_InitColormaps ();
@ -1049,215 +507,6 @@ const BYTE *R_GetColumn (FTexture *tex, int col)
return tex->GetColumn (col, NULL);
}
// Add all the miscellaneous 2D patches that are used to the texture manager
// Unfortunately, the wad format does not provide an elegant way to express
// which lumps are patches unless they are used in a wall texture, so I have
// to list them all here.
static void R_InitPatches ()
{
static const char patches[][9] =
{
"CONBACK",
"ADVISOR",
"BOSSBACK",
"PFUB1",
"PFUB2",
"END0",
"END1",
"END2",
"END3",
"END4",
"END5",
"END6",
"FINALE1",
"FINALE2",
"FINALE3",
"CHESSALL",
"CHESSC",
"CHESSM",
"FITEFACE",
"CLERFACE",
"MAGEFACE",
"M_NGAME",
"M_OPTION",
"M_RDTHIS",
"M_QUITG",
"M_JKILL",
"M_ROUGH",
"M_HURT",
"M_ULTRA",
"M_NMARE",
"M_LOADG",
"M_LSLEFT",
"M_LSCNTR",
"M_LSRGHT",
"M_FSLOT",
"M_SAVEG",
"M_DOOM",
"M_HTIC",
"M_STRIFE",
"M_NEWG",
"M_NGAME",
"M_SKILL",
"M_EPISOD",
"M_EPI1",
"M_EPI2",
"M_EPI3",
"M_EPI4",
"INTERPIC",
"WIOSTK",
"WIOSTI",
"WIF",
"WIMSTT",
"WIOSTS",
"WIOSTF",
"WITIME",
"WIPAR",
"WIMSTAR",
"WIMINUS",
"WIPCNT",
"WICOLON",
"WISUCKS",
"WIFRGS",
"WISCRT2",
"WIENTER",
"WIKILRS",
"WIVCTMS",
"IN_YAH",
"IN_X",
"FONTB13",
"FONTB05",
"FONTB26",
"FONTB15",
"FACEA0",
"FACEB0",
"STFDEAD0",
"STBANY",
"M_PAUSE",
"PAUSED",
"M_SKULL1",
"M_SKULL2",
"M_SLCTR1",
"M_SLCTR2",
"M_CURS1",
"M_CURS2",
"M_CURS3",
"M_CURS4",
"M_CURS5",
"M_CURS6",
"M_CURS7",
"M_CURS8",
"BRDR_TL",
"BRDR_T",
"BRDR_TR",
"BRDR_L",
"BRDR_R",
"BRDR_BL",
"BRDR_B",
"BRDR_BR",
"BORDTL",
"BORDT",
"BORDTR",
"BORDL",
"BORDR",
"BORDBL",
"BORDB",
"BORDBR",
"TITLE",
"CREDIT",
"ORDER",
"HELP",
"HELP1",
"HELP2",
"HELP3",
"HELP0",
"TITLEPIC",
"ENDPIC",
"STTPRCNT",
"STARMS",
"VICTORY2",
"STFBANY",
"STPBANY",
"RGELOGO",
"VELLOGO",
"FINAL1",
"FINAL2",
"E2END"
};
static const char spinners[][9] =
{
"SPINBK%d",
"SPFLY%d",
"SPSHLD%d",
"SPBOOT%d",
"SPMINO%d"
};
static const char classChars[3] = { 'F', 'C', 'M' };
int i, j;
char name[9];
for (i = countof(patches); i >= 0; --i)
{
TexMan.AddPatch (patches[i]);
}
// Some digits
for (i = 9; i >= 0; --i)
{
sprintf (name, "WINUM%d", i);
TexMan.AddPatch (name);
sprintf (name, "FONTB%d", i + 16);
TexMan.AddPatch (name);
sprintf (name, "AMMNUM%d", i);
TexMan.AddPatch (name);
}
// Spinning power up icons for Heretic and Hexen
for (j = countof(spinners)-1; j >= 0; --j)
{
for (i = 0; i <= 15; ++i)
{
sprintf (name, spinners[j], i);
TexMan.AddPatch (name);
}
}
// Player class animations for the Hexen new game menu
for (i = 2; i >= 0; --i)
{
sprintf (name, "M_%cBOX", classChars[i]);
TexMan.AddPatch (name);
for (j = 4; j >= 1; --j)
{
sprintf (name, "M_%cWALK%d", classChars[i], j);
TexMan.AddPatch (name);
}
}
// The spinning skull in Heretic's top-level menu
for (i = 0; i <= 17; ++i)
{
sprintf (name, "M_SKL%.2d", i);
TexMan.AddPatch (name);
}
// Strife story panels
for (i = 0; i <= 7; ++i)
{
sprintf (name, "PANEL%d", i);
TexMan.AddPatch (name);
}
for (i = 2; i <= 6; ++i)
{
for (j = 3 + (i < 5); j > 0; --j)
{
sprintf (name, "SS%dF%d", i, j);
TexMan.AddPatch (name);
}
}
}
#ifdef _DEBUG
// Prints the spans generated for a texture. Only needed for debugging.