Version 2 of the Bouncy Rectangle program has a refactored game loop engine. After looking around at ways to separate the rate that events happen in the game from the rendering speed of the system running the program I went with an approach called Fixed-Frequency Uncoupled method. Where the game engine will run certain events at fixed frequency, while uncoupling a second module of the program that does the rendering using as much CPU speed is available. To make it interesting I also added an AI opponent, which is another blue square that follows the player around the screen. (No music this time).
To implement multi-threading I used POSIX threads (-pthreads). The fixed frequency game logic would update the game state based on user input and the physics of the moving rectangle "characters". It would then sleep for 100 ms, basically updating the game state 10 times a second.
The render loop was in the main thread and would both update the screen with the updated game state as well as do some animation interpolation based on the trajectories of the rectangles from the last game state. This also had to trap any user input (keyboard keys) and then update the game state with these inputs for when the fixed-frequency thread would next check it. To avoid synchronization issues, I used mutexes when in critical sections that accessed the shared variables containing game state info. The render loop will run as fast as the system it runs on, on my box it was humming along at 140 fps.
This is a high level diagram of the separation of the game logic:
Valente, L., Conci, A., and Feijó, B. 2005. Real time game loop models for single-player computer games. In Proceedings of the IV Brazilian Symposium on Computer Games and Digital Entertainment, 89--99. Downloaded from: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.118.8892
For the last few weekends I have given PunchClock and my java work a break, to get back to the fun stuff in C. I am trying to learn the open source Simple DirectMedia Layer (libSDL) api for 2d graphics.
My first simple project was to draw a background and move an object around the foreground. Enter "Bouncy Rectangle".
This is a very simple program where you control the movements of the red square by using the keyboard arrows. Pressing in a direction will either speed you up or slow you down, when you hit one of the edges your speed will be translated into the opposite direction so you "bounce" like a pool ball.
I'd like to work further on separating the movement speeds from the "game loop" which will use 100% of whatever CPU you have if you let it, which would vary the speed from system to system.
The source code is all in one file, after installing libSDL (not so simple on Ubuntu) the program was developed in Eclipse CDT.
Full Source for reference:
/*
============================================================================
Name : BouncyRectangle.c
Author : ammianus
Version : 0.0.1
Copyright : Copyright Brian Laskey 2011
Description : Bouncy rectangle SDL example
Developed in Eclipse CDT, using SDL 1.3.0
============================================================================
*/
#include <stdio.h>
#include <sdl sdl.h="">
/* game loop states */
#define GAME_LOOP_RUNNING 1
#define GAME_LOOP_STOP 0
/* movement constant */
#define MAX_VELOCITY 20
#define VELOCITY_INCREMENT 10
/* pointer to the main window surface */
SDL_Surface *screen;
/*
* Draw the graphic to the coordinates specified on to the screen.
*
* (0,0) is the top left corner of the visible screen.
*
* \param graphic the pointer to a surface containing graphic
* to draw on screen
* \param x the x-coordinate on the screen surface
* of the top-left corner of the graphic to draw
* \param y the y-coordinate on the screen surface of the top-left corner
* of the graphic to draw
*/
void drawGraphic(SDL_Surface *graphic, const int x, const int y) {
/*initialize SDL_Rect struct to hold destination blitting data*/
SDL_Rect dst;