Yeah Edgar durring that block of code (that appears to be working, and I removed fromt he post). I'm copying the bitmaps/id's from my brush to the map. So if I click in the bottom right of my map, it will go out of bounds, but I check for it I get no issues from that.
I'm so out of Ideas at this point I'm just going to wipe this file and start from scratch thanks for taking the time to check though.
Edit: This just got a whole lot weirder. I tracked down the problem exactly And it's... strange.
Everything works fine up until I set a map id to equal exactly 26, if I try to load a file with a 26 in it it'll crash, 25 works, 27 works, 254 works... but 26 fails.
I'm just going to post the whole damn thing. There's nothing special about 26. So confused.
edit...
Well aparently it's just when I try to load a 26, The program is acting like the file ends So al_fread16be(...)When this encounters a 26 for some reason is returning as -1, and This is what's setting the rest of my map ids out of bounds
I've tried using al_fwrite/fread32le(), as well as casting the integers I'm saving to int32_t, and int16_t... nothings working.
#SelectExpand
1
2#include "headers.h"
3extern int currentLayer
;
4CLayer::CLayer
()
5{
6
7 created
= false;
8 loaded
= false;
9 selectRectStartX
= 0;
10 selectRectStartY
= 0;
11 selectRect
= false;
12 menu
= CMenu
();
13 menu.setVisible
(true);
14 brushVisible
= false;
15 pallet.setVisible
(false);
16 map.setVisible
(false);
17
18
19
20}
21
22void CLayer::create
(int a,
int b, std::string c
)
23{
24 ALLEGRO_FILE* file
;
25 file
= al_fopen(c.c_str
(),
"r");
26 if(file
)
27 {
28 al_fclose(file
);
29 created
= true;
30 pallet.pallet.clear
();
31 pallet
= CPallet
(c
);
32 map
= CMap
(a, b
);
33
34 map.setAllBmps
(pallet.getBmp
(0));
35 brush.clear
();
36 brush.push_back
(CTile
(pallet.getBmp
(0)));
37 brush
[0].setX
(0);
38 brush
[0].setY
(0);
39
40 brushWidth
= 1;
41 brushHeight
= 1;
42 brushVisible
= false;
43 map.setVisible
(false);
44 pallet.setVisible
(false);
45 menu.setVisible
(true);
46 std::cout
<< "Created a " << a
<< "X"<< b
<< " Map for your amusement!\n";
47 }
48 else
49 {
50 std::cout
<<"Could Not Open " << c
<< " Creation Aborted!\n ";
51 }
52
53}
54
55void CLayer::save
(std::string a
)
56{
57 if (created
== true)
58 {
59 ALLEGRO_FILE* file
;
60
61 file
= al_fopen(a.c_str
(),
"w");
62
63 if(file
)
64 al_fwrite16be(file, map.getWidth
());
65 al_fwrite16be(file, map.getHeight
());
66
67 for (int h
= 0; h
<map.getHeight
(); h
++)
68 {
69 for (int w
= 0; w
<map.getWidth
(); w
++)
70 {
71 al_fwrite16be(file, map.map
[h
*map.getWidth
()+w
].getId
());
72 }
73 }
74
75 al_fclose(file
);
76 std::cout
<< "Saved " << a
<< endl
;
77 }
78}
79
80void CLayer::load
(std::string a, std::string b
)
81{
82 ALLEGRO_FILE* file
;
83 ALLEGRO_FILE* file2
;
84
85 file
= al_fopen(a.c_str
(),
"r");
86 file2
= al_fopen(b.c_str
(),
"r");
87
88 if (file
&& file2
)
89 {al_fclose(file2
);
90 {
91 int fwidth
= 0;
92 int fheight
= 0;
93
94 fwidth
= al_fread16be(file
);
95 fheight
= al_fread16be(file
);
96 create
(fwidth, fheight, b
);
97 cout
<< "created map with width " << fwidth
<< " Height " << fheight
<< " with pallet " << b
<< endl
;
98
99 for (int h
= 0; h
<map.getHeight
(); h
++)
100 {
101 for (int w
= 0; w
<map.getWidth
(); w
++)
102 {
103 map.map
[h
*map.getWidth
()+w
].setId
(al_fread16be(file
));
104 map.map
[h
*map.getWidth
()+w
].setBmp
(pallet.pallet
[map.map
[h
*map.getWidth
()+w
].getId
()].getBmp
());//HERE if map.map[h*map.getWidth()+w].getId() is 26 the program crashes, 27 works, 244 works.
105 }
106 }
107
108 al_fclose(file
);
109 std::cout
<< "Loaded Map:"<< a
<< " With Pallet :" << b
<< endl
;
110 loaded
= true;
111 }
112 }
113 else
114 std::cout
<< "Could Not Load " << a
<< " Using " << b
<< endl
;
115}
116
117
118void CLayer::draw
()
119{
120 CInputManager
& im
= im.getInstance
();
121
122 if (map.isVisible
())
123 map.draw
();
124 if (menu.isVisible
())
125 menu.draw
();
126 if(pallet.isVisible
())
127 pallet.draw
();
128 if(brushVisible
)
129 {
130 for (int i
= 0; i
< brush.size
(); i
++)
131 {
132 brush
[i
].draw
();
133 }
134 }
135 if(selectRect
)
136 {
137 al_draw_rectangle(selectRectStartX, selectRectStartY, im.getMouseX
(), im.getMouseY
(),
al_map_rgb(255,
0,
0),
4);
138 }
139}
140
141void CLayer::getInput
()
142{
143 if (created
== true)
144 {
145 CInputManager
& im
= im.getInstance
();
146
147 if (im.esc
() && !im.oldEsc
())
148 {
149 loaded
= false;
150 if (!menu.isVisible
())
151 {
152 menu.setVisible
(true);
153 map.setVisible
(false);
154 brushVisible
=false;
155 pallet.setVisible
(false);
156 }
157 else
158 {
159 menu.setVisible
(false);
160 map.setVisible
(true);
161 brushVisible
= true;
162 }
163 }
164
165 if (im.tab
() && im.oldTab
() && !menu.isVisible
())
166 {
167 pallet.setVisible
(true);
168 map.setVisible
(true);
169 brushVisible
= false;
170 menu.setVisible
(false);
171 }
172 else
173 {
174 pallet.setVisible
(false);
175 }
176
177 if (!pallet.isVisible
() && !menu.isVisible
())
178 {
179 brushVisible
= true;
180 }
181
182 if (pallet.isVisible
())
183 {
184
185 if (im.rClick
() && !im.oldRClick
())
186 {
187 //start drawing a rectangle to select multiple tiles
188 selectRect
= true;
189 selectRectStartX
= im.getMouseX
();
190 selectRectStartY
= im.getMouseY
();
191 }
192
193
194 if (!im.rClick
() && im.oldRClick
())
195 {
196 //user let go of right click, they've selected the tiles they want to be coppied to brush
197 //setting this rectangle so that rectx1/recty1 is the top left
198 if (im.getMouseX
() <= selectRectStartX
)
199 {
200 rectX1
= im.getMouseX
();
201 rectX2
= selectRectStartX
;
202 }
203
204 else
205 {
206 rectX1
= selectRectStartX
;
207 rectX2
= im.getMouseX
();
208 }
209
210 if (im.getMouseY
() <= selectRectStartY
)
211 {
212 rectY1
= im.getMouseY
();
213 rectY2
= selectRectStartY
;
214 }
215 else
216 {
217 rectY1
= selectRectStartY
;
218 rectY2
= im.getMouseY
();
219 }
220
221 brush.clear
();
222 //clear brush and add selected tiles to it.
223 for (int i
= 0; i
< pallet.pallet.size
(); i
++)
224 {
225 if (pallet.pallet
[i
].getX
() + TILE_SIZE
>= rectX1
&& pallet.pallet
[i
].getX
() <= rectX2
&&
226 pallet.pallet
[i
].getY
() + TILE_SIZE
>= rectY1
&& pallet.pallet
[i
].getY
() <= rectY2
)
227 {
228 brush.push_back
(CTile
(pallet.pallet
[i
].getBmp
()));
229
230 brush
[brush.size
()-1].setId
(i
);//HERE--- If This is 26, and I copy it to my map and save it
231 // The program crashes when I load
232 brush
[brush.size
()-1].setX
(pallet.pallet
[i
].getX
()/TILE_SIZE
);
233 brush
[brush.size
()-1].setY
(pallet.pallet
[i
].getY
()/TILE_SIZE
);
234
235 }
236 }
237
238 if (brush.size
() <1 )
239 //if for some reason brush size == 0, fill it with something
240 {
241 brush.push_back
(CTile
(pallet.pallet
[0].getBmp
()));
242 }
243
244 for (int i
= 0; i
< brush.size
(); i
++)
245 {
246 // move the brush to the top left of the screen
247 if (i
== 0)
248 {
249 moveThisFarX
= brush
[i
].getX
()/TILE_SIZE
;
250 moveThisFarY
= brush
[i
].getY
()/TILE_SIZE
;
251 }
252 brush
[i
].setX
(brush
[i
].getX
()/TILE_SIZE
- moveThisFarX
);
253 brush
[i
].setY
(brush
[i
].getY
()/TILE_SIZE
- moveThisFarY
);
254 }
255
256 brushWidth
= (brush
[brush.size
()-1].getX
()/TILE_SIZE
+1);
257 brushHeight
= (brush
[brush.size
()-1].getY
()/TILE_SIZE
+1);
258 selectRect
= false;
259 }
260 }
261
262 if (map.isVisible
())
263 {
264 if (im.lClick
() && ! im.tab
())
265 {
266 //set an area of the map to equal what brush is.
267
268 for (int h
= 0; h
<map.getHeight
(); h
++)
269 {
270 for (int w
= 0; w
<map.getWidth
(); w
++)
271
272 {
273
274 if (map.map
[h
*map.getWidth
()+w
].getX
() < im.getMouseX
() && map.map
[h
*map.getWidth
()+w
].getX
() + TILE_SIZE
> im.getMouseX
() &&
275 map.map
[h
*map.getWidth
()+w
].getY
() < im.getMouseY
() && map.map
[h
*map.getWidth
()+w
].getY
() + TILE_SIZE
> im.getMouseY
() )
276 {
277
278 for (int hh
= 0; hh
<brushHeight
; hh
++)
279 {
280 for (int ww
= 0; ww
< brushWidth
; ww
++)
281 {
282 if ((h
+hh
)*map.getWidth
()+w
+ww
< map.getWidth
()*map.getHeight
())
283 {
284
285 map.map
[ (h
+hh
)*map.getWidth
()+w
+ww
].setBmp
(brush
[hh
*brushWidth
+ww
].getBmp
());
286
287 //if (brush[hh*brushWidth+ww].getId() != 26)//##### THIS If the Id is anything
288 //other Than 26, saving/loading works as intended
289 map.map
[ (h
+hh
)*map.getWidth
()+w
+ww
].setId
(brush
[hh
*brushWidth
+ww
].getId
());
290 }
291 }
292
293 }
294
295 }
296 }
297 }
298 }
299
300 if (!im.tab
())
301 {
302 map.getInput
();
303 }
304 }
305
306 if (pallet.isVisible
())
307 {
308 if (im.lClick
() )
309 {
310 //if left clicking pallet, set brush's id to equal that pallet Id
311
312 for (int h
= 0; h
<pallet.getHeight
(); h
++)
313 {
314 for (int w
= 0; w
<pallet.getWidth
(); w
++)
315 {
316 if (pallet.pallet
[h
*pallet.getWidth
()+w
].getX
() < im.getMouseX
() && pallet.pallet
[h
*pallet.getWidth
()+w
].getX
() + TILE_SIZE
>im.getMouseX
() &&
317 pallet.pallet
[h
*pallet.getWidth
()+w
].getY
() < im.getMouseY
() && pallet.pallet
[h
*pallet.getWidth
()+w
].getY
() + TILE_SIZE
>im.getMouseY
() )
318 {
319 brush.clear
();
320 brush.push_back
(CTile
(pallet.pallet
[h
*pallet.getWidth
()+w
].getBmp
()));
321 brush
[brush.size
()-1].setId
(h
*pallet.getWidth
()+w
);
322 brushWidth
= 1;
323 brushHeight
= 1;
324 }
325
326 }
327 }
328 }
329 pallet.getInput
();
330 }
331
332 }
333 if (menu.isVisible
())
334 {
335 menu.getInput
(currentLayer
);
336 }
337}
Edit:: problem Solved in another thread, thanks guys!!