I have a game I am developing that works perfectly when I use
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 800, 600, 0, 0);
but when I change it to
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 800, 600, 0, 0);
and recompile, it won't exit. That is the only change.
My game loop is basicly
bool exit_key = false;
while (!exit_key)
{
if (key[KEY_ESC]) exit_key = true;
// Game Stuff
}
Yet for some reason unknown to me it works fine in with 16 color depth but the while loop doesn't seem to exit in 32 color depth. Yet that is the only change! Am I missing something or what? Thanks!
Which version you are using?
Allegro version 4.2 (the binary for windows)
Can you post the full code? Does your card/monitor support that resolution?
Also, when you say "won't exit" is that exactly what you mean? Does it refuse to quit, or does Allegro not fully shutdown (it leaves your program sitting in the task bar), etc.
Also, next time, please give a better title to your posts than "Kindof Newbie question".
I think your problem probably lies in one of two areas. Are you sure that you are initialising the keyboard right? If so, it could be because of the window focus. This happened to me before, but then after downloading the newest allegro source and compiling it, now I don't have this problem anymore. Try ALT-TAB to another window, then switch back to your program. Now if it exits, then you know where your problem is.
By "doesn't exit" I mean it is as if the while (condition) is still being met, even though "condition" is in fact false and checks in other parts of the code show that "condition" HAS changed to false. Yet the while loop continues to loop with 32 color depth, but not 16.
Here is the code:
It's probably not the way it is supposed to be coded, but it works. I'm sure there are better ways to do a lot of it.
Try installing the keyboard after set_gfx_mode. That's all I've got other than the possibility of an Allegro bug.
Note that on some platforms the keyboard won't work unless you have set a graphic mode, even if this function returns zero before calling set_gfx_mode.
You can install it before setting the graphics; you just might not be able to use it until after.
What happens if you do some error checking?
set_color_depth(32); if ( set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 800, 600, 0, 0) < 0 ) { return -1; }
void update_variables() { if (key[KEY_ESC]) exit_key = true; } while(speed_counter > 0) { update_variables(); }
there could be something with the timer and speed_counter is never > 0.
Put the if key_escape line outside of the speed counter loop.
while( !exit_key ) { if (key[KEY_ESC]) exit_key = true; while(speed_counter > 0) { } }
Also, since you are a gnub, noob, ...
1. Indentation is your friend
every new section starts x spaces over and brackets line up. I personally like style b, but others choose a.
| 1 | //style a |
| 2 | if ( blah ) { |
| 3 | int new_var; |
| 4 | if ( blah2 ) { |
| 5 | |
| 6 | } |
| 7 | } |
| 8 | |
| 9 | //style b |
| 10 | if ( blah ) |
| 11 | { |
| 12 | int new_var; |
| 13 | if ( blah2 ) |
| 14 | { |
| 15 | } |
| 16 | } |
2. Error checking is your friend
| 1 | DATAFILE *data = NULL; |
| 2 | BITMAP *buffer = NULL; |
| 3 | |
| 4 | // style a |
| 5 | data = load_datafile( "data.dat" ); |
| 6 | if ( !data ) |
| 7 | { |
| 8 | return -1; |
| 9 | // this was an error |
| 10 | } |
| 11 | |
| 12 | // style b |
| 13 | if ( !( buffer = create_bitmap( 800, 600 ) ) ) |
| 14 | { |
| 15 | return -1; |
| 16 | // this was an error |
| 17 | } |
| 18 | |
| 19 | //destroy_bitmap( buffer ); |
| 20 | //unload_datafile(data); |
| 21 | |
| 22 | if ( buffer ) |
| 23 | { |
| 24 | destroy_bitmap( buffer ); |
| 25 | // this next line might not be necessary |
| 26 | buffer = NULL; |
| 27 | } |
| 28 | |
| 29 | if ( data ) |
| 30 | { |
| 31 | unload_datafile( data ); |
| 32 | // this next line might not be necessary |
| 33 | data = NULL; |
| 34 | } |
3. Look up coding styles. This is more of a help to those who look at your code
i.e. space after comma blah, blah, blah
space between parens ( blah )
----
There is a slight problem with my logic.
if ( !data ) return -1 if ( !buffer ) return -1;
if buffer fails then datafile is never unloaded. Instead you could do this
data = load_datafile(); if ( data ) { buffer = create_bitmap(); if ( buffer ) { //...// destroy_bitmap( buffer ); } unload_datafile( data ); }
Or you could do this.
| 1 | int init() |
| 2 | { |
| 3 | allegro_init(); |
| 4 | |
| 5 | data = load_datafile(); |
| 6 | if ( !data ) return -1; |
| 7 | |
| 8 | buffer = create_bitmap(); |
| 9 | if ( !buffer ) return -1; |
| 10 | |
| 11 | return 0; |
| 12 | } |
| 13 | |
| 14 | void destroy() |
| 15 | { |
| 16 | if ( buffer ) destroy_bitmap(); |
| 17 | if ( data ) unload_datafile(): |
| 18 | } |
| 19 | |
| 20 | int main() |
| 21 | { |
| 22 | if ( init() == 0 ) |
| 23 | { |
| 24 | // loop |
| 25 | } |
| 26 | |
| 27 | destroy(); |
| 28 | } |