• Stars
    star
    234
  • Rank 171,630 (Top 4 %)
  • Language
    C++
  • Created almost 8 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Creator plugin to support C++& Lua in cocos2d-x

Table of Contents generated with DocToc

Creator support for Cocos2d-x

Requirements

  • cocos2d-x: v3.14+
  • Cocos Creator: v1.4+

Limitations

Given that Creator uses a component based model to create its objects, and cocos2d-x has its monolithic structure, it is only possible to support a limited subset of Creator features.

Supported nodes:

  • Scene
  • Sprite
  • Canvas (but only one per scene)
  • ScrollView
  • Label
  • EditBox
  • ParticleSystem
  • TiledMap
  • Button
  • ProgressBar
  • RichText:
    • Need cocos2d-x 3.16+ to support img tag, refer to this issue for detail information.
    • Doesn't support line heightbecause cocos2d-x's RichText doesn't support this features.
    • Doesn't support horizontal alignmentbecause cocos2d-x's RichText doesn't support this features. Though cocos2d-x v3.16+ supports this feature, but it is hard for plugin to danymicly supporting it according cocos2d-x's version.
  • SpineSkeleton
  • Widget: only supports AlignOnce
  • Animations
  • VideoPlayer: iOS should add MediaPlayer.framework to the project
  • WebView
  • Slider
  • Toggle
  • ToggleGroup
  • PageView
  • Mask
  • Collider
  • Prefab
  • DragonBones
  • MotionStreak

Supporting JavaScript scripts would be overkill. If you need JavaScript scripting support, just use Creator.

Sample project

Can fetch this branch and run cpp-empty-test or lua-empty-test. The branch based on v3.15, don't forget to update external libraries.

Currently support on Mac, iOS, Android and Windows.

How to generate the needed files

  • download and install Cocos Creator
  • use Cocos Creator to open creator_project
  • click Project -> LuaCPP Support -> Setup Target Project
  • fill in Project Path, it is a c++ or lua project created by cocos2d-x(3.14+) console
  • click Build

Options

  • Export Resource Only, only resources include Creator scene files will be exported, the reader source code won't. It will be usefull when you don't want to cover the reader code in Cocos2d-x project.
  • Export Resource Dynamically Loaded, export the resources that might be used in runtime, those resources located at assets/resources.
  • Auto Build After Scene Saved, as the name said, auto build and export resources after Creator scene saved.

You will find:

  • all needed source codes are generated into NATIVE_PROJECT_ROOT/Classes/reader(it is NATIVE_PROJECT_ROOT/frameworks/runtime-src/Classes/reader for lua project)
  • all needed resources are generated into NATIVE_PROJECT_ROOT/Resources/creator(it is NATIVE_PROJECT_ROOT/frameworks/runtime-src/Resources/creator for lua project)

Header search path

For cpp projects, just add reader into header search path.

For lua projects, add the following header paths:

  • reader
  • reader/collider
  • reader/animation
  • reader/dragonbones/cocos2dx
  • reader/dragonbones/armature
  • reader/dragonbones/animation
  • reader/dragonbones/events
  • reader/dragonbones/factories
  • reader/dragonbones/core
  • reader/dragonbones/geom

If developing for Android, can just use existing Android.mk, for example, use the Android.mk into your game's Android.mk like this:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := MyGame_shared

LOCAL_MODULE_FILENAME := libMyGame

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END


LOCAL_STATIC_LIBRARIES := cocos2dx_static
# LOCAL_STATIC_LIBRARIES += creator_reader_lua  # for lua project 
LOCAL_STATIC_LIBRARIES += creator_reader   # add dependence

# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END

include $(BUILD_SHARED_LIBRARY)

$(call import-module,.)
$(call import-module, ./../../Classes/reader)  # import module path

If developing with Lua, then need to add CreatorReaderBinding.cpp into plugin's Android.mk.

Using it from C++

// mygame.cpp

#include "reader/CreatorReader.h"

void some_function()
{
    creator::CreatorReader* reader = creator::CreatorReader::createWithFilename("creator/scenes/sprites/CreatorSprites.ccreator");

    // will create the needed spritesheets + design resolution
    reader->setup();

    // get the scene graph
    Scene* scene = reader->getSceneGraph();

    // ...and use it
    Director::getInstance()->replaceScene(scene);
}

Using it from lua

Register creator binding codes in c++

#include "reader/lua-bindings/creator_reader_bindings.hpp"

...

register_creator_reader_manual(L);

Use in lua

local creatorReader = creator.CreatorReader:createWithFilename('creator/CreatorSprites.ccreator')
creatorReader:setup()
local scene = creatorReader:getSceneGraph()
cc.Director:getInstance():replaceScene(scene)

How to use ColliderManager

ColliderManager is used to manage collisions. Every scene has an instance of ColliderManager . You can use it like this to listen collision events:

creator::CreatorReader* reader = creator::CreatorReader::createWithFilename("creator/CreatorSprites.ccreator");

// will create the needed spritesheets + design resolution
reader->setup();

// get the scene graph
Scene* scene = reader->getSceneGraph();

auto colliderManager = scene->getColliderManager();
colliderManager->registerCollitionCallback([=](creator::Contract::CollisionType type,
                                                             creator::Collider* collider1,
                                                             creator::Collider* collider2) {
        if (type == creator::Contract::CollisionType::ENTER)
            colliderManager->enableDebugDraw(true);
        
        if (type == creator::Contract::CollisionType::EXIT)
            colliderManager->enableDebugDraw(false);
        
}, "");

More features of colliderManager can refer to the header file.

Use the plugin in your Cocos Creator project

You can install the released version from Creator, or you can copy creator_project/packages/creator_luacpp_support into Cocos Creator project/packages, then you will see the plugin in Project -> LuaCPP Support.

More Repositories

1

cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
C++
18,090
star
2

cocos2d-objc

Cocos2d for iOS and OS X, built using Objective-C
Objective-C
4,070
star
3

cocos2d-html5

Cocos2d for Web Browsers. Built using JavaScript.
JavaScript
3,055
star
4

cocos2d-js

cocos2d-x for JS
C++
1,858
star
5

CocosBuilder

CocosBuilder, the visual editor for cocos2d
Objective-C
1,012
star
6

cocos2d-iphone-extensions

3rd party extensions for cocos2d-iphone
Objective-C
638
star
7

cocos2d-x-samples

Contains different cocos2d-x samples
Python
575
star
8

cocos2d-x-3rd-party-libs-bin

C
197
star
9

cocos2d-js-tests

cocos2d JavaScript test cases and games.
JavaScript
187
star
10

bindings-generator

JSBindings generator for C++
Python
168
star
11

cocos2d-x-extensions

samples games & code snips for cocos2d-x
C++
152
star
12

cocos2d-console

cocos2d command line tool
Python
151
star
13

cocos2d-x-3rd-party-libs-src

Dependencies of cocos2d-x.
Perl
140
star
14

cocos2d-x-for-xna

port cocos2d-x into c# based on XNA
C#
123
star
15

cocos2d-x-docs

documentation of cocos2d-x
HTML
102
star
16

cocos2d-objc-ext

Extensions and utility classes for Cocos2D-iphone
Objective-C
58
star
17

cocos2d-iphone-classic

Repository of original V1 and V2 of Cocos2D-iphone
Objective-C
47
star
18

cocos2d-frame

Player and "stub" for cocos2d
C
37
star
19

cocos2d-js-samples

Includes different samples to be used with cocos2d
JavaScript
36
star
20

cocos2d-x-swift-bindings

Swift bindings for cocos2d-x
Objective-C++
32
star
21

cocos2d-x-external

use CMake to build external 3rd party modules for cocos2d-x
C
18
star
22

cocos2d-js__old__

cocos2d in JavaScript central repository
JavaScript
14
star
23

js-bindings

C++
13
star
24

cocos2d-x-classic

For full history commit logs of cocos2d-x
C
8
star
25

v8

V8 building
Python
7
star
26

migration-demo-objc

The Objective-C flavoured version of migration-demo-x
Objective-C
6
star
27

migration-demo-x

Demonstrates -obj to -x project migration
C++
5
star
28

console-binary

The binary files for cocos2d-console
4
star
29

cocos2d-x-docs-deps

Dependencies of building cocos2d-x docs
Shell
2
star
30

cocos2d.github.io

1
star