Skip to content

Commit

Permalink
C++: Added TextureLoadHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
splhack committed Feb 6, 2014
1 parent 74ad601 commit c03cab1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
14 changes: 12 additions & 2 deletions cplusplus/cocos2dx/lwf_cocos2dx_bitmap.cpp
Expand Up @@ -201,7 +201,12 @@ LWFBitmapRenderer::LWFBitmapRenderer(
const Format::TextureFragment &f =
l->data->textureFragments[b.textureFragmentId];
const Format::Texture &t = l->data->textures[f.textureId];
string filename = node->basePath + t.GetFilename(l->data.get());
string texturePath = t.GetFilename(l->data.get());
string filename = node->basePath + texturePath;

if (LWF::GetTextureLoadHandler())
filename = LWF::GetTextureLoadHandler()(
filename, node->basePath, texturePath);

m_sprite = LWFBitmap::create(filename.c_str(), t, f, bx);
if (!m_sprite)
Expand All @@ -223,7 +228,12 @@ LWFBitmapRenderer::LWFBitmapRenderer(
const Format::TextureFragment &f =
l->data->textureFragments[bx.textureFragmentId];
const Format::Texture &t = l->data->textures[f.textureId];
string filename = node->basePath + t.GetFilename(l->data.get());
string texturePath = t.GetFilename(l->data.get());
string filename = node->basePath + texturePath;

if (LWF::GetTextureLoadHandler())
filename = LWF::GetTextureLoadHandler()(
filename, node->basePath, texturePath);

m_sprite = LWFBitmap::create(filename.c_str(), t, f, bx);
if (!m_sprite)
Expand Down
11 changes: 11 additions & 0 deletions cplusplus/core/lwf_core.cpp
Expand Up @@ -29,6 +29,7 @@ namespace LWF {

int LWF::m_instanceOffset = 0;
int LWF::m_iObjectOffset = 0;
TextureLoadHandler LWF::m_textureLoadHandler = 0;
float LWF::ROUND_OFF_TICK_RATE = 0.05f;

LWF::LWF(shared_ptr<Data> d, shared_ptr<IRendererFactory> r, void *l)
Expand Down Expand Up @@ -737,4 +738,14 @@ void LWF::ClearTextRenderer(string textName)
it->second.second = 0;
}

void LWF::SetTextureLoadHandler(TextureLoadHandler h)
{
m_textureLoadHandler = h;
}

TextureLoadHandler LWF::GetTextureLoadHandler()
{
return m_textureLoadHandler;
}

} // namespace LWF
5 changes: 5 additions & 0 deletions cplusplus/core/lwf_core.h
Expand Up @@ -53,6 +53,7 @@ typedef vector<pair<int, ExecHandler> > ExecHandlerList;
typedef map<string, pair<string, TextRenderer *> > TextDictionary;
typedef map<int, bool> EventFunctions;
typedef vector<int> RenderingModes;
typedef function<string (string, string, string)> TextureLoadHandler;

class LWF
{
Expand All @@ -67,6 +68,7 @@ class LWF
private:
static int m_instanceOffset;
static int m_iObjectOffset;
static TextureLoadHandler m_textureLoadHandler;

public:
shared_ptr<Data> data;
Expand Down Expand Up @@ -319,6 +321,9 @@ class LWF
void SetAlphaMovie(string instanceName, float v);
void SetColorTransformMovie(string instanceName, const ColorTransform *c);

static void SetTextureLoadHandler(TextureLoadHandler h);
static TextureLoadHandler GetTextureLoadHandler();

#if defined(LWF_USE_LUA)
void InitLua();
void DestroyLua();
Expand Down
10 changes: 7 additions & 3 deletions cplusplus/uikit/LWFResourceCache.mm
Expand Up @@ -166,11 +166,15 @@
const string &dataPath, const string &texturePath)
{
size_t pos = dataPath.find_last_of('/');
string path;
string basePath;
if (pos == string::npos)
path = texturePath;
basePath = "";
else
path = dataPath.substr(0, pos + 1) + texturePath;
basePath = dataPath.substr(0, pos + 1);
string path = basePath + texturePath;

if (LWF::GetTextureLoadHandler())
path = LWF::GetTextureLoadHandler()(path, basePath, texturePath);

{
Autolock lock(m_textureSemaphore);
Expand Down

0 comments on commit c03cab1

Please sign in to comment.