2013-06-13 05:46:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCTextureCacheEmscripten.h"
|
|
|
|
#include "platform/CCImage.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#define MULTILINE(...) #__VA_ARGS__
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// Following methods are implemented in TextureCacheEmscripten.js:
|
2013-06-13 05:46:32 +08:00
|
|
|
extern "C" {
|
|
|
|
void cocos2dx_newAsyncImageLoader(int textureCache, int callback__ignored);
|
2013-06-24 10:21:36 +08:00
|
|
|
void cocos2dx_asyncImageLoader_LoadImage(const char *path, TextureCache::AsyncStruct *asyncData);
|
2013-06-13 05:46:32 +08:00
|
|
|
void cocos2dx_shutdownAsyncImageLoader();
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
// This C interface is exposed so that the JavaScript in
|
2013-06-20 14:13:12 +08:00
|
|
|
// TextureCacheEmscripten.js is able to call into these functions.
|
2013-06-24 10:21:36 +08:00
|
|
|
void TextureCacheEmscripten_addImageAsyncCallBack(TextureCacheEmscripten *tc, TextureCache::AsyncStruct *data, unsigned char *imgData, int width, int height);
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCacheEmscripten_preMultiplyImageRegion( unsigned char *in, int win, int hin, unsigned char *out, int wout, int hout, int xout, int yout);
|
2013-06-13 05:46:32 +08:00
|
|
|
};
|
|
|
|
|
2013-06-24 10:21:36 +08:00
|
|
|
void TextureCacheEmscripten_addImageAsyncCallBack(TextureCacheEmscripten *tc, TextureCache::AsyncStruct *data, unsigned char *imgData, int width, int height)
|
2013-06-13 05:46:32 +08:00
|
|
|
{
|
|
|
|
tc->addImageAsyncCallBack_emscripten(data, imgData, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* Construct a new TextureCacheEmscripten object. Note that the code in
|
|
|
|
* TextureCacheEmscripten.js will be injected into the JavaScript runtime as
|
2013-06-13 05:46:32 +08:00
|
|
|
* a side-affect.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureCacheEmscripten::TextureCacheEmscripten()
|
2013-06-13 05:46:32 +08:00
|
|
|
{
|
|
|
|
// Add dummy references to these functions so that the compiler will emit
|
|
|
|
// code for them prior to this point (which is before when we will call
|
|
|
|
// them.
|
|
|
|
int deps[] = {
|
2013-06-20 14:13:12 +08:00
|
|
|
(int)&TextureCacheEmscripten_addImageAsyncCallBack,
|
|
|
|
(int)&TextureCacheEmscripten_preMultiplyImageRegion
|
2013-06-13 05:46:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
cocos2dx_newAsyncImageLoader((int)this, (int)&deps);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy this object. Note that this will shutdown the image processing queue
|
|
|
|
* (and hence stall anything waiting for those images to finish loading), and
|
|
|
|
* unload the classes and objects instantiated in JavaScript to make this class
|
|
|
|
* work.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureCacheEmscripten::~TextureCacheEmscripten()
|
2013-06-13 05:46:32 +08:00
|
|
|
{
|
|
|
|
cocos2dx_shutdownAsyncImageLoader();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "Blit" the input image to a particular location on the target image. Output
|
|
|
|
* image will be alpha pre-multiplied in the process.
|
|
|
|
*
|
|
|
|
* @in: Pointer to input image raw data bytes
|
|
|
|
* @win: Width of input image in pixels
|
|
|
|
* @hin: Height of input image in pixels
|
|
|
|
* @out: Pointer to memory where output image raw pixels should be written
|
|
|
|
* @wout: Width of output image in pixels
|
|
|
|
* @hout: Height of output image in pixels
|
|
|
|
* @xout: x-offset into target image where blitted image should be placed
|
|
|
|
* @yout: y-offset into target image where blitted image should be placed
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCacheEmscripten_preMultiplyImageRegion(
|
2013-06-13 05:46:32 +08:00
|
|
|
unsigned char *in, int win, int hin, // Input image, its width and height
|
|
|
|
unsigned char *out, int wout, int hout, // Output image, its width and height
|
|
|
|
int xout, int yout) // x and y offsets into the output image
|
|
|
|
{
|
|
|
|
int iter = 0;
|
|
|
|
for(int j = 0; j < hin; j++)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < win; i++)
|
|
|
|
{
|
|
|
|
int inOffset = 4 * (j * win + i);
|
|
|
|
int outOffset = 4 * ((j + yout) * wout + (i + xout));
|
|
|
|
|
|
|
|
unsigned char *pin = in + inOffset;
|
|
|
|
unsigned int *pout = (unsigned int *) (out + outOffset);
|
|
|
|
|
|
|
|
*pout = CC_RGB_PREMULTIPLY_ALPHA( pin[0], pin[1], pin[2], pin[3] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "Private" callback method to load a texture based on the image data
|
2013-06-20 14:13:12 +08:00
|
|
|
* generated in TextureCacheEmscripten.js. Exposed here as public so that it
|
2013-06-13 05:46:32 +08:00
|
|
|
* can be called from the C wrapper method
|
2013-06-20 14:13:12 +08:00
|
|
|
* @TextureCacheEmscripten_addImageAsyncCallBack, above.
|
2013-06-13 05:46:32 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCacheEmscripten::addImageAsyncCallBack_emscripten(AsyncStruct *data, unsigned char *imgData, int width, int height)
|
2013-06-13 05:46:32 +08:00
|
|
|
{
|
|
|
|
const char *filename = data->filename.c_str();
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Image *pImage = new Image();
|
2013-06-13 05:46:32 +08:00
|
|
|
pImage->initWithRawData((unsigned char*) imgData, 4 * width * height, width, height, 8, true);
|
|
|
|
|
|
|
|
free(imgData);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D *texture = new Texture2D();
|
2013-06-13 05:46:32 +08:00
|
|
|
texture->initWithImage(pImage);
|
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
// cache the texture file name
|
2013-06-20 14:13:12 +08:00
|
|
|
VolatileTexture::addImageTexture(texture, filename, Image::kFmtRawData);
|
2013-06-13 05:46:32 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// cache the texture
|
2013-06-19 13:40:21 +08:00
|
|
|
_textures->setObject(texture, filename);
|
2013-06-13 05:46:32 +08:00
|
|
|
texture->autorelease();
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *target = data->target;
|
2013-06-13 05:46:32 +08:00
|
|
|
SEL_CallFuncO selector = data->selector;
|
|
|
|
|
|
|
|
if (target && selector)
|
|
|
|
{
|
|
|
|
(target->*selector)(texture);
|
|
|
|
target->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
pImage->release();
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextureCacheEmscripten::addImageAsync(const char *path, Object *target, SEL_CallFuncO selector)
|
2013-06-13 05:46:32 +08:00
|
|
|
{
|
|
|
|
CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL");
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D *texture = NULL;
|
2013-06-13 05:46:32 +08:00
|
|
|
|
|
|
|
// optimization
|
2013-07-12 12:03:39 +08:00
|
|
|
std::string pathKey = FileUtils::getInstance()->fullPathForFilename(path);
|
2013-06-20 14:13:12 +08:00
|
|
|
texture = (Texture2D*)_textures->objectForKey(pathKey.c_str());
|
2013-06-13 05:46:32 +08:00
|
|
|
|
|
|
|
std::string fullpath = pathKey;
|
|
|
|
if (texture != NULL)
|
|
|
|
{
|
|
|
|
if (target && selector)
|
|
|
|
{
|
|
|
|
(target->*selector)(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target)
|
|
|
|
{
|
|
|
|
target->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate async struct
|
|
|
|
AsyncStruct *data = new AsyncStruct(fullpath, target, selector);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// Call into JavaScript code in TextureCacheEmscripten.js to do the rest.
|
2013-06-13 05:46:32 +08:00
|
|
|
cocos2dx_asyncImageLoader_LoadImage(data->filename.c_str(), data);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|