Solkraft Tutorial 2: Setting Up States

This tutorial will cover how to set up and use your own states in the Solkraft Engine.

First, we recommend that you go to the yourpath\SolKraftEngine\projects\yourprojectname\code\ folder and create a new folder called “states”, like so:


Now since we made a new folder within the code directory, we have to modify the CMakeLists.txt (the one in \code\) so that the CMake build program knows that new files has been added. In this file, somewhere between the INCLUDE_DIRECTORIES function and the LIST(APPEND COMPLETESRC…) function, add the following lines:

FILE(GLOB SRC “foldername/*.cpp”)
FILE(GLOB HEAD “foldername/*.h”)
SOURCE_GROUP(“yourprojectname\\foldername” FILES ${SRCFOLDERNAME})

After this you need to update the beforementioned LIST(APPEND COMPLETESRC…) function a bit, you have to add ${SRCFOLDERNAME} to the end of the function, like so:


to ->


Take extra care to replace ALL generic names with your relevant names, even those in all caps. Remember that you have to do these steps every time you want to add a new folder in the \code\ directory.

Now CMake recognizes this folder and will include it in your project. Now in the new \states\ folder, create two new files named after your new state: statename.cpp and statename.h. Now these files need some code in them. For now, copy-paste the following for statename.h:

class StateName : public StateBase

///Runs when entering the state
void onEnter();
///Runs when exiting the state
void onExit();

///Runs pre frame
void preFrame();
///Runs on frame
void onFrame();
///Runs post frame
void postFrame();

As always, make sure to rename every generic “StateName” with your actual state’s name. Now here’s the code for the statename.cpp file:

#include “StateName.h”



void StateName::onEnter()

void StateName::onExit()

void StateName::preFrame()

void StateName::onFrame()

void StateName::postFrame()

After you have finished these two files there is some work to do in the other files we created in Tutorial 1. Let’s go to yourprojectname.cpp. In here we see an empty setupStates() function, let’s add some code to it:

StateName* t_StateName = new StateName();



Now when you compile and run the program it will enter the state defined in the setFirstState() function as it starts up, and the states different Frame() functions will run every frame. You can repeat this process to add as many states as you need, and switch between them using the StateManager class!

Leave a Reply

Your email address will not be published. Required fields are marked *