Skip to content

Commit

Permalink
Add EMSCRIPTEN_KEEPALIVE to texutre cache
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Mar 22, 2017
1 parent 904bd91 commit e751e47
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CocosDenshion/emscripten/SimpleAudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,11 @@ namespace CocosDenshion
{
sound = s_effects[key];
}
// TODO: @caiiiycuk make it works on linux

// This is safe here since Emscripten is just passing back an
// incrementing integer each time you use the Mix_LoadWAV method.
unsigned int result = (unsigned int) sound->chunk;
unsigned int result = (size_t) sound->chunk;

// XXX: This is a bit of a hack, but... Choose a channel based on the
// modulo of the # of channels. This allows us to set the volume
Expand Down
5 changes: 5 additions & 0 deletions cocos2dx/base_nodes/CCNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ void Node::setScale(float scale)
_transformDirty = _inverseDirty = true;
}

Size Node::getScaleAsSize() const
{
return Size( _scaleX, _scaleY );
}

/// scaleX getter
float Node::getScaleX() const
{
Expand Down
2 changes: 2 additions & 0 deletions cocos2dx/base_nodes/CCNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ class CC_DLL Node : public Object
* @return The scale factor of the node.
*/
virtual float getScale() const;

virtual Size getScaleAsSize() const;


/**
Expand Down
6 changes: 6 additions & 0 deletions cocos2dx/cocoa/CCGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ Size Size::operator*(float a) const
return Size(this->width * a, this->height * a);
}

Size Size::operator*(Size a) const
{
return Size( this->width * a.width, this->height * a.height );
}


Size Size::operator/(float a) const
{
CCASSERT(a!=0, "CCSize division by 0.");
Expand Down
76 changes: 76 additions & 0 deletions cocos2dx/cocoa/CCGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,80 @@ THE SOFTWARE.

NS_CC_BEGIN

const float NEAR_ZERO = 0.0001f;

inline bool floatEquals( float a, float b, float margin )
{
if( fabs( a - b ) < margin )
{
return true;
}
return false;
}


inline bool floatEquals( float a, float b )
{
if( fabs( a - b ) < NEAR_ZERO )
{
return true;
}
return false;
}

inline bool isZero( float value, float margin )
{
if( fabs( value ) < margin )
{
return true;
}
return false;
}

inline bool isZero( float value )
{
if( fabs( value ) < NEAR_ZERO )
{
return true;
}
return false;
}

inline int getDirection( int value )
{
if( value < 0 ) return -1;
if( value > 0 ) return 1;
return 0;
}

inline int getDirection( float value )
{
if( value < 0 ) return -1;
if( value > 0 ) return 1;
return 0;
}

inline int getDirectionOrZero( float value, float margin )
{
if( isZero( value, margin ) )
return 0;

if( value < 0 ) return -1;
if( value > 0 ) return 1;
return 0;
}

/** Clamp a value between from and to.
@since v0.99.1
*/
inline int clampi( int value, int min_inclusive, int max_inclusive)
{
if (min_inclusive > max_inclusive) {
CC_SWAP(min_inclusive, max_inclusive, int );
}
return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive;
}

/** Clamp a value between from and to.
@since v0.99.1
*/
Expand All @@ -45,6 +119,7 @@ inline float clampf(float value, float min_inclusive, float max_inclusive)
return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive;
}


/**
* @addtogroup data_structures
* @{
Expand Down Expand Up @@ -295,6 +370,7 @@ class CC_DLL Size
Size operator+(const Size& right) const;
Size operator-(const Size& right) const;
Size operator*(float a) const;
Size operator*(Size a) const;
Size operator/(float a) const;
void setSize(float width, float height);
bool equals(const Size& target) const;
Expand Down
14 changes: 14 additions & 0 deletions cocos2dx/label_nodes/CCLabelBMFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,20 @@ void LabelBMFont::setScaleY(float scaleY)
updateLabel();
}

void LabelBMFont::setScaleXY(float scaleX, float scaleY)
{
SpriteBatchNode::setScaleX(scaleX);
SpriteBatchNode::setScaleY(scaleY);
updateLabel( );
}

void LabelBMFont::modifyScaleXY(float deltaScaleX, float deltaScaleY )
{
SpriteBatchNode::setScaleX( getScaleX( ) * deltaScaleX );
SpriteBatchNode::setScaleY( getScaleY( ) * deltaScaleY );
updateLabel( );
}

float LabelBMFont::getLetterPosXLeft( Sprite* sp )
{
return sp->getPosition().x * _scaleX - (sp->getContentSize().width * _scaleX * sp->getAnchorPoint().x);
Expand Down
2 changes: 2 additions & 0 deletions cocos2dx/label_nodes/CCLabelBMFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ class CC_DLL LabelBMFont : public SpriteBatchNode, public LabelProtocol, public
virtual void setScale(float scale);
virtual void setScaleX(float scaleX);
virtual void setScaleY(float scaleY);
virtual void setScaleXY(float scaleX, float scaleY);
virtual void modifyScaleXY(float deltaScaleX, float deltaScaleY );

// RGBAProtocol
virtual bool isOpacityModifyRGB() const;
Expand Down
15 changes: 7 additions & 8 deletions cocos2dx/platform/emscripten/CCTextureCacheEmscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,13 @@ var LibraryCocosHelper = {
this.ctx.drawImage(args.img, args.i, args.j, rw, rh, args.i, args.j, rw, rh);

var imgData = this.ctx.getImageData(args.i, args.j, rw, rh);
var inImgData = _malloc(rw * rh * 4);
var inImgData = Module['_malloc'](rw * rh * 4);
Module.HEAP8.set(imgData.data, inImgData);

// Call into C++ code in CCTextureCacheEmscripten.cpp to do the actual
// copy and pre-multiply.
_CCTextureCacheEmscripten_preMultiplyImageRegion(inImgData, rw, rh, args.outImgData, args.w, args.h, args.i, args.j);

_free(inImgData);
Module['_TextureCacheEmscripten_preMultiplyImageRegion'](inImgData, rw, rh, args.outImgData, args.w, args.h, args.i, args.j);
Module['_free'](inImgData);
};

/**
Expand Down Expand Up @@ -254,7 +253,7 @@ var LibraryCocosHelper = {
};
that.operationQueue.enqueue(setupCanvas, { w: w, h: h });

var outImgData = _malloc(w * h * 4);
var outImgData = Module['_malloc'](w * h * 4);

for(var i = 0; i < w; i += that.regionSize)
{
Expand All @@ -271,7 +270,7 @@ var LibraryCocosHelper = {
}

var fireCallback = function(args) {
_CCTextureCacheEmscripten_addImageAsyncCallBack(that.cxxTextureCache, args.asyncData, args.imgData, args.w, args.h);
Module['_TextureCacheEmscripten_addImageAsyncCallBack'](that.cxxTextureCache, args.asyncData, args.imgData, args.w, args.h);
};
var opArgs = {
asyncData: asyncData,
Expand Down Expand Up @@ -348,8 +347,8 @@ window['cocos2dx'] = LibraryCocosHelper.$cocos2dx;
extern "C" {
// This C interface is exposed so that the JavaScript in
// TextureCacheEmscripten.js is able to call into these functions.
void TextureCacheEmscripten_addImageAsyncCallBack(TextureCacheEmscripten *tc, TextureCache::AsyncStruct *data, unsigned char *imgData, int width, int height);
void TextureCacheEmscripten_preMultiplyImageRegion( unsigned char *in, int win, int hin, unsigned char *out, int wout, int hout, int xout, int yout);
void EMSCRIPTEN_KEEPALIVE TextureCacheEmscripten_addImageAsyncCallBack(TextureCacheEmscripten *tc, TextureCache::AsyncStruct *data, unsigned char *imgData, int width, int height);
void EMSCRIPTEN_KEEPALIVE TextureCacheEmscripten_preMultiplyImageRegion( unsigned char *in, int win, int hin, unsigned char *out, int wout, int hout, int xout, int yout);
};

void TextureCacheEmscripten_addImageAsyncCallBack(TextureCacheEmscripten *tc, TextureCache::AsyncStruct *data, unsigned char *imgData, int width, int height)
Expand Down
6 changes: 6 additions & 0 deletions cocos2dx/sprite_nodes/CCSprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,12 @@ void Sprite::setScale(float fScale)
SET_DIRTY_RECURSIVELY();
}

void Sprite::setScale(float scaleX, float scaleY )
{
setScaleX( scaleX );
setScaleY( scaleY );
}

void Sprite::setVertexZ(float fVertexZ)
{
Node::setVertexZ(fVertexZ);
Expand Down
1 change: 1 addition & 0 deletions cocos2dx/sprite_nodes/CCSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ class CC_DLL Sprite : public NodeRGBA, public TextureProtocol
virtual void addChild(Node *child, int zOrder, int tag) override;
virtual void sortAllChildren() override;
virtual void setScale(float scale) override;
virtual void setScale(float scaleX, float scaleY);
virtual void setVertexZ(float vertexZ) override;
virtual void setAnchorPoint(const Point& anchor) override;
virtual void ignoreAnchorPointForPosition(bool value) override;
Expand Down
2 changes: 2 additions & 0 deletions generate-cmake-lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
include_lines = ""
for include in includes:
include = include.replace('/platform/emscripten', '/platform/linux')
include = include.replace('/platform/third_party/emscripten', '/platform/third_party/linux')
include = include.replace('libtiff/include', 'libtiff')
include_lines = include_lines + "\n\t PUBLIC " + include


Expand Down

0 comments on commit e751e47

Please sign in to comment.