|
Tetris blocks will not display |
cdxrd
Member #7,061
March 2006
|
Alright, using c++ and allegro.. I started throwing a tetris clone together, based in part off of the article, 'Tetris in an hour'. Problem is, It sets the current and next blocks (occasionaly It likes to leave the current block blank). It draws the next block properly everytime, and it draws the current block (as part of debugging) correctly every time over on the bottom left of the screen. Right now I have it set to display the current block in the center of the screen, and I draw a rectangle around it. The rectangle draws, but the block wont draw in the center?!?!?! Also, practically 2 out of 3 runs, it just crashes, which I havn't figured out yet. I know its a lot of partial code, but could I get some help with this? Here is what I think is the relevant source code, and I have linked the zip with the graphics, full source, etc. 1
2
3const int TILESIZE=30;
4const int MAPWIDTH=10;
5const int MAPHEIGHT=18;
6const int BLANKTILE = -1;
7const int REDTILE = 1;
8const int BLUETILE = 2;
9const int GREENTILE = 3;
10const int YELLOWTILE = 4;
11const int ORANGETILE = 5;
12
13#include <allegro.h>
14#include <string>
15#include <cstring>
16#include <vector>
17#include <cstdlib>
18#include "glyph.h"
19#include <fstream>
20#include <algorithm>
21
22int currentVideoMode;
23int musicvol;
24int soundfxvol;
25int exit_program = 0;
26int lines = 0;
27long score = 9876543;
28int level = 1;
29int game_state = 4; // 0 is for menu
30 // 1 is game in progress
31 // 2 is for game over
32 // 3 is for pause
33 // 4 is for new game
34
35
36int map[MAPHEIGHT][MAPWIDTH];
37
38struct Piece {
39 int matrix[4][4];
40 int x;
41 int y;
42 int set;
43};
44
45Piece sCurrentPiece;
46Piece sNextPiece;
47
48BITMAP *buffer;
49BITMAP *background;
50BITMAP *blueblock;
51BITMAP *redblock;
52BITMAP *greenblock;
53BITMAP *yellowblock;
54BITMAP *orangeblock;
55BITMAP *blankblock;
56
57
58volatile long speed_counter = 0;
59
60void increment_speed_counter(void);
61void game_init(void);
62void game_engine(void);
63void draw_sprites(void);
64void game_exit(void);
65void game_input(void);
66void move_piece(int a, int b);
67void game_pause(void);
68void game_new(void);
69void new_piece(void);
70void rotate_piece(void);
71
72
73
74
75//
76// game init()
77
78void game_init()
79{
80
81
82 set_window_title("Tetris Clone!");
83 text_mode(-1);
84
85 srand((unsigned)time(0));
86
87 buffer = create_bitmap(800,600); //Create an empty bitmap.
88 background = load_bitmap("background.bmp", NULL); // Load our picture
89 blueblock = load_bitmap("blueblock.bmp", NULL);
90 redblock = load_bitmap("redblock.bmp", NULL);
91 yellowblock = load_bitmap("yellowblock.bmp", NULL);
92 greenblock = load_bitmap("greenblock.bmp", NULL);
93 orangeblock = load_bitmap("orangeblock.bmp", NULL);
94 blankblock = load_bitmap("blankblock.bmp", NULL);
95}
96
97
98
99//
100// draw_sprites()
101
102void draw_sprites()
103{
104 int topx, topy;
105
106 topx = 250;
107 topy = 35;
108
109 blit(background, buffer, 0,0,0,0,800,600);//Draw the buffer to the screen
110
111 // draw map to screen
112 for(int a = 0; a < MAPHEIGHT; a++)
113 {
114 for(int b = 0; b < MAPWIDTH; b++)
115 {
116 int block = map[a]<b>;
117 switch(block)
118
119 {
120
121 case BLANKTILE:
122
123 blit(blankblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
124
125 break;
126
127 case REDTILE:
128
129 blit(redblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
130
131 break;
132
133 case BLUETILE:
134
135 blit(blueblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
136
137 break;
138
139 case YELLOWTILE:
140
141 blit(yellowblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
142
143 break;
144
145 case ORANGETILE:
146
147 blit(orangeblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
148
149 break;
150
151 case GREENTILE:
152
153 blit(greenblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
154
155 break;
156
157 }
158 }
159 }
160
161 topx = 615;
162 topy = 420;
163
164 // Show Next Piece
165
166 for(int a = 0; a < 4; a++)
167 {
168 for(int b = 0; b < 4; b++)
169 {
170 int block = sNextPiece.matrix[a]<b>;
171 switch(block)
172
173 {
174
175 case BLANKTILE:
176
177 blit(blankblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
178
179 break;
180
181 case REDTILE:
182
183 blit(redblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
184
185 break;
186
187 case BLUETILE:
188
189 blit(blueblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
190
191 break;
192
193 case YELLOWTILE:
194
195 blit(yellowblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
196
197 break;
198
199 case ORANGETILE:
200
201 blit(orangeblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
202
203 break;
204
205 case GREENTILE:
206
207 blit(greenblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
208
209 break;
210
211 }
212 }
213 }
214
215 // Show Current Piece
216
217 int xtopx = 250 + (TILESIZE * sCurrentPiece.x);
218 int ytopy = 35 + (TILESIZE * sCurrentPiece.y);
219
220 rect(buffer, xtopx - 1, ytopy - 1, xtopx + 121, ytopy + 121, WHITE);
221
222 int blocksdrawn = 0;
223 int blocksskipped = 0;
224 for(int a = 0; a < 4; a++)
225 {
226 for(int b = 0; b < 4; b++)
227 {
228 int block = sCurrentPiece.matrix[a]<b>;
229 switch(block)
230
231 {
232
233 case BLANKTILE:
234
235 blocksdrawn ++;
236
237 blit(blankblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);
238
239 break;
240
241 case REDTILE:
242
243 blocksdrawn ++;
244
245 blit(redblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);
246
247 break;
248
249 case BLUETILE:
250
251 blocksdrawn ++;
252
253 blit(blueblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);
254
255 break;
256
257 case YELLOWTILE:
258
259 blocksdrawn ++;
260
261 blit(yellowblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);
262
263 break;
264
265 case ORANGETILE:
266
267 blocksdrawn ++;
268
269 blit(orangeblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);
270
271 break;
272
273 case GREENTILE:
274
275 blocksdrawn ++;
276
277 blit(greenblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);
278
279 break;
280
281 default:
282
283 blocksskipped ++;
284
285 break;
286
287 }
288 }
289 }
290
291 // DEBUG - Show Current Piece down below on the screen and left
292 textprintf(buffer, gk_arial, 60,210,WHITE, "Blocks Drawn, %i", blocksdrawn);
293 textprintf(buffer, gk_arial, 60,230,WHITE, "Blocks Skipped, %i", blocksskipped);
294 textprintf(buffer, gk_arial, 60,250,WHITE, "Current Piece");
295 topx = 50;
296 topy = 280;
297 for(int a = 0; a < 4; a++)
298 {
299 for(int b = 0; b < 4; b++)
300 {
301 int block = sCurrentPiece.matrix[a]<b>;
302 switch(block)
303
304 {
305
306 case BLANKTILE:
307
308 blit(blankblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);
309
310 break;
311
312 case REDTILE:
313
314 blit(redblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);
315
316 break;
317
318 case BLUETILE:
319
320 blit(blueblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);
321
322 break;
323
324 case YELLOWTILE:
325
326 blit(yellowblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);
327
328 break;
329
330 case ORANGETILE:
331
332 blit(orangeblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);
333
334 break;
335
336 case GREENTILE:
337
338 blit(greenblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);
339
340 break;
341
342 }
343 }
344 }
345
346
347 textprintf(buffer, gk_arial, 15,160,WHITE, "Current x: %i", xtopx);
348 textprintf(buffer, gk_arial, 15,180,WHITE, "Current y: %i", ytopy);
349
350 textprintf_centre_ex(buffer, gk_sirt, 675, 60, WHITE, -1, "%d", score);
351 textprintf_centre_ex(buffer, gk_sirt, 675, 165, WHITE, -1, "%i", level);
352 textprintf_centre_ex(buffer, gk_sirt, 675, 275, WHITE, -1, "%i", lines);
353 blit(buffer, screen, 0,0,0,0,800,600);//Draw the buffer to the screen
354 clear_bitmap(buffer); // Clear the contents of the buffer bitmap
355}
http://www.squigglyfrog.com/sirt.zip <-- full download here, 2.09 meg |
CGamesPlay
Member #2,559
July 2002
|
The part below the comment "Show Current Piece" only draws the peice in a fixed location. Is that the intent? That's all I immediately notice, but I can't really understand your entire problem. -- Ryan Patterson - <http://cgamesplay.com/> |
Myrdos
Member #1,772
December 2001
|
Quote: Also, practically 2 out of 3 runs, it just crashes, which I havn't figured out yet. Well, you don't check if your graphics loaded correctly before using them. From the manual: bmp = load_bitmap("image.pcx", palette); if (!bmp) abort_on_error("Couldn't load image.pcx!"); However, that's not it, as it would be crashing every time. It could be a divide by zero. Every time you do a division, add a little check: if ((intOne != 0) && (intTwo != 0)) intOne = intOne / intTwo; else abort_on_error("Found a divide by zero!"); But if I had to wager money, I'd say you're accessing an invalid array index, since you seem to have a lot of arrays. 1 out of 3 times the invalid index is a part of memory that contains the rest of your game. But 2 out of 3 times the memory belongs to another program, at which point the OS stops your game with a segmentation fault. __________________________________________________ |
Nils Fagerburg
Member #7,193
May 2006
|
Quote:
if ((intOne != 0) && (intTwo != 0)) intOne = intOne / intTwo; else abort_on_error("Found a divide by zero!");
You don't need to check the dividend against zero. -- |
Myrdos
Member #1,772
December 2001
|
I wasn't sure, but I thought I'd throw it in there to be safe. __________________________________________________ |
Steve++
Member #1,816
January 2002
|
coughnumeratorcough |
Nils Fagerburg
Member #7,193
May 2006
|
Quote: coughnumeratorcough Numerator and dividend are both correct terms for an expression to be divided by another, though numerator is usually more specific to fractions while dividend is a more general term. -- |
|