My code:
Code: Select all
#include <allegro.h>
#include <fstream>
#include <string>
// define some run-states for the game
#define STATE_TITLE 0
#define STATE_MENU 1
#define STATE_GAME 2
#define STATE_SHUTDOWN 255
const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 800;
//quit flag
bool quit=false;
bool fullscreen=false;
//game state changer
int game_state = STATE_TITLE;
//buffer and bg
BITMAP *buffer;
BITMAP *bg;
//game elements
BITMAP *ball;
BITMAP *p1;
BITMAP *p2;
//things
BITMAP *title;
BITMAP *help;
//menu
BITMAP *play1;
BITMAP *play2;
BITMAP *help1;
BITMAP *help2;
BITMAP *exit1;
BITMAP *exit2;
//File stream for the game log.
std::ofstream logger( "log.txt" );
void log( std::string message )
{
//Write message to file
logger << message << std::endl;
//Flush the buffer
logger.flush();
}
void Init()
{
log("Initilizing allegro...");
allegro_init();
log("Initilizing keyboard...");
install_keyboard();
log("Initilizing mouse...");
install_mouse();
log("Initilizing timer...");
install_timer();
log("Initilizing sound...");
if(install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL) != 0)
{
allegro_exit();
log("Error setting up Sound");
exit(1);
}
log("Setting color depth...");
int cd = desktop_color_depth();
if (cd < 15) cd = 32;
set_color_depth(cd);
log(" ");
log("Loading all bitmaps...");
log("Buffer and bg...");
//buffer and bg
buffer = create_bitmap(SCREEN_WIDTH,SCREEN_HEIGHT);
bg = load_bitmap("gfx/bg.bmp",NULL);
log("Game Elements...");
//game elements
ball = load_bitmap("gfx/ball.bmp",NULL);
if (ball==NULL)
{
log("ball is null");
}
p1 = load_bitmap("gfx/red.bmp",NULL);
if (p1==NULL)
{
log("tp1 is null");
}
p2 = load_bitmap("gfx/blue.bmp",NULL);
if (p2==NULL)
{
log("p2 is null");
}
log("Title and help screen...");
//things
title = load_bitmap("gfx/title.bmp",NULL);
if (title==NULL)
{
log("title is null");
}
help = load_bitmap("gfx/help.bmp",NULL);
log("Menu...");
//menu
play1 = load_bitmap("gfx/play1.bmp",NULL);
play2 = load_bitmap("gfx/play2.bmp",NULL);
help1 = load_bitmap("gfx/help1.bmp",NULL);
help2 = load_bitmap("gfx/help2.bmp",NULL);
exit1 = load_bitmap("gfx/exit1.bmp",NULL);
exit2 = load_bitmap("gfx/exit2.bmp",NULL);
log("Bitmap loading completed.");
log(" ");
if (fullscreen==true)
{
log("Making fullscreen window...");
if (set_gfx_mode(GFX_AUTODETECT,SCREEN_WIDTH,SCREEN_HEIGHT, 0, 0) != 0)
{
allegro_exit();
log("Error setting graphics mode\n%s\n\n");
exit(1);
}
}
else
{
log("Making windowed window...");
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,SCREEN_WIDTH,SCREEN_HEIGHT, 0, 0) != 0)
{
allegro_exit();
log("Error setting graphics mode\n%s\n\n");
exit(1);
}
}
log("Clearing the buffer...");
clear(buffer);
log("Initilization complete.");
log(" ");
}
void ShowScreen ()
{
clear_bitmap(buffer);
log("Bliting buffer to the screen...");
// blit the whole double buffer to the screen
blit (buffer, // source Allegro BITMAP*
screen, // destination Allegro BITMAP*
0, 0, // source x,y
0, 0, // destination x, y
SCREEN_WIDTH, // destination width
SCREEN_HEIGHT); // destination height
log("Clearing the buffer...");
log("Showing the screen complete.");
}
void TITLE_LOGIC()
{
log("Bliting title to the buffer...");
blit(title,buffer,0,0,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
log("Done.");
log(" ");
ShowScreen();
if(key[KEY_ESC])
{
quit=true;
}
if(key[KEY_ENTER])
{
game_state=STATE_MENU;
}
}
void MENU_LOGIC()
{
}
void GAME_LOGIC()
{
}
void cleanUp()
{
log("Cleaning up...");
destroy_bitmap(buffer);
destroy_bitmap(bg);
destroy_bitmap(ball);
destroy_bitmap(p1);
destroy_bitmap(p2);
destroy_bitmap(title);
destroy_bitmap(help);
destroy_bitmap(play1);
destroy_bitmap(play2);
destroy_bitmap(help1);
destroy_bitmap(help2);
destroy_bitmap(exit1);
destroy_bitmap(exit2);
log("Exiting allegro...");
allegro_exit();
log("Done.");
log(" ");
}
int main()
{
Init();
while(quit==false)
{
switch(game_state)
{
case STATE_TITLE:
{
log(" ");
log("Doing title logic.");
TITLE_LOGIC();
}
case STATE_MENU:
{
MENU_LOGIC();
}
case STATE_GAME:
{
GAME_LOGIC();
}
}
}
cleanUp();
return 0;
}
END_OF_MAIN();