How to change Cocos2dx-wp8 from exe to Dynamic Link Library (dll)

Hi all,

Thanks for getting the WP8 up and running! Great addition to an already great game engine.

h1. I need help please!
in building a dll/lib from the provided sdk. This is what I did so far:

For Windows Phone 8
Using SDK: cocos2dx-0.13.0-wp8-0.8.zip
For existing C# (XAML) and C++ (DirectX) project. I want to use some of the code from cocos2dx-wp8 (for Windows Phone 8). To do this, I did the following:

  1. Open my C#/XAML/DirectX Game1 solution .sln project in Visual Studio 2012.

  2. Right click on solution in Solution Explorer. Add --> Existing Project…

  3. Selected: \cocos2dx-0.13.0-wp8-0.8\HelloWorld\cocos2d.vcxproj and clicked open

  4. Renamed the HelloWorld project to something else like “Cocos2dxWp8” and delete the HelloWorld Classes folder and all the Assets you don’t want (like the squirrels and background images)
    4b. Click Properties --> Configuration Properties --> General --> Configuration Type --> from "Application (.exe) change to “Dynamic Library (.dll)”

  5. Added new paths to Properties --> C/C++ --> Additional Include Directories -->

@$(ProjectDir);$(IntermediateOutputPath);…\cocos2dx\include;…\cocos2dx;…\cocos2dx\platform\third_party\win8_metro\libpng;…\cocos2dx\platform\third_party\win8_metro\zlib;…\cocos2dx\platform\third_party\win8_metro;…\Cocos2dxWp8;…\cocos2dx\platform\win8_metro;…\cocos2dx\platform\third_party\win8_metro\freetype;…\cocos2dx\platform\third_party\win8_metro\libjpeg;…\CocosDenshion\include;…\cocos2dx\platform;inc;%(AdditionalIncludeDirectories)
@

  1. Updated the paths in Properties --> Linker --> Input --> Additional Dependencies -->

@…\cocos2dx\platform\third_party\win8_metro\libraries\x86\freetype.lib;…\cocos2dx\platform\third_party\win8_metro\libraries\x86\zlib.lib;…\cocos2dx\platform\third_party\win8_metro\libraries\x86\libjpeg.lib;…\cocos2dx\platform\third_party\win8_metro\libraries\x86\libpng.lib;dxguid.lib;xaudio2.lib;dxgi.lib;uuid.lib;d3d11.lib;%(AdditionalDependencies)
@
7. Changed in Properties --> Linker --> Debugging --> Generate Debug Info --> No (as it was giving me warnings)

Build and no errors.

  1. Now in my C++ DirectXProject (still within my same solution .sln file), right click and select Properties.
  2. Top of the left pane select Common Properties --> Framework and Properties
  3. On the right pane, click “Add New Reference…” select the checkbox for the newly added Cocos2dxWp8 project. Keep defaults.
  4. In Project Dependencies, make sure the Cocos2dxWp8 project is selected, so when I build this DirectX project, it’ll also build the Cocos2dxWp8 project.
  5. Now in my sample CubeRenderer.cpp file I add:

@using namespace cocos2d;@

But it doesn’t find this namespace.

  1. Make sure to add:

@#include “…\Cocos2dxWp8\cocos2dx\CCConfiguration.h”@

  1. Now if you use any other classes like particles, I would include:

@#include “…\Cocos2dxWp8\cocos2dx\include\CCParticleExamples.h”@

Use it somewhere in code:

CCParticleSun* explosion = new CCParticleSun();

Anyhow, I’m still experimenting with cocos2dx classes in this template CubeRenderer.cpp file. I have yet to get any Cocos2d sprites or particles drawing on a XAML DrawingSurfaceBackgroundGrid control. If anyone else has it working, please chime in. Would be nice to know it works.

h1. EDITS:

Ok, that didn’t work, looks like a rebuild caught some problems. Errors:

@Error 1 Metadata file ‘F:\src\WindowsPhone\GameWp8Dx\Debug\GameWp8DxComponent\GameWp8DxComponent.winmd’ could not be found F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\GameWp8Dx\CSC GameWp8Dx
Warning 2 warning LNK4098: defaultlib ‘MSVCRT’ conflicts with use of other libs; use /NODEFAULTLIB:library F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\HelloWorld\LINK Cocos2dXWp8Framework
Error 3 error C1083: Cannot open include file: ‘CCObject.h’: No such file or directory (CubeRenderer.cpp) f:\src\windowsphone\GameWp8Dx\GameWp8Dx\cocos2dxwp8\cocos2dx\ccconfiguration.h 31 1 GameWp8DxComponent
4 IntelliSense: cannot open source file “CCObject.h” f:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\cocos2dx\CCConfiguration.h 31 1 GameWp8DxComponent
5 IntelliSense: cannot open source file “CCGL.h” f:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\cocos2dx\CCConfiguration.h 33 1 GameWp8DxComponent
6 IntelliSense: expected a ‘;’ f:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\cocos2dx\CCConfiguration.h 69 30 GameWp8DxComponent
7 IntelliSense: expected an identifier f:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\cocos2dx\CCConfiguration.h 150 1 GameWp8DxComponent
8 IntelliSense: expected a declaration f:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\cocos2dx\CCConfiguration.h 154 1 GameWp8DxComponent@

Actually I should include:

@#include “…\Cocos2dxWp8\cocos2dx\include\cocos2d.h”@

But even more errors! Looks like I’m missing something in my includes? Anyone know what this is?

@Error 1 error C1083: Cannot open include file: ‘CCPlatformConfig.h’: No such file or directory f:\src\windowsphone\gamewp8dx\gamewp8dx\cocos2dxwp8\cocos2dx\include\ccconfig.h 25 1 gamewp8dx
Error 2 Metadata file ‘F:\src\WindowsPhone\gamewp8dx\Debug\gamewp8dxComponent\gamewp8dxComponent.winmd’ could not be found F:\src\WindowsPhone\gamewp8dx\gamewp8dx\gamewp8dx\CSC gamewp8dx
3 IntelliSense: cannot open source file “CCPlatformConfig.h” f:\src\WindowsPhone\gamewp8dx\gamewp8dx\Cocos2dxWp8\cocos2dx\include\ccConfig.h 25 1 gamewp8dxComponent
4 IntelliSense: cannot open source file “CCCommon.h” f:\src\WindowsPhone\gamewp8dx\gamewp8dx\Cocos2dxWp8\cocos2dx\include\CCGeometry.h 30 1 gamewp8dxComponent
5 IntelliSense: incomplete type is not allowed f:\src\WindowsPhone\gamewp8dx\gamewp8dx\Cocos2dxWp8\cocos2dx\include\CCGeometry.h 36 14 gamewp8dxComponent
6 IntelliSense: expected a ‘;’ f:\src\WindowsPhone\gamewp8dx\gamewp8dx\Cocos2dxWp8\cocos2dx\include\CCGeometry.h 37 1 gamewp8dxComponent
@

h1. EDIT #2

I added to Properties --> C/C++ --> Additional Include Directories the same include folders that I included in the Cocos2dxWp8 project. And added to CubeRenderer.cpp:

@#include “CCPlatformConfig.h”
#include “CCGeometry.h”
#include “cocos2d.h”
@

Now, it compiles, but I get linker errors because I haven’ed linked to Cocos’ .lib file.

@@Error 1 error LNK2019: unresolved external symbol “public: __thiscall cocos2d::CCPoint::CCPoint(float,float)” (??0CCPoint@cocos2d@@QAE@MM@Z) referenced in function “void __cdecl cocos2d::`dynamic initializer for ‘CCPointZero’’(void)” (??__ECCPointZero@cocos2d@@YAXXZ) F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\GameWp8DxComponent\CubeRenderer.obj GameWp8DxComponent
@

However, the .lib file didn’t build in the Cocos2dxWp8 project. Why is that?

h1. EDIT 3

To build the .lib file I need to enable dll exports in the Cocos2dxWp8 project. To do this, I opened CCPlatformMacros.h and enabled CC_DLL:

@#define _USRDLL
#undef CC_DLL
#if defined(_USRDLL)
#define CC_DLL __declspec(dllexport)
#else /* use a DLL library */
#define CC_DLL __declspec(dllimport)
#endif@

Now the lib will export, but stops when errors occur in the linker:

715 Errors, 772 Warnings

Error 773 error LNK2001: unresolved external symbol “public: virtual __thiscall cocos2d::CCKeypadHandler::~CCKeypadHandler(void)” (??1CCKeypadHandler@cocos2d@@UAE@XZ) F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\HelloWorld\CCSpriteBatchNode.obj Cocos2dXWp8Framework
Error 774 error LNK2001: unresolved external symbol “public: virtual __thiscall cocos2d::CCKeypadHandler::~CCKeypadHandler(void)” (??1CCKeypadHandler@cocos2d@@UAE@XZ) F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\HelloWorld\cocos2dorig.obj Cocos2dXWp8Framework
Error 775 error LNK2001: unresolved external symbol “public: virtual __thiscall cocos2d::CCKeypadHandler::~CCKeypadHandler(void)” (??1CCKeypadHandler@cocos2d@@UAE@XZ) F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\HelloWorld\CCImage_win8_metro.obj Cocos2dXWp8Framework


@
Error 1482 error LNK2001: unresolved external symbol “public: virtual void __thiscall cocos2d::CCTMXMapInfo::textHandler(void *,char const *,int)” (?textHandler@CCTMXMapInfo@cocos2d@@UAEXPAXPBDH@Z) F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\HelloWorld\CCDrawingPrimitives.obj Cocos2dXWp8Framework
Error 1483 error LNK2001: unresolved external symbol “public: virtual void __thiscall cocos2d::CCTMXMapInfo::textHandler(void *,char const *,int)” (?textHandler@CCTMXMapInfo@cocos2d@@UAEXPAXPBDH@Z) F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\HelloWorld\cocos2d.obj Cocos2dXWp8Framework
Error 1484 error LNK2019: unresolved external symbol “public: virtual __thiscall cocos2d::CCKeypadDispatcher::~CCKeypadDispatcher(void)” (??1CCKeypadDispatcher@cocos2d@@UAE@XZ) referenced in function “public: virtual void * __thiscall cocos2d::CCKeypadDispatcher::`vector deleting destructor’(unsigned int)” (??_ECCKeypadDispatcher@cocos2d@@UAEPAXI@Z) F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\HelloWorld\CCDirector.obj Cocos2dXWp8Framework
Error 1485 error LNK2001: unresolved external symbol “public: virtual __thiscall cocos2d::CCKeypadDispatcher::~CCKeypadDispatcher(void)” (??1CCKeypadDispatcher@cocos2d@@UAE@XZ) F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\HelloWorld\CCLayer.obj Cocos2dXWp8Framework
Error 1486 error LNK2001: unresolved external symbol “public: virtual __thiscall cocos2d::CCKeypadDispatcher::~CCKeypadDispatcher(void)” (??1CCKeypadDispatcher@cocos2d@@UAE@XZ) F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\HelloWorld\CCEGLView_win8_metro.obj Cocos2dXWp8Framework
Error 1487 error LNK1120: 48 unresolved externals F:\src\WindowsPhone\GameWp8Dx\GameWp8Dx\Cocos2dxWp8\Win32\Debug\cocos2d\Cocos2dXWp8Framework.dll Cocos2dXWp8Framework@

Why are these symbols not found? I’m so confused, the include files are in the project. What do I need to link to?

Thanks.