Code:
#include <allegro.h>
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
void draw_rectangle ( int xx,int yy, int xxa, int yya, int colorr, int colorg, int colorb );
int allgero ( int srcx, int srcy );
int char_length ( char IT[100] );
void settings ( BITMAP *BG,BITMAP *ALLO,int xx,int yy,int srcx,int srcy );
void draw_button ( int xx,int yy, int selected, char button_text[50] );
volatile long speed_counter = 0;
volatile long logic_counter = 0;
BITMAP *buffer = NULL;
void increment_speed_counter() // A function to increment the speed counter
{
speed_counter++; // This will just increment the speed counter by one.
logic_counter++;
}
int main ( int argc, char *argv[] )
{
int aa;
cout<<"chosse res \n 1. 1680x1050\n 2. 1280x800\n 3. 1024x786\n 4. 800x600\n 5. Custom\n";
cin>>aa;
switch ( aa )
{
case 1:
{
allgero ( 1680,1050 );
break;
}
case 2:
{
allgero ( 1280,800 );
break;
}
case 3:
{
allgero ( 1024,786 );
break;
}
case 4:
{
allgero ( 800,600 );
break;
}
}
return 0;
exit ( 0 );
}
END_OF_MAIN();
int allgero ( int srcx, int srcy )
{
allegro_init();
install_keyboard();
install_timer()
LOCK_VARIABLE ( speed_counter );
LOCK_FUNCTION ( increment_speed_counter )
install_int_ex ( increment_speed_counter, BPS_TO_TIMER ( 70 ) );//Set beat's per second 60 works well, generally.
set_color_depth ( 32 );
set_gfx_mode ( GFX_AUTODETECT,srcx,srcy,0,0 );
BITMAP *BG = NULL;
BITMAP *ALLO = NULL;
BITMAP *arrow = NULL;
ALLO = load_bitmap ( "./allgero.bmp", NULL );
BG = load_bitmap ( "./back.bmp", NULL );
arrow = load_bitmap ( "./arrow.bmp", NULL );
buffer = create_bitmap ( srcx,srcy );
if ( BG == NULL )
{
set_gfx_mode ( GFX_TEXT,0,0,0,0 ); //Set the screen mode for allegro messages
allegro_message ( "Could not load back.bmp" );
exit ( EXIT_FAILURE );
}
if ( ALLO == NULL )
{
set_gfx_mode ( GFX_TEXT,0,0,0,0 ); //Set the screen mode for allegro messages
allegro_message ( "Could not load allgero.bmp" );
exit ( EXIT_FAILURE );
}
if ( arrow == NULL )
{
set_gfx_mode ( GFX_TEXT,0,0,0,0 ); //Set the screen mode for allegro messages
allegro_message ( "Could not load arrow.bmp" );
exit ( EXIT_FAILURE );
}
short ran_before1 = 0;
short ran_before = 0;
short ran_before2 = 0;
short ran_before3 = 0;
short ran_before4 = 0;
int xx = srcx/2-400;
int yy = srcy/2-300;
long button = 0;
int aa = -1;
int buttonx = 1;
/*int arrow_bb_left = xx;
int arrow_bb_top = yy;
int arrow_bb_bottom = ( arrow_bb_top + arrow->h );
int arrow_bb_right = ( arrow_bb_left + arrow->w );
int ALLO_bb_left = xxa;
int ALLO_bb_top = yya;
int ALLO_bb_right = ( ALLO_bb_left + ALLO->w );
int ALLO_bb_bottom = ( ALLO_bb_top + ALLO->h );*/
//int show_bbox = FALSE;
//int collision = FALSE;
char str[50];
char NUMBER1[100]="0";
char NUMBER[100]="0 0 0 00 0 ";
while ( !key[KEY_ESC] )
{
while ( speed_counter > 0 )
{
//collision = FALSE;
//if ( arrow_bb_top < ALLO_bb_top||arrow_bb_bottom > ALLO_bb_bottom||arrow_bb_right > ALLO_bb_right||arrow_bb_left < ALLO_bb_left )
//{
// collision = TRUE;
//}
if ( key[KEY_UP]&&ran_before == 0)
{
ran_before = 1;
button=button-1;
}
if ( !key[KEY_UP]&&ran_before == 1 )
{
ran_before = 0;
}
if ( key[KEY_DOWN]&&ran_before1 == 0)
{
cout<<"KEY_DOWN\n";
ran_before1 = 1;
button++;
}
if ( !key[KEY_DOWN]&&ran_before1 == 1 )
{
cout<<"!KEY_DOWN\n";
ran_before1 = 0;
}
if ( key[KEY_RIGHT]&&ran_before2 == 0)
{
buttonx++;
ran_before2 = 1;
}
if ( !key[KEY_RIGHT]&&ran_before2 == 1 )
{
ran_before2 = 0;
}
if ( key[KEY_LEFT]&&ran_before3 == 0 )
{
buttonx--;
ran_before3 = 1;
}
if ( !key[KEY_LEFT]&&ran_before3 == 1 )
{
ran_before3 = 0;
}
if ( key[KEY_W] )
{
yy--;
}
if ( key[KEY_A] )
{
xx--;
}
if ( key[KEY_S] )
{
yy++;
}
if ( key[KEY_D] )
{
xx++;
}
if ( key[KEY_R] )
{
xx = srcx/2-400;
yy = srcy/2-300;
}
if ( key[KEY_ENTER]&&button == 3&&buttonx == 1&&ran_before4 == 0 )
{
button = 0;
buttonx = 0;
settings ( BG,ALLO,xx,yy,srcx,srcy );
ran_before4 = 1;
}
if ( key[KEY_ENTER]&&button == 0&&buttonx == 0&&ran_before4 == 0 )
{
sprintf (NUMBER1, "%d",1);
NUMBER[aa]=NUMBER1[0];
aa++;
ran_before4 = 1;
}
if ( key[KEY_ENTER]&&button == 0&&buttonx == 1&&ran_before4 == 0 )
{
sprintf (NUMBER1, "%d",2);
NUMBER[aa]=NUMBER1[0];
aa++;
ran_before4 = 1;
}
if ( key[KEY_ENTER]&&button == 0&&buttonx == 2&&ran_before4 == 0 )
{
sprintf (NUMBER1, "%d",3);
NUMBER[aa]=NUMBER1[0];
aa++;
ran_before4 = 1;
}
if ( key[KEY_ENTER]&&button == 1&&buttonx == 0&&ran_before4 == 0 )
{
sprintf (NUMBER1, "%d",4);
NUMBER[aa]=NUMBER1[0];
aa++;
ran_before4 = 1;
}
if ( key[KEY_ENTER]&&button == 2&&buttonx == 0&&ran_before4 == 0 )
{
sprintf (NUMBER1, "%d",5);
NUMBER[aa]=NUMBER1[0];
aa++;
NUMBER[aa]='\0';
ran_before4 = 1;
}
if ( !key[KEY_ENTER]&&ran_before4 == 1 )
{
ran_before4 = 0;
}
/*arrow_bb_left = xx;
arrow_bb_top = yy;
arrow_bb_bottom = ( arrow_bb_top + arrow->h );
arrow_bb_right = ( arrow_bb_left + arrow->w );
ALLO_bb_left = xxa;
ALLO_bb_top = yya;
ALLO_bb_right = ( ALLO_bb_left + ALLO->w );
ALLO_bb_bottom = ( ALLO_bb_top + ALLO->h );*/
sprintf (str, "%d,%d,%d",xx,yy,aa);
speed_counter--;
}
blit ( BG, buffer, 0,0,0,0,srcx,srcy );
masked_blit ( ALLO, buffer, 0,0,xx,yy,800,600 );
draw_button ( 400-64+xx,300-128+yy, 0, "2" );
draw_button ( 400-64+xx,300-54+yy, 0, "5" );
draw_button ( 400-64+xx,320+yy, 0, "8" );
draw_button ( 400-64+xx,394+yy, 0, "Settings" );
draw_button ( 400-202+xx,300-128+yy, 0, "1" );
draw_button ( 400-202+xx,300-54+yy, 0, "4" );
draw_button ( 400-202+xx,320+yy, 0, "7" );
draw_button ( 474+xx,300-128+yy, 0, "3" );
draw_button ( 474+xx,300-54+yy, 0, "6" );
draw_button ( 474+xx,320+yy, 0, "9" );
draw_button ( 474+xx,394+yy, 0, "0" );
draw_rectangle ( 200+xx,75+yy,600+xx,120+yy,0,0,0 );
if ( button == 0&&buttonx == 1 )
{
draw_button ( 400-64+xx,300-128+yy, 1, "2" );
}
if ( button == 1&&buttonx == 1 )
{
draw_button ( 400-64+xx,300-54+yy, 1, "5" );
}
if ( button == 2&&buttonx == 1 )
{
draw_button ( 400-64+xx,300+20+yy, 1, "8" );
}
if ( button == 3&&buttonx == 1 )
{
draw_button ( 400-64+xx,300+94+yy, 1, "Settings" );
}
if ( button == 0&&buttonx == 0 )
{
draw_button ( 400-202+xx,300-128+yy, 1, "1" );
}
if ( button == 1&&buttonx == 0 )
{
draw_button ( 400-202+xx,300-54+yy, 1, "4" );
}
if ( button == 2&&buttonx == 0 )
{
draw_button ( 400-202+xx,300+20+yy, 1, "7" );
}
if ( button == 0&&buttonx == 2 )
{
draw_button ( 474+xx,300-128+yy, 1, "3" );
}
if ( button == 1&&buttonx == 2 )
{
draw_button ( 474+xx,300-54+yy, 1, "6" );
}
if ( button == 2&&buttonx == 2 )
{
draw_button ( 400+74+xx,300+20+yy, 1, "9" );
}
if ( button == 3&&buttonx == 2 )
{
draw_button ( 474+xx,394+yy, 1, "0" );
}
textprintf_ex ( buffer, font,0 , 0, makecol ( 0,0,0 ), -1, str );
textprintf_ex ( buffer, font,xx+100 , yy+80, makecol ( 0,0,0 ), -1, NUMBER);
textprintf_ex ( buffer, font,200 , 200, makecol ( 0,0,0 ), -1, NUMBER1);
blit ( buffer, screen, 0,0,0,0,srcx,srcy );
}
destroy_bitmap ( ALLO );
destroy_bitmap ( BG );
destroy_bitmap ( arrow );
return 0;
}
void settings ( BITMAP *BG,BITMAP *ALLO,int xx,int yy,int srcx,int srcy )
{
short ran_before1 = 0;
short ran_before = 0;
int buttony = 0;
while ( !(key[KEY_ENTER]&&buttony == 2))
{
while ( speed_counter > 0 )
{
if ( key[KEY_UP]&&ran_before == 0&&buttony!=0 )
{
ran_before = 1;
buttony=buttony-1;
}
if ( !key[KEY_UP]&&ran_before == 1 )
{
ran_before = 0;
}
if ( key[KEY_DOWN]&&ran_before1 == 0 )
{
ran_before1 = 1;
buttony=buttony+1;
}
if ( !key[KEY_DOWN]&&ran_before1 == 1 )
{
ran_before1 = 0;
}
if ( key[KEY_W] )
{
yy--;
}
if ( key[KEY_A] )
{
xx--;
}
if ( key[KEY_S] )
{
yy++;
}
if ( key[KEY_D] )
{
xx++;
}
speed_counter--;
}
blit ( BG, buffer, 0,0,0,0,srcx,srcy );
masked_blit ( ALLO, buffer, 0,0,xx,yy,800,600 );
draw_button ( 400-64+xx,300-128+yy, 0, "button 1" );
draw_button ( 400-64+xx,300-54+yy, 0, "button 2" );
draw_button ( 400-64+xx,300+20+yy, 0, "Back" );
if ( buttony == 0)
{
draw_button ( 400-64+xx,300-128+yy, 1, "button 1" );
}
if ( buttony == 1)
{
draw_button ( 400-64+xx,300-54+yy, 1, "button 2" );
}
if ( buttony == 2)
{
draw_button ( 400-64+xx,300+20+yy, 1, "Back" );
}
blit ( buffer, screen, 0,0,0,0,srcx,srcy );
}
}
void draw_button ( int xx,int yy, int selected, char button_text[50] )
{
textprintf_ex ( buffer, font,xx+64-char_length ( button_text ) *4 , yy+30, makecol ( 0,0,0 ), -1, button_text );
if ( selected == 1 )
{
draw_rectangle ( xx,yy,xx+128,yy+64,0,0,255 );
draw_rectangle ( xx-1,yy-1,xx+129,yy+65,0,0,255 );
}
else
{
draw_rectangle ( xx,yy,xx+128,yy+64,0,0,0 );
}
}
void draw_rectangle ( int xx,int yy, int xxa, int yya, int colorr, int colorg, int colorb )
{
line ( buffer,xx,yy,xx,yya,makecol ( colorr,colorg,colorb ) );
line ( buffer,xxa,yya,xx,yya,makecol ( colorr,colorg,colorb ) );
line ( buffer,xxa,yy,xx,yy, makecol ( colorr,colorg,colorb ) );
line ( buffer,xxa,yya,xxa,yy,makecol ( colorr,colorg,colorb ) );
}
int char_length ( char IT[100] )
{
int aa = 0;
while ( IT[aa]!='\0' )
{
aa++;
}
return aa;
}