1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00
Beef/BeefySysLib/ResLib.cpp

165 lines
3.9 KiB
C++
Raw Normal View History

2019-08-23 11:56:54 -07:00
#include "Common.h"
#include "BFApp.h"
#include "img/PSDReader.h"
#include "gfx/RenderDevice.h"
#include "gfx/Texture.h"
#include "util/PerfTimer.h"
2021-04-12 17:23:18 -04:00
#include "util/TLSingleton.h"
#include "img/JPEGData.h"
2023-01-23 06:54:58 -05:00
#include "img/TGAData.h"
#include "img/PNGData.h"
#include "img/PVRData.h"
#include "img/BFIData.h"
2021-04-12 17:23:18 -04:00
#pragma warning(disable:4190)
2019-08-23 11:56:54 -07:00
USING_NS_BF;
2021-04-12 17:23:18 -04:00
static TLSingleton<String> gResLib_TLStrReturn;
2019-08-23 11:56:54 -07:00
BF_EXPORT PSDReader* BF_CALLTYPE Res_OpenPSD(const char* fileName)
{
2023-01-23 06:54:58 -05:00
//gPerfManager->StartRecording();
2019-08-23 11:56:54 -07:00
PSDReader* aPSDReader = new PSDReader();
if (!aPSDReader->Init(fileName))
{
delete aPSDReader;
return NULL;
}
return aPSDReader;
}
BF_EXPORT void BF_CALLTYPE Res_DeletePSDReader(PSDReader* pSDReader)
{
delete pSDReader;
gPerfManager->StopRecording();
gPerfManager->DbgPrint();
}
BF_EXPORT TextureSegment* BF_CALLTYPE Res_PSD_GetLayerTexture(PSDReader* pSDReader, int layerIdx, int* ofsX, int* ofsY)
{
Texture* texture = pSDReader->LoadLayerTexture(layerIdx, ofsX, ofsY);
if (texture == NULL)
return NULL;
2023-01-23 06:54:58 -05:00
TextureSegment* textureSegment = new TextureSegment();
2019-08-23 11:56:54 -07:00
textureSegment->InitFromTexture(texture);
return textureSegment;
}
BF_EXPORT TextureSegment* BF_CALLTYPE Res_PSD_GetMergedLayerTexture(PSDReader* pSDReader, int* layerIndices, int count, int* ofsX, int* ofsY)
{
std::vector<int> aLayerIndices;
aLayerIndices.insert(aLayerIndices.begin(), layerIndices, layerIndices + count);
Texture* texture = pSDReader->LoadMergedLayerTexture(aLayerIndices, ofsX, ofsY);
if (texture == NULL)
return NULL;
2023-01-23 06:54:58 -05:00
TextureSegment* textureSegment = new TextureSegment();
2019-08-23 11:56:54 -07:00
textureSegment->InitFromTexture(texture);
return textureSegment;
}
BF_EXPORT int BF_CALLTYPE Res_PSD_GetLayerCount(PSDReader* pSDReader)
{
return (int) pSDReader->mPSDLayerInfoVector.size();
}
BF_EXPORT PSDLayerInfo* BF_CALLTYPE Res_PSD_GetLayerInfo(PSDReader* pSDReader, int layerIdx)
{
return pSDReader->mPSDLayerInfoVector[layerIdx];
}
BF_EXPORT void BF_CALLTYPE Res_PSDLayer_GetSize(PSDLayerInfo* layerInfo, int* x, int* y, int* width, int* height)
2023-01-23 06:54:58 -05:00
{
2019-08-23 11:56:54 -07:00
*x = layerInfo->mX;
*y = layerInfo->mY;
*width = layerInfo->mWidth;
2023-01-23 06:54:58 -05:00
*height = layerInfo->mHeight;
2019-08-23 11:56:54 -07:00
}
BF_EXPORT int BF_CALLTYPE Res_PSDLayer_GetLayerId(PSDLayerInfo* layerInfo)
2023-01-23 06:54:58 -05:00
{
2019-08-23 11:56:54 -07:00
return layerInfo->mLayerId;
}
BF_EXPORT const char* BF_CALLTYPE Res_PSDLayer_GetName(PSDLayerInfo* layerInfo)
2023-01-23 06:54:58 -05:00
{
return layerInfo->mName.c_str();
2019-08-23 11:56:54 -07:00
}
BF_EXPORT int BF_CALLTYPE Res_PSDLayer_IsVisible(PSDLayerInfo* layerInfo)
{
return layerInfo->mVisible ? 1 : 0;
}
2021-04-12 17:23:18 -04:00
///
2023-01-23 06:54:58 -05:00
BF_EXPORT uint32* BF_CALLTYPE Res_LoadImage(char* inFileName, int& width, int& height)
{
String fileName = inFileName;
int dotPos = (int)fileName.LastIndexOf('.');
String ext;
if (dotPos != -1)
ext = fileName.Substring(dotPos);
ImageData* imageData = NULL;
bool handled = false;
bool failed = false;
if (fileName == "!white")
{
imageData = new ImageData();
imageData->CreateNew(1, 1, true);
imageData->mBits[0] = 0xFFFFFFFF;
handled = true;
}
else if (ext == ".tga")
imageData = new TGAData();
else if (ext == ".png")
imageData = new PNGData();
else if (ext == ".jpg")
imageData = new JPEGData();
else if (ext == ".pvr")
imageData = new PVRData();
else
{
BF_FATAL("Unknown texture format");
return NULL; // Unknown format
}
if (!imageData->LoadFromFile(fileName))
{
imageData->Deref();
BF_FATAL("Failed to load image");
return NULL;
}
uint32* bits = imageData->mBits;
imageData->mBits = NULL;
width = imageData->mWidth;
height = imageData->mHeight;
imageData->Deref();
return bits;
}
2021-04-12 17:23:18 -04:00
BF_EXPORT StringView BF_CALLTYPE Res_JPEGCompress(uint32* bits, int width, int height, int quality)
{
String& outString = *gResLib_TLStrReturn.Get();
JPEGData jpegData;
jpegData.mBits = bits;
jpegData.mWidth = width;
jpegData.mHeight = height;
jpegData.Compress(quality);
jpegData.mBits = NULL;
outString.Clear();
outString.Insert(0, (char*)jpegData.mSrcData, jpegData.mSrcDataLen);
return outString;
}