• Advertisement
Sign in to follow this  

Tetris clone - block not drawing

This topic is 4222 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.


const int TILESIZE=30;
const int MAPWIDTH=10;
const int MAPHEIGHT=18;
const int BLANKTILE = -1;
const int REDTILE = 1;
const int BLUETILE = 2;
const int GREENTILE = 3;
const int YELLOWTILE = 4;
const int ORANGETILE = 5;

#include <allegro.h>
#include <string>
#include <cstring>
#include <vector>
#include <cstdlib>
#include "glyph.h"
#include <fstream>
#include <algorithm>

int currentVideoMode;
int musicvol;
int soundfxvol;
int exit_program = 0;
int lines = 0;
long score = 9876543;
int level = 1;
int game_state = 4;		// 0 is for menu
				// 1 is game in progress
				// 2 is for game over
				// 3 is for pause
				// 4 is for new game


int map[MAPHEIGHT][MAPWIDTH];

struct Piece {
	int matrix[4][4];
	int x;
	int y;
	int set;
};

Piece sCurrentPiece;
Piece sNextPiece;

BITMAP *buffer;
BITMAP *background;
BITMAP *blueblock;
BITMAP *redblock;
BITMAP *greenblock;
BITMAP *yellowblock;
BITMAP *orangeblock;
BITMAP *blankblock;

GLYPH_FACE *face;
GLYPH_REND *font_arial;
GLYPH_REND *font_bauhs;
GLYPH_REND *font_arialblack;
GLYPH_REND *font_sirt;

FONT *gk_arial;
FONT *gk_bauhs;
FONT *gk_arialblack;
FONT *gk_sirt;

volatile long speed_counter = 0;

void increment_speed_counter(void);
void game_init(void);
void game_engine(void);
void draw_sprites(void);
void game_exit(void);
void game_input(void);
void move_piece(int a, int b);
void game_pause(void);
void game_new(void);
void new_piece(void);
void rotate_piece(void);




//
// game init()

void game_init()
{


	set_window_title("Tetris Clone!");
	text_mode(-1);

	srand((unsigned)time(0));

	face = gk_load_face_from_file("arial.ttf",0);
	font_arial = gk_create_renderer(face,0);
	gk_rend_set_size_pixels(font_arial,18,18);
	gk_arial = gk_create_allegro_font(font_arial);

	face = gk_load_face_from_file("BAUHS93.ttf",0);
	font_bauhs = gk_create_renderer(face,0);
	gk_rend_set_size_pixels(font_bauhs,36,36);
	gk_bauhs = gk_create_allegro_font(font_bauhs);

	face = gk_load_face_from_file("ariblk.ttf",0);
	font_arialblack = gk_create_renderer(face,0);
	gk_rend_set_size_pixels(font_arialblack,64,64);
	gk_arialblack = gk_create_allegro_font(font_arialblack);

	face = gk_load_face_from_file("sirt.ttf",0);
	font_sirt = gk_create_renderer(face,0);
	gk_rend_set_size_pixels(font_sirt,36,36);
	gk_sirt = gk_create_allegro_font(font_sirt);

	buffer = create_bitmap(800,600); //Create an empty bitmap.
	background = load_bitmap("background.bmp", NULL); // Load our picture
	blueblock = load_bitmap("blueblock.bmp", NULL);
	redblock = load_bitmap("redblock.bmp", NULL);
	yellowblock = load_bitmap("yellowblock.bmp", NULL);
	greenblock = load_bitmap("greenblock.bmp", NULL);
	orangeblock = load_bitmap("orangeblock.bmp", NULL);
	blankblock = load_bitmap("blankblock.bmp", NULL);
}



//
// draw_sprites()

void draw_sprites()
{
	int topx, topy;

	topx = 250;
	topy = 35;

	blit(background, buffer, 0,0,0,0,800,600);//Draw the buffer to the screen

	for(int a = 0; a < MAPHEIGHT; a++)
	{
		for(int b = 0; b < MAPWIDTH; b++)
		{
			int block = map[a];
			switch(block)

			{

			case BLANKTILE:

				blit(blankblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case REDTILE:

				blit(redblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case BLUETILE:

				blit(blueblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case YELLOWTILE:

				blit(yellowblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case ORANGETILE:

				blit(orangeblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case GREENTILE:

				blit(greenblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			}
		}
	}

	topx = 615;
	topy = 420;

	// Show Next Piece

	for(int a = 0; a < 4; a++)
	{
		for(int b = 0; b < 4; b++)
		{
			int block = sNextPiece.matrix[a];
			switch(block)

			{

			case BLANKTILE:

				blit(blankblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case REDTILE:

				blit(redblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case BLUETILE:

				blit(blueblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case YELLOWTILE:

				blit(yellowblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case ORANGETILE:

				blit(orangeblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			case GREENTILE:

				blit(greenblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			}
		}
	}

	// Show Current Piece

	int xtopx = 250 + (TILESIZE * sCurrentPiece.x);
	int ytopy = 35 + (TILESIZE * sCurrentPiece.y);

	rect(buffer, xtopx - 1, ytopy - 1, xtopx + 121, ytopy + 121, WHITE);
	
	int blocksdrawn = 0;
	int blocksskipped = 0;
	for(int a = 0; a < 4; a++)
	{
		for(int b = 0; b < 4; b++)
		{
			int block = sCurrentPiece.matrix[a];
			switch(block)

			{

			case BLANKTILE:

				blocksdrawn ++;

				blit(blankblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);

				break;

			case REDTILE:

				blocksdrawn ++;

				blit(redblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);

				break;

			case BLUETILE:

				blocksdrawn ++;

				blit(blueblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);

				break;

			case YELLOWTILE:

				blocksdrawn ++;

				blit(yellowblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);

				break;

			case ORANGETILE:

				blocksdrawn ++;

				blit(orangeblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);

				break;

			case GREENTILE:

				blocksdrawn ++;

				blit(greenblock, buffer, 0,0,340,125,TILESIZE,TILESIZE);

				break;

			default:

				blocksskipped ++;

				break;

			}
		}
	}

	// DEBUG - Show Current Piece down below on the screen and left
	textprintf(buffer, gk_arial, 60,210,WHITE, "Blocks Drawn, %i", blocksdrawn);
	textprintf(buffer, gk_arial, 60,230,WHITE, "Blocks Skipped, %i", blocksskipped);
	textprintf(buffer, gk_arial, 60,250,WHITE, "Current Piece");
	topx = 50;
	topy = 280;
	for(int a = 0; a < 4; a++)
	{
		for(int b = 0; b < 4; b++)
		{
			int block = sCurrentPiece.matrix[a];
			switch(block)

			{

			case BLANKTILE:

				blit(blankblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);

				break;

			case REDTILE:

				blit(redblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);

				break;

			case BLUETILE:

				blit(blueblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);

				break;

			case YELLOWTILE:

				blit(yellowblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);

				break;

			case ORANGETILE:

				blit(orangeblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);

				break;

			case GREENTILE:

				blit(greenblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

				break;

			}
		}
	}


	textprintf(buffer, gk_arial, 15,160,WHITE, "Current x: %i", xtopx);
	textprintf(buffer, gk_arial, 15,180,WHITE, "Current y: %i", ytopy);

	textprintf_centre_ex(buffer, gk_sirt, 675, 60, WHITE, -1, "%d", score);
	textprintf_centre_ex(buffer, gk_sirt, 675, 165, WHITE, -1, "%i", level);
	textprintf_centre_ex(buffer, gk_sirt, 675, 275, WHITE, -1, "%i", lines);
	blit(buffer, screen, 0,0,0,0,800,600);//Draw the buffer to the screen
	clear_bitmap(buffer); // Clear the contents of the buffer bitmap
}





Sirt.zip - click to download

Share this post


Link to post
Share on other sites
Advertisement
No clues?

I've checked over this again and again this morning before work, But I just dont see what I am doing wrong to cause that block not to draw. Everything appears ok. It has to be something in draw_sprites() I would assume, since I know the piece is being generated correctly. most of the time..

Share this post


Link to post
Share on other sites
After a quick look I noticed that the cases in the switches for the current block look like:
blit(blueblock, buffer, 0,0,topx,topy,TILESIZE,TILESIZE);

While the cases for the next block look like:
blit(blueblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);.

I'm a bit tired so I didn't look too hard, but would this solve the (or one) problem?

Share this post


Link to post
Share on other sites
Actually, no it wouldn't help. The problem block is the one that is supposed to be displayed at a set of static coordinates. Its in the group with these lines:

blit(redblock, buffer, 0,0,topx + (TILESIZE * b),topy + (TILESIZE * a),TILESIZE,TILESIZE);

Its explicitly told where to draw it, but it doesnt draw at all. THe odd part is for debugging, I included an int counter, blocksdrawn. It should draw 16 blocks (blanks included) and the counter, when displayed shows 16 blocks drawn in that center section. But nothing shows up.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement