Key Press Help?!

Hey there, I know this is C++ Allegro coding but I know the majority here are all very experienced coders and this is probably a simple fix so even if you have no knowledge of allegro you could probably fix it. Basically I'm making a pong game, I want it so that when the main menu image comes up (picture.bmp) if the user presses the ESC key at any time the program will close. Currently I have the code as seen below however when I run that it goes terribly wrong and constantly opens new versions of the game up over and over and over. (I imagine this is because it is noticing that the ESC key is not pressed and therefore continues to carry out the creation of the image). Any help would be highly appreciated!
Cheers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main( void )
{
	while(!key[KEY_ESC])
	{
	BITMAP *bmp; //Pointer to the bitmap

	allegro_init(); //Initializes Allegro
	install_keyboard(); //Enables the ability for allegro to receive keyboard input
	set_color_depth( 32 ); //Setting the colour depth to 16 bit
	set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0 ); //Setting the graphics mode
	set_window_close_button( 1 );
	set_window_title ("PONG!");
	bmp = load_bitmap( "picture.bmp", NULL ); //Loads the bitmap file
	blit( bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h ); //Draw the bitmap
	}

	destroy_bitmap( bmp ); //Frees the memory allocated to bmp when closing the program
		return 0;
}

END_OF_MAIN() //Allegro specific end main function 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main( void )
{
	
	BITMAP *bmp; //Pointer to the bitmap

	allegro_init(); //Initializes Allegro
	install_keyboard(); //Enables the ability for allegro to receive keyboard input
	set_color_depth( 32 ); //Setting the colour depth to 16 bit
	set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0 ); //Setting the graphics mode
	set_window_close_button( 1 );
	set_window_title ("PONG!");
	bmp = load_bitmap( "picture.bmp", NULL ); //Loads the bitmap file
	blit( bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h ); //Draw the bitmap
	
        while(!key[KEY_ESC]){}

	destroy_bitmap( bmp ); //Frees the memory allocated to bmp when closing the program
		return 0;
}

END_OF_MAIN() //Allegro specific end main function 



I think you should somewhat re-think what went wrong in your code.
Hint: You blitted the image for each iteration.

I don't know the allegro library, but i think there's supposed to be a function to update the key pointer.
Thank you so much, works exactly as I wanted it to now!
Topic archived. No new replies allowed.