This page has been translated automatically.
Программирование
Fundamentals
Setting Up Development Environment
UnigineScript
High-Level Systems
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
Rendering-Related Classes
Внимание! Эта версия документация УСТАРЕЛА, поскольку относится к более ранней версии SDK! Пожалуйста, переключитесь на самую актуальную документацию для последней версии SDK.
Внимание! Эта версия документации описывает устаревшую версию SDK, которая больше не поддерживается! Пожалуйста, обновитесь до последней версии SDK.

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

Creating Empty C++ Application

It is very easy to start your own C++ project by using UNIGINE SDK Browser:

  1. Open the UNIGINE SDK Browser.
  2. Go to the Projects tab and click CREATE NEW.

  3. 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.
    Notice
    Read more about these parameters in this article
  4. Click the Create New Project button. The project will appear in the projects list.

You can run your project by clicking the Run button.

Notice
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++.

Implementing C++ Logic

In this section we will add logic to the empty C++ application project.

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 or Xcode:

    1. 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:

    1. On the created C++ project, click on the Other Actions button and then the Open Folder button.
      Other Actions button
      Open Folder button
    2. Go to the <YOUR PROJECT>\source\ folder and open the AppWorldLogic.cpp file with any plain text editor.
  2. Write (or copy) the following code in your project's AppWorldLogic.cpp file.
    Source code (C++)
    #include "AppWorldLogic.h"
    #include "UnigineEditor.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
    	Editor *editor = Editor::get();
    	node = editor->getNodeByName("material_ball");
    	return 1;
    }
    
    int AppWorldLogic::shutdown() {
    	return 1;
    }
    
    int AppWorldLogic::destroy() {
    	node.destroy();
    	return 1;
    }
    
    int AppWorldLogic::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
    	Unigine::Math::dmat4 transform = node->getTransform();
    	transform.setRotateZ(angle);
    
    	// set new transformation to the node
    	node->setTransform(node->getTransform() * transform);
    
    	return 1;
    }
    
    int AppWorldLogic::render() {
    	return 1;
    }
    
    int AppWorldLogic::flush() {
    	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 your use Visual Studio, do the following:

    1. Build your project by clicking Build -> Build Solution in Visual Studio or press F7.

    2. Start your project by clicking Debug -> Start in a proper mode in Visual Studio.

    If you created the Xcode project:

    1. Build your project by clicking Product -> Build in Xcode.

    2. Run the project by clicking Product -> Run.

    If you created GNU Make project:

    1. Execute the make command in the terminal to compile the application.
      Shell commands
      make
    2. Launch the application by using the run script.
Notice
To run your project from SDK Browser, specify the path to the .exe file of your C++ project in SDK Browser by customizing Run Project
Last update: 03.07.2017
Build: ()