Please ignore this thread. My problem is solved. (I re-wrote the code.)
After spending a long time on my code, I got a segmentation error. My ability isn't the best... but I want this to work.
My fault occurs not while compiling, but when I run the program. There is no problem with syntax or anything, my program just crashes.
If anyone could explain how to make it work, that would be great.
Here is my code. The class "particle_array" and all its functions are in the header file and another file.
The main file:
1 | #include <allegro.h> |
2 | #include <math.h> |
3 | #include <cstdlib> |
4 | #include <stdio.h> |
5 | #include <iostream> |
6 | #include "TWOSheader.h" |
7 | |
8 | |
9 | using namespace std; |
10 | |
11 | |
12 | |
13 | bool alternate; //used for moving sand + water back and forth |
14 | |
15 | int cursorsize = 3; //size of cursor |
16 | |
17 | |
18 | |
19 | int falses; //DEGUGGING |
20 | int trues;//DEBUGGING |
21 | |
22 | const bool RIGHT = true; |
23 | const int LEFT = false; |
24 | |
25 | |
26 | |
27 | BITMAP *buffer; //The buffer. |
28 | |
29 | int currentelement = 1; // Selected Element |
30 | |
31 | void display_elements (int current) |
32 | { |
33 | rectfill(buffer,600,0,800,600,makecol(50,50,50)); |
34 | if(currentelement == 1) |
35 | { |
36 | textout_ex( buffer, font, "Wall", 605, 10, makecol( 0, 0, 0), makecol( 255, 255, 255) ); |
37 | textout_ex( buffer, font, "Sand", 605, 20, makecol( 200, 200, 200), makecol( 0,0,0) ); |
38 | textout_ex( buffer, font, "Water", 605, 30, makecol( 200, 200, 200), makecol( 0,0,0) ); |
39 | |
40 | |
41 | |
42 | } |
43 | if(currentelement == 2) |
44 | { |
45 | textout_ex( buffer, font, "Wall", 605, 10, makecol( 200, 200, 200), makecol( 0, 0, 0) ); |
46 | textout_ex( buffer, font, "Sand", 605, 20, makecol( 0, 0, 0), makecol( 255, 255, 255) ); |
47 | textout_ex( buffer, font, "Water", 605, 30, makecol( 200, 200, 200), makecol( 0,0,0) ); |
48 | |
49 | |
50 | } |
51 | if(currentelement == 3) |
52 | { |
53 | textout_ex( buffer, font, "Wall", 605, 10, makecol( 200, 200, 200), makecol( 0, 0, 0) ); |
54 | textout_ex( buffer, font, "Sand", 605, 20, makecol( 200, 200, 200), makecol( 0,0,0) ); |
55 | textout_ex( buffer, font, "Water", 605, 30, makecol( 0, 0, 0), makecol( 255,255,255) ); |
56 | |
57 | |
58 | } |
59 | } |
60 | |
61 | |
62 | void interface_mouse(int xm, int ym) |
63 | { |
64 | if(ym > 10 && ym < 20) |
65 | { |
66 | currentelement = 1; |
67 | } |
68 | else if(ym > 20 && ym < 30) |
69 | { |
70 | currentelement = 2; |
71 | } |
72 | else if(ym > 30 && ym < 40) |
73 | { |
74 | currentelement = 3; |
75 | } |
76 | } |
77 | |
78 | |
79 | |
80 | |
81 | int main(){ |
82 | |
83 | allegro_init(); |
84 | install_keyboard(); |
85 | install_mouse(); |
86 | |
87 | currentelement = 0; |
88 | |
89 | particle_array part; //A new particle array object. |
90 | |
91 | alternate = true; |
92 | |
93 | buffer = create_bitmap(800,600); |
94 | |
95 | set_color_depth(8); |
96 | set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); |
97 | |
98 | set_window_title("Sandman's Dream"); |
99 | |
100 | display_elements(1); |
101 | |
102 | |
103 | |
104 | show_mouse(buffer); |
105 | |
106 | while ( !key[KEY_ESC] ) |
107 | { |
108 | |
109 | clear_keybuf(); |
110 | |
111 | rectfill(buffer,0,0,600,600,makecol(0,0,0)); |
112 | |
113 | |
114 | |
115 | if(mouse_b & 1 && mouse_x < 600) |
116 | { |
117 | |
118 | for(int i = 0; i < cursorsize; i++) |
119 | { |
120 | for(int j = 0; j < cursorsize; j++) |
121 | { |
122 | if((mouse_x/2)-j >= 0) |
123 | { |
124 | part.particles[(int)floor(mouse_x/2)-j][(int)floor(mouse_y/2)-i][0] = currentelement; |
125 | } |
126 | } |
127 | |
128 | } |
129 | } |
130 | if(mouse_b & 1 && mouse_x > 600) |
131 | { |
132 | interface_mouse(mouse_x,mouse_y); |
133 | } |
134 | |
135 | if(mouse_b & 2 && mouse_x <600) |
136 | { |
137 | for(int i = 0; i < 10; i++) |
138 | { |
139 | for(int j = 0; j < 10; j++) |
140 | { |
141 | part.particles[(int)floor(mouse_x/2)-i+5][(int)floor(mouse_y/2)-j+5][0] = 0; |
142 | } |
143 | } |
144 | |
145 | } |
146 | if(key[KEY_1]) |
147 | { |
148 | cursorsize = 3; |
149 | } |
150 | else if(key[KEY_2]) |
151 | { |
152 | cursorsize = 6; |
153 | } |
154 | else if(key[KEY_3]) |
155 | { |
156 | cursorsize = 10; |
157 | } |
158 | for(int i = 0; i < 300; i++) |
159 | { |
160 | for(int j = 0; j < 300; j++) |
161 | { |
162 | if(part.particles[j]<i>[0]==1) |
163 | { |
164 | rectfill( buffer, j*2, i*2, j*2+1, i*2+1, makecol( 100, 100, 100)); |
165 | } |
166 | } |
167 | }alternate = !alternate; |
168 | |
169 | part.update_particles(); |
170 | part.draw_particles(buffer); |
171 | part.refreshmoved(); |
172 | |
173 | if(mouse_x > 600) |
174 | { |
175 | display_elements(currentelement); |
176 | } |
177 | |
178 | |
179 | |
180 | draw_sprite(screen, buffer, 0, 0); |
181 | |
182 | |
183 | |
184 | rest(10); |
185 | |
186 | |
187 | |
188 | } |
189 | |
190 | return 0; |
191 | |
192 | } |
193 | END_OF_MAIN(); |
Spacing may be wierd, but it shouldn't be all that hard.
Thanks in advance for looking at my code.
Im pretty lazy right now so I'm not gonna look through the whole thing, but I would suggest doing
buffer = create_bitmap(800,600);
after the call to set_gfx_mode.
Thanks for the quick response, I edited my code and recompiled, but it didn't fix the problem.
I've read somewhere that the debug will tell you the position of the bug, but I'm using Dev-Cpp and it won't tell me.
And I wouldn't use show_mouse on anything other than the screen. Use
draw_sprite(buffer, mouse_sprite, mouse_x, mouse_y);
Right before blitting the buffer to the screen. And use blit to copy the buffer to the screen, not draw_sprite (unless you want the buffer to be masked, which you usually don't.
As for the crash.. check all return values. If a function can fail, check to make sure it doesn't.
Sorry... I'm not that good at debugging...
Checking return values? How do you do that, and what exactly does it accomplish?
I just found that when I make a break point in the code, it doesn't even get into the main part of the code. (The main function. )
Checking return values? How do you do that, and what exactly does it accomplish?
When the function returns, it'll give you back a value, assuming it doesn't return void. Like create_bitmap returns a BITMAP*. If that fails, the return value will be NULL. So you'd check:
buffer = create_bitmap(800,600); if(buffer == NULL) { allegro_message("Couldn't create bitmap!"); exit(1); }
If buffer isn't NULL, then it's a valid bitmap. Check Allegro's manual for what the other functions return for failure.
could use a switch
I don't understand why you are using a three-dimensional array for your particles, but a segmentation fault could be indicative of illegal array access. Try commenting out the particle system and see if it runs, if so, then that's where your problem is and it probably has to do with going outside the array bounds.
--- Kris Asick (Gemini)
--- http://www.pixelships.com
@Kitty Cat:
Okay, I tried that, but the program doesn't get far enough along to execute the code.
@Three Harris:
I assume you mean I could use a switch in the interface display function. I'll do that, I guess it is more efficient.
@Kris Asick:
I tried disabling all the particle array stuff, and it still didn't work.
Nothing I'm trying is working... maybe something is wrong with my header file?
Header:
Other file:
Yes, I know the spacing is all wrong.
I hope there is something wrong with my header file.
(Edit: )
Now I'm thinking of just re-writing this code... it's so disorganized and horrible looking. And maybe I'd get rid of that... interesting 3-d array. (And make it a 4 dimensional array! Mehehehe! Just kidding.)
As a tip, remove the paramter from display_elements; you don't use it.
Comment out the particle_array part line, for me... That line causes your program to allocate 2.5 megabytes on the stack, which is probably the crash.
I just commented out all the particle stuff, but the error still happens...
I just commented out the header file, but the error still happens.
Trying to think of when the error started happening... It happened when I split the class into multiple files. That help?
(Edit)
Oh, thanks for the tip. Didn't realise I wasn't using it.
while(goagain == true);
Take off the semicolon, of course.
Do this exactly 1 time at the beginning of your program, not several thousand times each second... it's just giving you the same random number for that whole second.
I ran your code, and it hung at that while loop. Then it hung someplace else. I commented out the particle code from the main function, and it crashed in the rectfill call. No, I don't know why.
But I actually got a window created. If you are still having the crash before set_gfx_mode, then you need to make sure you are actually rebuilding the code.
I'm rebuilding it every time. It does not crash at the set_gfx_mode, it crashes before the first thing I put in my main function.
...since it got so much further for you, I think I should reboot my computer or something.
Rebooted, still not working. I think I'm just going to re-write it.
(EDIT)
I started re-writing my code, but immediately ran into the error again.
I have cut down the cause to this: when I make a bitmap and set it equal to create_bitmap(800,600) it gives me the segmentation fault.
Here is the code:
1 | #include <iostream> |
2 | #include "allegro.h" |
3 | //My headers |
4 | //#include "particlegrid.h" |
5 | //using namespace particles; |
6 | |
7 | int particles[300][300]; |
8 | bool particlemoved[300][300]; |
9 | |
10 | |
11 | |
12 | int main() |
13 | { |
14 | BITMAP* buffer = create_bitmap(800,600); |
15 | |
16 | allegro_init(); |
17 | install_keyboard(); |
18 | install_mouse(); |
19 | |
20 | set_color_depth(8); |
21 | set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); |
22 | |
23 | |
24 | set_window_title("Sandman's Dream"); |
25 | |
26 | while(!key[KEY_ESC]) |
27 | { |
28 | |
29 | |
30 | |
31 | |
32 | } |
33 | } |
34 | END_OF_MAIN() |
You're trying to use an Allegro function before Allegro is initialized.
Oh jeez... I feel so stupid.
Okay, thanks.
One final tip. When something doesn't work, comment EVERYTHING in your main loop, then run the program. It should run fine since, well, nothing's happening! If it doesn't, your global variable initialization code is doing things it shouldn't. Then, start uncommenting sections one by one, compiling each time to see if it still runs or not. When it finally fails, then you have a better idea of what's causing an error like that.
Oh, and you don't need the semicolon on END_OF_MAIN().
--- Kris Asick (Gemini)
--- http://www.pixelships.com
And use an IDE with an integrated debugger
... or lots of printf()'s!
I am using Dev-C++.
A segmentation error IS NOT A COMPILE ERROR.
It is fixed now, please leave this thread to die.
The only thing I saw related to compile error is the ; after END_OF_MAIN. The other tips about commenting stuff out were debugging tips.