CocosBuilder Reader Ported to Cocos2d-X

I just ported the CCBReader class from the popular CocosBuilder project into Cocos2d-X. It can now handle and parse .ccb files created by CocosBuilder in a Cocos2d-X project.

GitHub: https://github.com/diwu/CCB-X-Reader

Due to C++’s not-that-dynamic nature as compared to Objective-C, I added a new CCBClassGenerator class to handle all the dynamic stuff for this port. Make sure you have properly implemented this class before testing your own .ccb files.

As of now, this project only supports .ccb (which is actually a .plist file by nature). .ccbi is currently not supported.

And, as of now, this project is not quite finished yet. But the enclosing example project can run without a problem.

Go take a look and let me know what you guys think.

YouTube Demo: http://www.youtube.com/watch?v=QgA0fkse-AA

So sorry I didn’t post my post in time


CCBReader is almost the same between these 2 port. While the difficult part is reflection. You use CCBClassGenerator.cpp to make it simple.
My implementation is smart but not good enough. I will keep improve it.

Hi, Diwu, thanks for your contribution! Eventually I refered your design and made c++ reflection easier.
The merged source is here http://github.com/cocos2d/cocos2d-x/pull/885
Also I add you into Authors list http://github.com/cocos2d/cocos2d-x/blob/gles20/AUTHORS

Thanks Walzer. Really appreciated it. :slight_smile:

This is awesome, thanks!

I’ve managed to get it working in my game. However, I did have some trouble with the way the reader handles strings that are in the dictionary.

This was all using the win32 version of cocos2d.

Here’s the line that gets the file type:

const char * fileType = (((CCString**) )) -> toStdString).c_str;
This calls the function CCString::toStdString, which is defined like this:
std::string toStdString
{
return m_sString;
}
This function returns a std::string which is a copy of the value of m_sString. Thus, this copy is made when the function is called on the CCString containing the fileType, and then the char** is accessed with c_str. However, when I first ran the reader, the value of fileType after the line was executed was an empty string (“”).

It seemed that what was happening was that the std::string returned by toStdString() was being destroyed immediately after this line was executed. This also destroyed the attached char* string. Why the value of fileType was “” rather than some random bit of memory I don’t know. Perhaps it’s my compiler settings.

Perhaps all of this is my compiler settings! But anyway, I got it working by changing the toStdString function to this:

std::string &toStdString()
{
return m_sString;
}

This seems like it might be dangerous if the value returned anywhere in the engine is modified. But at least for my game it all seems fine.

Anyway, I hope that makes sense. Thanks again for the port! Now if only someone would port cocosbuilder itself so I don’t have to run it on a VMWare box :stuck_out_tongue:

Isaac

Hi Isaac,

Thanks for the feedback. I’m super excited that the port is working in your game

Since this function CCString::toStdString() is used in a lot of places in the port, do you find all of them not working, or just that one or two places you mentioned in your feedback?

Besides, based on my understanding of your description, this is more likely to be a Cocos2d-X issue on Win32. You provided a way to fix it in the Cocos2d-X source. However, if the Cocos2d-X source has to be remain unchanged, do you have any suggestions that we may fix it on the CCBReader port part? Such as adding some runtime check?

Cheers,

Di

I have download the sample project on github — CocosBuilder Reader Ported to Cocos2d-X(http://www.cocos2d-x.org/boards/6/topics/10755)
When I compile it to android platform it shows the problem as the follows, do someone help me to point the problem out.

05-11 08:46:07.699: E/AndroidRuntime(3118): FATAL EXCEPTION: main
05-11 08:46:07.699: E/AndroidRuntime(3118): java.lang.ExceptionInInitializerError
05-11 08:46:07.699: E/AndroidRuntime(3118): at java.lang.Class.newInstanceImpl(Native Method)
05-11 08:46:07.699: E/AndroidRuntime(3118): at java.lang.Class.newInstance(Class.java:1409)
05-11 08:46:07.699: E/AndroidRuntime(3118): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-11 08:46:07.699: E/AndroidRuntime(3118): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
05-11 08:46:07.699: E/AndroidRuntime(3118): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-11 08:46:07.699: E/AndroidRuntime(3118): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-11 08:46:07.699: E/AndroidRuntime(3118): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-11 08:46:07.699: E/AndroidRuntime(3118): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 08:46:07.699: E/AndroidRuntime(3118): at android.os.Looper.loop(Looper.java:123)
05-11 08:46:07.699: E/AndroidRuntime(3118): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-11 08:46:07.699: E/AndroidRuntime(3118): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 08:46:07.699: E/AndroidRuntime(3118): at java.lang.reflect.Method.invoke(Method.java:507)
05-11 08:46:07.699: E/AndroidRuntime(3118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-11 08:46:07.699: E/AndroidRuntime(3118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-11 08:46:07.699: E/AndroidRuntime(3118): at dalvik.system.NativeStart.main(Native Method)
05-11 08:46:07.699: E/AndroidRuntime(3118): Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: link_image1963: 94 could not load needed library ‘libcocosdenshion.so’ for ‘libgame.so’ (load_library1105: Library ‘libcocosdenshion.so’ not found)
05-11 08:46:07.699: E/AndroidRuntime(3118): at java.lang.Runtime.loadLibrary(Runtime.java:434)
05-11 08:46:07.699: E/AndroidRuntime(3118): at java.lang.System.loadLibrary(System.java:554)
05-11 08:46:07.699: E/AndroidRuntime(3118): at com.diwublog.ccb.ccb.(ccb.java:60)
05-11 08:46:07.699: E/AndroidRuntime(3118): … 15 more

And the visual studio also throw the error like:

1>CCBReader.obj : error LNK2001: unresolved external symbol “public: virtual float thiscall cocos2d::CCParticleSystem::getTimeScale"
1>CCBReader.obj : error LNK2001: unresolved external symbol "public: virtual void
thiscall cocos2d::CCParticleSystem::setTimeScale(float)” (?setTimeScale@CCParticleSystem@cocos2d@UAEXMZ)

Did you modify the build_native.sh and change the path params into your own before compiling?

Example:

#change them into your own params
NDK_ROOT=/Users/diwwu/Dropbox/Cocos2d-x/android-ndk-r7c/
COCOS2DX_ROOT=…/libs
GAME_ROOT=…/
GAME_ANDROID_ROOT=$GAME_ROOT/android
RESOURCE_ROOT=$GAME_ROOT/Resources

Hi, I built it on Windows and I did not use the build_native.sh

this is my AndroidMake File

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := game

LOCAL_SRC_FILES := main.cpp …/…/Classes/AppDelegate.cpp …/…/Classes/CCBReader.cpp …/…/Classes/CCBClassGenerator.cpp …/…/Classes/HelloCocosBuilder.cpp

LOCAL_C_INCLUDES := …/…/libs/cocos2dx …/…/libs/cocos2dx/platform …/…/libs/cocos2dx/include …/…/libs/CocosDenshion/include $(LOCAL_PATH)/…/…/Classes/
LOCAL_SHARED_LIBRARIES := cocosdenshion_shared box2d_shared chipmunk_shared

LOCAL_STATIC_LIBRARIES := png_static_prebuilt
LOCAL_STATIC_LIBRARIES = xml2_static_prebuilt
LOCAL_STATIC_LIBRARIES
= jpeg_static_prebuilt
LOCAL_STATIC_LIBRARIES = curl_static_prebuilt
#LOCAL_WHOLE_STATIC_LIBRARIES := game_logic_static
LOCAL_WHOLE_STATIC_LIBRARIES
= cocos2dx_static

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libpng)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libxml2)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libjpeg)

Hi, Thank you for your promotion reply.
I have running CCBExample successfully on my computer with the latest version But the error occurs when I press the pressButton:
The problem seems because
m_pSpriteIcon->stopAllActions() ;
Do anyone have this problem also?

Hello.
I am working on CocosBuilder and lua-scripts integration for my project now.
I use ccb markup file and lua-script file for each scene.

In lua-script variables of type “Owner var” available. And selectors in ccb with Target=“Owner” are binded with functions from lua-script.

To implement this functionality I have made some modifications in CCBCustomClassProtocol class:

  1. “bool callbackSetChildren(const char* name, cocos2d::CCObject* node)”
    changed to
    “bool callbackSetChildren(const char* name, cocos2d::CCObject* node, const char* type)”

It is necessary to define types of variables in lua-script, not just CCObject type.

  1. “cocos2d::SEL_MenuHandler callbackGetSelectors(const char* selectorName)”
    changed to
    “void callbackGetSelectors(const char* selectorName, cocos2d::CCObject*& target, cocos2d::SEL_MenuHandler& selector)”
    I create wrapper-objects for selectors from script which I return as Target.

Can you apply this changed in main branch?
If you will apply it I can publish my integration as extension when it will be completed.

What version cocosbuilder you used ?

I both try v1.0 and v1.1, when i try to use v1.0, it does not run at all, but use v1.1, i get “Cocos2d: WARNING! Trying to load file made with a newer version of CocosBuilder, please update the CCBReader class” in console.
the contents of v1.0 :
fileVersion
1

v 1.1:
fileVersion
3

but in the example, the fileVersion is 2.

Did you resolve this problem? I have the same problem .

CocosBuilder v1.1-beta changed ccb file format A LOT, which is named v3.
Currently I only added support for ccb v1 and v2. About v3, I prefer to add it when CocosBuilder v.1.1-final release.

I reslove this problem.
Use text editor to open the example.ccb file and change “fileVersion” from “2” to “1” and then you can you CocosBuild version 1.0 to open the new one.

What’s the status for CocosBuilder v2? Is there somebody working on the import of .ccb(i) files generated with CocosBuilder v2?