The Allegro Wiki is migrating to github at

Allegro computer community medium level expansion

From Allegro Wiki
Jump to: navigation, search
This article is a stub. Please help Allegro by expanding it.

What it says.


Screen Update API

RP suggest CB's

Pixel Collision Lib


Particle Engine

"Particles are more and more common, and a handy set of functions which can be used to create and manage particles would be useful. Perhaps containing function pointers for draw functions?" - RP in the thread which inspired this.

Basic Game Loop

The following code was contributed by nonnus29 <highlightSyntax language="cpp"> /*

  • File: SimpleEngine.h
  • This is very self explanatory as you can
  • pretty much derive the api from the functions
  • prototypes. I'm just wrapping Allegro to
  • give me tilemaps, sprites, and a display.
  • Note: This is a work in progress
  • /
  1. ifndef SIMPLEENGINE_H
  2. define SIMPLEENGINE_H

//#define TRUE 1

  1. define FALSE 0

typedef struct {

   int posx, posy;
   int loopstart, loopend;
   int action,speed,curframe;  //0=none 1=loop 
   int count, frame;


typedef struct {

   int width, height;
   int camx, camy;
   int *data;

} MAP;

int initEngine(); void exitEngine(); int loadGfx(); void updateInput(); void render(); void updateSprite(int index); void drawSprites(); void drawMaps(); long long int getTime(); long long int getTickFreq();

// sprite functions

void setSpritePos(int index, int x, int y); void setSpriteImage(int index, int image); void setSpriteAction(int index, int start, int stop, int action, int speed);

// tilemap functions

void setMapPos(int index, int x, int y); int getMapTile(int x, int y); int loadMap(int index, char *file); void displayTitle(int b);

// input

int keyRIGHT(); int keyLEFT(); int keyUP(); int keyDOWN(); int keyA(); int keyS(); int keyESC();

void logFile(char *);

  1. endif

The following code was contributed by KC and amended by OscarGiner

Here's a small improvement to Kitty Cat's code. It has an extra parameter, max_frame_skip, that tells the maximum number of frames that can be skipped (not drawn).

Pass -1 for unlimited (what original Kitty Cat's code does), 0 to force drawing all frames (no frame skipping at all, the game will slow down on old computers instead of skipping frames), 1 to allow at much 1 frame to be skipped, and so on.

Further improvements: Use <= in currtic<=ticker, so the logic is called at least once before drawing - else you may get odd effect in the very first drawn frame, since it draws a state which never was handled by the logic.

Significantly improved performance by never drawing the same frame twice, and adding a config option not hog the CPU, which may increase the responsiveness in certain cases, and decrease it in others. So it would be best to let the user adjust it in case you just want to run it as smooth as possible, and only use as much CPU as necessary in other case, since your program look very poorly written if it uses 100% CPU just to scroll a tilemap. And it will keep the CPU temperature low, making batteries last longer and fans be more quiet.

<highlightSyntax language="cpp"> int config_use_100_cpu = 0; int config_count_fps = 0;

static volatile unsigned long ticker; static void timer_proc(void) {



void run_game_loop(int (*_logic)(unsigned long tic, void *arg), void (*_draw)(void *arg), void *arg, int speed, int max_frame_skip) {

  int logic_ret;
  unsigned long currtic;
  static int inited = FALSE;
  int last_drawn = 0;
  ticker = currtic = 0;
  install_int_ex(timer_proc, BSP_TO_TIMER(speed));
  do {
     int logic_loop_count = 0;
     logic_ret = 0;
     while(currtic <= ticker)
        logic_ret |= _logic(currtic, arg);
        if (max_frame_skip >= 0 && logic_loop_count > max_frame_skip)
           ticker = currtic;
           break;  // to be sure ticker doesn't increase just here and another loop is performed
     if(logic_ret > 0)
        /* No point drawing the same frame twice. */
        if (config_count_fps || last_drawn <= currtic)
           last_drawn = currtic;
        else if(!config_use_100_cpu)
           /* Wait 1 millisecond. This will normally wait longer, causing CPU to drop. */
  } while(logic_ret >= 0);



_logic returns: 1=try to draw when logic is caught up, 0=don't draw, -1=quit -- KC

Attract Mode / Menu System