设为首页 加入收藏

TOP

win10 vs2022 搭建 cocos2d-x 3.17 开发环境(一)
2023-07-23 13:28:34 】 浏览:45
Tags:win10 vs2022 搭建 cocos2d-x 3.17

引擎下载地址

https://cocos2d-x.org/download/

也可以在 github 下载

https://github.com/cocos2d/cocos2d-x/tags

手册地址

https://docs.cocos2d-x.org/cocos2d-x/v3/zh/

api 文档地址

https://docs.cocos2d-x.org/api-ref/cplusplus/v3x/index.html

hello world

安装注意事项

  1. 需要 python2 , 安装后可以用 python2 的绝对路径运行 cocos 的 setup.py
  2. 如何没有把 python2 加入到环境变量, 可以修改引擎路径下的 tools\cocos2d-console\bin\cocos.bat
@echo off
@python2 绝对路径 "%~dp0/cocos.py" %*

新建项目

cocos new HelloCocos -p com.laolang.hellococos -l cpp

然后打开 proj.win32 目录下的 .sln 文件, 提示升级点确认, 编译运行即可

中文乱码问题

参考:
Cocos2d-x 中文标题设置
Cocos2d-x 中文内容设置

标题直接用下面的函数转一下, Label等内容的中文显示还需要中文字体的支持, 可以从网上下载或者从C盘拷一个, 例如宋体常规, 此字体文件名为:simsun.ttc

std::string CommonUtil::GBKToUTF8(const std::string& strGBK)
{
    std::string strOutUTF8 = "";
    WCHAR* str1;
    int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
    str1 = new WCHAR[n];
    MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
    n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
    char* str2 = new char[n];
    WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
    strOutUTF8 = str2;
    delete[]str1;
    str1 = NULL;
    delete[]str2;
    str2 = NULL;
    return strOutUTF8;
}

hello world 关键代码注释

AppDelegate的applicationDidFinishLaunching方法
其四个方法含义分别为(https://blog.csdn.net/xiayao2012/article/details/49472423)

initGLContextAttrs();//设置 OpenGL环境
applicationDidFinishLaunching(); //逻辑初始化
applicationDidEnterBackground(); //切换到后台
applicationWillEnterForeground(); //切换到前台

bool AppDelegate::applicationDidFinishLaunching() {
    // 初始化 director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    // 初始化 opengl 试图
    if(!glview) {
        glview = GLViewImpl::createWithRect(CommonUtil::GBKToUTF8("第一个 Cocos2d-X 程序"), cocos2d::Rect(0, 0, resolutionSize.width, resolutionSize.height));
        director->setOpenGLView(glview);
    }

    // 开启左下角 FPS 状态信息
    director->setDisplayStats(true);
    // 设置 FPS 为 60, 默认为 60
    director->setAnimationInterval(1.0f / 60);

    // 设置屏幕分辨率
    glview->setDesignResolutionSize(resolutionSize.width, resolutionSize.height, ResolutionPolicy::NO_BORDER);

    register_all_packages();

    // 创建 Hello World 场景, 对象的释放交给 cocos2d-x 管理
    auto scene = HelloWorld::createScene();

    // 运行
    director->runWithScene(scene);

    return true;
}

HelloWorldScene的init方法

bool HelloWorld::init()
{
    // 先执行父类的 init
    if (!Scene::init())
    {
        return false;
    }

    // 获得可视区域大小
    const auto visibleSize = Director::getInstance()->getVisibleSize();
    log("width:%f , height:%f", visibleSize.width, visibleSize.height);
    // 获得原点位置 左下角
    const Vec2  origin = Director::getInstance()->getVisibleOrigin();
    log("x:%f , y:%f", origin.x, origin.y);


    // 关闭按钮, 是一个 MenuItemImage, 表示一个菜单项
    auto closeItem = MenuItemImage::create(
        "CloseNormal.png", // 非选中状态
        "CloseSelected.png", // 选中状态
        CC_CALLBACK_1(HelloWorld::menuCloseCallback, this) // 回调函数
    );
    // 按钮位置, 窗口右下角
    const float x = origin.x + visibleSize.width - closeItem->getContentSize().width / 2;
    const float y = origin.y + closeItem->getContentSize().height / 2;
    closeItem->setPosition(Vec2(x, y));

    // 把菜单项添加到菜单中
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    // 菜单添加到当前层
    this->addChild(menu, 1)
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++ 指针 下一篇ubuntu 搭建 cmake + vscode 的 c..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目