我是靠谱客的博主 顺心蜡烛,最近开发中收集的这篇文章主要介绍Cocos2d-x引擎学习笔记之(一)Win32下开发环境的搭建一. 为什么要学习CCX引擎?二. 在Win32下搭建开发环境,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一. 为什么要学习CCX引擎?

一个强大的引擎,对于程序员来说,就是一个强大的工具。他能简化游戏编程的难度,让我们放更多精力在游戏的逻辑上而不是在游戏框架的设计上。比如说:作为一个游戏开发初学者,很难写出优雅的代码。

可重用、可维护、便于理解、符合面向对象思想,这些在我看来是使用OOP语言最大的难题,如果单单的只是为了完成一个功能,那盲目的开发是没问题的,可是后续潜在的问题我们不能抛弃之。CCX非常强大,为我们准备好了足够好的设计理念。

比如说:游戏由场景Scene组成,每个场景拥有多个层Layer,控制场景的导演Director。还有虚函数回调事件的方法,都是作为初学者很难设计好的。不如由简入手,学习这些比较好的思想,为以后创建自己的引擎打基础。

 

二. 在Win32下搭建开发环境

直入主题,我是win8系统,VS2010。(PS:不知为何,VS2012无法编译CCX的项目,2010却可以。)

1. 下载CCX http://cocos2d-x.org/projects/cocos2d-x/wiki/Download 我当前版本是 cocos2d-2.1rc0-x-2.1.2-hotfix


2. 解压cocos2d-2.1rc0-x-2.1.2-hotfix目录 打开 cocos2d-win32.vc2010.sln 并且编译解决方案


3. 等其编译完成后,可以跑跑testCpp项目,能跑就没问题了。


4. 接下来是安装模板,只需要双击运行“install-templates-msvc.bat”就可以了。


5. 创建项目,虽然安装了模板,但是创建项目并不使用模板,所以我觉得吧不安装也罢,创建项目要用到py写的工具。在Tools目录下的project-creatorcreate-project.py,原版的必须用py2.x运行才行,具体参数是 project package language。这里我使用了朋友改过的py,可以解决py3.x不支持的问题。

#!/usr/bin/python
# create_project.py
# Create cross-platform cocos2d-x project
# Copyright (c) 2012 cocos2d-x.org
# Author: WangZhe

# define global variables
context = {
"language"          : "undefined",
"src_project_name"  : "undefined",
"src_package_name"  : "undefined", 
"dst_project_name"  : "undeifned",
"dst_package_name"  : "undefined",
"src_project_path"  : "undefined",
"dst_project_path"  : "undefined",
"script_dir"        : "undefined",
}
platforms_list = []

# begin
import sys
import os, os.path
import json
import shutil

def dumpUsage():
    print("Usage: create_project.py -project PROJECT_NAME -package PACKAGE_NAME -language PROGRAMING_LANGUAGE")
    print("Options:")
    print("  -n   PROJECT_NAME          Project name, for example: MyGame")
    print("  -p   PACKAGE_NAME          Package name, for example: com.MyCompany.MyAwesomeGame")
    print("  -l  PROGRAMING_LANGUAGE   Major programing lanauge you want to used, should be [cpp | lua | javascript]")
    print("")
    print("Sample 1: ./create_project.py -n MyGame -p com.MyCompany.AwesomeGame")
    print("Sample 2: ./create_project.py -n MyGame -p com.MyCompany.AwesomeGame -l javascript")
    print("")

def checkParams(context):
    # generate our internal params
    
    global platforms_list
    
    # invalid invoke, tell users how to input params
    if (len(sys.argv) != 7 and len(sys.argv) != 5):
        dumpUsage()
        sys.exit();
    raise_error = False
    # find our params
    for i in range(1, len(sys.argv)):
        if "-n" == sys.argv[i]:
            # read the next param as project_name
            context["dst_project_name"] = sys.argv[i+1]
            context["dst_project_path"] = os.getcwd() + "/../../projects/" + context["dst_project_name"]
        elif "-p" == sys.argv[i]:
            # read the next param as g_PackageName
            context["dst_package_name"] = sys.argv[i+1]
        elif "-l" == sys.argv[i]:
            # choose a scripting language
            context["language"] = sys.argv[i+1]
    if len(sys.argv) == 5 and "undefined" == context["language"]:
        context["language"] = "cpp"
    # pinrt error log our required paramters are not ready
    if context["dst_project_name"] == "undefined":
        print("Invalid -project parameter")
        raise_error = True
    if context["dst_package_name"] == "undefined":
        print("Invalid -package parameter")
        raise_error = True
    if context["language"] == "undefined":
        print("Invalid -language parameter")
        raise_error = True
    if raise_error != False:
        dumpUsage()
        sys.exit()
    # fill in src_project_name and src_package_name according to "language"
    if ("cpp" == context["language"]):
        context["src_project_name"] = "HelloCpp"
        context["src_package_name"] = "org.cocos2dx.hellocpp"
        context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-cpp"
        platforms_list = ["ios",
                          "android",
                          "win32"] #,
                          #"mac",
                          #"blackberry",
                          #"linux",
                          #"marmalade"]
    elif ("lua" == context["language"]):
        context["src_project_name"] = "HelloLua"
        context["src_package_name"] = "org.cocos2dx.hellolua"
        context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-lua"
        platforms_list = ["ios",
                          "android",
                          "win32",
                          "blackberry",
                          "linux",
                          "marmalade"]
    elif ("javascript" == context["language"]):
        context["src_project_name"] = "HelloJavascript"
        context["src_package_name"] = "org.cocos2dx.hellojavascript"
        context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-js"
        platforms_list = ["ios",
                          "android",
                          "win32"]
    else:
        dumpUsage()
        sys.exit()
# end of checkParams(context) function

def replaceString(filepath, src_string, dst_string):
    content = ""
    f1 = open(filepath, "rb")
    for buffer in f1:
        line = buffer.decode("utf-8")
        if src_string in line:
            content += line.replace(src_string, dst_string)
        else:
            content += line
    f1.close()
    f2 = open(filepath, "wb")
    f2.write(content.encode("utf-8"))
    f2.close()
# end of replaceString

def processPlatformProjects(platform):
    # determine proj_path
    proj_path = context["dst_project_path"] + "/proj.%s/" % platform
    java_package_path = ""

    # read josn config file or the current platform
    f = open("%s.json" % platform)
    data = json.load(f)

    # rename package path, like "org.cocos2dx.hello" to "com.company.game". This is a special process for android
    if (platform == "android"):
        src_pkg = context["src_package_name"].split('.')
        dst_pkg = context["dst_package_name"].split('.')
        os.rename(proj_path + "src/" + src_pkg[0],
                  proj_path + "src/" + dst_pkg[0])
        os.rename(proj_path + "src/" + dst_pkg[0] + "/" + src_pkg[1],
                  proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1])
        os.rename(proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + src_pkg[2],
                  proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2])
        java_package_path = dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2]

    # rename files and folders
    for i in range(0, len(data["rename"])):
        tmp = data["rename"][i].replace("PACKAGE_PATH", java_package_path)
        src = tmp.replace("PROJECT_NAME", context["src_project_name"])
        dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
        if (os.path.exists(proj_path + src) == True):
            os.rename(proj_path + src, proj_path + dst)

    # remove useless files and folders
    for i in range(0, len(data["remove"])):
        dst = data["remove"][i].replace("PROJECT_NAME", context["dst_project_name"])
        if (os.path.exists(proj_path + dst) == True):
            shutil.rmtree(proj_path + dst)
    
    # rename package_name. This should be replaced at first. Don't change this sequence
    for i in range(0, len(data["replace_package_name"])):
        tmp = data["replace_package_name"][i].replace("PACKAGE_PATH", java_package_path)
        dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
        if (os.path.exists(proj_path + dst) == True):
            replaceString(proj_path + dst, context["src_package_name"], context["dst_package_name"])
    
    # rename project_name
    for i in range(0, len(data["replace_project_name"])):
        tmp = data["replace_project_name"][i].replace("PACKAGE_PATH", java_package_path)
        dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
        if (os.path.exists(proj_path + dst) == True):
            replaceString(proj_path + dst, context["src_project_name"], context["dst_project_name"])
                  
    # done!
    print("proj.%stt: Done!" % platform)
# end of processPlatformProjects


def main():
    # -------------- main --------------
    # dump argvs
    # print sys.argv
    context["script_dir"] = os.getcwd() + "/"
    # prepare valid "context" dictionary
    checkParams(context)
    # import pprint
    # pprint.pprint(context)

    # copy "lauguage"(cpp/lua/javascript) platform.proj into cocos2d-x/projects/<project_name>/folder
    if (os.path.exists(context["dst_project_path"]) == True):
        print("Error:" + context["dst_project_path"] + " folder is already existing")
        print("Please remove the old project or choose a new PROJECT_NAME in -project parameter")
        sys.exit()
    else:
        shutil.copytree(context["src_project_path"], context["dst_project_path"], True)

    # call process_proj from each platform's script folder          
    for platform in platforms_list:
        processPlatformProjects(platform)
    #    exec "import %s.handle_project_files" % (platform)
    #    exec "%s.handle_project_files.handle_project_files(context)" % (platform)

    print("New project has been created in this path: " + context["dst_project_path"].replace("/tools/project-creator/../..", ""))
    print("Have Fun!")

if __name__ == '__main__':
    main()


谢谢好基友vizee  = 3=


6. 创建一个bat: create_project.py -n ccxHelloWorld -p org.able.ccx -l cpp,跑起来,就可以看到projects项目下多了一个你的项目,开心咯。



7. 编译,跑起来。


最后

以上就是顺心蜡烛为你收集整理的Cocos2d-x引擎学习笔记之(一)Win32下开发环境的搭建一. 为什么要学习CCX引擎?二. 在Win32下搭建开发环境的全部内容,希望文章能够帮你解决Cocos2d-x引擎学习笔记之(一)Win32下开发环境的搭建一. 为什么要学习CCX引擎?二. 在Win32下搭建开发环境所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部