Skip to content

Commit

Permalink
MAP MIRROR X/Y FEATURE. BUGFIXES.
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Mar 10, 2024
1 parent bc66ff4 commit 14bb9b8
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 99 deletions.
8 changes: 1 addition & 7 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11719,13 +11719,7 @@ int Bsp::getBspTextureSize(int textureid)
sz += sizeof(COLOR3) * 256; // pallette
}

for (int i = 0; i < MIPLEVELS; i++)
{
int div = 1 << i;
int mipWidth = tex->nWidth / div;
int mipHeight = tex->nHeight / div;
sz += mipWidth * mipHeight;
}
sz += calcMipsSize(tex->nWidth, tex->nHeight);
}
return sz;
}
Expand Down
66 changes: 28 additions & 38 deletions src/bsp/Wad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ bool Wad::readInfo()

if (!fileExists(file))
{
print_log(get_localized_string(LANG_0247),filename);
print_log(get_localized_string(LANG_0247), filename);
return false;
}

filedata = (unsigned char*)loadFile(file, fileLen);

if (!filedata)
{
print_log(get_localized_string(LANG_1043),filename);
print_log(get_localized_string(LANG_1043), filename);
filedata = NULL;
return false;
}
Expand All @@ -81,7 +81,7 @@ bool Wad::readInfo()
{
delete[] filedata;
filedata = NULL;
print_log(get_localized_string(LANG_0248),filename);
print_log(get_localized_string(LANG_0248), filename);
return false;
}

Expand All @@ -93,15 +93,15 @@ bool Wad::readInfo()
{
delete[] filedata;
filedata = NULL;
print_log(get_localized_string(LANG_0249),filename);
print_log(get_localized_string(LANG_0249), filename);
return false;
}

if (header.nDirOffset >= (int)fileLen)
{
delete[] filedata;
filedata = NULL;
print_log(get_localized_string(LANG_0250),filename);
print_log(get_localized_string(LANG_0250), filename);
return false;
}

Expand Down Expand Up @@ -215,14 +215,11 @@ WADTEX* Wad::readTexture(const std::string& texname, int* texturetype)
memcpy((char*)&mtex, &filedata[offset], sizeof(BSPMIPTEX));
offset += sizeof(BSPMIPTEX);
if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0255),mtex.szName,mtex.nWidth,mtex.nHeight);
print_log(get_localized_string(LANG_0255), mtex.szName, mtex.nWidth, mtex.nHeight);
int w = mtex.nWidth;
int h = mtex.nHeight;
int sz = w * h; // miptex 0
int sz2 = sz / 4; // miptex 1
int sz3 = sz2 / 4; // miptex 2
int sz4 = sz3 / 4; // miptex 3
int szAll = sz + sz2 + sz3 + sz4 + sizeof(short) + /*pal size*/ sizeof(COLOR3) * 256;

int szAll = calcMipsSize(w, h) + sizeof(short) + /*pal size*/ sizeof(COLOR3) * 256;

unsigned char* data = new unsigned char[szAll];/* 4 bytes padding */

Expand All @@ -232,15 +229,16 @@ WADTEX* Wad::readTexture(const std::string& texname, int* texturetype)

WADTEX* tex = new WADTEX();
memcpy(tex->szName, mtex.szName, MAXTEXTURENAME);

for (int i = 0; i < MIPLEVELS; i++)
tex->nOffsets[i] = mtex.nOffsets[i];
tex->nWidth = mtex.nWidth;
tex->nHeight = mtex.nHeight;
tex->nWidth = w;
tex->nHeight = h;
tex->data = data;
tex->dataLen = szAll;
tex->needclean = true;
if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0256),tex->szName,tex->nWidth,tex->nHeight);
print_log(get_localized_string(LANG_0256), tex->szName, tex->nWidth, tex->nHeight);
return tex;
}

Expand Down Expand Up @@ -272,11 +270,8 @@ bool Wad::write(const std::string& _filename, std::vector<WADTEX*> textures)
{
int w = textures[i]->nWidth;
int h = textures[i]->nHeight;
int sz = w * h; // miptex 0
int sz2 = sz / 4; // miptex 1
int sz3 = sz2 / 4; // miptex 2
int sz4 = sz3 / 4; // miptex 3
int szAll = sz + sz2 + sz3 + sz4 + sizeof(short) + /* pal num */ sizeof(COLOR3) * 256;

int szAll = calcMipsSize(w, h) + sizeof(short) + /* pal num */ sizeof(COLOR3) * 256;

szAll = (szAll + 3) & ~3; // 4 bytes padding

Expand All @@ -302,7 +297,7 @@ bool Wad::write(const std::string& _filename, std::vector<WADTEX*> textures)
int szAll = sz + sz2 + sz3 + sz4 + sizeof(short) /* pal num*/ + sizeof(COLOR3) * 256;

int padding = ((szAll + 3) & ~3) - szAll; // 4 bytes padding

miptex.nWidth = w;
miptex.nHeight = h;
miptex.nOffsets[0] = sizeof(BSPMIPTEX);
Expand All @@ -321,7 +316,7 @@ bool Wad::write(const std::string& _filename, std::vector<WADTEX*> textures)
{
unsigned char* zeropad = new unsigned char[padding];
memset(zeropad, 0, padding);
myFile.write((const char *)zeropad, padding);
myFile.write((const char*)zeropad, padding);
delete[] zeropad;
}
}
Expand All @@ -331,13 +326,8 @@ bool Wad::write(const std::string& _filename, std::vector<WADTEX*> textures)
{
WADDIRENTRY entry = WADDIRENTRY();
entry.nFilePos = offset;
int w = textures[i]->nWidth;
int h = textures[i]->nHeight;
int sz = w * h; // miptex 0
int sz2 = sz / 4; // miptex 1
int sz3 = sz2 / 4; // miptex 2
int sz4 = sz3 / 4; // miptex 3
int szAll = sz + sz2 + sz3 + sz4 + sizeof(short) + /* pal num */ sizeof(COLOR3) * 256;

int szAll = calcMipsSize(textures[i]->nWidth, textures[i]->nHeight) + sizeof(short) + /* pal num */ sizeof(COLOR3) * 256;

szAll = (szAll + 3) & ~3; // 4 bytes padding

Expand Down Expand Up @@ -381,7 +371,7 @@ WADTEX* create_wadtex(const char* name, COLOR3* rgbdata, int width, int height)
mip[0] = new unsigned char[width * height];

bool do_magic = false;
if ( name[0] == '{' )
if (name[0] == '{')
{
int sz = width * height;
for (int i = 0; i < sz; i++)
Expand Down Expand Up @@ -522,7 +512,7 @@ WADTEX* create_wadtex(const char* name, COLOR3* rgbdata, int width, int height)
COLOR3* ConvertWadTexToRGB(WADTEX* wadTex, COLOR3* palette)
{
if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0257),wadTex->szName,wadTex->nWidth,wadTex->nHeight);
print_log(get_localized_string(LANG_0257), wadTex->szName, wadTex->nWidth, wadTex->nHeight);
int lastMipSize = (wadTex->nWidth >> 3) * (wadTex->nHeight >> 3);
if (palette == NULL)
palette = (COLOR3*)(wadTex->data + wadTex->nOffsets[3] + lastMipSize + sizeof(short) - sizeof(BSPMIPTEX));
Expand All @@ -538,14 +528,14 @@ COLOR3* ConvertWadTexToRGB(WADTEX* wadTex, COLOR3* palette)
}

if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0258),wadTex->szName,wadTex->nWidth,wadTex->nHeight);
print_log(get_localized_string(LANG_0258), wadTex->szName, wadTex->nWidth, wadTex->nHeight);
return imageData;
}

COLOR3* ConvertMipTexToRGB(BSPMIPTEX* tex, COLOR3* palette)
{
if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0259),tex->szName,tex->nWidth,tex->nHeight);
print_log(get_localized_string(LANG_0259), tex->szName, tex->nWidth, tex->nHeight);
int lastMipSize = (tex->nWidth >> 3) * (tex->nHeight >> 3);

if (palette == NULL)
Expand All @@ -561,15 +551,15 @@ COLOR3* ConvertMipTexToRGB(BSPMIPTEX* tex, COLOR3* palette)
}

if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0260),tex->szName,tex->nWidth,tex->nHeight);
print_log(get_localized_string(LANG_0260), tex->szName, tex->nWidth, tex->nHeight);
return imageData;
}


COLOR4* ConvertWadTexToRGBA(WADTEX* wadTex, COLOR3* palette, int colors)
{
if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0261),wadTex->szName,wadTex->nWidth,wadTex->nHeight);
print_log(get_localized_string(LANG_0261), wadTex->szName, wadTex->nWidth, wadTex->nHeight);
int lastMipSize = (wadTex->nWidth >> 3) * (wadTex->nHeight >> 3);

if (palette == NULL)
Expand All @@ -592,14 +582,14 @@ COLOR4* ConvertWadTexToRGBA(WADTEX* wadTex, COLOR3* palette, int colors)
}

if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0262),wadTex->szName,wadTex->nWidth,wadTex->nHeight);
print_log(get_localized_string(LANG_0262), wadTex->szName, wadTex->nWidth, wadTex->nHeight);
return imageData;
}

COLOR4* ConvertMipTexToRGBA(BSPMIPTEX* tex, COLOR3* palette, int colors)
{
if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0263),tex->szName,tex->nWidth,tex->nHeight);
print_log(get_localized_string(LANG_0263), tex->szName, tex->nWidth, tex->nHeight);
int lastMipSize = (tex->nWidth >> 3) * (tex->nHeight >> 3);

if (palette == NULL)
Expand All @@ -611,7 +601,7 @@ COLOR4* ConvertMipTexToRGBA(BSPMIPTEX* tex, COLOR3* palette, int colors)

for (int k = 0; k < sz; k++)
{
if (tex->szName[0] == '{' && (colors - 1 == src[k] || palette[src[k]] == COLOR3(0, 0, 255)))
if (tex->szName[0] == '{' && (colors - 1 == src[k] || palette[src[k]] == COLOR3(0, 0, 255)))
{
imageData[k] = COLOR4(0, 0, 0, 0);
}
Expand All @@ -622,7 +612,7 @@ COLOR4* ConvertMipTexToRGBA(BSPMIPTEX* tex, COLOR3* palette, int colors)
}

if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0264),tex->szName,tex->nWidth,tex->nHeight);
print_log(get_localized_string(LANG_0264), tex->szName, tex->nWidth, tex->nHeight);
return imageData;
}

Expand Down
10 changes: 2 additions & 8 deletions src/bsp/Wad.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include "bsplimits.h"
#include "bsptypes.h"
int calcMipsSize(int w, int h);

#pragma pack(push, 1)

Expand Down Expand Up @@ -75,14 +76,7 @@ struct WADTEX
return;
}

int sz = 0;
for (int i = 0; i < MIPLEVELS; i++)
{
int div = 1 << i;
int mipWidth = tex->nWidth / div;
int mipHeight = tex->nHeight / div;
sz += mipWidth * mipHeight;
}
int sz = calcMipsSize(tex->nWidth,tex->nHeight);

dataLen = sz + sizeof(short) + sizeof(COLOR3) * 256;
data = new unsigned char[dataLen];
Expand Down
65 changes: 60 additions & 5 deletions src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3862,6 +3862,63 @@ void Gui::drawMenuBar()
ImGui::EndTooltip();
}

if (ImGui::MenuItem("Mirror map x/y [WIP]", NULL, false, map))
{
for (int i = 0; i < map->vertCount; i++)
{
std::swap(map->verts[i].x, map->verts[i].y);
}

for (int i = 0; i < map->faceCount; i++)
{
int* start = &map->surfedges[map->faces[i].iFirstEdge];
int* end = &map->surfedges[map->faces[i].iFirstEdge + map->faces[i].nEdges];
std::reverse(start, end);
}

for (int i = 0; i < map->planeCount; i++)
{
std::swap(map->planes[i].vNormal.x, map->planes[i].vNormal.y);
map->planes[i].update_plane(false);
}

for (int i = 0; i < map->texinfoCount; i++)
{
std::swap(map->texinfos[i].vS.x, map->texinfos[i].vS.y);
std::swap(map->texinfos[i].vT.x, map->texinfos[i].vT.y);
}

for (int i = 0; i < map->ents.size(); i++)
{
if (!map->ents[i]->origin.IsZero())
{
std::swap(map->ents[i]->origin.x, map->ents[i]->origin.y);
map->ents[i]->setOrAddKeyvalue("origin", map->ents[i]->origin.toKeyvalueString());
}
}

for (int i = 0; i < map->leafCount; i++)
{
std::swap(map->leaves[i].nMins.x, map->leaves[i].nMins.y);
std::swap(map->leaves[i].nMaxs.x, map->leaves[i].nMaxs.y);
}

for (int i = 0; i < map->modelCount; i++)
{
std::swap(map->models[i].nMins.x, map->models[i].nMins.y);
std::swap(map->models[i].nMaxs.x, map->models[i].nMaxs.y);
}

for (int i = 0; i < map->nodeCount; i++)
{
std::swap(map->nodes[i].nMins.x, map->nodes[i].nMins.y);
std::swap(map->nodes[i].nMaxs.x, map->nodes[i].nMaxs.y);
}
app->reloading = true;
rend->reload();
app->reloading = false;
}

if (ImGui::BeginMenu("Scale map (WIP)", map))
{
static bool ScaleOnlySelected = false;
Expand Down Expand Up @@ -4782,11 +4839,9 @@ void Gui::drawMenuBar()
{
int w = tex->nWidth;
int h = tex->nHeight;
int sz = w * h; // miptex 0
int sz2 = sz / 4; // miptex 1
int sz3 = sz2 / 4; // miptex 2
int sz4 = sz3 / 4; // miptex 3
int szAll = sz + sz2 + sz3 + sz4;

int szAll = calcMipsSize(w,h);

unsigned char* texdata = (unsigned char*)(((unsigned char*)tex) + tex->nOffsets[0]);
colors = (int)*(unsigned short*)(texdata + szAll);
}
Expand Down
Loading

0 comments on commit 14bb9b8

Please sign in to comment.