Original Post
To clear up any confusion: I'm using DirectX 9 - The December 2005 SDK release. Non-managed. Fixed-function pipelining. Microsoft Visual Studio 2003 C++. The problem that (I think) I am having is within the class I have written to handle all my map models and units that I'm using for a top-down 3-d strategy game. The probem I am experiencing is that when I run my program, the screen renders and I am able to see the LPDIRECT3DTEXT9 text that I have set up to draw during the renderloop, and the screen updates itself normally and shows the FPS count and everything, but the actual game-world is not rendered. The problem may be in the class itself, or in some memory management problems with me and pointers and the new operator (which I'm not quite used to doing in C++, I'm a java guy, really - but you can't do good 3-d in Java so.....). I'll post code of my class and renderloop as well as other misc. code. Any and all help is appreciated (not in the form of flames, they're only half-appreciated if they're half-helpful). Declarations (note that the Direct3d objects and device objects ARE initalized later on, but not shown here): Here's the class' source - it's probably the problem: Here's the initialization of GameObject class' objects: Here is my renderloop (I cut off the boring ending part where the text is drawn and the scene is ended and presented): Edit: I even added source code complete with a compiled EXE for people to download and see that it doesn't work. But WHY!? http://rapidshare.de/files/22410308/NSRM.rar.html [Edited by - DragonGeo2 on June 7, 2006 6:52:09 PM]
#include<d3d9.h>
#include<d3dx9.h>
#include <mmsystem.h> // Needed for the time
#include"CCamera.h"
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
#include <fstream>
using namespace std;
#include <cstdlib>
#include <time.h>
// Our vertex stucture define.
#define D3DFVF_D3DVertex (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
HWND hwnd; // Handle to the window.
// Direct3D objects.
LPDIRECT3D9 Direct3D_Object = NULL;
LPDIRECT3DDEVICE9 D3D_Device = NULL;
struct Vertex
{
FLOAT x, y, z;
DWORD color;
FLOAT tu, tv;
}; // Declarations, includes, and whatnot
class GameObject
{
protected:
Vertex* Vertices;
LPDIRECT3DVERTEXBUFFER9* VB;
bool VBCreated;
LPDIRECT3DTEXTURE9* Texture;
int Health;
int UnitType;
unsigned int SizeX;
unsigned int SizeY;
float PosX;
float PosY;
bool AliveStatus;
public:
GameObject::GameObject() // Constructor 1
{
SetVertBuffer(NULL);
SetTexture(NULL);
SetHealth(0);
UnitType = 0;
SizeX = 0;
SizeY = 0;
SetPosition(0,0);
VB = new LPDIRECT3DVERTEXBUFFER9;
Vertices = new Vertex;
Texture = new LPDIRECT3DTEXTURE9;
VBCreated = false;
}
GameObject::GameObject(LPDIRECT3DTEXTURE9* Tex)
{
SetVertBuffer(NULL);
SetTexture(Tex);
SetHealth(0);
UnitType = 0;
SizeX = 0;
SizeY = 0;
SetPosition(0,0);
VB = new LPDIRECT3DVERTEXBUFFER9;
Vertices = new Vertex;
Texture = new LPDIRECT3DTEXTURE9;
VBCreated = false;
}
GameObject::GameObject(float XPos, float YPos)
{
SetVertBuffer(NULL);
SetTexture(NULL);
SetHealth(0);
UnitType = 0;
SizeX = 0;
SizeY = 0;
SetPosition(XPos, YPos);
VB = new LPDIRECT3DVERTEXBUFFER9;
Vertices = new Vertex;
Texture = new LPDIRECT3DTEXTURE9;
VBCreated = false;
}
GameObject::GameObject(float XPos, float YPos, LPDIRECT3DTEXTURE9* Tex)
{
SetVertBuffer(NULL);
SetTexture(Tex);
SetHealth(0);
UnitType = 0;
SizeX = 0;
SizeY = 0;
SetPosition(XPos, YPos);
VB = new LPDIRECT3DVERTEXBUFFER9;
Vertices = new Vertex;
Texture = new LPDIRECT3DTEXTURE9;
VBCreated = false;
}
int GameObject::GetHealth()
{
return Health;
}
bool GameObject::SetHealth(int HP)
{
Health = HP;
return GetAliveStatus();
}
bool GameObject::GetAliveStatus()
{
return AliveStatus;
}
void GameObject::SetAliveStatus(bool Alive)
{
AliveStatus = Alive;
}
int GetUnitType()
{
return UnitType;
}
LPDIRECT3DTEXTURE9* GameObject::GetTexture()
{
return Texture;
}
LPDIRECT3DVERTEXBUFFER9* GameObject::GetVertBuffer()
{
return VB;
}
float GameObject::GetPositionX()
{
return PosX;
}
float GameObject::GetPositionY()
{
return PosY;
}
int GameObject::GetSizeX()
{
return SizeX;
}
int GameObject::GetSizeY()
{
return SizeY;
}
bool GameObject::SetPosition(float x, float y)
{
return ((SetPositionX(x)) && (SetPositionY(y)));
}
bool GameObject::SetPositionX(float x)
{
PosX = x;
return GetAliveStatus();
}
bool GameObject::SetPositionY(float y)
{
PosY = y;
return GetAliveStatus();
}
bool GameObject::SetSizeX(int x)
{
SizeX = x;
return GetAliveStatus();
}
bool GameObject::SetSizeY(int y)
{
SizeY = y;
return GetAliveStatus();
}
bool GameObject::SetSize(int x, int y)
{
return (SetSizeX(x) && SetSizeY(y));
}
bool GameObject::SetTexture(LPDIRECT3DTEXTURE9* Tex)
{
Texture = Tex;
return GetAliveStatus();
}
bool GameObject::SetVertBuffer(LPDIRECT3DVERTEXBUFFER9* VertBuffer)
{
VB = VertBuffer;
return GetAliveStatus();
}
void GameObject::Destroy()
{
SetAliveStatus(false);
SetVertBuffer(NULL);
SetTexture(NULL);
SetHealth(0);
UnitType = 0;
SizeX = 0;
SizeY = 0;
SetPosition(0,0);
}
void GameObject::Create(float PositionX, float PositionY, int Health)
{
SetPosition(PositionX, PositionY);
if (Health > 0)
{
SetHealth(Health);
SetAliveStatus(true);
}
else
{
SetHealth(Health);
SetAliveStatus(false);
}
}
bool GameObject::DrawMe(LPDIRECT3DDEVICE9 Dev)
{
if (VBCreated == false)
{
if(MakeVB(Dev))
{
VBCreated = true;
}
else
{
MessageBox(hwnd, "Could not create vertex buffer!", NULL, NULL);
}
}
else
{
}
if (VBCreated == true)
{
if (GetTexture() == NULL)
{
Dev->SetTexture(0,NULL);
//MessageBox(hwnd, "Texture not found", NULL, NULL);
}
else
{
Dev->SetTexture(0,*GetTexture());
//MessageBox(hwnd, "Texture found", NULL, NULL);
}
Dev->SetStreamSource(0, *GetVertBuffer(), 0, sizeof(Vertex));
Dev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
return true;
}
else
{
return false;
}
}
bool GameObject::MakeVB(LPDIRECT3DDEVICE9 Dev)
{
Vertex Verts[4];
// Topright Vert
Verts[0].x = float(GetPositionX() + (0.5 * GetSizeX()));
Verts[0].z = float(GetPositionY() - (0.5 * GetSizeY()));
Verts[0].tu = 0.0f;
Verts[0].tv = 1.0f;
Verts[0].y = 0.0f;
Verts[0].color = 0xffffffff;
// Topleft Vert
Verts[1].x = float(GetPositionX() - (0.5 * GetSizeX()));
Verts[1].z = float(GetPositionY() - (0.5 * GetSizeY()));
Verts[1].tu = 0.0f;
Verts[1].tv = 0.0f;
Verts[1].y = 0.0f;
Verts[1].color = 0xffffffff;
// Bottomright Vert
Verts[2].x = float(GetPositionX() + (0.5 * GetSizeX()));
Verts[2].z = float(GetPositionY() + (0.5 * GetSizeY()));
Verts[2].tu = 1.0f;
Verts[2].tv = 1.0f;
Verts[2].y = 0.0f;
Verts[2].color = 0xffffffff;
// Bottomleft Vert
Verts[3].x = float(GetPositionX() - (0.5 * GetSizeX()));
Verts[3].z = float(GetPositionY() + (0.5 * GetSizeY()));
Verts[3].tu = 1.0f;
Verts[3].tv = 0.0f;
Verts[3].y = 0.0f;
Verts[3].color = 0xffffffff;
if(FAILED(Dev->CreateVertexBuffer(sizeof(Verts),0, D3DFVF_D3DVertex,D3DPOOL_DEFAULT, GetVertBuffer(), NULL)))
return false;
if (FAILED((*GetVertBuffer())->Lock(0,sizeof(Verts), (void**)Vertices, 0)))
{
return false;
}
memcpy(Vertices, Verts, sizeof(Verts));
if (FAILED((*GetVertBuffer())->Unlock()))
return false;
return true;
}
};
GameObject* GOArray[10];
for (int x = 0; x < 10; x++)
{
GOArray[x] = new GameObject();
LPDIRECT3DTEXTURE9* TempTex = new LPDIRECT3DTEXTURE9;
if (FAILED(D3DXCreateTextureFromFile(D3D_Device, "C:\\Documents and Settings\\DragonGeo2\\My Documents\\Visual Studio Projects\\NSRM\\Debug\\Gifcloud2.tga", TempTex)))
MessageBox(hwnd, "Could not load Texture!", NULL, NULL);
GOArray[x]->SetTexture(TempTex);
GOArray->Create(float(x*5), float(5), 1);
Number_GOs += 1; // global variable
}
// Tell Direct 3D we are now going to start drawing.
D3D_Device->BeginScene();
// Set the view matrix (camera position).
SetCamera();
// This will bind the vertex data in the buffer to the Direct3D device.
D3D_Device->SetFVF(D3DFVF_D3DVertex);
for (int x = 0; x < Number_GOs; x++)
{
GOArray[x]->DrawMe(D3D_Device);
}