Skip to content

First Project

sppmacd edited this page Aug 26, 2021 · 15 revisions

In this tutorial you will find out how to create your first project in bare EGE library.

Setup

First you must to do is create a project folder following this tutorial. Use ege-gui as dependent module.

Add a new file (main.cpp or something) to src folder.

Creating your first blank window

Like normal C++ programs, the EGE code starts executing from int main() function.

int main()
{
    return 0;
}

The main thing needed to make a working game in engine is a game loop. It is created using EGE::GUIGameLoop class from <ege/gui.h>. The game loop is a structure, which manages game updates like event handling (mouse, keyboard etc.), updating your game logic (moving characters etc.) and rendering game (displaying it on the screen).

The loop need to be run using loop.run().

#include <ege/gui.h>

int main()
{
    EGE::GUIGameLoop loop;
    return loop.run();
}

When you run it, you will be welcomed with the following output:

EGE/STDOUT W: GUIGameLoop: No ResourceManager set, using default dummy one
EGE/STDOUT N: Inspector dump: 
Root
└─ GUIGameLoop
EGE/STDOUT N: Inspector dump END

As you see, this doesn't do anything concrete yet. We need to create a window in which we will draw our game. Let's create a blank window at beginning. Add this code to main():

auto window = loop.openWindow(sf::VideoMode(500, 500), "Hello World!");

Because all of the EGE code uses std smart pointers, it's required to wrap all pointers in std::*_ptr wrappers from header. The auto keyword is just a replacement for std::shared_ptr<EGE::Window> to auto-deduce type.

This creates a blank window of size 500x500 and title "Hello World". If you are familiar with SFML, you will notice that this function takes exactly the same arguments as SFML's sf::Window constructor (or sf::Window::create()).

The window will be displayed, but you won't be able to close. That is because you need to assign a event handler which will close the window if you click the close button.

Anyways, we need a container for our content (widgets like buttons, labels etc, and obviously our game display!). It is named EGE::GUIScreen, and you create it using Window::setNewGUIScreen() method:

auto guiScreen = window->setNewGUIScreen<EGE::GUIScreen>();

The template argument can be used if you define custom GUIScreens. When you run this code, you will see the same as before, but you will be able to close window because GUIScreen handles it.

image

You will also see in Inspector Dump that the GUIScreen was created:

EGE/STDOUT W: GUIGameLoop: No ResourceManager set, using default dummy one
EGE/STDOUT N: Inspector dump: 
Root
└─ GUIGameLoop
   └─ Window: Hello World!
      └─ GUIScreen (root)
EGE/STDOUT N: Inspector dump END

Displaying something in the window

Let's a couple of simple widgets to the new GUI we opened previously:

auto label = guiScreen->addNewWidget<EGE::Label>();
label->setString("Hello World!");
label->setColor(EGE::Colors::white);

The default label color is black, like background color, hence the label->setColor() call. When you run it, you will see an assertion that font was not found. This is because you need to provide a font manually.

Add some font to res folder.

Then, you need to setup a ResourceManager to specify which font file do you want to load:

auto resourceManager = make<EGE::GUIResourceManager>();
resourceManager->registerDefaultFont("font.ttf");
loop.setResourceManager(resourceManager);

This will create a new instance of ResourceManager, assign a default font font.ttf (which will be automatically loaded), and assign a ResourceManager to loop. The resource folder (res) is automatically used. You can change it with EGE::CommonPaths::setResourceDir("other-resource-folder") from <ege/util.h>

When you run this code, you should see a "Hello World" text in upper left corner of screen:

image

Conclusion

After this tutorial you should know how to open windows and load basic resources in EGE. The next tutorial: Adding Scene and Objects