2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2009-07-06 02:10:26 +00:00
|
|
|
#include <cstring>
|
2014-03-12 19:33:41 +00:00
|
|
|
#include <string>
|
2009-07-06 02:10:26 +00:00
|
|
|
#include <utility>
|
2010-03-09 22:17:33 +00:00
|
|
|
#include <SOIL/SOIL.h>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileSearch.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/HiresTextures.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
namespace HiresTextures
|
|
|
|
{
|
|
|
|
|
|
|
|
std::map<std::string, std::string> textureMap;
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
void Init(const std::string& gameCode)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2011-02-05 10:08:06 +00:00
|
|
|
textureMap.clear();
|
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
CFileSearch::XStringVector Directories;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
std::string szDir = StringFromFormat("%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode.c_str());
|
|
|
|
Directories.push_back(szDir);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < Directories.size(); i++)
|
|
|
|
{
|
|
|
|
File::FSTEntry FST_Temp;
|
2011-03-01 03:06:14 +00:00
|
|
|
File::ScanDirectoryTree(Directories[i], FST_Temp);
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& entry : FST_Temp.children)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
if (entry.isDirectory)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
bool duplicate = false;
|
2014-03-12 19:33:41 +00:00
|
|
|
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& Directory : Directories)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2014-03-12 19:33:41 +00:00
|
|
|
if (Directory == entry.physicalName)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
duplicate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 19:33:41 +00:00
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
if (!duplicate)
|
2014-03-12 19:33:41 +00:00
|
|
|
Directories.push_back(entry.physicalName);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
CFileSearch::XStringVector Extensions = {
|
|
|
|
"*.png",
|
|
|
|
"*.bmp",
|
|
|
|
"*.tga",
|
|
|
|
"*.dds",
|
|
|
|
"*.jpg" // Why not? Could be useful for large photo-like textures
|
|
|
|
};
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
CFileSearch FileSearch(Extensions, Directories);
|
|
|
|
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
|
2014-03-12 19:33:41 +00:00
|
|
|
|
|
|
|
const std::string code = StringFromFormat("%s_", gameCode.c_str());
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
if (rFilenames.size() > 0)
|
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& rFilename : rFilenames)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
std::string FileName;
|
2014-03-09 20:14:26 +00:00
|
|
|
SplitPath(rFilename, nullptr, &FileName, nullptr);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
if (FileName.substr(0, code.length()).compare(code) == 0 && textureMap.find(FileName) == textureMap.end())
|
2013-10-29 05:09:01 +00:00
|
|
|
textureMap.insert(std::map<std::string, std::string>::value_type(FileName, rFilename));
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
bool HiresTexExists(const std::string& filename)
|
2012-05-12 12:31:38 +00:00
|
|
|
{
|
2014-03-12 19:33:41 +00:00
|
|
|
return textureMap.find(filename) != textureMap.end();
|
2012-05-12 12:31:38 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
PC_TexFormat GetHiresTex(const std::string& filename, unsigned int* pWidth, unsigned int* pHeight, unsigned int* required_size, int texformat, unsigned int data_size, u8* data)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2014-03-12 19:33:41 +00:00
|
|
|
if (textureMap.find(filename) == textureMap.end())
|
2009-07-06 02:10:26 +00:00
|
|
|
return PC_TEX_FMT_NONE;
|
|
|
|
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int channels;
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
u8 *temp = SOIL_load_image(textureMap[filename].c_str(), &width, &height, &channels, SOIL_LOAD_RGBA);
|
2014-03-09 20:14:26 +00:00
|
|
|
if (temp == nullptr)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2014-03-12 19:33:41 +00:00
|
|
|
ERROR_LOG(VIDEO, "Custom texture %s failed to load", textureMap[filename].c_str());
|
2009-07-06 02:10:26 +00:00
|
|
|
return PC_TEX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
2012-05-13 13:38:56 +00:00
|
|
|
*pWidth = width;
|
|
|
|
*pHeight = height;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-06-03 23:04:52 +00:00
|
|
|
//int offset = 0;
|
2012-05-13 13:38:56 +00:00
|
|
|
PC_TexFormat returnTex = PC_TEX_FMT_NONE;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
switch (texformat)
|
|
|
|
{
|
2013-11-07 16:05:43 +00:00
|
|
|
// TODO(neobrain): This function currently has no way to enforce RGBA32
|
|
|
|
// output, which however is required on some configurations to function
|
|
|
|
// properly. As a lazy workaround, we hence disable the optimized code
|
|
|
|
// path for now.
|
|
|
|
#if 0
|
2009-07-06 02:10:26 +00:00
|
|
|
case GX_TF_I4:
|
|
|
|
case GX_TF_I8:
|
|
|
|
case GX_TF_IA4:
|
|
|
|
case GX_TF_IA8:
|
2012-05-13 13:38:56 +00:00
|
|
|
*required_size = width * height * 8;
|
|
|
|
if (data_size < *required_size)
|
|
|
|
goto cleanup;
|
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
for (int i = 0; i < width * height * 4; i += 4)
|
|
|
|
{
|
|
|
|
// Rather than use a luminosity function, just use the most intense color for luminance
|
2012-05-13 13:38:56 +00:00
|
|
|
// TODO(neobrain): Isn't this kind of.. stupid?
|
2009-07-06 02:10:26 +00:00
|
|
|
data[offset++] = *std::max_element(temp+i, temp+i+3);
|
|
|
|
data[offset++] = temp[i+3];
|
|
|
|
}
|
|
|
|
returnTex = PC_TEX_FMT_IA8;
|
|
|
|
break;
|
2013-11-07 16:05:43 +00:00
|
|
|
#endif
|
2009-07-06 02:10:26 +00:00
|
|
|
default:
|
2012-05-13 13:38:56 +00:00
|
|
|
*required_size = width * height * 4;
|
|
|
|
if (data_size < *required_size)
|
|
|
|
goto cleanup;
|
|
|
|
|
2010-01-21 19:31:48 +00:00
|
|
|
memcpy(data, temp, width * height * 4);
|
2009-07-06 02:10:26 +00:00
|
|
|
returnTex = PC_TEX_FMT_RGBA32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
INFO_LOG(VIDEO, "Loading custom texture from %s", textureMap[filename].c_str());
|
2012-05-13 13:38:56 +00:00
|
|
|
cleanup:
|
|
|
|
SOIL_free_image_data(temp);
|
2009-07-06 02:10:26 +00:00
|
|
|
return returnTex;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|