This is a very quick tutorial on how to get started using SMGL. Our goal is just to load a character and let him move around.
There are three main parts to SMGL; the Engine, the World, and the Actors. Every game must have an Engine, a World, and usually Actors. SMGL comes with defaults for all three, but the World must be over-ridden for your game to draw anything.
Since our goal is to just load a character and let him walk around, we will use the default Engine. It is usually only necessary to over-ride the Engine if you want to add in menu or intro/outro animations.
It will be necessary for us to override the default world. In SMGL, the engine instructs the world to update/draw itself, which in turn tells all the objects in the world to update/draw themselves. Almost all character will be stored inside the world. Since we want our game to have a character, our World will have an instance of the default character. Also, because are character doesn't need to do anything special, we can use the default Duke character.
The first then we should do is write our World class. I'm calling it TestWorld, and it should inherit World.
#include <smgl/World.h>
class TestWorld : public World
{
public:
TestWorld();
virtual ~TestWorld();
};
Now, since we want a character in our game, let's add a reference to the default Duke character.
#include <smgl/World.h>
#include <smgl/Duke.h>
class TestWorld : public World
{
public:
TestWorld();
virtual ~TestWorld();
protected:
Duke *_duke;
};
Now we also need to update and draw the character, so we'll also over-ride the draw and update functions.
--Duke.h
#include <smgl/World.h>
#include <smgl/Duke.h>
class TestWorld : public World
{
public:
TestWorld();
virtual ~TestWorld();
virtual void draw();
virtual int update();
protected:
Duke *_duke;
};
Now, let's go ahead an implement this in Duke.cpp
#include "TestWorld.h"
TestWorld::TestWorld()
: World()
{
// load in a default mesh for the world
loadMesh("meshes/World.jbm");
// call World's init to finish setting everything up
init();
//create a new duke character
_duke = new Duke();
}
TestWorld::~TestWorld()
{
deinit();
}
void TestWorld::draw()
{
// first draw the world
World::draw();
// tell the character to draw itself
_duke->draw();
}
int TestWorld::update()
{
// update the character
// if we don't animation and
// key input won't work
_duke->update();
// return the result of World::update()
return World::update();
}
Pretty easy huh ? Our Duke character is automatically going to handle is own animations, handle it's own keyboard input, and make sure it stays inside the World !
Now, we need to create our main function to start everything.
--main.cpp
#include <smgl/Engine.h>
#include "TestWorld.h"
int main(int argc, char **argv)
{
// create a new default engine
Engine *e = new Engine();
// initialize it !
// this MUST be initialized before we do
// anything else
if (!e->initialize())
return 1;
// create a new instance of our world with
// duke in it
TestWorld *w = new TestWorld();
// we have to register it, or it won't be called
w->registerWorld(w);
// start our main game loop
// when this returns, the game is over
e->run();
// quit with no errors
return 0;
}
That's all there is to it. We first created a new default engine which creates a window and sets up opengl for us. We then create an instance of our TestWorld and we register it. If we don't register it, then it will never be called by the default engine ! Once we register our world, we are ready to go into the main engine game loop where it will automatically tell the world to update, then to draw.
To compile this example under linux do:
g++ -c TestWorld.cpp `sdl-config --cflags`
g++ -c main.cpp `sdl-config --cflags`
g++ -o Test TestWorld.o main.o `sdl-config --libs` -lGL -lGLU -lsmgl