Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

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:

yourpath\SolKraftEngine\projects\yourprojectname\code\states\

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”)
LIST(APPEND SRCFOLDERNAME ${SRC} ${HEAD})
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:

LIST(APPEND COMPLETESRC ${SRCSBASE})

to ->

LIST(APPEND COMPLETESRC ${SRCSBASE} ${SRCFOLDERNAME})

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
{
public:
StateName();
~StateName();

///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”

StateName::StateName()
{
}

StateName::~StateName()
{
}

void StateName::onEnter()
{
}

void StateName::onExit()
{
}

void StateName::preFrame()
{
StateBase::preFrame();
}

void StateName::onFrame()
{
StateBase::onFrame();
}

void StateName::postFrame()
{
StateBase::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();
t_StateName->setStateName(“StateName”);
t_StateName->setCurrentLevel(“”);

StateManager::instance()->addState((StateBase*)t_StateName);

StateManager::instance()->setFirstState(“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 comment  

name*

email*

website

Submit comment