天天看点

VS2010 + cocos2dx 2.X版本 + python 3

Cocos2dx就不废话介绍了, 非常火的游戏引擎,关键是它开源,可以免费下载、学习、开发,不用搞这么多激活的东西。下面以Cocos2dx 2.x为例说明这个平台的一些基本东西。虽然现在Cocos2dx早已去到3.x的稳定版本,但之所以选用Cocos2dx 2.x版本,主要是这方面的资料比较多,本来搞Cocos2dx的人就不多了,搞了也没几个人写写编程记录,再去毫无资料只有一堆难以看懂的官方文档的Cocos2dx 3.x,基本上等于瞎整。

首先,由于这引擎在Windows的主流平台是通过大家熟悉的C++写出来的,所以Cocos2dx 2.x的开发,你首先要准备Visual Studio 2010以上的版本。如果你选用Cocos2dx 3.x还要支持准备Visual Studio 2012以上的版本。已经不支持Visual Studio 2008了,这就没办法了,怪怪下载一个完整安装。网上一搜一堆,不说了。

之后,由于Cocos2dx 2.x在当今网站的最后一版2.2.6不再提供InstallWizardForVS2010.js这鬼东西,需要用Python语言创建新的工程,因此你可以参看我之前的《【Python】Windows版本的Python开发环境的配置,Helloworld,Python中文问题,输入输出、条件、循环、数组、类》(点击打开链接)先配好Python2.x的环境。

然后,你才在Cocos2dx的中文官网(点击打开链接),如下图,拉到最下面,下载Cocos2dx 2.x版本。

具体下载地址为:http://54.192.159.100/cocos2d-x-2.2.6.zip?fid=D2qKo-*f7vaAbUj7fijGQlgs5hzdkV4YAAAAAOeOX4E0-gk5fRKd*Y-Bb8j7lCvn&mid=666&threshold=150&tid=5D3FD9855047216E67D27C85E859FC2D&srcid=119&verno=1

VS2010 + cocos2dx 2.X版本 + python 3

下载之后,得到一个cocos2d-x-2.2.6.zip,解压,这个解压位置就是你以后cocos2dx的开发环境、工程的所在目录,和PHP是一样的,不好迁移,请小心选择。

在你的cocos2dx的解压目录找到build-win32.bat这东西,双击运行。

VS2010 + cocos2dx 2.X版本 + python 3

经历如下的一个漫长的编译过程:

VS2010 + cocos2dx 2.X版本 + python 3

搞好之后,会出现如下的一个界面,cocos2dx官方提供的一大堆例子,有兴趣看看之后,可以直接关闭。

VS2010 + cocos2dx 2.X版本 + python 3

至此,Cocos2dx的配置完成。

下面创建一个属于我们自己的Helloworld,不要用官方的例子,根本看不出什么端倪。

利用命令行进入.\cocos2d-x-2.2.6\tools\project-creator这个文件夹,利用python命令,如下图,创建一个工程。

[plain] view plain copy print ?

  1. create_project.py -project 你的工程文件夹名称 -package 包名 -language 开发语言(基本上为cpp,不排除有大神精通肥猪流的Lua,也不建议使用javascript开发!) 

用python 3的朋友会遇到新的问题:上面py文件出错。

解决方案是。修改之:

  1. #!/usr/bin/python  
  2. #coding=utf-8  
  3. # create_project_v3.py  
  4. # Create cross-platform cocos2d-x project  
  5. # Copyright (c) 2012 cocos2d-x.org  
  6. # Author: WangZhe  
  7. # define global variables  
  8. context = {  
  9. "language"          : "undefined",  
  10. "src_project_name"  : "undefined",  
  11. "src_package_name"  : "undefined",   
  12. "dst_project_name"  : "undeifned",  
  13. "dst_package_name"  : "undefined",  
  14. "src_project_path"  : "undefined",  
  15. "dst_project_path"  : "undefined",  
  16. "script_dir"        : "undefined",  
  17. }  
  18. platforms_list = []  
  19. # begin  
  20. import sys  
  21. import os, os.path  
  22. import json  
  23. import shutil  
  24. def dumpUsage():  
  25.     print ("Usage: create_project.py -project PROJECT_NAME -package PACKAGE_NAME -language PROGRAMING_LANGUAGE")  
  26.     print ("Options:")  
  27.     print ("  -project   PROJECT_NAME          Project name, for example: MyGame")  
  28.     print ("  -package   PACKAGE_NAME          Package name, for example: com.MyCompany.MyAwesomeGame")  
  29.     print ("  -language  PROGRAMING_LANGUAGE   Major programing lanauge you want to used, should be [cpp | lua | javascript]")  
  30.     print ("")  
  31.     print ("Sample 1: ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame")  
  32.     print ("Sample 2: ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame -language javascript")  
  33.     print ("")  
  34. def checkParams(context):  
  35.     # generate our internal params  
  36.     context["script_dir"] = os.getcwd() + "/"  
  37.     global platforms_list  
  38.     # invalid invoke, tell users how to input params  
  39.     if len(sys.argv) < 7:  
  40.         dumpUsage()  
  41.         sys.exit()  
  42.     # find our params  
  43.     for i in range(1, len(sys.argv)):  
  44.         if "-project" == sys.argv[i]:  
  45.             # read the next param as project_name  
  46.             context["dst_project_name"] = sys.argv[i+1]  
  47.             context["dst_project_path"] = os.getcwd() + "/../../projects/" + context["dst_project_name"]  
  48.         elif "-package" == sys.argv[i]:  
  49.             # read the next param as g_PackageName  
  50.             context["dst_package_name"] = sys.argv[i+1]  
  51.         elif "-language" == sys.argv[i]:  
  52.             # choose a scripting language  
  53.             context["language"] = sys.argv[i+1]  
  54.     # pinrt error log our required paramters are not ready  
  55.     raise_error = False  
  56.     if context["dst_project_name"] == "undefined":  
  57.         print ("Invalid -project parameter")  
  58.         raise_error = True  
  59.     if context["dst_package_name"] == "undefined":  
  60.         print ("Invalid -package parameter")  
  61.         raise_error = True  
  62.     if context["language"] == "undefined":  
  63.         print ("Invalid -language parameter")  
  64.         raise_error = True  
  65.     if raise_error != False:  
  66.         sys.exit()  
  67.     # fill in src_project_name and src_package_name according to "language"  
  68.     if ("cpp" == context["language"]):  
  69.         context["src_project_name"] = "HelloCpp"  
  70.         context["src_package_name"] = "org.cocos2dx.hellocpp"  
  71.         context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-cpp"  
  72.         platforms_list = ["ios",  
  73.                           "android",  
  74.                           "win32",  
  75.                           "winrt",  
  76.                           "wp8",  
  77.                           "mac",  
  78.                           "blackberry",  
  79.                           "linux",  
  80.                           "marmalade",  
  81.                           "tizen",  
  82.                           "wp8-xaml"]  
  83.     elif ("lua" == context["language"]):  
  84.         context["src_project_name"] = "HelloLua"  
  85.         context["src_package_name"] = "org.cocos2dx.hellolua"  
  86.         context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-lua"  
  87.         platforms_list = ["ios",  
  88.                           "android",  
  89.                           "win32",  
  90.                           "blackberry",  
  91.                           "linux",  
  92.                           "marmalade"]  
  93.     elif ("javascript" == context["language"]):  
  94.         context["src_project_name"] = "HelloJavascript"  
  95.         context["src_package_name"] = "org.cocos2dx.hellojavascript"  
  96.         context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-js"  
  97.         platforms_list = ["ios",  
  98.                           "android",  
  99.                           "win32"]  
  100. # end of checkParams(context) function  
  101. def replaceString(filepath, src_string, dst_string):  
  102.     content = ""  
  103.     f1 = open(filepath, "rb")  
  104.     for line in f1:  
  105.         temp_content = line.decode('utf-8')  
  106.         if src_string in temp_content:  
  107.             content += temp_content.replace(src_string, dst_string)  
  108.         else:  
  109.             content += temp_content  
  110.     f1.close()  
  111.     f2 = open(filepath, "wb")  
  112.     f2.write(content.encode(encoding='utf_8', errors='strict'))  
  113.     f2.close()  
  114. # end of replaceString  
  115. def replaceLastNameInPath(raw_path):  
  116.     cnt = raw_path.count("PROJECT_NAME")  
  117.     if (cnt > 0):  
  118.         raw_path = raw_path.replace("PROJECT_NAME", context["src_project_name"], cnt - 1)  
  119.         dst = raw_path.replace("PROJECT_NAME", context["dst_project_name"])  
  120.         return dst  
  121.     return ""  
  122. # end of replaceLastNameInPath  
  123. def processPlatformProjects(platform):  
  124.     # determine proj_path  
  125.     proj_path = context["dst_project_path"] + "/proj.%s/" % platform  
  126.     java_package_path = ""  
  127.     # read josn config file or the current platform  
  128.     f = open("%s.json" % platform)  
  129.     data = json.load(f)  
  130.     # rename package path, like "org.cocos2dx.hello" to "com.company.game". This is a special process for android  
  131.     if (platform == "android"):  
  132.         src_pkg = context["src_package_name"].split('.')  
  133.         dst_pkg = context["dst_package_name"].split('.')  
  134.         os.rename(proj_path + "src/" + src_pkg[0],  
  135.                   proj_path + "src/" + dst_pkg[0])  
  136.         os.rename(proj_path + "src/" + dst_pkg[0] + "/" + src_pkg[1],  
  137.                   proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1])  
  138.         os.rename(proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + src_pkg[2],  
  139.                   proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2])  
  140.         java_package_path = dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2]  
  141.     # rename files and folders  
  142.     for i in range(0, len(data["rename"])):  
  143.         tmp = data["rename"][i].replace("PACKAGE_PATH", java_package_path)  
  144.         src = tmp.replace("PROJECT_NAME", context["src_project_name"])  
  145.         if (platform == "wp8-xaml"):  
  146.             dst = replaceLastNameInPath(tmp)  
  147.         else:  
  148.             dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])  
  149.         if (os.path.exists(proj_path + src) == True):  
  150.             os.rename(proj_path + src, proj_path + dst)  
  151.     # remove useless files and folders  
  152.     for i in range(0, len(data["remove"])):  
  153.         dst = data["remove"][i].replace("PROJECT_NAME", context["dst_project_name"])  
  154.         if (os.path.exists(proj_path + dst) == True):  
  155.             shutil.rmtree(proj_path + dst)  
  156.     # rename package_name. This should be replaced at first. Don't change this sequence  
  157.     for i in range(0, len(data["replace_package_name"])):  
  158.         tmp = data["replace_package_name"][i].replace("PACKAGE_PATH", java_package_path)  
  159.         dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])  
  160.         if (os.path.exists(proj_path + dst) == True):  
  161.             replaceString(proj_path + dst, context["src_package_name"], context["dst_package_name"])  
  162.     # rename project_name  
  163.     for i in range(0, len(data["replace_project_name"])):  
  164.         tmp = data["replace_project_name"][i].replace("PACKAGE_PATH", java_package_path)  
  165.         dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])  
  166.         if (os.path.exists(proj_path + dst) == True):  
  167.             replaceString(proj_path + dst, context["src_project_name"], context["dst_project_name"])  
  168.     # done!  
  169.     print ("proj.%s\t\t: Done!" % platform)  
  170.     # end of processPlatformProjects  
  171. # -------------- main --------------  
  172. # dump argvs  
  173. # print sys.argv  
  174. # prepare valid "context" dictionary  
  175. checkParams(context)  
  176. # import pprint  
  177. # pprint.pprint(context)  
  178. # copy "lauguage"(cpp/lua/javascript) platform.proj into cocos2d-x/projects/<project_name>/folder  
  179. if (os.path.exists(context["dst_project_path"]) == True):  
  180.     print ("Error:" + context["dst_project_path"] + " folder is already existing")  
  181.     print ("Please remove the old project or choose a new PROJECT_NAME in -project parameter")  
  182.     sys.exit()  
  183. else:  
  184.     shutil.copytree(context["src_project_path"], context["dst_project_path"], True)  
  185. # call process_proj from each platform's script folder            
  186. for platform in platforms_list:  
  187.     processPlatformProjects(platform)  
  188. #    exec "import %s.handle_project_files" % (platform)  
  189. #    exec "%s.handle_project_files.handle_project_files(context)" % (platform)  
  190. print ("New project has been created in this path: " + context["dst_project_path"].replace("/tools/project-creator/../..", ""))  
  191. print ("Have Fun!") 
VS2010 + cocos2dx 2.X版本 + python 3

在创建的过程中,可能会有如上图的报错,不用管,因为你创建的MyDemo工程已经成功出现在.\cocos2d-x-2.2.6\projects文件夹了,进入.\cocos2d-x-2.2.6\projects\MyDemo\proj.win32打开HelloCpp.sln,开始利用Cocos2dx引擎(框架)在Windows上开发游戏。

你可以观察到在.\cocos2d-x-2.2.6\projects\MyDemo,出现proj.Android、proj.iOS等平台的文件夹,这里意味着,你可以在这些平台同样利用Cocos2dx引擎(框架)进行开发。不是说一个平台开发,多平台共同编译……

VS2010 + cocos2dx 2.X版本 + python 3

打开HelloCpp.sln之后,等待Visual Studio 2010加载一大堆外部库之后,你可以看到如下的文件结构:

VS2010 + cocos2dx 2.X版本 + python 3

首先,你要明白一个概念,Cocos2dx中最简单的Helloworld,是由一个叫Helloworld的场景、然后在这个Helloworld场景上放上一个Helloworld字符串所组成的,

官方初始的Helloworld还在Helloworld这个场景中放上背景图片、关闭按钮等杂七杂八的东西,让Helloworld文件看起来比较复杂。

我们先从上图的main.cpp与AppDelegate.cpp入手,先设置好程序的一些基本东西。

首先是main.cpp,这东西的主函数就5行代码,将其关于程序标题与窗口尺寸的18、19行进行修改,如下:

[cpp] view plain copy print ?

  1. #include "main.h"  
  2. #include "AppDelegate.h"  
  3. #include "CCEGLView.h"  
  4. USING_NS_CC;  
  5. int APIENTRY _tWinMain(HINSTANCE hInstance,  
  6.                        HINSTANCE hPrevInstance,  
  7.                        LPTSTR    lpCmdLine,  
  8.                        int       nCmdShow)  
  9. {  
  10.     UNREFERENCED_PARAMETER(hPrevInstance);  
  11.     UNREFERENCED_PARAMETER(lpCmdLine);  
  12.     // create the application instance  
  13.     AppDelegate app;  
  14.     CCEGLView* eglView = CCEGLView::sharedOpenGLView();  
  15.     eglView->setViewName("HelloWorld");//修改程序运行的标题为Helloworld  
  16.     eglView->setFrameSize(1024, 768);//修成程序运行的尺寸为1024x768  
  17.     return CCApplication::sharedApplication()->run();  
  18. }  

其余代码看不懂暂时先不要管,就像你当初学C语言,不必在乎#include<stdio.h>与void main(){}是什么鬼,你首先要会改printf()中的内容。关于引入、函数这些东西,回头再来学。

之后,还没有改完,对AppDelegate.cpp进行修改,把第22行的帧数调试信息关了,第25行的帧数有兴趣可以改下,不过改了也看不出效果,修改如下:

[cpp] view plain copy print ?

  1. #include "AppDelegate.h"  
  2. #include "HelloWorldScene.h"  
  3. USING_NS_CC;  
  4. AppDelegate::AppDelegate() {  
  5. }  
  6. AppDelegate::~AppDelegate()   
  7. {  
  8. }  
  9. bool AppDelegate::applicationDidFinishLaunching() {  
  10.     // initialize director  
  11.     CCDirector* pDirector = CCDirector::sharedDirector();  
  12.     CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();  
  13.     pDirector->setOpenGLView(pEGLView);  
  14.     // turn on display FPS  
  15.     pDirector->setDisplayStats(false);//关闭调试信息  
  16.     // set FPS. the default value is 1.0/60 if you don't call this  
  17.     pDirector->setAnimationInterval(1.0 / 60);//这里是设置游戏运行的帧数锁定为60,一般牛B机器、牛B的游戏都是锁这个帧数,30帧是低配  
  18.     // create a scene. it's an autorelease object  
  19.     CCScene *pScene = HelloWorld::scene();  
  20.     // run  
  21.     pDirector->runWithScene(pScene);  
  22.     return true;  
  23. }  
  24. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too  
  25. void AppDelegate::applicationDidEnterBackground() {  
  26.     CCDirector::sharedDirector()->stopAnimation();  
  27.     // if you use SimpleAudioEngine, it must be pause  
  28.     // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
  29. }  
  30. // this function will be called when the app is active again  
  31. void AppDelegate::applicationWillEnterForeground() {  
  32.     CCDirector::sharedDirector()->startAnimation();  
  33.     // if you use SimpleAudioEngine, it must resume here  
  34.     // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
  35. }  

其余那些什么初始化代码之类,看不懂,先不要管,也不要改。

之后,对整个程序的核心,管理HelloWorld这个场景的HelloWorldScene.cpp进行修改,放上属于我们自己的东西,去掉官方的东西。主要对HelloWorldScene.cpp中的初始化参数bool HelloWorld::init(){}进行修改,其余不要动,整个HelloWorldScene.cpp修改如下:

[cpp] view plain copy print ?

  1. #include "HelloWorldScene.h"  
  2. USING_NS_CC;  
  3. CCScene* HelloWorld::scene()  
  4. {  
  5.     // 'scene' is an autorelease object  
  6.     CCScene *scene = CCScene::create();  
  7.     // 'layer' is an autorelease object  
  8.     HelloWorld *layer = HelloWorld::create();  
  9.     // add layer as a child to scene  
  10.     scene->addChild(layer);  
  11.     // return the scene  
  12.     return scene;  
  13. }  
  14. // on "init" you need to initialize your instance  
  15. bool HelloWorld::init()  
  16. {  
  17.     //保留已经存在的初始化部分,不要进行修改,开始  
  18.     if ( !CCLayer::init() )  
  19.     {  
  20.         return false;  
  21.     }  
  22.     CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();  
  23.     CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();  
  24.     //保留已经存在的初始化部分,不要进行修改,结束  
  25.     CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 96);//声明一个标签文本Helloworld,其字体为Arial,字号为96  
  26.     pLabel->setPosition(ccp(origin.x + visibleSize.width/2,//设置标签文本的位置ccp是必须的,visibleSize.width/2为屏幕的中央,前面的origin.x是必须加上,用于多平台固定好位置  
  27.                             origin.y + visibleSize.height/2));//y部分同理  
  28.     this->addChild(pLabel, 1);//把这个标签文本Helloworld放到场景中,层叠关系为1,相当于css中的z-index,这个数字越大,越在上面  
  29.     return true;  
  30. }  
  31. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  32. {  
  33. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)  
  34.     CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");  
  35. #else  
  36.     CCDirector::sharedDirector()->end();  
  37. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  38.     exit(0);  
  39. #endif  
  40. #endif  
  41. }  

利用Ctrl+F5运行,或者点击上方的运行按钮则看到如下效果:

VS2010 + cocos2dx 2.X版本 + python 3

如果在编译的时候,遇到如下图的“LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏”错误:

VS2010 + cocos2dx 2.X版本 + python 3

那不关Cocos2dx的问题,是由于电脑中有多个Visual Studio冲突,或者之前版本的Visual Studio没有卸载干净。当前系统中存在两个cvtres.exe文件,版本不同就会有问题了。我们让VS2010直接使用系统的cvtres.exe程序。重命名或删除:(vs2010安装的位置).\VC\bin\cvtres.exe

如图,我就把cvtres.exe搞成_cvtres.exe完事。

VS2010 + cocos2dx 2.X版本 + python 3

最后总结一下,Cocos2dx的Helloworld比起SSH算是简单多了,代码清晰。关键是注意好版本,正如php5.5之后不支持windows2003这样的问题,你必须搞清楚Visual Studio 2010只能支持Cocos2dx 2.x之类的。就是安装可能有些繁琐,毕竟python环境的安装对于很多人来说比较陌生,Visual Studio 2010的安装的耗时实在是无力吐槽了,再加上Cocos2dx 2.x的编译过程,就是比较久。最主要网上许多蛋疼的资料就不说了……