NOGDUS
October 21, 2018, 02:27:27 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Blogs Help Search Tags Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Allegro Game Template v6.0  (Read 3220 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
202192397
Administrator
Member
*

Respect: 3425
Offline Offline

Posts: 1027


happy


« on: July 22, 2009, 12:52:10 AM »

Don't use this template. Use Allegro Game Template v6.2 instead!









Allegro Game Template v6.0

Features:
  • Handles maintaining the same speed on different computers with an FPS lock to 30 FPS
  • Handles the X close button on the window properly
  • Starts with keyboard and mouse support and a 800x600 window

You are free to use this as long as you give me some credit somewhere in your game.

Code:
// main.cpp
// Project: Allegro Game Template v6.0
// Author: Richard Marks <ccpsceo@gmail.com>

#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <allegro.h>

// we need this for handling timing
static volatile int allegrotimerspeedcounter = 0;
static void my_allegro_timer_speed_controller()
{
allegrotimerspeedcounter++;
}
END_OF_FUNCTION(my_allegro_timer_speed_controller)

// we need this to handle closing the window via the [X] button (you NEED this)
static volatile bool mainthreadisrunning = true;
static void my_allegro_close_button_handler()
{
mainthreadisrunning = false;
}
END_OF_FUNCTION(my_allegro_close_button_handler)

bool setup_game();
void shutdown_game();

// constants for the screen resolution
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;

int main(int argc, char* argv[])
{
// init allegro and add keyboard and mouse support
allegro_init();
install_timer();
install_keyboard();
install_mouse();

// set the video mode to 800x600 @ 16bpp
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0);
set_window_title("Allegro Game Template v6.0 -- Richard Marks <ccpsceo@gmail.com>");

// create the back buffer bitmap
BITMAP* backbuffer = create_bitmap(SCREEN_W, SCREEN_H);

// seed the random number generator
srand(time(0));

// lock the static functions and variables we need for handling timing and closing the window via the [X] button
LOCK_FUNCTION(my_allegro_close_button_handler);
LOCK_FUNCTION(my_allegro_timer_speed_controller);
LOCK_VARIABLE(allegrotimerspeedcounter);

// set the callback function for the close-button to our global handler function
set_close_button_callback(my_allegro_close_button_handler);

// set our FPS lock timing global function
install_int_ex(my_allegro_timer_speed_controller, BPS_TO_TIMER(30));

// setup the game
if (!setup_game())
{
fprintf(stderr, "The game initialization has failed. Cannot continue!\n");
exit(1);
}

// main loop
bool gameover = false;
while(!gameover)
{
// if our global is ever false
if (!mainthreadisrunning)
{
gameover = true;
}

// we only update during our FPS lock time
while (allegrotimerspeedcounter > 0)
{
if (keyboard_needs_poll())
{
poll_keyboard();
}

if (key[KEY_ESC])
{
gameover = true;
}

// TODO: *** update stuff here

// decrement the global timing var so that we can leave the update loop!
allegrotimerspeedcounter--;
}

// start rendering the scene

clear_bitmap(backbuffer);


// TODO: *** draw stuff here

// make it all visible
blit(backbuffer, screen, 0, 0, 0, 0, backbuffer->w, backbuffer->h);

// a little rest to keep CPU usage down ^-^
rest(10);
}

// shutdown the game
shutdown_game();

// clean up the back buffer
if (backbuffer)
{
destroy_bitmap(backbuffer);
}

return 0;
}
END_OF_MAIN();

bool setup_game()
{
// load game resources and stuff here
// return false if something failed to load or init

// return true to let the main() function know that we're good to go
return true;
}

void shutdown_game()
{
// unallocate anything you allocated for your game here
}

If you have questions, let me know.
Logged

Tags:
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!