Tetris - display problem

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.

There are lots of things that don’t make sense to me about the code. I think you are on the right track with removal or commenting out of the stuff that does not need to be there untill all of the blocks can be displayed. It seems like pieces should have a color element added. Anyway if you get rid of the stuff and link or post the code I could figure it out pretty fast but it’s hurting my head as it is. The way the data is used from one array to the other may not match up and the off by 1 thing could mess u up too (have 4 of something but count 0,1,2,3,4 which is really 5 things).

There are lots of things that don’t make sense to me about the code. I think you are on the right track with removal or commenting out of the stuff that does not need to be there untill all of the blocks can be displayed. It seems like pieces should have a color element added. Anyway if you get rid of the stuff and link or post the code I could figure it out pretty fast but it’s hurting my head as it is. The way the data is used from one array to the other may not match up and the off by 1 thing could mess u up too (have 4 of something but count 0,1,2,3,4 which is really 5 things).

http://www.sendspace.com/file/54ejxr

The whole file with code and image files as asked. Hope u can find the issue. Cheers :wink:
oh, currentXPos, and currentYPos are nothing other than a value pointing to where compared to gameArea[][] is the block located, if its 0,0 that means the top left element of 4x4 block, is located in 0x0 element of gameArea[][]

Cant pass an array to a function for one unless each part is seperate. There may be a fancy way to get around that but globals that are not passed as parameters is an easy and direct way to fix it. Also this:SDL_Rect clip[COLOR_NO]; is an array that holds seven rects correct? It looks like it’s not treated like that if I’m correct.
The entire thing would be much more clear if we just use one type of array like maybe int block_id[200]; int block_type[200]; int block_row[200]; int block_cullum[200]; int block_X[200]; int block_Y[200]; int block_color_was[200]; int block_color_is[200]; int block_color_turning[200]; int block_face[200]; int block_in_motion[200]; int block_destX[200]; int block_destY[200]; and #define MAX_BLOCKS = 199; or something like that. Could just add onto pieces

edit>>
Here is a simple update to help you get rid of the Uint32 issue I had to resolve before I could get the code to rune in a msv compiler. It also makes the code more compact and simple but I use c++ not strict c so I’m not 100% that all of my code, to include this, will work for you>:
SDL_SetColorKey(item, SDL_SRCCOLORKEY, SDL_MapRGB(item->format, 0, 255, 0));
SDL_SetColorKey(frame, SDL_SRCCOLORKEY, SDL_MapRGB(frame->format, 0, 255, 0));
//notice I changed trans to green and the lines before these should be deleted

edit>>
The file has 7 sets of 4, and each of the for has 4 int components so I have no idea what the 10 by 15 block has to do with the file data. Is it a pattern that you wish to replicate with the blocks in the box somehow?

Cant pass an array to a function for one unless each part is seperate. There may be a fancy way to get around that but globals that are not passed as parameters is an easy and direct way to fix it. Also this:SDL_Rect clip[COLOR_NO]; is an array that holds seven rects correct? It looks like it’s not treated like that if I’m correct.
The entire thing would be much more clear if we just use one type of array like maybe int block_id[200]; int block_type[200]; int block_row[200]; int block_cullum[200]; int block_X[200]; int block_Y[200]; int block_color_was[200]; int block_color_is[200]; int block_color_turning[200]; int block_face[200]; int block_in_motion[200]; int block_destX[200]; int block_destY[200]; and #define MAX_BLOCKS = 199; or something like that. Could just add onto pieces

edit>>
Here is a simple update to help you get rid of the Uint32 issue I had to resolve before I could get the code to rune in a msv compiler. It also makes the code more compact and simple but I use c++ not strict c so I’m not 100% that all of my code, to include this, will work for you>:
SDL_SetColorKey(item, SDL_SRCCOLORKEY, SDL_MapRGB(item->format, 0, 255, 0));
SDL_SetColorKey(frame, SDL_SRCCOLORKEY, SDL_MapRGB(frame->format, 0, 255, 0));
//notice I changed trans to green and the lines before these should be deleted

edit>>
The file has 7 sets of 4, and each of the for has 4 int components so I have no idea what the 10 by 15 block has to do with the file data. Is it a pattern that you wish to replicate with the blocks in the box somehow?

I already stated it, there are 2 separate arrays we have to use, 1 is for the board and for the static non moving blocks which is the array 10 by 15. and the second one is a pieces array that holds the pieces. I dont really have an SDL problem i think, but the function displayMoving is causing issue’s which i cant seem to find.

And as i said earlier, the code is messy, so trans a r diffrent, they will be changed when i start cleaning up the code, so its not that important anyway. The problem is the pieces display which is somewhat related to the for loop inside displayMoving()

#1
Whut? The file has 7 sets of 4, and each of the for has 4 int components so I have no idea what the 10 by 15 game area has to do with the file data. With no understanding of what the data represents I can’t say what to do with it. What do you expect to happen with this data?

#2
You say "the “pieces” blocks dont rly display at all (movingBlocks(), cant figure out whats wrong. There are no syntax error’s"
I don’t see that displayMoving() ever gets called. What do you expect to happen with this data?