Mouse-enabled program immediately exits due to error
David_at_wedu

Chances are, this problem reeks of newbieness -- that maybe I'm just not initializing something right -- but it's a problem nonetheless. A program I copied from the book I'm reading, for the sake of messing around with mouse input, compiles just fine, but every time I execute it, the program immediately crashes and gives me one of those "Do you wish to report this error to MS?" dialogs.

If it helps to produce a solution, I'm working on a notebook computer with Windows XP Profesional. I'm kind of baffled here. :(

Here's the code:

1#include <allegro.h>
2 
3#define WHITE makecol(255,255,255)
4#define BLACK makecol(0,0,0)
5#define RED makecol(255,0,0)
6#define GREEN makecol(0,255,0)
7#define BLUE makecol(0,0,255)
8#define SMOKE makecol(140,130,120)
9 
10// point structure used to draw lines
11typedef struct POINT
12{
13 int x, y;
14}POINT;
15 
16// points array holds do_line points for drawing a line
17POINT points[2000];
18int curpoint, totalpoints;
19 
20// bitmap images
21BITMAP* buffer;
22BITMAP* crosshair;
23BITMAP* city;
24 
25// misc variables
26int st_x,st_y,en_x,en_y;
27int done = 0;
28int destroyed = 1;
29int n;
30int mx,my,mb;
31int score = -1;
32 
33void updatescore()
34{
35 // update and display the score
36 score = score+1;
37 textprintf_right_ex(buffer, font, SCREEN_W-5, 1, WHITE, -1, "SCORE: %d", score);
38}
39 
40void explosion(BITMAP* bmp, int x, int y, int finalcolor)
41{
42 int color, size;
43 
44 for (n=0;n<20;n++)
45 {
46 // generate a random color
47 color = makecol(rand()%255,rand()%255,rand()%255);
48 // random explosion side
49 size = 20+rand()%20;
50 // draw the filled circle
51 circlefill(bmp, x, y, size, color);
52 // short pause
53 rest(2);
54 }
55 // missile tracker looks for this explosion color
56 circlefill(bmp, x, y, 40, finalcolor);
57}
58 
59void doline(BITMAP* bmp, int x, int y, int d)
60{
61 // line callback function... fills the points array
62 points[totalpoints].x = x;
63 points[totalpoints].y = y;
64 totalpoints++;
65}
66 
67void firenewmissile()
68{
69 // activate the new missile
70 destroyed = 0;
71 totalpoints = 0;
72 curpoint = 0;
73 
74 // random starting location
75 st_x = rand() % (SCREEN_W-1);
76 st_y = 20;
77 
78 // random ending location
79 en_x = rand() % (SCREEN_W-1);
80 en_y = SCREEN_H-50;
81 
82 // construct the line point-by-point
83 do_line(buffer, st_x, st_y, en_x, en_y, 0, &doline);
84}
85 
86void movemissile()
87{
88 // grab a local copy of the current point
89 int x = points[curpoint].x;
90 int y = points[curpoint].y;
91 
92 // hide mouse pointer
93 scare_mouse();
94 
95 // erase missile
96 rectfill(buffer, x-6, y-3, x+6, y+1, BLACK);
97 
98 // see if missile was hit by defense weapon
99 if (getpixel(screen, x, y) == GREEN)
100 {
101 // missile destroyed! score a point
102 destroyed++;
103 updatescore();
104 } else {
105 // no hit, just draw the missile and smoke trail
106 //draw the smoke trail
107 putpixel(buffer, x, y-3, SMOKE);
108 // draw the missle
109 circlefill(buffer, x, y, 2, BLUE);
110 }
111 // show mouse pointer
112 unscare_mouse();
113 
114 // did the missle hit a city?
115 curpoint++;
116 if (curpoint >= totalpoints)
117 {
118 // destroy the missile
119 destroyed++;
120 // animate explosion directly on screen
121 explosion(screen, x, y, BLACK);
122 // show the damage on the backbuffer
123 circlefill(buffer, x, y, 40, BLACK);
124 }
125}
126 
127 
128int main()
129{
130 allegro_init();
131 set_color_depth(16);
132 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
133 install_keyboard();
134 install_mouse();
135 install_timer();
136 srand(time(NULL));
137 
138 //create a secondary screen buffer
139 buffer = create_bitmap(640,480);
140 
141 // display title
142 textout_ex(buffer, font, "Strategic Defense (ESC to quit)", 0, 1, WHITE, -1);
143 
144 //display score
145 updatescore();
146 
147 //draw border around screen
148 rect(buffer, 0, 12, SCREEN_W-2, SCREEN_H-2, RED);
149 
150 // load and draw the city images
151 city = load_bitmap("city.bmp", NULL);
152 
153 for (n=0;n<5;n++)
154 {
155 masked_blit(city, buffer, 0, 0, 50+n*120, SCREEN_H-city->h-2, city->w, city->h);
156 }
157 
158 // load the mouse cursor
159 crosshair = load_bitmap("crosshair.bmp", NULL);
160 set_mouse_sprite(crosshair);
161 set_mouse_sprite_focus(15,15);
162 show_mouse(buffer);
163 
164 while (!key[KEY_ESC])
165 {
166 // grab the current mouse values
167 mx = mouse_x;
168 my = mouse_y;
169 mb = (mouse_b & 1);
170 
171 // fire another missile if needed
172 if (destroyed)
173 {
174 firenewmissile();
175 }
176 
177 // left mouse button, fire the defense weapon
178 if (mb)
179 {
180 explosion(screen, mx, my, GREEN);
181 }
182 
183 // update enemy missile position
184 movemissile();
185 
186 // update_screen
187 blit(buffer, screen, 0, 0, 0, 0, 640, 480);
188 
189 // pause
190 rest(10);
191 }
192 
193 set_mouse_sprite(NULL);
194 destroy_bitmap(city);
195 destroy_bitmap(crosshair);
196 allegro_exit();
197}
198END_OF_MAIN();

If anyone can help, it would be greatly appreciated! :)

miran

Why do you not do any error checking?

EDIT: Oh yeah, I see now. You're reading that book... :P

Solution: check for errors and it will soon all be clear. Most probably one or more bitmaps that you try to load can't be loaded.

Christopher Bludau

What is this famous book, that people talk about here on the boards from time to time?

gnolam

The Necronomicon All In One, by Jonathan Alhazred.

David_at_wedu
Quote:

What is this famous book, that people talk about here on the boards from time to time?

It's called Game Programming All in One: second edition and the author is Jonathan Harbour. I really don't think the book is all that bad, but I have picked up on some of what people here have been saying in regards to bad coding practices, how he's basically anti-C++ (because he probably doesn't get it), and apparently the guy's a jerk if anyone dares to call him on any of his mistakes.

But, if the book gets me at least rolling on working with Allegro -- I'm about halfway through the book -- then I'll be happy. :)

BrknPhoenix

I found the book pretty handy to get started, as well as for random things here and there, but now that I'm started I've pretty much already left that book behind :P As soon as the example programs in it started crashing I knew something was terribly wrong, hehe.

I don't know why he hates C++ though :( I went ahead and put all my allegro stuff in classes anyway, screw him >_>

David_at_wedu
Quote:

As soon as the example programs in it started crashing I knew something was terribly wrong, hehe.

I haven't really had any of the examples crash, save for a few that used variable names that were defined elsewhere already (MAX comes to mind). Other than that, I've only encountered depreciated text handling functions, which I quickly looked up in the Allegro manual and updated.

Still, I do wish the book were C++-centric, if only because I learn better off seeing examples.

Thread #586215. Printed from Allegro.cc