iw4x-client/src/Components/Modules/AssetInterfaces/IGfxImage.cpp

182 lines
4.9 KiB
C++
Raw Normal View History

2016-07-11 11:14:58 -04:00
#include <STDInclude.hpp>
#define IW4X_IMG_VERSION "0"
2016-07-11 11:14:58 -04:00
namespace Assets
{
void IGfxImage::load(Game::XAssetHeader* header, std::string name, Components::ZoneBuilder::Zone* builder)
2016-07-11 11:14:58 -04:00
{
Game::GfxImage* image = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_IMAGE, name.data()).image;
if (image && name[0] != '*') return;
2016-07-11 11:14:58 -04:00
image = builder->getAllocator()->allocate<Game::GfxImage>();
2016-07-11 11:14:58 -04:00
if (!image)
{
Components::Logger::Error("Failed to allocate GfxImage structure!");
return;
}
image->name = builder->getAllocator()->duplicateString(name);
2016-07-11 11:14:58 -04:00
image->semantic = 2;
image->category = 0;
image->cardMemory = 0;
const char* tempName = image->name;
if (tempName[0] == '*') tempName++;
2016-07-11 11:14:58 -04:00
Components::FileSystem::File imageFile(fmt::sprintf("images/%s.iw4xImage", tempName));
if (imageFile.exists())
2016-07-11 11:14:58 -04:00
{
Utils::Stream::Reader reader(builder->getAllocator(), imageFile.getBuffer());
2016-07-11 11:14:58 -04:00
if (reader.read<__int64>() != *reinterpret_cast<__int64*>("IW4xImg" IW4X_IMG_VERSION))
{
Components::Logger::Error(0, "Reading image '%s' failed, header is invalid!", name.data());
}
2016-07-11 11:14:58 -04:00
AssertSize(Game::MapType, 1);
image->mapType = reader.read<Game::MapType>();
image->semantic = reader.read<char>();
image->category = reader.read<char>();
2016-07-11 11:14:58 -04:00
image->dataLen1 = reader.read<int>();
image->dataLen2 = image->dataLen1;
2016-07-11 11:14:58 -04:00
image->loadDef = reinterpret_cast<Game::GfxImageLoadDef*>(reader.readArray<char>(image->dataLen1 + 16));
image->height = image->loadDef->dimensions[0];
image->width = image->loadDef->dimensions[1];
image->depth = image->loadDef->dimensions[2];
image->loaded = true;
image->loadDef->flags = 0;
if (Utils::String::StartsWith(name, "*lightmap"))
{
image->loadDef->dimensions[0] = 0;
image->loadDef->dimensions[1] = 2;
image->loadDef->dimensions[2] = 0;
}
header->image = image;
}
else if(name[0] != '*')
2016-07-11 11:14:58 -04:00
{
char nameBuffer[MAX_PATH] = { 0 };
Components::Materials::FormatImagePath(nameBuffer, sizeof(nameBuffer), 0, 0, name.data());
Components::FileSystem::File iwi(nameBuffer);
2016-07-11 11:14:58 -04:00
if (!iwi.exists())
2016-07-11 11:14:58 -04:00
{
Components::Logger::Error("Loading image '%s' failed!", iwi.getName().data());
return;
2016-07-11 11:14:58 -04:00
}
auto iwiBuffer = iwi.getBuffer();
const Game::GfxImageFileHeader* iwiHeader = reinterpret_cast<const Game::GfxImageFileHeader*>(iwiBuffer.data());
2016-07-11 11:14:58 -04:00
2016-12-26 16:28:46 -05:00
if (std::memcmp(iwiHeader->tag, "IWi", 3) && iwiHeader->version == 8)
{
Components::Logger::Error("Image is not a valid IWi!");
return;
}
image->mapType = Game::MAPTYPE_2D;
image->dataLen1 = iwiHeader->fileSizeForPicmip[0] - 32;
image->dataLen2 = iwiHeader->fileSizeForPicmip[0] - 32;
2016-12-26 16:28:46 -05:00
image->loadDef = builder->getAllocator()->allocate<Game::GfxImageLoadDef>();
if (!image->loadDef)
2016-07-11 11:14:58 -04:00
{
2016-12-26 16:28:46 -05:00
Components::Logger::Error("Failed to allocate GfxImageLoadDef structure!");
return;
2016-07-11 11:14:58 -04:00
}
std::memcpy(image->loadDef->dimensions, iwiHeader->dimensions, 6);
image->loadDef->flags = 0;
image->loadDef->levelCount = 0;
image->height = image->loadDef->dimensions[0];
image->width = image->loadDef->dimensions[1];
image->depth = image->loadDef->dimensions[2];
switch (iwiHeader->format)
2016-07-11 11:14:58 -04:00
{
case Game::IWI_COMPRESSION::IWI_ARGB:
{
image->loadDef->format = 21;
break;
}
case Game::IWI_COMPRESSION::IWI_RGB8:
{
image->loadDef->format = 20;
break;
}
case Game::IWI_COMPRESSION::IWI_DXT1:
{
image->loadDef->format = 0x31545844;
break;
}
case Game::IWI_COMPRESSION::IWI_DXT3:
{
image->loadDef->format = 0x33545844;
break;
}
case Game::IWI_COMPRESSION::IWI_DXT5:
{
image->loadDef->format = 0x35545844;
break;
}
2016-07-11 11:14:58 -04:00
}
header->image = image;
}
2016-07-11 11:14:58 -04:00
}
void IGfxImage::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
2016-07-11 11:14:58 -04:00
{
AssertSize(Game::GfxImage, 32);
AssertSize(Game::MapType, 1);
2016-07-11 11:14:58 -04:00
Utils::Stream* buffer = builder->getBuffer();
2016-07-11 11:14:58 -04:00
Game::GfxImage* asset = header.image;
Game::GfxImage* dest = buffer->dest<Game::GfxImage>();
buffer->save(asset);
2016-07-11 11:14:58 -04:00
buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL);
2016-07-11 11:14:58 -04:00
if (asset->name)
{
buffer->saveString(builder->getAssetName(this->getType(), asset->name));
2016-07-11 11:14:58 -04:00
Utils::Stream::ClearPointer(&dest->name);
}
buffer->pushBlock(Game::XFILE_BLOCK_TEMP);
2016-07-11 11:14:58 -04:00
if (asset->loadDef)
2016-07-11 11:14:58 -04:00
{
buffer->align(Utils::Stream::ALIGN_4);
2016-07-11 11:14:58 -04:00
Game::GfxImageLoadDef* destTexture = buffer->dest<Game::GfxImageLoadDef>();
buffer->save(asset->loadDef, 16, 1);
builder->incrementExternalSize(asset->loadDef->resourceSize);
2016-07-11 11:14:58 -04:00
if (destTexture->resourceSize > 0)
{
buffer->save(asset->loadDef->data, asset->loadDef->resourceSize);
}
Utils::Stream::ClearPointer(&dest->loadDef);
2016-07-11 11:14:58 -04:00
}
buffer->popBlock();
buffer->popBlock();
2016-07-11 11:14:58 -04:00
}
}