Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Level Editor crashes when keyboard input checked

Credits go to Arthur Kalliokoski and ReyBrujo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Level Editor crashes when keyboard input checked
Snake Snakes
Member #7,003
March 2006

Hello, i seem to have run into a problem with a pet project of mine. Any help would be greatly appreciated. This particular project is a 2D Tile Map. A multi-dimensional array is created and drawn to the buffer (double buffering). This program is the likes of a level-editor in progress. I get the typical (WinXP, "This program has encountered an error and must close, to see debug......blah blah blah") error ONLY when i include the keyboard check for the spacebar. Otherwise, there is no problem.

1#include <allegro.h> // You must include the Allegro Header file
2#include <time.h>
3#include <iostream.h>
4#include <fstream.h>
5#include <string.h>
6 
7#define WIDTH 640
8#define HEIGHT 480
9 
10volatile long speed_counter = 0; // A long integer which will store the value of the
11 // speed counter.
12void increment_speed_counter() // A function to increment the speed counter
13{
14 speed_counter++; // This will just increment the speed counter by one. :)
15}
16END_OF_FUNCTION(increment_speed_counter); // Make sure you tell it that it's the end of the
17 
18int main(int argc, char *argv[])
19{
20 bool done = false; // game state
21
22 int ii, jj; // counter variables map
23
24 short unsigned int x = 0;
25 short unsigned int y = 0;
26 
27 //int frame_counter = 0; // A counter for understanding fps
28
29 srand (time(NULL)); // seed the random number generator
30
31 allegro_init(); // Initialize Allegro
32 
33 install_keyboard(); // Initialize keyboard routines
34 install_timer(); // initialize the timer routine
35 
36 LOCK_VARIABLE(speed_counter); //Used to set the timer - which regulates the game's
37 LOCK_FUNCTION(increment_speed_counter);//speed.
38 
39 install_int_ex(increment_speed_counter, BPS_TO_TIMER(20));//Set our BPS
40 
41 set_color_depth(16); // Set the color depth
42 set_gfx_mode(GFX_AUTODETECT_WINDOWED, WIDTH, HEIGHT, 0, 0);
43 set_window_title("template");
44 
45 BITMAP *buffer = create_bitmap(WIDTH,HEIGHT);
46 BITMAP *tile_grid = load_bitmap("tile_grid.bmp", NULL);
47 BITMAP *cursor = load_bitmap("cursor.bmp", NULL);
48 BITMAP *brick = load_bitmap("brick.bmp", NULL);
49
50 char map[16][12]; // 192 tile map
51 
52 //create map
53 map[0][0] = 'b';
54 map[1][1] = 'b';
55 map[2][0] = 'b';
56 
57 bool draw = false;
58 
59 // draw the entire map array to the buffer
60
61 // main game loop
62 while (done != true)
63 {
64
65 // while loop for logic
66 while (speed_counter > 0)
67 {
68
69 // keyboard input
70 if(key[KEY_ENTER])
71 {
72 // pause the game
73 allegro_message("Game Paused");
74 }
75 if (key[KEY_RIGHT])
76 {
77 x = x + 1;
78 }
79 
80 if (key[KEY_LEFT])
81 {
82 x = x - 1;
83 }
84 
85 if (key[KEY_UP])
86 {
87 y = y - 1;
88 }
89 
90 if (key[KEY_DOWN])
91 {
92 y = y + 1;
93 }
94 
95 if (key[KEY_SPACE])
96 {
97 map[ii][jj] = 'b';
98 }
99 
100 if (key[KEY_ESC])
101 {
102 done = true;
103 }
104 
105 if (key[KEY_T])
106 {
107 draw = true;
108 }
109 if (key[KEY_F])
110 {
111 draw = false;
112 }
113 
114 
115 speed_counter--;
116 // frame_counter++;
117 
118 } // end of logic while loop
119
120 // do all graphic drawing routines after the end of the logic while loop
121 // the drawing is done immediately after the speed_counter decrement.
122 
123 // draw experimental grid to the buffer
124 draw_sprite(buffer, tile_grid, 0, 0);
125 
126 //draw tile map
127 if (draw == true)
128 {
129 for (ii=0; ii<16; ii++)
130 {
131 for (jj=0; jj<12; jj++)
132 {
133 if (map[ii][jj] == 'b')
134 {
135 draw_sprite(buffer, brick, ii*40, jj*40);
136 }
137 }
138 }
139 }
140 
141 draw_sprite(buffer, cursor, 40*x, 40*y);
142
143 // draw diagnostic information to buffer
144 if (draw == true)
145 {
146 textprintf_ex(buffer, font, 2, (HEIGHT-10), makecol(0,255,255), -1, "draw: %c", 't');
147 }
148 if (draw == false)
149 {
150 textprintf_ex(buffer, font, 2, (HEIGHT-10), makecol(0,255,255), -1, "draw: %c", 'f');
151 }
152 
153
154 // draw the buffer to the screen
155
156 blit(buffer, screen, 0,0,0,0,WIDTH,HEIGHT);
157 clear_bitmap(buffer);
158 }
159 
160 destroy_bitmap(tile_grid);
161 destroy_bitmap(buffer);
162 destroy_bitmap(cursor);
163 destroy_bitmap(brick);
164
165
166 return 0; // Exit with no errors
167}
168 
169END_OF_MAIN() // This must be called right after the closing bracket of your MAIN function.
170 // It is Allegro specific.

Specifically, the spacebar will eventually be a toggle for placing a tile on the map. why it does not work is beyond me. perhaps it has to do with how/when i am drawing the map to the buffer?

gnolam
Member #2,030
March 2002
avatar

The first thing you should do is to start checking return values.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Snake Snakes
Member #7,003
March 2006

I did include code to check for the current state of the variable, however i didn't include it because with or without the "state" checking, it still returns the error. but thank you for the tip!

ReyBrujo
Moderator
January 2001
avatar

Moved to correct forum.

To help: Download and install DrMinGW, compile your program in debug mode, run and let it crash. When the window appears with the crash report, choose Debug, and see the call stack in DrMinGW.

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

Arthur Kalliokoski
Second in Command
February 2005
avatar

As far as I can see, 'ii' & 'jj' haven't been initialized by the loop when you get to them when checking for key_space.

They all watch too much MSNBC... they get ideas.

Snake Snakes
Member #7,003
March 2006

thank you for all the replies they helped a bunch!

Go to: