|
Allegro program has "stopped working" |
Logan poole
Member #13,902
January 2012
|
1#include <allegro.h>
2#include <string.h>
3void recchange();
4void fulltest();
5void rectanglemake();
6
7 BITMAP *sqr4 = create_bitmap(1200, 900); // create the 5 sqaures for text to appear on
8 BITMAP *sqr3 = create_bitmap(1200, 900);
9 BITMAP *sqr2 = create_bitmap(1200, 900);
10 BITMAP *sqr1 = create_bitmap(1200, 900);
11
12 BITMAP *A = load_bitmap("sprites/AAA.bmp", NULL); // load all of the characters from the sprites folder
13 BITMAP *B = load_bitmap("sprites/BBB.bmp", NULL); // to be used as text
14 BITMAP *C = load_bitmap("sprites/CCC.bmp", NULL);
15 BITMAP *D = load_bitmap("sprites/DDD.bmp", NULL);
16 BITMAP *E = load_bitmap("sprites/EEE.bmp", NULL);
17 BITMAP *F = load_bitmap("sprites/FFF.bmp", NULL);
18 BITMAP *G = load_bitmap("sprites/GGG.bmp", NULL);
19 BITMAP *H = load_bitmap("sprites/HHH.bmp", NULL);
20 BITMAP *I = load_bitmap("sprites/III.bmp", NULL);
21 BITMAP *J = load_bitmap("sprites/JJJ.bmp", NULL);
22 BITMAP *K = load_bitmap("sprites/KKK.bmp", NULL);
23 BITMAP *L = load_bitmap("sprites/LLL.bmp", NULL);
24 BITMAP *M = load_bitmap("sprites/MMM.bmp", NULL);
25 BITMAP *N = load_bitmap("sprites/NNN.bmp", NULL);
26 BITMAP *O = load_bitmap("sprites/OOO.bmp", NULL);
27 BITMAP *P = load_bitmap("sprites/PPP.bmp", NULL);
28 BITMAP *Q = load_bitmap("sprites/QQQ.bmp", NULL);
29 BITMAP *R = load_bitmap("sprites/RRR.bmp", NULL);
30 BITMAP *S = load_bitmap("sprites/SSS.bmp", NULL);
31 BITMAP *T = load_bitmap("sprites/TTT.bmp", NULL);
32 BITMAP *U = load_bitmap("sprites/UUU.bmp", NULL);
33 BITMAP *V = load_bitmap("sprites/VVV.bmp", NULL);
34 BITMAP *W = load_bitmap("sprites/WWW.bmp", NULL);
35 BITMAP *X = load_bitmap("sprites/XXX.bmp", NULL);
36 BITMAP *Y = load_bitmap("sprites/YYY.bmp", NULL);
37 BITMAP *Z = load_bitmap("sprites/ZZZ.bmp", NULL);
38
39
40char Sqr4[5][25] =
41 {
42 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
43 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
44 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
45 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
46 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
47 };
48 char Sqr3[5][25] =
49 {
50 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
51 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
52 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
53 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
54 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
55 };
56 char Sqr2[5][25] =
57 {
58 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
59 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
60 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
61 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
62 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
63 };
64 char Sqr1[5][25] =
65 {
66 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
67 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
68 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
69 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
70 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
71 };
72
73int main()
74 {
75 allegro_init(); // initialize allegro
76 install_mouse(); // install the mouse functions
77 install_keyboard(); // install the keyboard functions
78 set_color_depth(32); // set the color depth to 32
79 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1000, 680, 0, 0); // make the window 1000x680 and windowed
80 clear_to_color( screen, makecol( 0, 0, 0)); //make the screen background color black
81
82
83 while(!key [KEY_ESC])
84 {
85 rectanglemake();
86 }
87 destroy_bitmap(A);
88 destroy_bitmap(B);
89 destroy_bitmap(C);
90 destroy_bitmap(D);
91 destroy_bitmap(E);
92 destroy_bitmap(F);
93 destroy_bitmap(G);
94 destroy_bitmap(H);
95 destroy_bitmap(I);
96 destroy_bitmap(J);
97 destroy_bitmap(K);
98 destroy_bitmap(L);
99 destroy_bitmap(M);
100 destroy_bitmap(N);
101 destroy_bitmap(O);
102 destroy_bitmap(P);
103 destroy_bitmap(Q);
104 destroy_bitmap(R);
105 destroy_bitmap(S);
106 destroy_bitmap(T);
107 destroy_bitmap(U);
108 destroy_bitmap(V);
109 destroy_bitmap(W);
110 destroy_bitmap(X);
111 destroy_bitmap(Y);
112 destroy_bitmap(Z);
113
114 destroy_bitmap(sqr1);
115 destroy_bitmap(sqr2);
116 destroy_bitmap(sqr3);
117 destroy_bitmap(sqr4);
118 } END_OF_MAIN(); // the end of main. . .
119
120 void recchange()
121 {
122 for( int a = 0;a<=5;++a)
123 {
124 for(int b = 0;b<=25;++b)
125 {
126 Sqr3[a][b] = Sqr4[a][b];
127 }
128 }
129
130 for( int a = 0;a<=5;++a)
131 {
132 for(int b = 0;b<=25;++b)
133 {
134 Sqr2[a][b] = Sqr3[a][b];
135 }
136 }
137
138 for( int a = 0;a<=5;++a)
139 {
140 for(int b = 0;b<=25;++b)
141 {
142 Sqr1[a][b] = Sqr2[a][b];
143 }
144 }
145
146 for( int a = 0;a<=5;++a)
147 {
148 for(int b = 0;b<=25;++b)
149 {
150 Sqr1[a][b] = 0;
151 }
152 }
153
154 clear_to_color( sqr1, makecol( 0, 0, 0));
155
156
157 }
158
159 void rectanglemake()
160 {
161 rectfill(sqr4, 0, 0, 680, 150, makecol(100, 100, 100)); // 680 is width, 150 is height
162 draw_sprite(screen, sqr4, 10, 10); // 6 rows and 27 cloumns
163
164 rectfill(sqr3, 0, 0, 680, 150, makecol(100, 100, 100));
165 draw_sprite(screen, sqr3, 10, 20+150);
166
167 rectfill(sqr2, 0, 0, 680, 150, makecol(100, 100, 100));
168 draw_sprite(screen, sqr2, 10, 30+150*2);
169
170 rectfill(sqr1, 0, 0, 680, 150, makecol(100, 100, 100));
171 draw_sprite(screen, sqr1, 10, 40+150*3);
172 }
Well, the code above does not work. When I start it up it says: |
verthex
Member #11,340
September 2009
|
Are you getting a runtime error from windows? Is it a segfault? You need to pay your elves more!
|
weapon_S
Member #7,859
October 2006
|
You're loading and creating bitmaps on global scope. Things on global scope happen before normal program execution. Allegro doesn't like that. |
Logan poole
Member #13,902
January 2012
|
I don't want to have to declare them in every single function or pass them to each one... What can I do? |
Arvidsson
Member #4,603
May 2004
|
Declare them globally, but initialize them, i.e. create/load bitmaps, in a function after you initialize allegro correctly.
|
Logan poole
Member #13,902
January 2012
|
How might one go about doing that? I only know how to do it the way above. Is it like this?: A = load_bitmap("sprites/CCC.bmp", NULL);
|
Arvidsson
Member #4,603
May 2004
|
BITMAP* bmp; void load_bitmaps() { bmp = load_bitmap("blah.bmp", NULL); } int main() { init_allegro_and_whatever(); load_bitmaps(); do_whatever_you_like_now(); return 0; } Of course, you should always check for return values, i.e. did you succeed in loading the bitmap.
|
Logan poole
Member #13,902
January 2012
|
OH, so I basically declare them globally and add their values or whatever you may call it in a function. Thanks a ton! *EDIT* 1#include <allegro.h>
2#include <string.h>
3void rec_change();
4void full_test();
5void rectangle_make();
6void load_sprites();
7
8 BITMAP *sqr4; // create the 4 sqaures for text to appear on
9 BITMAP *sqr3;
10 BITMAP *sqr2;
11 BITMAP *sqr1;
12
13 BITMAP *A;
14 BITMAP *B;
15 BITMAP *C;
16 /*BITMAP *D;
17 BITMAP *E;
18 BITMAP *F;
19 BITMAP *G;
20 BITMAP *H;
21 BITMAP *I;
22 BITMAP *J;
23 BITMAP *K;
24 BITMAP *L;
25 BITMAP *M;
26 BITMAP *N;
27 BITMAP *O;
28 BITMAP *P;
29 BITMAP *Q;
30 BITMAP *R;
31 BITMAP *S;
32 BITMAP *T;
33 BITMAP *U;
34 BITMAP *V;
35 BITMAP *W;
36 BITMAP *X;
37 BITMAP *Y;
38 BITMAP *Z; */
39
40
41char Sqr4[5][25] =
42 {
43 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
44 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
45 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
46 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
47 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
48 };
49 char Sqr3[5][25] =
50 {
51 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
52 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
53 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
54 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
55 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
56 };
57 char Sqr2[5][25] =
58 {
59 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
60 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
61 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
62 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
63 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
64 };
65 char Sqr1[5][25] =
66 {
67 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
68 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
69 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
70 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
71 { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
72 };
73
74int main()
75 {
76 allegro_init(); // initialize allegro
77 install_mouse(); // install the mouse functions
78 install_keyboard(); // install the keyboard functions
79 set_color_depth(32); // set the color depth to 32
80 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1000, 680, 0, 0); // make the window 1000x680 and windowed
81 clear_to_color( screen, makecol( 0, 0, 0)); //make the screen background color black
82
83 rectangle_make();
84 load_sprites();
85 while(!key [KEY_ESC])
86 {
87 }
88 destroy_bitmap(A);
89 destroy_bitmap(B);
90 destroy_bitmap(C);
91 /*destroy_bitmap(D);
92 destroy_bitmap(E);
93 destroy_bitmap(F);
94 destroy_bitmap(G);
95 destroy_bitmap(H);
96 destroy_bitmap(I);
97 destroy_bitmap(J);
98 destroy_bitmap(K);
99 destroy_bitmap(L);
100 destroy_bitmap(M);
101 destroy_bitmap(N);
102 destroy_bitmap(O);
103 destroy_bitmap(P);
104 destroy_bitmap(Q);
105 destroy_bitmap(R);
106 destroy_bitmap(S);
107 destroy_bitmap(T);
108 destroy_bitmap(U);
109 destroy_bitmap(V);
110 destroy_bitmap(W);
111 destroy_bitmap(X);
112 destroy_bitmap(Y);
113 destroy_bitmap(Z); */
114
115 destroy_bitmap(sqr1);
116 destroy_bitmap(sqr2);
117 destroy_bitmap(sqr3);
118 destroy_bitmap(sqr4);
119
120 return 0;
121 } END_OF_MAIN(); // the end of main. . .
122
123 void rec_change() // function to move the text up to the next square
124 {
125 for( int a = 0;a<=5;++a)
126 {
127 for(int b = 0;b<=25;++b)
128 {
129 Sqr3[a][b] = Sqr4[a][b];
130 }
131 }
132 for( int a = 0;a<=5;++a)
133 {
134 for(int b = 0;b<=25;++b)
135 {
136 Sqr2[a][b] = Sqr3[a][b];
137 }
138 }
139 for( int a = 0;a<=5;++a)
140 {
141 for(int b = 0;b<=25;++b)
142 {
143 Sqr1[a][b] = Sqr2[a][b];
144 }
145 }
146 for( int a = 0;a<=5;++a)
147 {
148 for(int b = 0;b<=25;++b)
149 {
150 Sqr1[a][b] = 0;
151 }
152 }
153 clear_to_color( sqr1, makecol( 0, 0, 0));
154 }
155
156 void rectangle_make() // function to draw the sqaures
157 {
158 rectfill(sqr4, 0, 0, 680, 150, makecol(100, 100, 100)); // 680 is width, 150 is height
159 draw_sprite(screen, sqr4, 10, 10); // 5 rows and 25 cloumns
160
161 rectfill(sqr3, 0, 0, 680, 150, makecol(100, 100, 100));
162 draw_sprite(screen, sqr3, 10, 20+150);
163
164 rectfill(sqr2, 0, 0, 680, 150, makecol(100, 100, 100));
165 draw_sprite(screen, sqr2, 10, 30+150*2);
166
167 rectfill(sqr1, 0, 0, 680, 150, makecol(100, 100, 100));
168 draw_sprite(screen, sqr1, 10, 40+150*3);
169 }
170
171 void load_sprites()
172 {
173 sqr4 = create_bitmap(1200, 900);
174 sqr3 = create_bitmap(1200, 900);
175 sqr2 = create_bitmap(1200, 900);
176 sqr1 = create_bitmap(1200, 900);
177
178 A = load_bitmap("sprites/AAA.bmp", NULL);
179 B = load_bitmap("sprites/BBB.bmp", NULL);
180 C = load_bitmap("sprites/CCC.bmp", NULL);
181 }
I did all of it and even made a few changes. It still crashes, but now the allegro window comes up, not just the console. |
LennyLen
Member #5,313
December 2004
|
Logan poole said: It still crashes
Arvidsson said: Of course, you should always check for return values, i.e. did you succeed in loading the bitmap. Also, instead of having 26 variable names, have you considered having an array of 26 bitmaps? BITMAP *letter[26]; // Takes up much less space.
|
Logan poole
Member #13,902
January 2012
|
What do you mean check for return values? |
Karadoc ~~
Member #2,749
September 2002
|
Logan poole said: What do you mean check for return values? Something like this: bmp = load_bitmap("blah.bmp", NULL); if (bmp == NULL) { fprintf(stderr, "blah.bmp failed to load. I guess we can't play the game after all. Sorry guyz.\n"); exit(1); } It won't fix the problem, but it will at least help you work out what the problem is. ----------- |
Logan poole
Member #13,902
January 2012
|
Sorry but the help you gave does not help because it is crashing before even the sqr bitmaps show up, the whole screen is just black, thus I can't tell if the text has popped up or not. |
Karadoc ~~
Member #2,749
September 2002
|
... I realise that the crash is before the bitmaps show up. If the bitmaps had already showed up then it would be pointless to check if they had loaded, wouldn't it? If you have having trouble seeing the output from stderr, here are a few (independent) options:
----------- |
Logan poole
Member #13,902
January 2012
|
Found the problem. Was drawing the sqr sprites before they had been given their sprites. Thanks for the help. |
|