This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
专业(SIM)
UnigineEditor
界面概述
资源工作流程
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
使用范例
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
Materials and Shaders
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
创建内容
内容优化
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

创建C ++应用程序

A UNIGINE-based application can be implemented by means of C++ only, without using UnigineScript.基于UNIGINE的应用程序只能通过C ++来实现,而无需使用UnigineScript。

This article describes how to add logic to your project by using the C++ language. Code written in C++ is the same for all supported platforms: Windows and Linux. The difference is in the way of compiling the project.本文介绍了如何使用C ++语言向您的项目添加逻辑。对于所有受支持的平台,用C ++编写的代码都是相同的:Windows和Linux。区别在于编译项目的方式。

See also也可以看看#

  • Articles in the Development for Different Platforms section to learn more on how to prepare the development environment, install the UNIGINE SDK and build the application for different platforms.针对不同平台的开发部分中的文章,以了解有关如何准备开发环境,安装UNIGINE SDK以及针对不同平台构建应用程序的更多信息。
  • Examples located in the <UnigineSDK>/source/samples/Api and <UnigineSDK>/source/samples/App folders.示例位于<UnigineSDK>/source/samples/Api<UnigineSDK>/source/samples/App文件夹中。

Creating Empty C++ Application创建空的C ++应用程序#

It is very easy to start your own C++ project by using UNIGINE SDK Browser:使用UNIGINE SDK浏览器来启动自己的C ++项目非常容易:

  1. Open the UNIGINE SDK Browser.打开UNIGINE SDK浏览器。
  2. Go to the Projects tab and click CREATE NEW.
    转到Projects选项卡,然后单击CREATE NEW
  3. Specify the following parameters:
    • Project name — specify the name of your project. Project name — specify the name of your project.
    • Location — specify the path to your project folder. Location — specify the path to your project folder.
    • SDK — choose the UNIGINE SDK edition. SDK — choose the UNIGINE SDK edition.
    • API+IDE — choose C++ to start working with the C++ API.

      This parameter depends on a platform:This parameter depends on a platform:

      • On Windows, you can create C++ Visual Studio 2015 project.On Windows, you can create C++ Visual Studio 2015 project.
      • On Linux you can create C++ GNU make project.On Linux you can create C++ GNU make project.

      You can also can create C++ CMake projectYou can also can create C++ CMake project

      This parameter depends on a platform:On Windows, you can create C++ Visual Studio 2015 project.On Linux you can create C++ GNU make project.You can also can create C++ CMake project
      API+IDE — choose C++ to start working with the C++ API.

      This parameter depends on a platform:This parameter depends on a platform:

      • On Windows, you can create C++ Visual Studio 2015 project.On Windows, you can create C++ Visual Studio 2015 project.
      • On Linux you can create C++ GNU make project.On Linux you can create C++ GNU make project.

      You can also can create C++ CMake projectYou can also can create C++ CMake project

    • Precision - specify the precision. In this example we will use double precision.Precision - specify the precision. In this example we will use double precision.
    注意
    Read more about these parameters in this article Read more about these parameters in this article
    Project name — specify the name of your project. Location — specify the path to your project folder. SDK — choose the UNIGINE SDK edition. API+IDE — choose C++ to start working with the C++ API.

    This parameter depends on a platform:This parameter depends on a platform:

    • On Windows, you can create C++ Visual Studio 2015 project.On Windows, you can create C++ Visual Studio 2015 project.
    • On Linux you can create C++ GNU make project.On Linux you can create C++ GNU make project.

    You can also can create C++ CMake projectYou can also can create C++ CMake project

    This parameter depends on a platform:On Windows, you can create C++ Visual Studio 2015 project.On Linux you can create C++ GNU make project.You can also can create C++ CMake projectPrecision - specify the precision. In this example we will use double precision.Read more about these parameters in this article
    指定以下参数:
    • Project name — specify the name of your project.Project name-指定项目的名称。
    • Location — specify the path to your project folder.Location-指定项目文件夹的路径。
    • SDK — choose the UNIGINE SDK edition.SDK —选择UNIGINE SDK版本。
    • API+IDE — choose C++ to start working with the C++ API.

      This parameter depends on a platform:This parameter depends on a platform:

      • On Windows, you can create C++ Visual Studio 2015 project.On Windows, you can create C++ Visual Studio 2015 project.
      • On Linux you can create C++ GNU make project.On Linux you can create C++ GNU make project.

      You can also can create C++ CMake projectYou can also can create C++ CMake project

      This parameter depends on a platform:On Windows, you can create C++ Visual Studio 2015 project.On Linux you can create C++ GNU make project.You can also can create C++ CMake project
      API+IDE —选择 C ++ 开始使用C ++ API。

      This parameter depends on a platform:此参数取决于平台:

      • On Windows, you can create C++ Visual Studio 2015 project. Windows 上,您可以创建 C ++ Visual Studio 2015 项目。
      • On Linux you can create C++ GNU make project. Linux 上,您可以创建 C ++ GNU make 项目。

      You can also can create C++ CMake project您还可以创建 C ++ CMake 项目

    • Precision - specify the precision. In this example we will use double precision.Precision-指定精度。在此示例中,我们将使用双精度
    注意
    Read more about these parameters in this article 本文中阅读有关这些参数的更多信息。
  4. Click the CREATE NEW PROJECT button. The project will appear in the projects list.
    单击CREATE NEW PROJECT按钮。该项目将出现在项目列表中。

You can run your project by clicking the Run button.您可以通过单击Run按钮运行您的项目。

注意
By default, in the world script file a WorldLight and a PlayerSpectator are created. You can leave functions of the world script empty, and create your own lights and players by using C++.默认情况下,在世界脚本文件中创建WorldLight和PlayerSpectator。您可以将世界脚本的功能留空,并使用C ++创建自己的灯光和播放器。

Implementing C++ Logic实现C ++逻辑#

In this section we will add logic to the empty C++ application project.在本节中,我们将为空的C ++应用程序项目添加逻辑。

The following example shows how to rotate the material ball that was created by default in your project.以下示例显示如何旋转项目中默认创建的材质球。

  1. If you created the C++ project for Visual Studio:如果您为Visual Studio创建了 C ++项目

    1. Choose your C++ project in the UNIGINE SDK Browser and click the Open Code IDE button to open the project in IDE.
      在UNIGINE SDK浏览器中选择您的C ++项目,然后单击Open Code IDE按钮以在IDE中打开该项目。

    If you created C++ GNU make project:如果创建了C++ GNU make project

    1. On the created C++ project, click on the Other Actions button and then the Open Folder button.

      在创建的C ++项目上,单击Other Actions按钮,然后单击Open Folder按钮。

    2. Go to the <YOUR PROJECT>\source\ folder and open the AppWorldLogic.cpp file with any plain text editor.转到<YOUR PROJECT>\source\文件夹,然后使用任何纯文本编辑器打开AppWorldLogic.cpp文件。
  2. Write (or copy) the following code in your project's AppWorldLogic.cpp file.
    源代码 (C++)
    #include "AppWorldLogic.h"
    #include <UnigineWorld.h>
    #include <UnigineGame.h>
    
    using namespace Unigine;
    
    // define pointer to node
    NodePtr node;
    
    AppWorldLogic::AppWorldLogic() {
    	
    }
    
    AppWorldLogic::~AppWorldLogic() {
    	
    }
    
    int AppWorldLogic::init() {
    	// get the material ball node
    	node = World::getNodeByName("material_ball");
    	return 1;
    }
    
    int AppWorldLogic::shutdown() {
    	return 1;
    }
    
    int AppWorldLogic::update() {
    	// get the frame duration
    	float ifps = Game::getIFps();
    
    	// set the angle of rotation
    	double angle = ifps * 90.0f;
    
    	// set the angle to the transformation matrix
    	Unigine::Math::Mat4 transform = node->getTransform();
    	transform.setRotateZ(angle);
    
    	// set new transformation to the node
    	node->setTransform(node->getTransform() * transform);
    
    	return 1;
    }
    
    int AppWorldLogic::postUpdate() {
    	return 1;
    }
    
    int AppWorldLogic::updatePhysics() {
    	return 1;
    }
    
    int AppWorldLogic::save(const Unigine::StreamPtr &stream) {
    	UNIGINE_UNUSED(stream);
    	return 1;
    }
    
    int AppWorldLogic::restore(const Unigine::StreamPtr &stream) {
    	UNIGINE_UNUSED(stream);
    	return 1;
    }
    在项目的AppWorldLogic.cpp文件中编写(或复制)以下代码。
    源代码 (C++)
    #include "AppWorldLogic.h"
    #include <UnigineWorld.h>
    #include <UnigineGame.h>
    
    using namespace Unigine;
    
    // define pointer to node
    NodePtr node;
    
    AppWorldLogic::AppWorldLogic() {
    	
    }
    
    AppWorldLogic::~AppWorldLogic() {
    	
    }
    
    int AppWorldLogic::init() {
    	// get the material ball node
    	node = World::getNodeByName("material_ball");
    	return 1;
    }
    
    int AppWorldLogic::shutdown() {
    	return 1;
    }
    
    int AppWorldLogic::update() {
    	// get the frame duration
    	float ifps = Game::getIFps();
    
    	// set the angle of rotation
    	double angle = ifps * 90.0f;
    
    	// set the angle to the transformation matrix
    	Unigine::Math::Mat4 transform = node->getTransform();
    	transform.setRotateZ(angle);
    
    	// set new transformation to the node
    	node->setTransform(node->getTransform() * transform);
    
    	return 1;
    }
    
    int AppWorldLogic::postUpdate() {
    	return 1;
    }
    
    int AppWorldLogic::updatePhysics() {
    	return 1;
    }
    
    int AppWorldLogic::save(const Unigine::StreamPtr &stream) {
    	UNIGINE_UNUSED(stream);
    	return 1;
    }
    
    int AppWorldLogic::restore(const Unigine::StreamPtr &stream) {
    	UNIGINE_UNUSED(stream);
    	return 1;
    }
  3. If you use Visual Studio, do the following:如果使用Visual Studio,请执行以下操作:

    1. Before compiling your code, check that the appropriate platform and configuration settings for your project are set correctly.
      在编译代码之前,请检查是否为您的项目正确设置了合适的平台和配置设置。
    2. Build your project by clicking Build -> Build Solution in Visual Studio.
      通过单击Visual Studio中的Build -> Build Solution来构建项目。
    3. Start your project by clicking Debug -> Start in a proper mode in Visual Studio.
      Visual Studio中以适当的模式单击Debug -> Start来启动项目。

    If you created a GNU Make project:如果创建了GNU Make project

    1. Execute the make command in the terminal to compile the application.
      命令行
      make
      在终端中执行make命令以编译应用程序。
      命令行
      make
    2. Launch the application by using the run script.使用 run 脚本启动应用程序。
注意
To run the debug version of your project from SDK Browser, enable the Debug mode in Customize Run Options. 要从SDK浏览器运行项目的调试版本,请在Customize Run Options.中启用Debug模式
最新更新: 2022-12-14
Build: ()