Creating C++ Application
A Unigine-based application can be implemented by means of C++ only, without using 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, Linux and OS X. The difference is in the way of compiling the project.
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.
- Articles on Typical Architecture of a Unigine-Based Application and Engine Architecture for better understanding of the Unigine C++ API operation in the engine architecture.
- Examples located in the <UnigineSDK>/source/samples/Api and <UnigineSDK>/source/samples/App folders.
Creating Empty C++ Application
It is very easy to start your own C++ project by using UNIGINE SDK Browser:
- Open the UNIGINE SDK Browser.
- Go to the Projects tab and click CREATE NEW.
- Specify the following parameters:
- Project - specify the name of your project.
- Project Path - specify the path to your project folder.
- SDK - choose the Unigine SDK.
- API - here choose C++ to start working with the C++ API.
This parameter depends on a platform:
- On Windows you can create C++ Visual Studio 2010 or C++ Visual Studio 2013 project.
- On Linux you can create C++ GNU make project.
- On OS X you can create C++ Xcode project or C++ GNU make project.
- Architecture - specify the architecture of your target platform.
- Precision - specify the precision. In this example we will use double precision.
Read more about these parameters in this article -
Click the Create New Project button. The project will appear in the projects list.
You can run your project by clicking the Run button.
Implementing C++ Logic
In this section we will add logic to the empty C++ application project.
The following example shows how to add a primitive box mesh to the world and rotate it.
- In the Unigine SDK Browser, choose your C++ project and click the Edit Content button.
- In the Unigine Editor that will be opened, add the primitive box as described in this section. Also name the primitive as in the example.
- Save the World, by clicking File -> Save World on the Menu bar or press CTRL + S and close Unigine Editor.
If you created the C++ project for Visual Studio or Xcode:
- Choose your C++ project in the Unigine SDK Browser and click the Edit Code button to open the project in IDE.
If you created C++ GNU make project:
- On the created C++ project, click on the Other Actions button and then the Open Folder button.
Other Actions buttonOpen Folder button
- Go to the <YOUR PROJECT>\source\ folder and open the *.cpp file with any plain text editor.
-
Write (or copy) the following code in your project's .cpp file.
#include <UnigineEngine.h> #include <UnigineConsole.h> #include <UnigineGame.h> #include <UnigineLightWorld.h> #include <UnigineMaterials.h> #include <UniginePlayerDummy.h> #include <UnigineWorld.h> #include <UnigineObjectMeshStatic.h> #include <UnigineType.h> #include <UnigineEditor.h> /* */ using namespace Unigine; NodePtr node; /* */ void my_update() { // get the frame duration Game *game = Game::get(); float ifps = game->getIFps(); // set the angle of rotation double angle = ifps * 90.0f; // set the angle to the transformation matrix dmat4 transform = node->getTransform(); transform.setRotateZ(angle); // set new transformation to the node node->setTransform(node->getTransform() * transform); } /* */ void my_init() { // get the created box Editor *editor = Editor::get(); node = editor->getNodeByName("box"); } /* */ void my_shutdown() { } /* */ #ifdef _WIN32 int wmain(int argc, wchar_t **argv) { #else int main(int argc, char **argv) { #endif // initialize the engine EnginePtr engine(UNIGINE_VERSION, argc, argv); // load the world by using console Console *console = Console::get(); console->run("world_load cpp_project/cpp_project"); console->flush(); // application initialization my_init(); // enter the main loop while (engine->isDone() == 0) { // update in the main loop engine->update(); // update the application my_update(); // render in the main loop engine->render(); // swap in the main loop engine->swap(); } // shutdown all custom resources my_shutdown(); return 0; }
In the code above we load the cpp_project/cpp_project.cpp world script file. You should specify the name of your project's world script file. -
If your use Visual Studio, do the following:
- Build your project by clicking Build -> Build Solution in Visual Studio or press F7.
- Start your project by clicking Debug -> Start in a proper mode in Visual Studio.
If you created the Xcode project:
- Build your project by clicking Product -> Build in Xcode.
- Run the project by clicking Product -> Run.
If you created GNU Make project:
- Execute the make command in the terminal to compile the application.
make
- Launch the application by using the run script.