创建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 ++项目非常容易:
- Open the UNIGINE SDK Browser.打开UNIGINE SDK浏览器。
- Go to the Projects tab and click CREATE NEW.
转到Projects选项卡,然后单击CREATE NEW。
- 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 2022 project.On Windows, you can create C++ Visual Studio 2022 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 2022 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 2022 project.On Windows, you can create C++ Visual Studio 2022 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.
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.Read more about these parameters in this article Read more about these parameters in this articleThis parameter depends on a platform:This parameter depends on a platform:
- On Windows, you can create C++ Visual Studio 2022 project.On Windows, you can create C++ Visual Studio 2022 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 2022 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 2022 project.On Windows, you can create C++ Visual Studio 2022 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 2022 project.On Linux you can create C++ GNU make project.You can also can create C++ CMake projectAPI+IDE —选择 C ++ 开始使用C ++ API。This parameter depends on a platform:此参数取决于平台:
- On Windows, you can create C++ Visual Studio 2022 project.在 Windows 上,您可以创建 C ++ Visual Studio 2022 项目。
- 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 在本文中阅读有关这些参数的更多信息。 - 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按钮运行您的项目。
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.以下示例显示如何旋转项目中默认创建的材质球。
-
If you created the C++ project for Visual Studio:如果您为Visual Studio创建了 C ++项目:
- 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:
- On the created C++ project, click on the Other Actions button and then the Open Folder button. 在创建的C ++项目上,单击Other Actions按钮,然后单击Open Folder按钮。
- Go to the <YOUR PROJECT>\source\ folder and open the AppWorldLogic.cpp file with any plain text editor.转到<YOUR PROJECT>\source\文件夹,然后使用任何纯文本编辑器打开AppWorldLogic.cpp文件。
- Choose your C++ project in the UNIGINE SDK Browser and click the Open Code IDE button to open the project in IDE.
- Write (or copy) the following code in your project's AppWorldLogic.cpp file.
在项目的AppWorldLogic.cpp文件中编写(或复制)以下代码。
#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; }
#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; }
-
If you use Visual Studio, do the following:如果使用Visual Studio,请执行以下操作:
- Before compiling your code, check that the appropriate platform and configuration settings for your project are set correctly.
在编译代码之前,请检查是否为您的项目正确设置了合适的平台和配置设置。
- Build your project by clicking Build -> Build Solution in Visual Studio.
通过单击Visual Studio中的Build -> Build Solution来构建项目。
- 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:
- Execute the make command in the terminal to compile the application.
在终端中执行make命令以编译应用程序。
make
make
- Launch the application by using the run script.使用 run 脚本启动应用程序。
- Before compiling your code, check that the appropriate platform and configuration settings for your project are set correctly.