Creating a game

Initial setup

To start off you will need a main.cpp file and that will typically contain:

int main(){
    return 0;
}

Then you will want the include headers of the game engine at the top of this file:

#include <Unified-Engine/core/instance.h>

What this then allows you to do is start initialising the game. Here is an example setup followed by an explanation of each setting.

UnifiedEngine::__GLOBAL_CONFIG__.VersionMajor = 4;
UnifiedEngine::__GLOBAL_CONFIG__.VersionMinor = 6;

UnifiedEngine::__INIT__ENGINE();

UnifiedEngine::WindowConfig WinConf = {.x = 1260, .y = 720, .res_x = 0, .res_y = 0, .title = "Test Program", .resizable = true, .fullScreen = false, .vsync = true, .fps = 60};
WinConf.backgroundColor = {0, 0, 0};

UnifiedEngine::Window GameWindow(WinConf);

The global conifg of VersionMajor and VersionMinor are fine to be left at 4.6 assuming that your system is fairly new and supports the OpenGL version 4.6 which most do. If you end up with errors around graphical functions when you try to run the code, you may need to adjust this value. You can find the latest version supported on your GPUs info site.

UnifiedEngine::__INIT__ENGINE(); is vital for the setup of all the OpenGL configuration and will not allow the game to function if not ran! It will create a UnifiedEngine::__GAME_GLOBAL_INSTANCE__ object that can be reference and accessed from anywhere. However, by default no window is attached to it.

The Window Config variable goes into more depth on the documentation, the purpose is to define what the window should show as when the GameWindow is created, this automatically hooks onto the global game instance.

Now, when you run the game you will find it closes instantly. This is because we now need a game loop. So after the configuration you will want to add:

// Game loop
while (!glfwWindowShouldClose(GameWindow.Context()))
{
    // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
    if(UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Update()){
        FAULT("GAME_INSTANCE::FAILED TO UPDATE!");
        //TODO: Call some sort of deinitializer (UnifiedEngine::Terminate())
        exit(10);
    }

    // Your game code

    if(UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Render()){
        FAULT("GAME_INSTANCE::FAILED TO RENDER!");
        //TODO: Call some sort of deinitializer (UnifiedEngine::Terminate())
        exit(11);
    }
}

// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();

To start it will loop whilst the game is still marked as active, if not it runs a terminate command to close and unload all buffers.

There are two main functions needed Update and Render. Update is used to allow the game engine to process all internal actions, such as the camera moving/rotating, physics and polling for any inputs received by the user. Render does what it says and will allow rendering of all the components to the screen and controlling FPS.

Now you have the game part setup. However, we still can't run just yet as there is no Camera for rendering.

Last updated