issue #2161: android supports ETC format now

This commit is contained in:
minggo 2013-05-27 14:42:22 +08:00
parent e07761688e
commit beef61fc90
12 changed files with 432 additions and 1 deletions

View File

@ -127,6 +127,7 @@ text_input_node/CCTextFieldTTF.cpp \
textures/CCTexture2D.cpp \
textures/CCTextureAtlas.cpp \
textures/CCTextureCache.cpp \
textures/CCTextureETC.cpp \
textures/CCTexturePVR.cpp \
tilemap_parallax_nodes/CCParallaxNode.cpp \
tilemap_parallax_nodes/CCTMXLayer.cpp \

View File

@ -0,0 +1,104 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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.
****************************************************************************/
package org.cocos2dx.lib;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import android.content.Context;
import android.content.res.AssetManager;
import android.opengl.ETC1Util;
import android.util.Log;
public class Cocos2dxETCLoader {
private static final String ASSETS_PATH = "assets/";
private static Context context;
public static boolean loadTexture(String filePath) {
if (! ETC1Util.isETC1Supported()) {
return false;
}
if (filePath.length() == 0) {
return false;
}
// Create ETC1Texture
InputStream inputStream = null;
ETC1Util.ETC1Texture texture = null;
AssetManager assetManager = null;
try {
if (filePath.charAt(0) == '/') {
// absolute path
inputStream = new FileInputStream(filePath);
} else {
// remove prefix: "assets/"
if (filePath.startsWith(ASSETS_PATH)) {
filePath = filePath.substring(ASSETS_PATH.length());
}
assetManager = context.getAssets();
inputStream = assetManager.open(filePath);
}
texture = ETC1Util.createTexture(inputStream);
inputStream.close();
assetManager.close();
} catch (Exception e) {
Log.d("Cocos2dx", "Unable to create texture for " + filePath);
texture = null;
}
if (texture != null) {
try {
int width = texture.getWidth();
int height = texture.getHeight();
final byte[] data = new byte[width * height * 3];
final ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.nativeOrder());
buf.put(texture.getData());
nativeSetTextureInfo(width,
height,
data);
} catch (Exception e)
{
Log.d("invoke native function error", e.toString());
}
return true;
} else {
return false;
}
}
public static void setContext(Context context) {
Cocos2dxETCLoader.context = context;
}
private static native void nativeSetTextureInfo(final int width, final int height, final byte[] data);
}

View File

@ -75,6 +75,7 @@ public class Cocos2dxHelper {
Cocos2dxHelper.sCocos2dSound = new Cocos2dxSound(pContext);
Cocos2dxHelper.sAssetManager = pContext.getAssets();
Cocos2dxBitmap.setContext(pContext);
Cocos2dxETCLoader.setContext(pContext);
}
// ===========================================================

View File

@ -1 +1 @@
89741f647de6b40616d5c45625fcd3c2d8e9d20a
3a74fba6589c007b54123dfc2d385ab7d3d81919

View File

@ -40,6 +40,7 @@ THE SOFTWARE.
#include "support/ccUtils.h"
#include "platform/CCPlatformMacros.h"
#include "textures/CCTexturePVR.h"
#include "textures/CCTextureETC.h"
#include "CCDirector.h"
#include "shaders/CCGLProgram.h"
#include "shaders/ccGLStateCache.h"
@ -722,6 +723,34 @@ bool CCTexture2D::initWithPVRFile(const char* file)
return bRet;
}
bool CCTexture2D::initWithETCFile(const char* file)
{
bool bRet = false;
// nothing to do with CCObject::init
CCTextureETC *etc = new CCTextureETC;
bRet = etc->initWithFile(file);
if (bRet)
{
m_uName = etc->getName();
m_fMaxS = 1.0f;
m_fMaxT = 1.0f;
m_uPixelsWide = etc->getWidth();
m_uPixelsHigh = etc->getHeight();
m_tContentSize = CCSizeMake((float)m_uPixelsWide, (float)m_uPixelsHigh);
m_bHasPremultipliedAlpha = true;
etc->release();
}
else
{
CCLOG("cocos2d: Couldn't load ETC image %s", file);
}
return bRet;
}
void CCTexture2D::PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied)
{
PVRHaveAlphaPremultiplied_ = haveAlphaPremultiplied;

View File

@ -151,6 +151,9 @@ public:
/** Initializes a texture from a PVR file */
bool initWithPVRFile(const char* file);
/** Initializes a texture from a ETC file */
bool initWithETCFile(const char* file);
/** sets the min filter, mag filter, wrap s and wrap t texture parameters.
If the texture size is NPOT (non power of 2), then in can only use GL_CLAMP_TO_EDGE in GL_TEXTURE_WRAP_{S,T}.

View File

@ -399,6 +399,11 @@ CCTexture2D * CCTextureCache::addImage(const char * path)
{
texture = this->addPVRImage(fullpath.c_str());
}
else if (std::string::npos != lowerCase.find(".pkm"))
{
// ETC1 file format, only supportted on Android
texture = this->addETCImage(fullpath.c_str());
}
else
{
CCImage::EImageFormat eImageFormat = CCImage::kFmtUnKnown;
@ -484,6 +489,35 @@ CCTexture2D * CCTextureCache::addPVRImage(const char* path)
return texture;
}
CCTexture2D* CCTextureCache::addETCImage(const char* path)
{
CCAssert(path != NULL, "TextureCache: fileimage MUST not be nil");
CCTexture2D* texture = NULL;
std::string key(path);
if( (texture = (CCTexture2D*)m_pTextures->objectForKey(key.c_str())) )
{
return texture;
}
// Split up directory and filename
std::string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename(key.c_str());
texture = new CCTexture2D();
if(texture != NULL && texture->initWithETCFile(fullpath.c_str()))
{
m_pTextures->setObject(texture, key.c_str());
texture->autorelease();
}
else
{
CCLOG("cocos2d: Couldn't add ETCImage:%s in CCTextureCache",key.c_str());
CC_SAFE_DELETE(texture);
}
return texture;
}
CCTexture2D* CCTextureCache::addUIImage(CCImage *image, const char *key)
{
CCAssert(image != NULL, "TextureCache: image MUST not be nil");

View File

@ -155,6 +155,12 @@ public:
*/
CCTexture2D* addPVRImage(const char* filename);
/** Returns a Texture2D object given an ETC filename
* If the file image was not previously loaded, it will create a new CCTexture2D
* object and it will return it. Otherwise it will return a reference of a previously loaded image
*/
CCTexture2D* addETCImage(const char* filename);
/** Reload all textures
It's only useful when the value of CC_ENABLE_CACHE_TEXTURE_DATA is 1
*/

View File

@ -0,0 +1,147 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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 "CCTextureETC.h"
#include "platform/CCPlatformConfig.h"
#include "platform/CCFileUtils.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#endif
NS_CC_BEGIN
CCTextureETC::CCTextureETC()
: _name(0)
, _width(0)
, _height(0)
{}
CCTextureETC::~CCTextureETC()
{
}
bool CCTextureETC::initWithFile(const char *file)
{
// Only Android supports ETC file format
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
bool ret = loadTexture(CCFileUtils::sharedFileUtils()->fullPathForFilename(file).c_str());
return ret;
#else
return false;
#endif
}
unsigned int CCTextureETC::getName() const
{
return _name;
}
unsigned int CCTextureETC::getWidth() const
{
return _width;
}
unsigned int CCTextureETC::getHeight() const
{
return _height;
}
// Call back function for java
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#define LOG_TAG "CCTextureETC.cpp"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
static unsigned int sWidth = 0;
static unsigned int sHeight = 0;
static unsigned char *sData = NULL;
static unsigned int sLength = 0;
extern "C"
{
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxETCLoader_nativeSetTextureInfo(JNIEnv* env, jobject thiz, jint width, jint height, jbyteArray data)
{
sWidth = (unsigned int)width;
sHeight = (unsigned int)height;
sLength = sWidth * sHeight * 3;
sData = new unsigned char[sLength];
env->GetByteArrayRegion(data, 0, sLength, (jbyte*)sData);
}
}
#endif
bool CCTextureETC::loadTexture(const char* file)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxETCLoader", "loadTexture", "(Ljava/lang/String;)Z"))
{
jstring stringArg1 = t.env->NewStringUTF(file);
jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg1);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID);
if (ret)
{
_width = sWidth;
_height = sHeight;
glGenTextures(1, &_name);
glBindTexture(GL_TEXTURE_2D, _name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES, _width, _height, 0, sLength, sData);
glBindTexture(GL_TEXTURE_2D, 0);
delete [] sData;
sData = NULL;
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
LOGD("width %d, height %d, lenght %d", _width, _height, sLength);
LOGD("cocos2d: TextureETC: Error uploading compressed texture %s glError: 0x%04X", file, err);
return false;
}
return true;
}
else
{
return false;
}
}
#else
return false;
#endif
}
NS_CC_END

View File

@ -0,0 +1,57 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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.
****************************************************************************/
#ifndef __CCETCTEXTURE_H__
#define __CCETCTEXTURE_H__
#include "cocoa/CCObject.h"
#include "platform/CCPlatformMacros.h"
#include "CCGL.h"
NS_CC_BEGIN
class CC_DLL CCTextureETC : public CCObject
{
public:
CCTextureETC();
virtual ~CCTextureETC();
bool initWithFile(const char* file);
unsigned int getName() const;
unsigned int getWidth() const;
unsigned int getHeight() const;
private:
bool loadTexture(const char* file);
private:
GLuint _name;
unsigned int _width;
unsigned int _height;
};
NS_CC_END
#endif /* defined(__CCETCTEXTURE_H__) */

View File

@ -66,6 +66,8 @@ TESTLAYER_CREATE_FUNC(TextureCache1);
TESTLAYER_CREATE_FUNC(TextureDrawAtPoint);
TESTLAYER_CREATE_FUNC(TextureDrawInRect);
TESTLAYER_CREATE_FUNC(TextureETC1);
static NEWTEXTURE2DTESTFUNC createFunctions[] =
{
createTextureMemoryAlloc,
@ -118,6 +120,8 @@ static NEWTEXTURE2DTESTFUNC createFunctions[] =
createTextureCache1,
createTextureDrawAtPoint,
createTextureDrawInRect,
createTextureETC1,
};
static unsigned int TEST_CASE_COUNT = sizeof(createFunctions) / sizeof(createFunctions[0]);
@ -2050,3 +2054,38 @@ void TexturePVRv3Premult::transformSprite(cocos2d::CCSprite *sprite)
CCRepeatForever *repeat = CCRepeatForever::create(seq);
sprite->runAction(repeat);
}
// Implementation of ETC1
/*
class TextureETC1 : public TextureDemo
{
public:
TextureETC1();
virtual std::string title();
virtual std::string subtitle();
};
*/
TextureETC1::TextureETC1()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCSprite *sprite = CCSprite::create("Images/ETC1.pkm");
CCSize size = CCDirector::sharedDirector()->getWinSize();
sprite->setPosition(ccp(size.width/2, size.height/2));
addChild(sprite);
#endif
}
std::string TextureETC1::title()
{
return "ETC1 texture";
}
std::string TextureETC1::subtitle()
{
return "only supported on android";
}

View File

@ -442,4 +442,14 @@ public:
void transformSprite(cocos2d::CCSprite *sprite);
};
// ETC1 texture format test
class TextureETC1 : public TextureDemo
{
public:
TextureETC1();
virtual std::string title();
virtual std::string subtitle();
};
#endif // __TEXTURE2D_TEST_H__