1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 21:29:56 +00:00
openmw/components/esm3/fogstate.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
3.4 KiB
C++
Raw Normal View History

#include "fogstate.hpp"
#include "esmreader.hpp"
#include "esmwriter.hpp"
#include <osgDB/ReadFile>
#include <components/debug/debuglog.hpp>
#include <components/files/memorystream.hpp>
namespace ESM
{
namespace
{
void convertFogOfWar(std::vector<char>& imageData)
2022-09-22 18:26:05 +00:00
{
if (imageData.empty())
2022-09-22 18:26:05 +00:00
{
return;
}
osgDB::ReaderWriter* tgaReader = osgDB::Registry::instance()->getReaderWriterForExtension("tga");
if (!tgaReader)
2022-09-22 18:26:05 +00:00
{
Log(Debug::Error) << "Error: Unable to load fog, can't find a tga ReaderWriter";
2022-09-22 18:26:05 +00:00
return;
}
2022-09-03 15:41:35 +00:00
Files::IMemStream in(imageData.data(), imageData.size());
2022-09-22 18:26:05 +00:00
osgDB::ReaderWriter::ReadResult result = tgaReader->readImage(in);
if (!result.success())
2022-09-22 18:26:05 +00:00
{
Log(Debug::Error) << "Error: Failed to read fog: " << result.message() << " code " << result.status();
return;
2022-09-22 18:26:05 +00:00
}
osgDB::ReaderWriter* pngWriter = osgDB::Registry::instance()->getReaderWriterForExtension("png");
if (!pngWriter)
2022-09-22 18:26:05 +00:00
{
Log(Debug::Error) << "Error: Unable to write fog, can't find a png ReaderWriter";
2022-09-22 18:26:05 +00:00
return;
}
2022-09-22 18:26:05 +00:00
std::ostringstream ostream;
osgDB::ReaderWriter::WriteResult png = pngWriter->writeImage(*result.getImage(), ostream);
if (!png.success())
2022-09-22 18:26:05 +00:00
{
Log(Debug::Error) << "Error: Unable to write fog: " << png.message() << " code " << png.status();
2022-09-22 18:26:05 +00:00
return;
}
std::string str = ostream.str();
imageData = std::vector<char>(str.begin(), str.end());
2022-09-22 18:26:05 +00:00
}
}
void FogState::load(ESMReader& esm)
{
if (esm.isNextSub("BOUN"))
esm.getHT(mBounds.mMinX, mBounds.mMinY, mBounds.mMaxX, mBounds.mMaxY);
esm.getHNOT(mNorthMarkerAngle, "ANGL");
if (!esm.getHNOT("CNTR", mCenterX, mCenterY))
{
mCenterX = (mBounds.mMinX + mBounds.mMaxX) / 2;
mCenterY = (mBounds.mMinY + mBounds.mMaxY) / 2;
}
const FormatVersion dataFormat = esm.getFormatVersion();
while (esm.isNextSub("FTEX"))
2022-09-22 18:26:05 +00:00
{
esm.getSubHeader();
FogTexture tex;
2022-09-22 18:26:05 +00:00
esm.getT(tex.mX);
esm.getT(tex.mY);
2022-09-22 18:26:05 +00:00
const std::size_t imageSize = esm.getSubSize() - sizeof(int32_t) * 2;
tex.mImageData.resize(imageSize);
2023-02-17 21:56:56 +00:00
esm.getExact(tex.mImageData.data(), imageSize);
2022-09-22 18:26:05 +00:00
if (dataFormat <= MaxOldFogOfWarFormatVersion)
convertFogOfWar(tex.mImageData);
2022-09-22 18:26:05 +00:00
mFogTextures.push_back(tex);
2022-09-22 18:26:05 +00:00
}
}
void FogState::save(ESMWriter& esm, bool interiorCell) const
{
if (interiorCell)
2022-09-22 18:26:05 +00:00
{
esm.writeHNT("BOUN", mBounds);
esm.writeHNT("ANGL", mNorthMarkerAngle);
esm.startSubRecord("CNTR");
esm.writeT(mCenterX);
esm.writeT(mCenterY);
esm.endRecord("CNTR");
2022-09-22 18:26:05 +00:00
}
for (const FogTexture& texture : mFogTextures)
2022-09-22 18:26:05 +00:00
{
esm.startSubRecord("FTEX");
esm.writeT(texture.mX);
esm.writeT(texture.mY);
esm.write(texture.mImageData.data(), texture.mImageData.size());
esm.endRecord("FTEX");
2022-09-22 18:26:05 +00:00
}
}
}