Jump to content

  • Log In with Google      Sign In   
  • Create Account


thestien

Member Since 10 Mar 2012
Offline Last Active May 21 2012 02:25 AM
-----

Topics I've Started

Examples of component design in simple game such as pong

24 April 2012 - 01:05 AM

Hi Guys n Girls,

First of all i am not asking for a detailed coded example i dont care about the specific language or graphics although if you do want to type semi c++ then i could probably understand it a little faster :)

I wouldnt say im dumb but i have been having a bit of trouble grasping the component based code and i think it would help me if you could show me how you would implement this in a simple game like pong or something else quite simple.

How i understand components is that say i have classes cMove, cDraw, cCollision ect i then have an cEntity object which takes a pointer to each component i need it to have.

so maybe
class cEntity
{
	 cPosition* coords;
	 cMove* mover;
	 cDraw* drawer;
	 cCollision* collider;
}

now whether that is right or wrong im not sure but after that i am stuck as to how to implement it do i simply make a container of components and fill it from each type of different cEntity i have so i could end up with.


// these are the walls names

componentArray[0] = topWall.cPosition;
componentArray[1] = bottomWall.cPosition;
componentArray[2] = leftWall.cPosition;
componentArray[3] = rightWall.cPosition;
// and then maybe

for (int i = 0; i < totalcomponents; i++)
{
	 componentArray[i]->whereIsMyPosition();
}

now please dont judge my code style i know it may be difficult to read but it is purley for an example im just wondering if i am on the right track or if i am not could you please show me with a similar example.

Many Thanks in advance :)

Include problems

02 April 2012 - 01:42 PM

Hi Guys,

i always seem to get this problem even after searching the web and i feel its time to get closure.

i have a few functions that i want several classes to all use. but because all the classes are linked in some way it always says these functions are already defined.
--------------------------
globals.h
somefunction();
---------------------------
CObject.h
#include "globas.h"
class CObject
{
};
-------------------------
CBullet.h
#include "CObject.h"
#include "globals.h"
class CBullet : public CObject
{
};
---------------------------
CPlayer.h
#include "CObject.h"
#include "globals.h"
class CPlayer : public CObject
{
};

main.cpp
#include "CObject.h"
#nclude "CBullet.h"
#include "CPlayer,h"
#include "globas.h"
i can get the inheritance to work but i want the classes to use stuff from the globals like constants and functions .
normally i would just stick all the classes and my globals in my main cpp but thats not easy to sort my code out when i have problems or need to change it.

many thanks in advance :)

Error when program terminates

26 March 2012 - 03:47 PM

Hi guys i have just posted another thread a min ago but i didnt want to put to many subjects in one thread so i started this one Posted Image

i am getting an error when my program shuts down
HEAP[classfull.exe]: Invalid address specified to RtlValidateHeap( 006D0000, 00593710 )
Windows has triggered a breakpoint in classfull.exe.

This may be due to a corruption of the heap, which indicates a bug in classfull.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while classfull.exe has focus.

The output window may have more diagnostic information.


i think this is happening when i add objects or frames to a parent frame( or to a child or any frame lol)
i had this error when loading only one frame with one mesh and when i commented out the addobject() i didnt get the error anymore.

class CZenFrame;
typedef int (*FRAME_MOVEMENT_CALLBACK)( CZenFrame* pFrame, void* Parameter);


class CZenFrame
{
public:
   	 CZenFrame();
		~CZenFrame();

		void* m_pParameter;

//  Variables

protected:
	D3DXMATRIX m_Local;
	D3DXVECTOR3 m_vPosition;
	D3DXVECTOR3 m_vVelocity;

	float m_Yaw, m_Pitch, m_Roll;

	CZenObject* m_pObjectList;
	CZenFrame* m_pNext;
	CZenFrame* m_pChildFrameList;
	CZenFrame* m_pParentFrame;
	FRAME_MOVEMENT_CALLBACK m_pfnCallback;
	BOOL m_bCallback;


public:
	HRESULT SetCallback( FRAME_MOVEMENT_CALLBACK pfnCallback);

	void SetVelocity( float x, float y, float z);
	void GetVelocity( float& x, float& y, float& z);

	void SetPosition( float x, float y, float z);
	void GetPosition( float& x, float& y, float& z);

	void GetLocal( D3DXMATRIX& pMatrix);

	void SetYaw( float Yaw){ m_Yaw = Yaw;}
	void GetYaw( float& Yaw){ Yaw= m_Yaw;}

	void SetPitch( float Pitch){ m_Pitch = Pitch;}
	void GetPitch( float& Pitch){ Pitch = m_Pitch;}

	void SetRoll( float Roll){ m_Roll = Roll;}
	void GetRoll( float& Roll){ Roll = m_Roll;}

	void Update();

	HRESULT AddObject( CZenObject* pNewObject);
	HRESULT Render();

	void SetNext( CZenFrame* pNext){ m_pNext = pNext;}
	CZenFrame* GetNext(){ return m_pNext;}
	HRESULT AddFrame( CZenFrame* pNewFrame);

protected:
	void SetParent( CZenFrame* pParent){ m_pParentFrame = pParent;}
	CZenFrame* GetParent(){ return m_pParentFrame;}

};


HRESULT CZenFrame::AddFrame( CZenFrame* pNewFrame)
{
	if( !pNewFrame)
	{
		SetError( "Failed to add child frame");
		return E_FAIL;
	}

	pNewFrame->SetParent( this);

	if( !m_pChildFrameList)
	{
		m_pChildFrameList = pNewFrame;
	}
	else
	{
		CZenFrame* pTempFrame = m_pChildFrameList;

		while( pTempFrame->GetNext())
			pTempFrame = pTempFrame->GetNext();

		pTempFrame->SetNext( pNewFrame);
	}

	return S_OK;
}


HRESULT CZenFrame::AddObject( CZenObject* pNewObject)
{

	if( !pNewObject)
		return E_FAIL;

	pNewObject->SetParentFrame( this);

	if( !m_pObjectList)
	{
		m_pObjectList = pNewObject;
	}
	else
	{
		CZenObject* pObject = m_pObjectList;

		while( pObject->GetNext())
			pObject = (CZenObject*)pObject->GetNext();

		pObject->SetNext( pNewObject);
	}

	return S_OK;
}


CZenFrame::~CZenFrame()
{

	CZenObject* pObject = m_pObjectList;
	CZenObject* pTemp = 0;

	while( pObject)
	{

		pTemp = (CZenObject*)pObject->GetNext();
		free( pObject);
		pObject = pTemp;

	}

}


can you see any obvious causes for this error?

many thanks in advance Posted Image

3d objects in the distance being draw in the front of closer objects

26 March 2012 - 03:33 PM

Hi guys, i have been following an example code in a book and have now finished the book and the last example is a mock solor system.

The code is Frame based the frames are Scene, Sun, Planets, Earth and Moon.

The Scene is the parent of sun which is the parent of the Planets and Earth and earth is the parent of Moon.

now all of the objects rotate around each other in the expected way and when they pass behind the Sun they are drawn behind it. the problem occurs when they pass infront of the sun. The sun is still drawn infront evan though it is smaller and further away from my view.

i dont really want to post the code as there are many lines and im not sure which parts will be relavent so could you advise me as to why this is happening? i have enabled the z buffer but i think i may not have done it correctly?

i will post any code or all of it if you think it would be easier to help me.

the Frames have objects added to them all of which are derived from a base class which has a function Render() so when i come to render them all i call the Scene's Render function which goes through the list of Frames and Objects and calls all of thier Render functions. could it be the order that they are called? or should the z buffer sort this out for me?

well thanks for your help in advance :)

Drawing lines that wont show as coloured

21 March 2012 - 05:50 PM

Hi guys only me Posted Image

Another simple program causing me headache. it is supposed to draw coloured lines but they only apprear black. ill try to only include relavent code.

int GameInit()
{
	
	HRESULT r = 0;

	g_pD3D = Direct3DCreate9( D3D_SDK_VERSION);

	if( g_pD3D == NULL)
	{
		SetError( " Could not create IDIRECT3D9 object");
		return E_FAIL;
	}

	r = InitDirect3DDevice( g_hWndMain, 640, 480, TRUE, D3DFMT_A8R8G8B8, g_pD3D, &g_pDevice);

	if( FAILED( r))
	{
		SetError( "Init of the device failed");
		return E_FAIL;
	}
	
	g_Camera.Reset();


	CreateViewport();
	SetProjectionMatrix();

	g_pDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 0, 0, 0),10.f, 0);
	r = g_pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &g_pBackSurface);

	if( FAILED( r))
	{
			SetError( "Couldnt get backbuffer");
			return E_FAIL;
	}

	
	srand( GetTickCount());
	InitTiming();

	for( int i = 0; i < g_NumPoints; i++)
	{
		g_Lines[i].SetProps(		  0,0,0,D3DCOLOR_XRGB(rand()%255,rand()%255,rand()%255),					
							  (float)(rand()%10)-rand()%10,
							  (float)(rand()%10)-rand()%10,
							  (float)(rand()%10)-rand()%10,
							  D3DCOLOR_XRGB(rand()%255, rand()%255, rand()%255));
	}

	return S_OK;
}

this should setup 300 lines of random colour all starting at the origin and extending out randomly.
g_NumPoints = 300;

int Render()
{
	//Pause(1000);
	

	HRESULT r = 0;
	g_pDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 255, 50, 50, 50),10.f, 0);

	if(!g_pDevice)
	{
		SetError( "cannot render because there is no device");
		return E_FAIL;
	}



	g_pDevice->BeginScene();

	D3DXMATRIX RotationX, RotationY, WorldMatrix;
	D3DXMatrixRotationY( &RotationY, timeGetTime()/200.0f);
	D3DXMatrixRotationX( &RotationX, timeGetTime()/800.0f);
	D3DXMatrixMultiply( &WorldMatrix, &RotationX, &RotationY);

	/*
	static D3DLIGHT9 Light;
	ZeroMemory( &Light, sizeof(D3DLIGHT9));
	Light.Type = D3DLIGHT_POINT;
	Light.Diffuse.r = 1.0f;
	Light.Diffuse.g = 1.0f;
	Light.Diffuse.b = 1.0f;
	Light.Position = D3DXVECTOR3( 0, 0, -10);
	Light.Range = 10.0f;


	g_pDevice->SetLight( 0, &Light);
	g_pDevice->LightEnable( 0, TRUE);
	g_pDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

	
	D3DMATERIAL9 Material;
	ZeroMemory( &Material, sizeof(D3DMATERIAL9));
	Material.Diffuse.r = 0.0f;
	Material.Diffuse.g = 0.5f;
	Material.Diffuse.b = 0.0f;
	Material.Diffuse.a = 1.0f;
	Material.Ambient.r = 1.0f;
	Material.Ambient.b = 0.0f;
	Material.Ambient.g = 0.0f;
	Material.Ambient.a = 1.0f;

	g_pDevice->SetMaterial( &Material);
	*/
	g_pDevice->SetFVF( ZENVERTEX_TYPE);
	g_pDevice->SetTransform( D3DTS_WORLD, &WorldMatrix);

	for( int i = 0; i < g_NumPoints; i++)
	{
		g_Lines[i].Render();
	}

	g_pDevice->EndScene();

	g_pDevice->Present( NULL, NULL, NULL, NULL);
	


	static float z = 0;
	static BOOL bForward = FALSE;
	if(bForward == TRUE)
	{
		z += 0.1f;
		if(z > 0.0f)
		{
			z = 0.0f;
			bForward=FALSE;
		}
	}
	else
	{
		z -= 0.1f;
		if(z < -50.0f)
		{
			z = -50.0f;
			bForward = TRUE;
		}
	}
	g_Camera.SetPosition(0,0,z);
	g_Camera.Update();
	
	return S_OK;

}

now this is supposed to draw those lines to the screen i have no idea whats wrong with it i have tried messing about with light and material which is commented out but i have had no luck with it :(

HRESULT CZenLine::Render()
{
	HRESULT r = 0;
	LPDIRECT3DVERTEXBUFFER9 pVB = 0;

	r = g_pDevice->CreateVertexBuffer( sizeof( CZenVertex) * 2, D3DUSAGE_WRITEONLY,
									   ZENVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB, NULL);
	if (FAILED(r))
	{
		SetError("ZenLine Dont Work");
		return E_FAIL;
	}

	BYTE* pData = 0;

	r = pVB->Lock( 0, 0, (void**)&pData, 0);
	if( FAILED(r))
	{
		pVB->Release();
		return E_FAIL;
	}
	CopyMemory( pData, (void*)&m_StartPoint, sizeof( CZenVertex));
	CopyMemory( pData + sizeof(CZenVertex), (void*)&m_EndPoint, sizeof( CZenVertex));

	pVB->Unlock();

	g_pDevice->SetStreamSource( 0, pVB, 0, sizeof( CZenVertex));
	g_pDevice->DrawPrimitive( D3DPT_LINELIST, 0, 1);
	pVB->Release();
	return S_OK;
}

this is the render function for the lines.
and finally

#define ZENVERTEX_TYPE	( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE |	D3DFVF_SPECULAR |D3DFVF_TEX1)

class CZenVertex
{
public:
	CZenVertex();
	CZenVertex( float x, float y, float z, float nx, float ny, float nz, D3DCOLOR DiffuseColor,
				D3DCOLOR SpecularColor, float tu, float tv);
	~CZenVertex();

public:
	void Set( float x, float y, float z, float nx, float ny, float nz, D3DCOLOR DiffuseColor,
				D3DCOLOR SpecularColor, float tu, float tv);

protected:

public:
	D3DVECTOR m_Position;
	D3DVECTOR m_Normal;
	D3DCOLOR m_DiffuseColor;
	D3DCOLOR m_SpecularColor;
	float m_tu, m_tv;

protected:
};

this is the vertex info.


sorry i have posted so much code but hopefully you can help me figure out what the problem is as i have had the same problem with this happening with a similar program drawing points.

thanks in advance, your help is appreciated Posted Image

PARTNERS