Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Karaulov committed Dec 11, 2022
1 parent 888de54 commit d1ea57d
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 117 deletions.
50 changes: 39 additions & 11 deletions src/bsp/Wad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,27 @@ Wad::~Wad(void)
filedata = NULL;
}

bool Wad::readInfo(bool allowempty)
void W_CleanupName(const char* in, char* out)
{
int i;
int c;

for (i = 0; i < MAXTEXTURENAME; i++) {
c = in[i];
if (!c)
break;

if (c >= 'A' && c <= 'Z')
c += ('a' - 'A');
out[i] = c;
}

for (; i < MAXTEXTURENAME; i++)
out[i] = 0;
}


bool Wad::readInfo()
{
std::string file = filename;

Expand Down Expand Up @@ -62,7 +82,6 @@ bool Wad::readInfo(bool allowempty)
int offset = 0;

memcpy((char*)&header, &filedata[offset], sizeof(WADHEADER));
offset += sizeof(WADHEADER);

if (std::string(header.szMagic).find("WAD3") != 0)
{
Expand All @@ -87,7 +106,10 @@ bool Wad::readInfo(bool allowempty)

dirEntries.clear();

bool usableTextures = false;
usableTextures = false;

//logf("D %d %d\n", header.nDirOffset, header.nDir);

for (int i = 0; i < header.nDir; i++)
{
WADDIRENTRY tmpWadEntry = WADDIRENTRY();
Expand All @@ -101,18 +123,19 @@ bool Wad::readInfo(bool allowempty)
memcpy((char*)&tmpWadEntry, &filedata[offset], sizeof(WADDIRENTRY));
offset += sizeof(WADDIRENTRY);

W_CleanupName(tmpWadEntry.szName, tmpWadEntry.szName);

dirEntries.push_back(tmpWadEntry);

if (dirEntries[i].nType == 0x43) usableTextures = true;
}


if (!usableTextures && !allowempty)
if (!usableTextures)
{
dirEntries.clear();
header.nDir = 0;
logf("%s contains no regular textures\n", filename.c_str());
return false; // we can't use these types of textures (see fonts.wad as an example)
logf("Info: %s contains no regular textures\n", basename(filename).c_str());
if (!dirEntries.size())
return false;
}

return true;
Expand All @@ -135,7 +158,7 @@ bool Wad::hasTexture(int dirIndex)
return true;
}

WADTEX* Wad::readTexture(int dirIndex)
WADTEX* Wad::readTexture(int dirIndex, int * texturetype)
{
if (dirIndex < 0 || dirIndex >= dirEntries.size())
{
Expand All @@ -145,10 +168,10 @@ WADTEX* Wad::readTexture(int dirIndex)
//if (cache != NULL)
//return cache[dirIndex];
std::string name = std::string(dirEntries[dirIndex].szName);
return readTexture(name);
return readTexture(name, texturetype);
}

WADTEX* Wad::readTexture(const std::string& texname)
WADTEX* Wad::readTexture(const std::string& texname, int * texturetype)
{
int idx = -1;
for (int d = 0; d < header.nDir; d++)
Expand All @@ -173,6 +196,11 @@ WADTEX* Wad::readTexture(const std::string& texname)

int offset = dirEntries[idx].nFilePos;

if (texturetype)
{
*texturetype = dirEntries[idx].nType;
}

BSPMIPTEX mtex = BSPMIPTEX();
memcpy((char*)&mtex, &filedata[offset], sizeof(BSPMIPTEX));
offset += sizeof(BSPMIPTEX);
Expand Down
12 changes: 7 additions & 5 deletions src/bsp/Wad.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ struct WADTEX
}
};

#pragma pack(pop)


class Wad
{
public:
std::string filename = std::string();

unsigned char* filedata = NULL;
int fileLen = 0;
bool usableTextures = false;

WADHEADER header = WADHEADER();

Expand All @@ -71,7 +75,7 @@ class Wad

~Wad(void);

bool readInfo(bool allowempty = false);
bool readInfo();

bool hasTexture(int dirIndex);
bool hasTexture(const std::string& name);
Expand All @@ -80,11 +84,9 @@ class Wad
bool write(WADTEX** textures, size_t numTex);
bool write(std::vector<WADTEX*> textures);

WADTEX* readTexture(int dirIndex);
WADTEX* readTexture(const std::string& texname);
WADTEX* readTexture(int dirIndex, int* texturetype = NULL);
WADTEX* readTexture(const std::string& texname, int* texturetype = NULL);
};

WADTEX* create_wadtex(const char* name, COLOR3* data, int width, int height);
COLOR3* ConvertWadTexToRGB(WADTEX* wadTex);

#pragma pack(pop)
65 changes: 4 additions & 61 deletions src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ BspRenderer::BspRenderer(Bsp* _map, ShaderProgram* _bspShader, ShaderProgram* _f
memset(&undoLumpState, 0, sizeof(LumpState));

undoEntityState = std::map<int, Entity>();

mapTexsUsage = std::map<std::string, std::set<std::string>>();
}

void BspRenderer::loadTextures()
Expand Down Expand Up @@ -1422,7 +1420,7 @@ BspRenderer::~BspRenderer()

}

void BspRenderer::ReuploadTextures()
void BspRenderer::reuploadTextures()
{
deleteTextures();

Expand All @@ -1432,9 +1430,9 @@ void BspRenderer::ReuploadTextures()

for (int i = 0; i < map->textureCount; i++)
{
if (!glTextures[i]->uploaded)
glTextures[i]->upload(GL_RGB);
glTextures[i]->upload(GL_RGB);
}

numLoadedTextures = map->textureCount;

texturesLoaded = true;
Expand All @@ -1460,62 +1458,7 @@ void BspRenderer::delayLoadData()

if (!texturesLoaded && texturesFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
{
deleteTextures();

glTextures = glTexturesSwap;

for (int i = 0; i < map->textureCount; i++)
{
if (glTextures[i] && !glTextures[i]->uploaded)
glTextures[i]->upload(GL_RGB);
}
numLoadedTextures = map->textureCount;

texturesLoaded = true;
preRenderFaces();

mapTexsUsage.clear();

for (int i = 0; i < map->faceCount; i++)
{
BSPTEXTUREINFO& texinfo = map->texinfos[map->faces[i].iTextureInfo];
int texOffset = ((int*)map->textures)[texinfo.iMiptex + 1];
if (texOffset != -1 && texinfo.iMiptex != -1)
{
BSPMIPTEX& tex = *((BSPMIPTEX*)(map->textures + texOffset));

if (tex.szName[0] != '\0')
{
if (tex.nOffsets[0] <= 0)
{
bool fondTex = false;
for (auto& s : wads)
{
if (s->hasTexture(tex.szName))
{
if (!mapTexsUsage[basename(s->filename)].count(tex.szName))
mapTexsUsage[basename(s->filename)].insert(tex.szName);

fondTex = true;
}
}
if (!fondTex)
{
if (!mapTexsUsage["notfound"].count(tex.szName))
mapTexsUsage["notfound"].insert(tex.szName);
}
}
else
{
if (!mapTexsUsage["internal"].count(tex.szName))
mapTexsUsage["internal"].insert(tex.szName);
}
}
}
}

if (mapTexsUsage.size())
logf("Used %d wad files(include map file)\n", (int)mapTexsUsage.size());
reuploadTextures();
}

if (!clipnodesLoaded && clipnodesFuture.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
Expand Down
4 changes: 1 addition & 3 deletions src/editor/BspRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class BspRenderer

void loadTextures(); // will reload them if already loaded
void reloadTextures();
void ReuploadTextures();
void reuploadTextures();

void updateLightmapInfos();
bool isFinishedLoading();
Expand Down Expand Up @@ -236,8 +236,6 @@ class BspRenderer
std::map<int, Entity> undoEntityState;
LumpState undoLumpState = LumpState();

std::map<std::string, std::set<std::string>> mapTexsUsage;

void pushModelUndoState(const std::string& actionDesc, unsigned int targetLumps);
void pushEntityUndoState(const std::string& actionDesc, int entIdx);
void pushUndoCommand(Command* cmd);
Expand Down
Loading

0 comments on commit d1ea57d

Please sign in to comment.