Table problem[found]

Started by
0 comments, last by kontrolkl 18 years, 8 months ago
I'm moving this topic here, was originaly posted on the wrong board. I'm curently making a tetris game. I have one problem, it's seem that the program have trouble to acces my table and i can't find out why. Here's the name of the 2 table i'm having problem with int m_iSquare_X_Position[4]; int m_iSquare_Y_Position[4]; I can store data in it, i get no trouble, but when i try to retrieve the variables in my table, all the case of my table are 0, but i know i did set the numbers in my table. It happen when i call Get_Position with my Draw_Squares function and Draw_Squares.

//main.cpp

#include <SDL.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <conio.h>
#include <time.h>
#include "Interface.h"
#include "Piece.h"

using namespace std;

CInterface oInterface;
CPiece oPiece;

Uint32 SDL_GetTicks(void);

int main(int argc, char *argv[])
{
	oInterface.Init_SDL();
	oInterface.Init_Images();
	oInterface.Draw_Bg();
	oPiece.Random_Piece();
	oInterface.Draw_Squares();
	int iQuit = 0;

	
	while(iQuit == 0) // While the user doens't want to quit
	{
		SDL_Event event;
		Uint8* keys;
		keys = SDL_GetKeyState(NULL);
		while ( SDL_PollEvent(&event) )
		{
			if ( event.type == SDL_QUIT )
			{  
				iQuit = 1; 
			}

			if ( event.type == SDL_KEYDOWN )
			{
				if ( event.key.keysym.sym == SDLK_ESCAPE ) 
				{ 
					iQuit = 1; 
				}
			}
		}

		
		if ( keys[SDLK_LEFT] ) 
		{ 
			oPiece.Move_Piece(1);
		}

		if ( keys[SDLK_RIGHT] ) 
		{ 
			oPiece.Move_Piece(2);
		}
		if ( keys[SDLK_DOWN] )
		{
			oPiece.Move_Piece(3);
		}
		oInterface.Draw_Scenes();
	}


	return 0;
}





//Interface.h

#pragma once
#include <SDL.h>
#include <iostream>

using namespace std;

class CInterface
{
public:
	CInterface(void);
	virtual ~CInterface(void);
	void Init_SDL(void);
	void Init_Images(void);
	void Draw_Bg(void);
	void DrawIMG(SDL_Surface *img, int x, int y);
	void Draw_Scenes(void);
	void Set_Position(int x, int y, int x2, int y2, int x3, int y3, int x4, int y4);
	int Get_Position(int i, int j); //i for X or Y, J = Table position
	void Draw_Squares();
private:
	int m_iBg_X_Position;
	int m_iBg_Y_Position;

	int m_iSquare_X_Position[4];
	int m_iSquare_Y_Position[4];

	SDL_Surface *screen;
	SDL_Surface *Background;
	
protected:
	SDL_Surface *Square[4];
};





//Interface.cpp

#include ".\interface.h"

CInterface::CInterface(void)
{
	m_iBg_X_Position = 0, m_iBg_Y_Position = 0; //X and Y Start position for the Background
	
	for (int i = 0; i < 4; i++)
	{
		m_iSquare_X_Position = 0;
	}
	for (int j = 0; j < 4; j++)
	{
		m_iSquare_Y_Position[j] = 0;
	}
}

CInterface::~CInterface(void)
{
}

void CInterface::Init_SDL(void)
{
	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0 )
	{
		cout << "Can't init SDL" << endl << SDL_GetError();
	}

	screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);

	if ( screen == NULL )
	{
		cout << "Can't set resolution to 640x480x32" << endl << SDL_GetError();
	}
}

void CInterface::Init_Images(void)
{ 
	Background = SDL_LoadBMP("Background.bmp");
	Square[0]  = SDL_LoadBMP("Square.bmp");
	Square[1]  = SDL_LoadBMP("Square2.bmp");
	Square[2]  = SDL_LoadBMP("Square3.bmp");
	Square[3]  = SDL_LoadBMP("Square4.bmp");
}

void CInterface::Draw_Bg(void)
{
	DrawIMG(Background, m_iBg_X_Position, m_iBg_Y_Position);
}

void CInterface::DrawIMG(SDL_Surface *img, int x, int y)
{
	SDL_Rect dest;
	dest.x = x;
	dest.y = y;
	SDL_BlitSurface(img, NULL, screen, &dest);
}

void CInterface::Draw_Scenes(void)
{
	Draw_Bg();
	Draw_Squares();
}

void CInterface::Set_Position(int x, int y, int x2, int y2, int x3, int y3, int x4, int y4)
{
	m_iSquare_X_Position[0] = x;
	m_iSquare_X_Position[1] = x2;
	m_iSquare_X_Position[2] = x3;
	m_iSquare_X_Position[3] = x4;

	m_iSquare_Y_Position[0] = y;
	m_iSquare_Y_Position[1] = y2;
	m_iSquare_Y_Position[2] = y3;
	m_iSquare_Y_Position[3] = y4;
}

void CInterface::Draw_Squares(void)
{
    DrawIMG(Square[0], m_iSquare_X_Position[0], m_iSquare_Y_Position[0]);
	DrawIMG(Square[1], m_iSquare_X_Position[1], m_iSquare_Y_Position[1]);
	DrawIMG(Square[2], m_iSquare_X_Position[2], m_iSquare_Y_Position[2]);
	DrawIMG(Square[3], m_iSquare_X_Position[3], m_iSquare_Y_Position[3]);
	SDL_Flip(screen);
}



int CInterface::Get_Position(int i, int j)//i for X or Y, J = Table position
{
	if ( i == 0 )//X
	{
		return m_iSquare_X_Position[j];
	}
	else //Y
	{
		return m_iSquare_Y_Position[j];
	}
}





//Piece.h

#pragma once
#include <time.h>
#include "Interface.h"

using namespace std;

class CPiece : public CInterface
{
public:
	CPiece(void);
	virtual ~CPiece(void);
	void Random_Piece(void);
	void Move_Down(void);
	void Rotate(void);
	void Fade_Color(void);
	void Move_Piece(int iDirection);
};





//Piece.cpp

#include ".\piece.h"

CPiece::CPiece(void)
{
}

CPiece::~CPiece(void)
{
}

void CPiece::Random_Piece(void)
{
	int iRandom;
	srand(time(0));
	iRandom = rand()%7+1;
	
	switch (iRandom)
	{
		case  1 : Set_Position(90,30,110,30,110,50,130,50);
			break;
		case 2 : Set_Position(110,30,130,30,90,50,110,50);
			break;
		case 3 : Set_Position(90,10,90,30,90,50,110,50);
			break;
		case 4 : Set_Position(110,10,110,30,110,50,130,50);
			break;
		case 5 : Set_Position(110,30,90,50,110,50,130,50);
			break;
		case 6 : Set_Position(110,30,130,30,110,50,130,50);
			break;
		case 7 : Set_Position(70,50,90,50,110,50,130,50);
			break;
	};

}

void CPiece::Move_Down(void)
{
}

void CPiece::Rotate(void)
{
}

void CPiece::Fade_Color(void)
{
}

void CPiece::Move_Piece(int iDirection)
{
	// 1 = left
	// 2 = Right
	// 3 = Down
	switch ( iDirection )
	{
	case 1 : Set_Position(Get_Position(0,0)-20, Get_Position(1,0),
							Get_Position(0,1)-20, Get_Position(1,1),
							Get_Position(0,2)-20, Get_Position(1,2),
							Get_Position(0,3)-20, Get_Position(1,3));
		break;
	case 2 : Set_Position(Get_Position(0,0)+20, Get_Position(1,0),
							Get_Position(0,1)+20, Get_Position(1,1),
							Get_Position(0,2)+20, Get_Position(1,2),
							Get_Position(0,3)+20, Get_Position(1,3));
		break;
	case 3 : Set_Position(Get_Position(0,0), Get_Position(1,0)+20,
							Get_Position(0,1), Get_Position(1,1)+20,
							Get_Position(0,2), Get_Position(1,2)+20,
							Get_Position(0,3), Get_Position(1,3)+20);
		break;
	};
}





[Edited by - deathwearer on August 9, 2005 10:28:41 PM]
Advertisement
Ok i found my problem. After realizing for the 30 time that i was using the same table with 2 different address, how? the table was being used by 2 different object so not the same adress. Stupid heh? :(

[Edited by - deathwearer on August 9, 2005 10:42:01 PM]

This topic is closed to new replies.

Advertisement