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
|
|
|
|
|
|
|
#include "HiresTextures.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <utility>
|
|
|
|
#include <algorithm>
|
2010-03-09 22:17:33 +00:00
|
|
|
#include <SOIL/SOIL.h>
|
2009-07-06 02:10:26 +00:00
|
|
|
#include "CommonPaths.h"
|
|
|
|
#include "FileUtil.h"
|
|
|
|
#include "FileSearch.h"
|
2009-07-12 21:58:32 +00:00
|
|
|
#include "StringUtil.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
namespace HiresTextures
|
|
|
|
{
|
|
|
|
|
|
|
|
std::map<std::string, std::string> textureMap;
|
|
|
|
|
|
|
|
void Init(const char *gameCode)
|
|
|
|
{
|
2011-02-05 10:08:06 +00:00
|
|
|
textureMap.clear();
|
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
CFileSearch::XStringVector Directories;
|
2011-02-28 20:40:15 +00:00
|
|
|
//Directories.push_back(File::GetUserPath(D_HIRESTEXTURES_IDX));
|
2009-07-06 02:10:26 +00:00
|
|
|
char szDir[MAX_PATH];
|
2011-02-28 20:40:15 +00:00
|
|
|
sprintf(szDir, "%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode);
|
2009-07-06 02:10:26 +00:00
|
|
|
Directories.push_back(std::string(szDir));
|
2013-10-29 05:23:17 +00:00
|
|
|
|
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;
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& Directory : Directories)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
if (strcmp(Directory.c_str(), entry.physicalName.c_str()) == 0)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
duplicate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!duplicate)
|
2013-10-29 05:09:01 +00:00
|
|
|
Directories.push_back(entry.physicalName.c_str());
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CFileSearch::XStringVector Extensions;
|
|
|
|
Extensions.push_back("*.png");
|
|
|
|
Extensions.push_back("*.bmp");
|
|
|
|
Extensions.push_back("*.tga");
|
|
|
|
Extensions.push_back("*.dds");
|
|
|
|
Extensions.push_back("*.jpg"); // Why not? Could be useful for large photo-like textures
|
|
|
|
|
|
|
|
CFileSearch FileSearch(Extensions, Directories);
|
|
|
|
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
|
|
|
|
char code[MAX_PATH];
|
|
|
|
sprintf(code, "%s_", gameCode);
|
|
|
|
|
|
|
|
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;
|
2013-10-29 05:09:01 +00:00
|
|
|
SplitPath(rFilename, NULL, &FileName, NULL);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
if (FileName.substr(0, strlen(code)).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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-12 12:31:38 +00:00
|
|
|
bool HiresTexExists(const char* filename)
|
|
|
|
{
|
|
|
|
std::string key(filename);
|
|
|
|
return textureMap.find(key) != textureMap.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
PC_TexFormat GetHiresTex(const char *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
|
|
|
{
|
|
|
|
std::string key(fileName);
|
2010-01-21 19:31:48 +00:00
|
|
|
if (textureMap.find(key) == textureMap.end())
|
2009-07-06 02:10:26 +00:00
|
|
|
return PC_TEX_FMT_NONE;
|
|
|
|
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int channels;
|
|
|
|
|
|
|
|
u8 *temp = SOIL_load_image(textureMap[key].c_str(), &width, &height, &channels, SOIL_LOAD_RGBA);
|
2009-07-26 09:52:35 +00:00
|
|
|
if (temp == NULL)
|
|
|
|
{
|
2010-12-05 09:04:34 +00:00
|
|
|
ERROR_LOG(VIDEO, "Custom texture %s failed to load", textureMap[key].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
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-31 23:10:21 +00:00
|
|
|
INFO_LOG(VIDEO, "Loading custom texture from %s", textureMap[key].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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|