Alright, since its my first post in here, I’l say “hi”, my name is Peter, and I rly badly need your help, cus apparantly i’ve got a problem, I’ve been trying to fix for the last 3 hours.
I’m trying to make a Tetris clone game for my Programming and Data Structures labs, yet i’ve encountered a slight issue.
To explain it, I have 3 arrays, first one “gameArea[10][15]” holding data from all static blocks in the game, second being “pieces[7][4][4][4]” which is a 4 dimensional array holding single pieces and their rotation in a 4x4 array, and lastly “colorMove[4][4]” which holds the color for the current piece that is displayed. The reason why I’m using 2 diffrent arrays for roughly the same thing, is because we received the pieces file from the lecturer and are not allowed to modify it. (sucks i kno)
Now the problem is that while the “gameArea” blocks display just fine (displayBoard(); function), the “pieces” blocks dont rly display at all (movingBlocks();), cant figure out whats wrong. There are no syntax error’s,
I’m using C, since its a must on the laboratories.
Removed totally unimportant functions from the pasted code so its better readable. The randomization function was tested and it works as intended, reason why its not added.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "SDL/SDL.h"
#include "pieces.inl"
#define SCREEN_WIDTH 640 /* Resolution X */
#define SCREEN_HEIGHT 480 /* Resolution Y */
#define PIXEL_BIT 24 /* 24 - Bit Colour */
#define FAILURE 1 /* Return FALSE */
#define SUCCESS 0 /* Return TRUE */
#define BLOCK_SIZE 25 /* Single Block Size in Pixel */
#define EMPTY_BLOCK 0
#define COLOR_NO 7 /* Number of colors */
#define BOARD_X 10 /* How many blocks per board X */
#define BOARD_Y 15 /* How many blocks per board Y */
#define BOARD_START_X 51/* Top - Left corner of the Board PX Size */
#define BOARD_START_Y 51/* Top - Left corner of the Board PX Size */
#define FRAME_START_X 43
#define FRAME_START_Y 46
#define PIECES_SIZE 4
short int screenSetup();
short int imageLoad(char* image);
void imageFree();
void updateScreen();
void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip);
void setPattern(); /* Set SDL sprite sheet for block color types */
void displayBoard(); /* display static blocks */
void displayMoving(); /* display moving blocks */
void colorMoving(int which, int rotation); /* color the moving blocks, and save the data into colorMove array */
void randomFill(); /* Fill the array with some not rly random values */
int displayFrame(); /* display Frame around the game board */
int displayBackground(); /* display background */
void checkLine(); /* check if there is a full row, remove if there is */
void blockDown(); /* move all blocks down if theres a completely empty row below */
int randomNumber(int from, int to); /* Pseudo random number gen: from - to */
SDL_Surface* item = NULL;
SDL_Surface* screen = NULL;
SDL_Rect clip[COLOR_NO]; /* Values from 0 to 6 depending on Block Color - CHECK #define */
int gameArea[BOARD_X][BOARD_Y]; /* Array holding block color */
extern char pieces[7/*type*/][4/*rotation*/][4/*x*/][4/*y*/];
int currentPosX;
int currentPosY;
int colorMove[4][4]; /* Holding color for the moving blocks */
int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
if(screenSetup())
{
return FAILURE;
}
if(imageLoad("tetris_Block.bmp"))
{
return FAILURE;
}
currentPosX = 0;
currentPosY = 0;
setPattern();
randomFill();
checkLine();
blockDown();
colorMoving(0,0);
displayMoving();
displayBackground();
displayBoard();
displayFrame();
updateScreen();
SDL_Delay(2000);
imageFree();
SDL_Quit();
return SUCCESS;
}
short int imageLoad(char* image)
{
item = SDL_LoadBMP(image);
if(item == NULL)
{
return FAILURE;
}
Uint32 colorkey = SDL_MapRGB(item->format, 0, 0, 0xFF);
SDL_SetColorKey(item, SDL_SRCCOLORKEY, colorkey);
return SUCCESS;
}
void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, clip, destination, &offset);
}
void setPattern()
{
clip[0].x = 0;
clip[0].y = 0;
clip[0].w = 25;
clip[0].h = 27;
clip[1].x = 25;
clip[1].y = 0;
clip[1].w = 27;
clip[1].h = 27;
clip[2].x = 50;
clip[2].y = 0;
clip[2].w = 27;
clip[2].h = 27;
clip[3].x = 75;
clip[3].y = 0;
clip[3].w = 27;
clip[3].h = 27;
clip[4].x = 100;
clip[4].y = 0;
clip[4].w = 27;
clip[4].h = 27;
clip[5].x = 125;
clip[5].y = 0;
clip[5].w = 27;
clip[5].h = 27;
clip[6].x = 150;
clip[6].y = 0;
clip[6].w = 27;
clip[6].h = 27;
return;
}
void displayBoard() /* Display Static Blocks */
{
int counterX = 0;
int counterY = 0;
int currentLocX = BOARD_START_X;
int currentLocY = BOARD_START_Y;
for(;counterX < BOARD_X; counterX++)
{
for(;counterY < BOARD_Y; counterY++)
{
currentLocX += BLOCK_SIZE*counterX;
currentLocY += BLOCK_SIZE*counterY;
applySurface(currentLocX, currentLocY, item, screen, &clip[gameArea[counterX][counterY]]);
currentLocX = BOARD_START_X;
currentLocY = BOARD_START_Y;
}
counterY = 0;
}
return;
}
void displayMoving() /* Display Moving Blocks */
{
int counterX = 0;
int counterY = 0;
int currentLocX = BOARD_START_X + currentPosX*BLOCK_SIZE;
int currentLocY = BOARD_START_Y + currentPosY*BLOCK_SIZE;
for(;counterX < PIECES_SIZE; counterX++)
{
for(;counterY < PIECES_SIZE; counterY++)
{
currentLocX += BLOCK_SIZE*counterX;
currentLocY += BLOCK_SIZE*counterY;
applySurface(currentLocX, currentLocY, item, screen, &clip[colorMove[counterX][counterY]]);
currentLocX = BOARD_START_X + currentPosX*BLOCK_SIZE;
currentLocY = BOARD_START_Y + currentPosX*BLOCK_SIZE;
}
counterY = 0;
}
return;
}
/* The following function will fill the table that holds colors for the moving blocks */
void colorMoving(int which, int rotation)
{
int counterX = 0;
int counterY = 0;
int randomColor;
randomColor = randomNumber(1, COLOR_NO - 1);
for(;counterX < PIECES_SIZE; counterX++)
{
for(;counterY < PIECES_SIZE; counterY++)
{
if(pieces[which][rotation][counterX][counterY] != EMPTY_BLOCK)
{
colorMove[counterX][counterY] = randomColor;
}
}
counterY = 0;
}
return;
}
void randomFill() /* randomFill - fill the gameArea with faulty-random blocks */
{
int counterX = 0;
int counterY = 0;
for(;counterX < BOARD_X; counterX++)
{
for(;counterY < BOARD_Y; counterY++)
{
gameArea[counterX][counterY] = rand() % COLOR_NO;
}
counterY = 0;
}
return;
}
P.S - I know my syntax looks horrible, but its a work in progress, and i usually make things messy (like putting values into a function instead of structure) until i’ve got everything finished.