You are viewing the NetWars Project

The entrance (WinMain) to the application is in NetWars.cpp. It is currently utilizing DirectX, D3DX, XInput (360 controller), Box2D and eventually Enet for networking.

Last Updated 5/30/08

B2DWrapper.cpp
B2DWrapper.h
Engine.cpp
Engine.h
GameBorder.cpp
GameBorder.h
LinkedList.h
NetWars.cpp
NetWars.h
SceneObject.cpp
SceneObject.h
Shield.cpp
Shield.h
SimpleWindow.cpp
SimpleWindow.h
SpaceShip.cpp
SpaceShip.h
UserInput.cpp
UserInput.h

Zips :
The zip file contains the exe and the dll's needed to run the application.

NetWars.zip


Syntax Highlighting by: SyntaxHighlighter
//-----------------------------------------------------------------------------
// SceneObject implementation.
//-----------------------------------------------------------------------------

#include "NetWars.h"

//-----------------------------------------------------------------------------
// The SceneObject class constructor.
//-----------------------------------------------------------------------------
SceneObject::SceneObject( ) 
{
	Initialize( 0.0f, 0.0f );
}

//-----------------------------------------------------------------------------
// The SceneObject class constructor.
//-----------------------------------------------------------------------------
SceneObject::SceneObject( float x, float y ) 
{
	m_objectType = "SO";
	simplevertex vertices[] =
	{
		{ -3.0f, -3.0f, 0.0f, }, 
		{ -3.0f,  3.0f, 0.0f, },
		{  3.0f,  3.0f, 0.0f, }, 
		{  3.0f, -3.0f, 0.0f, }, 
		{ -3.0f, -3.0f, 0.0f, },  
	};

	Initialize( x, y );

	int numberOfVerticies = (int) sizeof( vertices ) / sizeof( simplevertex );
	CreateVertexBuffer( vertices, numberOfVerticies );
}

void SceneObject::Initialize( float x, float y ) 
{
	D3DXMatrixIdentity( &m_matWorld );
	D3DXMatrixIdentity( &m_matRotation );
	D3DXMatrixTranslation( &m_matTranslation, x, y, 0.0f );

	m_forceStrength = 0.0f;
	m_forceAngle = 0.0f;
	m_forceUpdated = false;

	SetUpPhysics( x, y );
}

//-----------------------------------------------------------------------------
// The SceneObject This creates the Vertex Buffers 
//-----------------------------------------------------------------------------
void SceneObject::CreateVertexBuffer( simplevertex vertices[], int numberOfVerticies )
{
	m_numberOfVerticies = numberOfVerticies;
	
	// Setting up the Vertex Buffer for the Triangle
	if( FAILED( g_Engine->GetD3DDevice()->CreateVertexBuffer( m_numberOfVerticies*sizeof(simplevertex), 0 /*Usage*/, D3DFVF_SIMPLEVERTEX, 
			D3DPOOL_DEFAULT, &m_vertexBuffer, NULL ) ) )
			MessageBox(NULL,L"Failed to create the vertex buffer!",L"InitializeSceneObject()",MB_OK);

	VOID* p_BufferedTriangleVertices;
	if( FAILED( m_vertexBuffer->Lock( 0, m_numberOfVerticies*sizeof(simplevertex), (void**)&p_BufferedTriangleVertices, 0 ) ) )
		MessageBox(NULL,L"Failed to lock the vertex buffer!",L"InitializeSceneObject()",MB_OK);

	memcpy( p_BufferedTriangleVertices, vertices, m_numberOfVerticies*sizeof(simplevertex) );

	m_vertexBuffer->Unlock();
}

//-----------------------------------------------------------------------------
// The SceneObject class destructor.
//-----------------------------------------------------------------------------
SceneObject::~SceneObject( ) 
{
	SAFE_RELEASE( m_vertexBuffer );
}

//-----------------------------------------------------------------------------
// The SceneObject rotate method
//-----------------------------------------------------------------------------
void SceneObject::Rotate( float angle ) 
{
	m_forceAngle = angle;
	D3DXMatrixRotationZ( &m_matRotation, m_forceAngle + 1.5f);
}

//-----------------------------------------------------------------------------
// The SceneObject move method
//-----------------------------------------------------------------------------
void SceneObject::Move( float x, float y ) 
{
	D3DXMatrixTranslation( &m_matTranslation, x, y, 0.0f );
}

//-----------------------------------------------------------------------------
// The SceneObject SetForce method
//-----------------------------------------------------------------------------
void SceneObject::Update( ) 
{
	if( m_physicalBody )
	{
		Move( -m_Body->GetPosition().x, m_Body->GetPosition().y );
		//Rotate( m_Body->GetAngle() );
	}
	
}


//-----------------------------------------------------------------------------
// The SceneObject AddForce method
//-----------------------------------------------------------------------------
void SceneObject::AddForce( b2Vec2 dir, float strength )
{

	//m_body->SetLinearVelocity( strength * dir ); //, b2Vec2( GetX(), GetY() ) );
	m_Body->ApplyForce( strength * dir, m_Body->GetPosition() );

	//m_forceStrength = strength;
	//m_forceUpdated = true;
}


//-----------------------------------------------------------------------------
// The SceneObject render method
//-----------------------------------------------------------------------------
bool SceneObject::Render( ) 
{

	//D3DXMatrixRotationZ( &matWorld, timeGetTime()/150.0f );
	D3DXMatrixMultiply( &m_matWorld, &m_matRotation, &m_matTranslation );
	g_Engine->GetD3DDevice()->SetTransform( D3DTS_WORLD, &m_matWorld );

	g_Engine->GetD3DDevice()->SetStreamSource( 0, m_vertexBuffer, 0, sizeof(simplevertex) );
	g_Engine->GetD3DDevice()->SetFVF( D3DFVF_SIMPLEVERTEX );
	g_Engine->GetD3DDevice()->DrawPrimitive( D3DPT_LINESTRIP, 0, m_numberOfVerticies - 1 );

	m_forceDirection = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
	m_forceUpdated = false;

	return true;

}

//-----------------------------------------------------------------------------
// Return the X position of the SceneObject
//-----------------------------------------------------------------------------
float SceneObject::GetX()
{
	return m_matTranslation._41;
}

//-----------------------------------------------------------------------------
// Return the Y position of the SceneObject
//-----------------------------------------------------------------------------
float SceneObject::GetY()
{
	return m_matTranslation._42;
}

//-----------------------------------------------------------------------------
// The SceneObject SetForce method
//-----------------------------------------------------------------------------
void SceneObject::SetForce( D3DXVECTOR3 dir, float strength )
{
	m_forceDirection = dir;
	m_forceStrength = strength;
}

//-----------------------------------------------------------------------------
// The SceneObject SetForceDir method
//-----------------------------------------------------------------------------
void SceneObject::SetForceDir( D3DXVECTOR3 dir )
{
	m_forceDirection = dir;
}

//-----------------------------------------------------------------------------
// The SceneObject GetForceDir method
//-----------------------------------------------------------------------------
D3DXVECTOR3 SceneObject::GetForceDir( )
{
	return m_forceDirection;
}

//-----------------------------------------------------------------------------
// The SceneObject GetForceDir method
//-----------------------------------------------------------------------------
float SceneObject::GetForceStrength( )
{
	return m_forceStrength;
}

//-----------------------------------------------------------------------------
// The SceneObject Method that creates the Physical body.
//-----------------------------------------------------------------------------
void SceneObject::SetUpPhysics( float x, float y )
{
	// Set up the b2D Body Def:
	b2BodyDef bodyDef;
	bodyDef.position.Set(x, y);
	bodyDef.allowSleep = false;
	
	// Set up the b2D Body Def:
	b2PolygonDef shapeDef;
	shapeDef.SetAsBox(3.0f, 3.0f);
	shapeDef.density = 0.5f;
	shapeDef.friction = 0.3f;

	g_Engine->GetB2D()->AddBody( &m_Body, &bodyDef, &shapeDef );
	
	// The default SceneBody has infinate mass so it's not 
	// necessary to update the object.
	m_physicalBody = true;

}