Cogs and Levers A blog full of technical stuff

State Management in Games

The title of this post does mention “Games”. In all honesty, with very minor modification you could change this implementation to suit any state requisition situation.

It’s the boring stuff that stops me half-way through developing a computer game. Building a menu, a title screen, a high-score chart.. you know, the stuff that takes you away from that big eye-candy feature that’s in the game.

Anyway, the implementation starts with a base class that manages some state. It’s a class that has provision to allow any derivatives of it to provide reaction code. The state manager will actually cause these “reactions” to occur. You’ll see this one shortly.

Here’s the class itself:

	
class state {
  public:
     state(void) : _finished(false) {
        std::cout << "constructed" << std::endl;
     }
 
     virtual ~state(void) {
        std::cout << "destructed" << std::endl;
     }
 
     virtual bool init(void) {
        std::cout << "initializing" << std::endl;
        return true;
     }
 
     virtual bool teardown(void) {
        std::cout << "tearing down" << std::endl;
        return true;
     }
 
     virtual void progress(void) { }
 
     const bool finished(void) { return _finished; }
     state* next(void) { return _next.get(); }
     state& next_state(void) { return *_next.get(); }
  protected:
     boost::shared_ptr<state> _next;
     bool  _finished;
};

Of course, this is all nice and abstract. It doesn’t really do anything. Here’s a simple derivative. Don’t expect too much, it really doesn’t do that much at all:

	
class first_state : public state {
  public:
     first_state(void) : state() { }
 
     virtual bool init(void) {
        return state::init();
     }
 
     virtual bool teardown(void) {
        return state::teardown();
     }
 
     virtual void progress(void) {
        std::cout << "progressing ..." << std::endl;
        _next.reset();
        _finished = true;
     }
};

Finally, we have the state machine. It is the kernel, the guy “in charge”. This will progress execution through the chain of states to ensure the story-board is presented to the user in such a way that they have a nice feeling of continuity throughout the game:

class state_machine {
  public:
     state_machine(void) { }
     state_machine(state *s) : _current(s) { }
     ~state_machine(void) { }
 
     void run(void) {
 
        if (_current)
           _current->init();
 
        while (_current) {
 
           _current->progress();
 
           if (_current->finished()) {
              _current->teardown();
              _current.reset(_current->next());
 
              if (_current) {
                 _current->init();
              }
           }
 
        }
 
     }
 
  private:
     boost::shared_ptr<state> _current;
 
};

To really make this of immediate use to a games programmer, I suggest the reader takes this code as an example and implements the following necessities:

Time management

Giving the programmer the ability to tell how long it was between frame renders or even being able to control the number of frames processed per second is a requirement. This is the heartbeat/lifeblood to giving your program the ability to exist in a human (or non-human) consistent time scale.

Eventing

Most media libraries define some sort of eventing to allow the library to interact with the hosting operating system. These interrupts or notion of “important incoming information” needs to be embraced into the state framework as one of the most important side-effects can come from external interation (keyboards, mouse, etc).

Enjoy.

Python and Relational Data Access

If there’s one job that I’m never keen on getting done, it’s the mind-numbing task of wiring up data access into my application. I’m a big fan of purely abstracted data objects just so the underlying data implementation can really be anything (within reason).

With all of the Flask development that I’m doing at the moment, it was hard to go past SQLAlchemy. Quite an old-school library to all the python-heads, but quite the lifesaver to me.

http://www.sqlalchemy.org/features.html

Out of my (un-creative) hands.

It’s no secret. I suck at style.

I don’t like it, it doesn’t like me.

Problem is, we need to work in harmony (somehow) as in order to host web-based projects, I need some sort of design to host the application within.

Enter: Twitter Bootstrap (http://twitter.github.com/bootstrap/)

The plugins, designs and documentation allow me to assemble websites without much more though.

I can go back to being an engineer!

Inlining Assembly Language with GCC

Noteworthy without this article is Lockless (http://locklessinc.com/) for the great super-technical write-ups that they do.

Inlining assembly language using the GNU toolset has always escaped me (until now). It’s not so much that I didn’t know how to do it, I just felt it much more comfortable to write my assembly in a separately linked object. This article has put my mind, stomach and C blending assembly fingers at ease.

http://locklessinc.com/articles/gcc_asm/

Hello, 64bit Assembly!

So, I’m a tragic for assembly language. I’ve finally gotten around to sharpening up my skills to attack Linux with some assembly language and why not upgrade the skills into the 64bit world at the same time.

“Hello world” follows:

section .data
	hello:      db 'Hello world!',10    ; text plus a linefeed char
	helloLen:   equ $-hello             ; length of the string

section .text
	global _start
	
_start:
	mov   rax, 4            ; system call for "write" (sys_write)
	mov   rbx, 1            ; file descriptor (stdout = 1)
	mov   rcx, hello        ; offset to write
	mov   rdx, helloLen     ; number of bytes to write
	int   0x80              ; syscall
	
	mov   rax, 1            ; system call for exit (sys_exit)
	mov   rbx, 0            ; error code (no error = 0)
	int   0x80              ; syscall

Build at the command line with nasm:

nasm -f elf64 hello.asm
ld -s -o hello hello.o

Some further reading: