From f0a79242d7713648af0f9efa44d170d1cf11ea14 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 14 Mar 2018 10:28:03 +0800 Subject: [PATCH] Android compile ok. (#1145) * Android compile ok. * Fixes for Android build . --- native/cocos/Android.mk | 10 +- native/cocos/base/CCLog.cpp | 1 + native/cocos/base/CCValue.cpp | 5 +- native/cocos/base/ccUTF8.cpp | 4 +- native/cocos/base/csscolorparser.hpp | 1 + native/cocos/platform/CCFileUtils.cpp | 41 ++- native/cocos/platform/CCFileUtils.h | 3 + native/cocos/platform/android/Android.mk | 5 +- .../CCCanvasRenderingContext2D-android.cpp | 287 ++++++++++++++++++ .../platform/android/CCFileUtils-android.cpp | 27 +- native/cocos/renderer/gfx/Program.cpp | 1 + .../js-bindings/manual/jsb_global.cpp | 34 +-- 12 files changed, 368 insertions(+), 51 deletions(-) create mode 100644 native/cocos/platform/android/CCCanvasRenderingContext2D-android.cpp diff --git a/native/cocos/Android.mk b/native/cocos/Android.mk index ee1abe62f8..6cca479dc4 100644 --- a/native/cocos/Android.mk +++ b/native/cocos/Android.mk @@ -50,6 +50,7 @@ base/etc1.cpp \ base/pvr.cpp \ base/CCLog.cpp \ base/CCScheduler.cpp \ +base/csscolorparser.cpp \ renderer/Types.cpp \ renderer/gfx/DeviceGraphics.cpp \ renderer/gfx/FrameBuffer.cpp \ @@ -78,18 +79,25 @@ renderer/renderer//Scene.cpp \ renderer/renderer/Technique.cpp \ renderer/renderer/View.cpp \ renderer/renderer/ForwardRenderer.cpp \ +storage/local-storage/LocalStorage-android.cpp \ scripting/js-bindings/auto/jsb_gfx_auto.cpp \ scripting/js-bindings/auto/jsb_cocos2dx_network_auto.cpp \ scripting/js-bindings/auto/jsb_renderer_auto.cpp \ +scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp \ +scripting/js-bindings/manual/JavaScriptJavaBridge.cpp \ +scripting/js-bindings/manual/jsb_opengl_functions.cpp \ +scripting/js-bindings/manual/jsb_opengl_manual.cpp \ +scripting/js-bindings/manual/jsb_opengl_registration.cpp \ +scripting/js-bindings/manual/jsb_opengl_utils.cpp \ scripting/js-bindings/manual/jsb_classtype.cpp \ scripting/js-bindings/manual/jsb_conversions.cpp \ +scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp \ scripting/js-bindings/manual/jsb_gfx_manual.cpp \ scripting/js-bindings/manual/jsb_global.cpp \ scripting/js-bindings/manual/jsb_renderer_manual.cpp \ scripting/js-bindings/manual/jsb_socketio.cpp \ scripting/js-bindings/manual/jsb_websocket.cpp \ scripting/js-bindings/manual/jsb_xmlhttprequest.cpp \ -scripting/js-bindings/auto/jsb_renderer_auto.cpp \ scripting/js-bindings/jswrapper/config.cpp \ scripting/js-bindings/jswrapper/HandleObject.cpp \ scripting/js-bindings/jswrapper/MappingUtils.cpp \ diff --git a/native/cocos/base/CCLog.cpp b/native/cocos/base/CCLog.cpp index db46af5b96..d5f61d9b54 100644 --- a/native/cocos/base/CCLog.cpp +++ b/native/cocos/base/CCLog.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #include diff --git a/native/cocos/base/CCValue.cpp b/native/cocos/base/CCValue.cpp index 2f1fa7e7e6..1829348417 100644 --- a/native/cocos/base/CCValue.cpp +++ b/native/cocos/base/CCValue.cpp @@ -24,12 +24,15 @@ ****************************************************************************/ #include "base/CCValue.h" +#include "base/ccUtils.h" + #include #include #include #include #include -#include "base/ccUtils.h" +#include + NS_CC_BEGIN diff --git a/native/cocos/base/ccUTF8.cpp b/native/cocos/base/ccUTF8.cpp index ce66f1e055..a11b99628e 100644 --- a/native/cocos/base/ccUTF8.cpp +++ b/native/cocos/base/ccUTF8.cpp @@ -25,9 +25,11 @@ ****************************************************************************/ #include "base/ccUTF8.h" -#include #include "ConvertUTF/ConvertUTF.h" +#include +#include + NS_CC_BEGIN namespace StringUtils { diff --git a/native/cocos/base/csscolorparser.hpp b/native/cocos/base/csscolorparser.hpp index d3933d937c..69d1292ff5 100644 --- a/native/cocos/base/csscolorparser.hpp +++ b/native/cocos/base/csscolorparser.hpp @@ -26,6 +26,7 @@ #define CSS_COLOR_PARSER_CPP #include +#include namespace CSSColorParser { diff --git a/native/cocos/platform/CCFileUtils.cpp b/native/cocos/platform/CCFileUtils.cpp index 1091f4833e..a025361734 100644 --- a/native/cocos/platform/CCFileUtils.cpp +++ b/native/cocos/platform/CCFileUtils.cpp @@ -41,6 +41,7 @@ THE SOFTWARE. #include "unzip/unzip.h" #endif #include +#include NS_CC_BEGIN @@ -778,7 +779,7 @@ std::string FileUtils::fullPathForFilename(const std::string &filename) const if (isAbsolutePath(filename)) { - return filename; + return normalizePath(filename); } // Already Cached ? @@ -968,6 +969,7 @@ std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& dir ret += '/'; } ret += filename; + ret = normalizePath(ret); // if the file doesn't exist, return an empty string if (!isFileExistInternal(ret)) { @@ -980,7 +982,7 @@ bool FileUtils::isFileExist(const std::string& filename) const { if (isAbsolutePath(filename)) { - return isFileExistInternal(filename); + return isFileExistInternal(normalizePath(filename)); } else { @@ -1003,7 +1005,7 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const if (isAbsolutePath(dirPath)) { - return isDirectoryExistInternal(dirPath); + return isDirectoryExistInternal(normalizePath(dirPath)); } // Already Cached ? @@ -1432,4 +1434,37 @@ void FileUtils::valueVectorCompact(ValueVector &valueVector) { } +std::string FileUtils::getFileDir(const std::string& path) const +{ + std::string ret; + size_t pos = path.rfind("/"); + if (pos != std::string::npos) + { + ret = path.substr(0, pos); + } + + normalizePath(ret); + + return ret; +} + +std::string FileUtils::normalizePath(const std::string& path) const +{ + std::string ret; + // Normalize: remove . and .. + ret = std::regex_replace(path, std::regex("/\\./"), "/"); + ret = std::regex_replace(ret, std::regex("/\\.$"), ""); + + size_t pos; + while ((pos = ret.find("..")) != std::string::npos) + { + size_t prevSlash = ret.rfind("/", pos-2); + if (prevSlash == std::string::npos) + break; + + ret = ret.replace(prevSlash, pos - prevSlash + 2 , ""); + } + return ret; +} + NS_CC_END diff --git a/native/cocos/platform/CCFileUtils.h b/native/cocos/platform/CCFileUtils.h index 8f69593e5b..aa48b51532 100644 --- a/native/cocos/platform/CCFileUtils.h +++ b/native/cocos/platform/CCFileUtils.h @@ -639,6 +639,9 @@ public: /** Returns the full path cache. */ const std::unordered_map& getFullPathCache() const { return _fullPathCache; } + std::string normalizePath(const std::string& path) const; + std::string getFileDir(const std::string& path) const; + protected: /** * The default constructor. diff --git a/native/cocos/platform/android/Android.mk b/native/cocos/platform/android/Android.mk index 40bd1104ba..20f55f06b7 100644 --- a/native/cocos/platform/android/Android.mk +++ b/native/cocos/platform/android/Android.mk @@ -10,6 +10,7 @@ LOCAL_SRC_FILES := \ CCDevice-android.cpp \ CCFileUtils-android.cpp \ CCApplication-android.cpp \ +CCCanvasRenderingContext2D-android.cpp \ jni/JniImp.cpp \ jni/JniHelper.cpp \ @@ -18,9 +19,9 @@ LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) LOCAL_C_INCLUDES := $(LOCAL_PATH) \ $(LOCAL_PATH)/.. \ $(LOCAL_PATH)/../.. \ + $(LOCAL_PATH)/../../.. -LOCAL_EXPORT_LDLIBS := -lGLESv1_CM \ - -lGLESv2 \ +LOCAL_EXPORT_LDLIBS := -lGLESv2 \ -lEGL \ -llog \ -landroid diff --git a/native/cocos/platform/android/CCCanvasRenderingContext2D-android.cpp b/native/cocos/platform/android/CCCanvasRenderingContext2D-android.cpp new file mode 100644 index 0000000000..7d571b786b --- /dev/null +++ b/native/cocos/platform/android/CCCanvasRenderingContext2D-android.cpp @@ -0,0 +1,287 @@ +#include "platform/CCCanvasRenderingContext2D.h" +#include "base/ccTypes.h" +#include "base/csscolorparser.hpp" + +#include "cocos/scripting/js-bindings/jswrapper/SeApi.h" + + +#include + +enum class CanvasTextAlign { + LEFT, + CENTER, + RIGHT +}; + +enum class CanvasTextBaseline { + TOP, + MIDDLE, + BOTTOM +}; + +namespace { + void fillRectWithColor(uint8_t* buf, uint32_t totalWidth, uint32_t totalHeight, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint8_t r, uint8_t g, uint8_t b) + { + assert(x + width <= totalWidth); + assert(y + height <= totalHeight); + + uint32_t y0 = totalHeight - (y + height); + uint32_t y1 = totalHeight - y; + uint8_t* p; + for (uint32_t offsetY = y0; offsetY < y1; ++offsetY) + { + for (uint32_t offsetX = x; offsetX < (x + width); ++offsetX) + { + p = buf + (totalWidth * offsetY + offsetX) * 3; + *p++ = r; + *p++ = g; + *p++ = b; + } + } + } +} + +NS_CC_BEGIN + +CanvasGradient::CanvasGradient() +{ + SE_LOGD("CanvasGradient constructor: %p\n", this); +} + +CanvasGradient::~CanvasGradient() +{ + SE_LOGD("CanvasGradient destructor: %p\n", this); +} + +void CanvasGradient::addColorStop(float offset, const std::string& color) +{ + SE_LOGD("CanvasGradient::addColorStop: %p\n", this); +} + +// CanvasRenderingContext2D + +CanvasRenderingContext2D::CanvasRenderingContext2D(float width, float height) +: __width(width) +, __height(height) +{ + SE_LOGD("CanvasGradient constructor: %p, width: %f, height: %f\n", this, width, height); + // _impl = [[CanvasRenderingContext2DImpl alloc] init]; +} + +CanvasRenderingContext2D::~CanvasRenderingContext2D() +{ + SE_LOGD("CanvasGradient destructor: %p\n", this); + // [_impl release]; +} + +void CanvasRenderingContext2D::recreateBuffer() +{ + _isBufferSizeDirty = false; + // [_impl recreateBufferWithWidth: __width height:__height]; +} + +void CanvasRenderingContext2D::clearRect(float x, float y, float width, float height) +{ + SE_LOGD("CanvasGradient::clearRect: %p, %f, %f, %f, %f\n", this, x, y, width, height); + // [_impl clearRect:CGRectMake(x, y, width, height)]; +} + +void CanvasRenderingContext2D::fillRect(float x, float y, float width, float height) +{ + // [_impl fillRect:CGRectMake(x, y, width, height)]; + + if (_canvasBufferUpdatedCB != nullptr) + { + // _canvasBufferUpdatedCB([_impl getDataRef]); + } +} + +void CanvasRenderingContext2D::fillText(const std::string& text, float x, float y, float maxWidth) +{ + SE_LOGD("CanvasRenderingContext2D::fillText: %s, %f, %f, %f\n", text.c_str(), x, y, maxWidth); + if (text.empty()) + return; + if (_isBufferSizeDirty) + recreateBuffer(); + + // [_impl fillText:[NSString stringWithUTF8String:text.c_str()] x:x y:y maxWidth:maxWidth]; + // if (_canvasBufferUpdatedCB != nullptr) + // _canvasBufferUpdatedCB([_impl getDataRef]); +} + +void CanvasRenderingContext2D::strokeText(const std::string& text, float x, float y, float maxWidth) +{ + SE_LOGD("CanvasRenderingContext2D::strokeText: %s, %f, %f, %f\n", text.c_str(), x, y, maxWidth); + if (text.empty()) + return; + if (_isBufferSizeDirty) + recreateBuffer(); + +// if (_canvasBufferUpdatedCB != nullptr) +// _canvasBufferUpdatedCB([_impl getDataRef]); +} + +cocos2d::Size CanvasRenderingContext2D::measureText(const std::string& text) +{ + SE_LOGD("CanvasRenderingContext2D::measureText: %s\n", text.c_str()); + // CGSize size = [_impl measureText: [NSString stringWithUTF8String:text.c_str()]]; + // return cocos2d::Size(size.width, size.height); + return cocos2d::Size(100, 30); +} + +CanvasGradient* CanvasRenderingContext2D::createLinearGradient(float x0, float y0, float x1, float y1) +{ + return nullptr; +} + +void CanvasRenderingContext2D::save() +{ + +} + +void CanvasRenderingContext2D::beginPath() +{ + +} + +void CanvasRenderingContext2D::closePath() +{ + +} + +void CanvasRenderingContext2D::moveTo(float x, float y) +{ + +} + +void CanvasRenderingContext2D::lineTo(float x, float y) +{ + +} + +void CanvasRenderingContext2D::stroke() +{ + +} + +void CanvasRenderingContext2D::restore() +{ + +} + +void CanvasRenderingContext2D::setCanvasBufferUpdatedCallback(const CanvasBufferUpdatedCallback& cb) +{ + _canvasBufferUpdatedCB = cb; +} + +void CanvasRenderingContext2D::set__width(float width) +{ + SE_LOGD("CanvasRenderingContext2D::set__width: %f\n", width); + __width = width; + _isBufferSizeDirty = true; + recreateBuffer(); +} + +void CanvasRenderingContext2D::set__height(float height) +{ + SE_LOGD("CanvasRenderingContext2D::set__height: %f\n", height); + __height = height; + _isBufferSizeDirty = true; + recreateBuffer(); +} + +void CanvasRenderingContext2D::set_lineWidth(float lineWidth) +{ + +} + +void CanvasRenderingContext2D::set_lineJoin(const std::string& lineJoin) +{ + +} + +void CanvasRenderingContext2D::set_font(const std::string& font) +{ + if (_font != font) + { + _font = font; + + // TODO: cjh implements bold + std::string bold; + std::string fontName = "Arial"; + std::string fontSizeStr = "30"; + + std::regex re("(bold)?\\s*(\\d+)px\\s+(\\w+)"); + std::match_results results; + if (std::regex_search(_font.cbegin(), _font.cend(), results, re)) + { + bold = results[1].str(); + fontSizeStr = results[2].str(); + fontName = results[3].str(); + } + + // CGFloat fontSize = atof(fontSizeStr.c_str()); + // [_impl updateFontWithName:[NSString stringWithUTF8String:fontName.c_str()] fontSize:fontSize]; + } +} + +void CanvasRenderingContext2D::set_textAlign(const std::string& textAlign) +{ + SE_LOGD("CanvasRenderingContext2D::set_textAlign: %s\n", textAlign.c_str()); + if (textAlign == "left") + { + // _impl.textAlign = CanvasTextAlign::LEFT; + } + else if (textAlign == "center") + { + // _impl.textAlign = CanvasTextAlign::CENTER; + } + else if (textAlign == "right") + { + // _impl.textAlign = CanvasTextAlign::RIGHT; + } + else + { + assert(false); + } +} + +void CanvasRenderingContext2D::set_textBaseline(const std::string& textBaseline) +{ + SE_LOGD("CanvasRenderingContext2D::set_textBaseline: %s\n", textBaseline.c_str()); + if (textBaseline == "top") + { + // _impl.textBaseLine = CanvasTextBaseline::TOP; + } + else if (textBaseline == "middle") + { + // _impl.textBaseLine = CanvasTextBaseline::MIDDLE; + } + else if (textBaseline == "bottom") + { + // _impl.textBaseLine = CanvasTextBaseline::BOTTOM; + } + else + { + assert(false); + } +} + +void CanvasRenderingContext2D::set_fillStyle(const std::string& fillStyle) +{ + CSSColorParser::Color color = CSSColorParser::parse(fillStyle); + // [_impl setFillStyleWithRed:color.r/255.0f green:color.g/255.0f blue:color.b/255.0f alpha:color.a]; + SE_LOGD("CanvasRenderingContext2D::set_fillStyle: %s, (%d, %d, %d, %f)\n", fillStyle.c_str(), color.r, color.g, color.b, color.a); +} + +void CanvasRenderingContext2D::set_strokeStyle(const std::string& strokeStyle) +{ + +} + +void CanvasRenderingContext2D::set_globalCompositeOperation(const std::string& globalCompositeOperation) +{ + +} + +NS_CC_END diff --git a/native/cocos/platform/android/CCFileUtils-android.cpp b/native/cocos/platform/android/CCFileUtils-android.cpp index 6b43acd5bd..73c9419f09 100644 --- a/native/cocos/platform/android/CCFileUtils-android.cpp +++ b/native/cocos/platform/android/CCFileUtils-android.cpp @@ -196,21 +196,25 @@ bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const return bFound; } -bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) const +bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath_) const { - if (dirPath.empty()) + if (dirPath_.empty()) { return false; } - const char* s = dirPath.c_str(); - - // find absolute path in flash memory - if (s[0] == '/') + std::string dirPath = dirPath_; + if (dirPath[dirPath.length() - 1] == '/') { - CCLOG("find in flash memory dirPath(%s)", s); + dirPath[dirPath.length() - 1] = '\0'; + } + + // find absolute path in flash memory + if (dirPath[0] == '/') + { + CCLOG("find in flash memory dirPath(%s)", dirPath.c_str()); struct stat st; - if (stat(s, &st) == 0) + if (stat(dirPath.c_str(), &st) == 0) { return S_ISDIR(st.st_mode); } @@ -219,7 +223,8 @@ bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) cons { // find it in apk's assets dir // Found "assets/" at the beginning of the path and we don't want it - CCLOG("find in apk dirPath(%s)", s); + CCLOG("find in apk dirPath(%s)", dirPath.c_str()); + const char* s = dirPath.c_str(); if (dirPath.find(ASSETS_FOLDER_NAME) == 0) { s += ASSETS_FOLDER_NAME_LENGTH; @@ -258,6 +263,8 @@ FileUtils::Status FileUtilsAndroid::getContents(const std::string& filename, Res return FileUtils::Status::NotExists; string fullPath = fullPathForFilename(filename); + if (fullPath.empty()) + return FileUtils::Status::NotExists; if (fullPath[0] == '/') return FileUtils::getContents(fullPath, buffer); @@ -284,7 +291,7 @@ FileUtils::Status FileUtilsAndroid::getContents(const std::string& filename, Res AAsset* asset = AAssetManager_open(assetmanager, relativePath.data(), AASSET_MODE_UNKNOWN); if (nullptr == asset) { - LOGD("asset is nullptr"); + LOGD("asset (%s) is nullptr", filename.c_str()); return FileUtils::Status::OpenFailed; } diff --git a/native/cocos/renderer/gfx/Program.cpp b/native/cocos/renderer/gfx/Program.cpp index 08383fbbd9..6e2d83b0af 100644 --- a/native/cocos/renderer/gfx/Program.cpp +++ b/native/cocos/renderer/gfx/Program.cpp @@ -27,6 +27,7 @@ #include #include +#include namespace { diff --git a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp index 7f14b8d89b..8f3c928d5d 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp @@ -208,37 +208,6 @@ namespace { // } // SE_BIND_FUNC(require) - static void normalizePath(std::string& path) - { - // Normalize: remove . and .. - path = std::regex_replace(path, std::regex("/\\./"), "/"); - path = std::regex_replace(path, std::regex("/\\.$"), ""); - - size_t pos; - while ((pos = path.find("..")) != std::string::npos) - { - size_t prevSlash = path.rfind("/", pos-2); - if (prevSlash == std::string::npos) - break; - - path = path.replace(prevSlash, pos - prevSlash + 2 , ""); - } - } - - static std::string getFileDir(const std::string& path) - { - std::string ret; - size_t pos = path.rfind("/"); - if (pos != std::string::npos) - { - ret = path.substr(0, pos); - } - - normalizePath(ret); - - return ret; - } - static bool doModuleRequire(const std::string& path, se::Value* ret, const std::string& prevScriptFileDir) { se::AutoHandleScope hs; @@ -282,7 +251,6 @@ namespace { fullPath = fileOperationDelegate.onGetFullPath(pathWithSuffix); } - normalizePath(fullPath); if (!scriptBuffer.empty()) { @@ -293,7 +261,7 @@ namespace { // printf("Found cache: %s, value: %d\n", fullPath.c_str(), (int)ret->getType()); return true; } - std::string currentScriptFileDir = getFileDir(fullPath); + std::string currentScriptFileDir = FileUtils::getInstance()->getFileDir(fullPath); // Add closure for evalutate the script char prefix[] = "(function(currentScriptDir){ window.module = window.module || {}; var exports = window.module.exports = {}; ";