Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Kindof Newbie question

This thread is locked; no one can reply to it. rss feed Print
Kindof Newbie question
Craig Harrison
Member #5,255
November 2004
avatar

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!

ReyBrujo
Moderator
January 2001
avatar

Which version you are using?

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Craig Harrison
Member #5,255
November 2004
avatar

Allegro version 4.2 (the binary for windows)

ReyBrujo
Moderator
January 2001
avatar

Can you post the full code? Does your card/monitor support that resolution?

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Chris Katko
Member #1,881
January 2002
avatar

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.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Elverion
Member #6,239
September 2005
avatar

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.

--
SolarStrike Software - MicroMacro home - Automation software.

Craig Harrison
Member #5,255
November 2004
avatar

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:

#SelectExpand
1#include <allegro.h> // Allegro Header file 2#include <math.h> 3#include "data.h" 4 5 //////////////////////////////////////////////////////////////////////////// 6 ///////// VARIABLES //////////////////////////////////////////////////// 7 //////////////////////////////////////////////////////////////////////////// 8 9// Core Variables 10bool exit_key = false; 11const int max_bullets = 300; 12const int max_enemies = 100; 13const int tmax_speed = 8; 14const int bullet_sprite[3] = {bantibody,bbantibody,bbbantibody}; 15float max_speed = tmax_speed; 16 17// Bullet Structure 18struct bullet { 19 int type; 20 float xspeed, yspeed; 21 float x, y; 22 int sprite; 23} p_sys[max_bullets]; 24 25// Player Structure 26struct WBC { 27 float xspeed, x, y, kickback, frame; 28} player; 29 30// Enemy Structure 31struct enemy { 32 float type, x, y, xspeed, yspeed, kickback, frame, health, width, height, offset; 33 int sprite; 34} enemy[max_enemies]; 35 36// Game Variables 37int gun_type = 1; 38int gun_lvl = 1; 39int screen_x = 0; 40int screen_y = 9999999; 41int sprite_w = 9; // Bullet sprite offset 42int star_x = 0; 43int star_y = 0; 44int shipsize = 35; 45int padding = 45 + shipsize; 46 47 //////////////////////////////////////////////////////////////////////////// 48 ///////// FUNCTIONS //////////////////////////////////////////////////// 49 //////////////////////////////////////////////////////////////////////////// 50 51// Make Bullet 52void make_bullet(float xoff, float yoff, float xspeed, float yspeed) 53{ 54 xspeed += (rand()%21-10)*0.1; 55 for (int p = 0; p < max_bullets; p++) 56 { 57 if (p_sys[p].type == 0) {p_sys[p].type = gun_type; p_sys[p].xspeed = xspeed; p_sys[p].yspeed = yspeed; p_sys[p].x = int(player.x + xoff); p_sys[p].y = int(screen_y + 500 - yoff); p_sys[p].sprite = rand()%3; p = max_bullets * 2;} 58 } 59} 60 61// Control Ship 62void ship_control() 63{ 64 // Kickback from bullets 65 if (player.y < player.kickback) player.y += 10; 66 if (player.y > player.kickback) player.y = player.kickback; 67 if (player.kickback > 0) player.kickback -= 1.5; 68 69 // Get player input (left/right) 70 if (key[KEY_LEFT] && player.xspeed > -max_speed) 71 { 72 player.xspeed -= 0.2; 73 } 74 if (key[KEY_RIGHT] && player.xspeed < max_speed) 75 { 76 player.xspeed += 0.2; 77 } 78 player.x += player.xspeed; 79 80 // Edges 81 if (player.x < padding * 2 && player.xspeed < 0) {max_speed = tmax_speed * (player.x - padding)/padding; if (player.xspeed < -max_speed) player.xspeed = -max_speed;} 82 else if (player.x > 800 - (padding * 2) && player.xspeed > 0) {max_speed = tmax_speed * ((800 - player.x) - padding)/padding; if (player.xspeed > max_speed) player.xspeed = max_speed;} 83 else max_speed = tmax_speed; 84 if ((player.x < padding && player.xspeed < 0) || ((800 - player.x) < padding && player.xspeed > 0)) max_speed = 0; 85 86 // Friction 87 if (!key[KEY_LEFT] && player.xspeed < 0) {player.xspeed += 0.2; if (player.xspeed > 0) player.xspeed = 0;} 88 if (!key[KEY_RIGHT] && player.xspeed > 0) {player.xspeed -= 0.2; if (player.xspeed < 0) player.xspeed = 0;} 89 90 // Animation 91 player.frame += 0.1; 92 if (player.frame >= 3) player.frame = 0; 93} 94 95void player_input() 96{ 97 // SWITCH GUNS 98 if (key[KEY_1]) gun_lvl = 1; 99 if (key[KEY_2]) gun_lvl = 2; 100 if (key[KEY_3]) gun_lvl = 3; 101 if (key[KEY_4]) gun_lvl = 4; 102 if (key[KEY_5]) gun_lvl = 5; 103} 104 105void ship_shoot() 106{ 107 if (key[KEY_SPACE] && player.kickback <= 0) 108 { 109 // Set player offset 110 player.kickback = 30; 111 // Set Bullet Attributes 112 if (gun_lvl == 1) {make_bullet(0,0,0,-12);} 113 if (gun_lvl == 2) {make_bullet(-24,-40,0,-12); make_bullet(24,-40,0,-12);} 114 if (gun_lvl == 3) {make_bullet(-24,-40,-2,-12); make_bullet(24,-40,2,-12); make_bullet(0,0,0,-12);} 115 if (gun_lvl == 4) {make_bullet(-24,-40,-2,-12); make_bullet(24,-40,2,-12); make_bullet(-13,-40,-1,-13); make_bullet(13,-40,1,-13);} 116 if (gun_lvl == 5) {make_bullet(0,0,0,-13); make_bullet(-24,-40,-2,-12); make_bullet(24,-40,2,-12); make_bullet(-13,-40,-1,-13); make_bullet(13,-40,1,-13);} 117 } 118} 119 120// Move Background 121void move_background() 122{ 123 if (screen_x >= 0) star_x = (int(screen_x / 640) * 640) - screen_x; 124 if (screen_y >= 0) star_y = (int(screen_y / 800) * 800) - screen_y; 125 if (screen_x < 0) star_x = (int(screen_x / 640) * 640) + screen_x; 126 if (screen_y < 0) star_y = (int(screen_y / 800) * 800) + screen_y; 127} 128 129// Timer 130volatile long speed_counter = 0; 131void increment_speed_counter() 132{ 133 speed_counter++; 134} 135END_OF_FUNCTION(increment_speed_counter); 136 137// Update Variables at beginning of each game loop 138void update_variables() 139{ 140 // SPEED COUNTER 141 speed_counter --; 142 // SCREEN POSITION 143 screen_y -= 1; 144 // EXIT 145 if (key[KEY_ESC]) exit_key = true; 146} 147 148 149 //////////////////////////////////////////////////////////////////////////// 150 ///////// MAIN FUNCTION //////////////////////////////////////////////// 151 //////////////////////////////////////////////////////////////////////////// 152 153int main(void) 154{ 155 allegro_init(); // Initialize Allegro 156 install_keyboard(); // Initialize keyboard routines 157 install_timer(); // Initialize the timer routines 158 install_mouse(); // Initialize the mouse routines 159 install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT , 0); // Initialize the sound routines 160 161 LOCK_VARIABLE(speed_counter); 162 LOCK_FUNCTION(increment_speed_counter); 163 install_int_ex(increment_speed_counter, BPS_TO_TIMER(60)); 164 165 set_color_depth(32); 166 set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 800, 600, 0, 0); 167 text_mode(-1); 168 srand(5); 169 170 // DATABASE 171 DATAFILE *data; 172 data = load_datafile("data.dat"); 173 174 // Bitmaps 175 BITMAP *buffer = create_bitmap(800,600); 176 177 // CLEAR PARTICLE SYSTEM 178 for (int x = 0; x < max_bullets; x++) 179 p_sys[x].type = 0; 180 181 // CLEAR ENEMY SYSTEM 182 for (int x = 0; x < max_enemies; x++) 183 enemy[x].type = 0; 184 185 // Set Player to Center 186 player.x = 200; 187 188 //////////////////////////////////////////////////////////////////////////// 189 ///////// GAME LOOP //////////////////////////////////////////////////// 190 //////////////////////////////////////////////////////////////////////////// 191 192 193 while(!exit_key) 194 { 195 while(speed_counter > 0) 196 { 197 update_variables(); 198 199 // UPDATE BULLETS (move, draw, stop, and delete) 200 for (int x = 0; x < max_bullets; x++) 201 { 202 if (p_sys[x].type == 1) 203 { 204 p_sys[x].x += p_sys[x].xspeed; 205 p_sys[x].y += p_sys[x].yspeed; 206 if (p_sys[x].x < padding - 35 || 800 - p_sys[x].x < padding - 35) {p_sys[x].xspeed = 0; p_sys[x].yspeed = 0; p_sys[x].x += rand()%20 - 10; p_sys[x].type = -1;} 207 } 208 // Delete if off screen 209 if (p_sys[x].y + sprite_w * 2 < screen_y || p_sys[x].y - sprite_w * 2 > screen_y + 600) p_sys[x].type = 0; 210 // Blit Bullet Sprite 211 if (p_sys[x].type != 0) masked_blit((BITMAP *)data[bullet_sprite[int(p_sys[x].sprite)]].dat, buffer, 0, 0,(int(p_sys[x].x) - sprite_w) - screen_x,int(p_sys[x].y) - screen_y,62,62); 212 } 213 214 // PLAYER SHIP (draw and control) 215 masked_blit((BITMAP *)data[int(player.frame)].dat, buffer, 0, 0,int(player.x)-shipsize,500 + int(player.y),62,62); 216 ship_control(); 217 218 // GUNS (shoot and change) 219 ship_shoot(); 220 player_input(); 221 222 // SCREEN (blit and clear) 223 acquire_screen(); 224 blit(buffer, screen, 0,0,0,0,800,600); 225 release_screen(); 226 clear_to_color( buffer,makecol( 0, 0, 0) ); 227 228 // BACKGROUND (move and draw) 229 move_background(); 230 blit((BITMAP *)data[xback].dat, buffer, 0,0,0,star_y,800,800); 231 blit((BITMAP *)data[xback].dat, buffer, 0,0,0,star_y + 800,800,800); 232 } 233 234 //////////////////////////////////////////////////////////////////////////// 235 ///////// END OF GAME LOOP ///////////////////////////////////////////// 236 //////////////////////////////////////////////////////////////////////////// 237 238} 239 // Clear Data 240 destroy_bitmap(buffer); 241 unload_datafile(data); 242 243 return(0);// Exit with no errors 244} 245END_OF_MAIN();

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.

Chris Katko
Member #1,881
January 2002
avatar

Try installing the keyboard after set_gfx_mode. That's all I've got other than the possibility of an Allegro bug.

install_keyboard documentation said:

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.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Matthew Leverton
Supreme Loser
January 1999
avatar

You can install it before setting the graphics; you just might not be able to use it until after.

DanielH
Member #934
January 2001
avatar

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
2if ( blah ) {
3 int new_var;
4 if ( blah2 ) {
5 
6 }
7}
8 
9//style b
10if ( blah )
11{
12 int new_var;
13 if ( blah2 )
14 {
15 }
16}

2. Error checking is your friend

1DATAFILE *data = NULL;
2BITMAP *buffer = NULL;
3 
4// style a
5data = load_datafile( "data.dat" );
6if ( !data )
7{
8 return -1;
9 // this was an error
10}
11 
12// style b
13if ( !( 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 
22if ( buffer )
23{
24 destroy_bitmap( buffer );
25 // this next line might not be necessary
26 buffer = NULL;
27}
28 
29if ( 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.

1int 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 
14void destroy()
15{
16 if ( buffer ) destroy_bitmap();
17 if ( data ) unload_datafile():
18}
19 
20int main()
21{
22 if ( init() == 0 )
23 {
24 // loop
25 }
26 
27 destroy();
28}

Go to: