2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <algorithm>
|
2009-07-05 20:10:26 -06:00
|
|
|
#include <cstring>
|
2014-03-12 13:33:41 -06:00
|
|
|
#include <string>
|
2009-07-05 20:10:26 -06:00
|
|
|
#include <utility>
|
2010-03-09 15:17:33 -07:00
|
|
|
#include <SOIL/SOIL.h>
|
2014-02-17 03:18:15 -07:00
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileSearch.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/HiresTextures.h"
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
namespace HiresTextures
|
|
|
|
{
|
|
|
|
|
2014-07-08 07:58:25 -06:00
|
|
|
static std::map<std::string, std::string> textureMap;
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
void Init(const std::string& gameCode)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2011-02-05 03:08:06 -07:00
|
|
|
textureMap.clear();
|
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
CFileSearch::XStringVector Directories;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
std::string szDir = StringFromFormat("%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode.c_str());
|
|
|
|
Directories.push_back(szDir);
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
for (u32 i = 0; i < Directories.size(); i++)
|
|
|
|
{
|
|
|
|
File::FSTEntry FST_Temp;
|
2011-02-28 20:06:14 -07:00
|
|
|
File::ScanDirectoryTree(Directories[i], FST_Temp);
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& entry : FST_Temp.children)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2013-10-28 23:09:01 -06:00
|
|
|
if (entry.isDirectory)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
bool duplicate = false;
|
2014-03-12 13:33:41 -06:00
|
|
|
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& Directory : Directories)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2014-03-12 13:33:41 -06:00
|
|
|
if (Directory == entry.physicalName)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
duplicate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 13:33:41 -06:00
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
if (!duplicate)
|
2014-03-12 13:33:41 -06:00
|
|
|
Directories.push_back(entry.physicalName);
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
CFileSearch::XStringVector Extensions = {
|
|
|
|
"*.png",
|
|
|
|
"*.bmp",
|
|
|
|
"*.tga",
|
|
|
|
"*.dds",
|
|
|
|
"*.jpg" // Why not? Could be useful for large photo-like textures
|
|
|
|
};
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
CFileSearch FileSearch(Extensions, Directories);
|
|
|
|
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
|
2014-03-12 13:33:41 -06:00
|
|
|
|
|
|
|
const std::string code = StringFromFormat("%s_", gameCode.c_str());
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
if (rFilenames.size() > 0)
|
|
|
|
{
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& rFilename : rFilenames)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
std::string FileName;
|
2014-03-09 14:14:26 -06:00
|
|
|
SplitPath(rFilename, nullptr, &FileName, nullptr);
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
if (FileName.substr(0, code.length()).compare(code) == 0 && textureMap.find(FileName) == textureMap.end())
|
2013-10-28 23:09:01 -06:00
|
|
|
textureMap.insert(std::map<std::string, std::string>::value_type(FileName, rFilename));
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool HiresTexExists(const std::string& filename)
|
2012-05-12 06:31:38 -06:00
|
|
|
{
|
2014-03-12 13:33:41 -06:00
|
|
|
return textureMap.find(filename) != textureMap.end();
|
2012-05-12 06:31:38 -06:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06: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-05 20:10:26 -06:00
|
|
|
{
|
2014-03-12 13:33:41 -06:00
|
|
|
if (textureMap.find(filename) == textureMap.end())
|
2009-07-05 20:10:26 -06:00
|
|
|
return PC_TEX_FMT_NONE;
|
|
|
|
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int channels;
|
|
|
|
|
2014-09-22 10:13:29 -06:00
|
|
|
File::IOFile file;
|
|
|
|
file.Open(textureMap[filename], "rb");
|
|
|
|
std::vector<u8> buffer(file.GetSize());
|
|
|
|
file.ReadBytes(buffer.data(), file.GetSize());
|
|
|
|
|
|
|
|
u8* temp = SOIL_load_image_from_memory(buffer.data(), (int)buffer.size(), &width, &height, &channels, SOIL_LOAD_RGBA);
|
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
if (temp == nullptr)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2014-03-12 13:33:41 -06:00
|
|
|
ERROR_LOG(VIDEO, "Custom texture %s failed to load", textureMap[filename].c_str());
|
2009-07-05 20:10:26 -06:00
|
|
|
return PC_TEX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
2012-05-13 07:38:56 -06:00
|
|
|
*pWidth = width;
|
|
|
|
*pHeight = height;
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-06-03 17:04:52 -06:00
|
|
|
//int offset = 0;
|
2012-05-13 07:38:56 -06:00
|
|
|
PC_TexFormat returnTex = PC_TEX_FMT_NONE;
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2013-11-07 09:05:43 -07: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
|
2014-08-19 21:16:50 -06:00
|
|
|
switch (texformat)
|
|
|
|
{
|
2009-07-05 20:10:26 -06:00
|
|
|
case GX_TF_I4:
|
|
|
|
case GX_TF_I8:
|
|
|
|
case GX_TF_IA4:
|
|
|
|
case GX_TF_IA8:
|
2012-05-13 07:38:56 -06:00
|
|
|
*required_size = width * height * 8;
|
|
|
|
if (data_size < *required_size)
|
|
|
|
goto cleanup;
|
|
|
|
|
2009-07-05 20:10:26 -06: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 07:38:56 -06:00
|
|
|
// TODO(neobrain): Isn't this kind of.. stupid?
|
2009-07-05 20:10:26 -06:00
|
|
|
data[offset++] = *std::max_element(temp+i, temp+i+3);
|
|
|
|
data[offset++] = temp[i+3];
|
|
|
|
}
|
|
|
|
returnTex = PC_TEX_FMT_IA8;
|
|
|
|
break;
|
|
|
|
default:
|
2012-05-13 07:38:56 -06:00
|
|
|
*required_size = width * height * 4;
|
|
|
|
if (data_size < *required_size)
|
|
|
|
goto cleanup;
|
|
|
|
|
2010-01-21 12:31:48 -07:00
|
|
|
memcpy(data, temp, width * height * 4);
|
2009-07-05 20:10:26 -06:00
|
|
|
returnTex = PC_TEX_FMT_RGBA32;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-19 21:16:50 -06:00
|
|
|
#else
|
|
|
|
*required_size = width * height * 4;
|
|
|
|
if (data_size < *required_size)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
memcpy(data, temp, width * height * 4);
|
|
|
|
returnTex = PC_TEX_FMT_RGBA32;
|
|
|
|
#endif
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
INFO_LOG(VIDEO, "Loading custom texture from %s", textureMap[filename].c_str());
|
2012-05-13 07:38:56 -06:00
|
|
|
cleanup:
|
|
|
|
SOIL_free_image_data(temp);
|
2009-07-05 20:10:26 -06:00
|
|
|
return returnTex;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|