class GraphicObject
{
public:
typedef struct {
float x, y, z, rhw; //screen coords
float u, v; //tex coords
}sVertex;
float CallNumVerts( ); // Calls the number of vertex points set.
bool InitializeDirectX( ); // Initializes the direcx graphics
void SetNumVerts ( int ); // Set number of vertex points needed
void SetUpVertex ( float x, float y ); // Set up tile position
void SetTileSize ( float TileSize ); // Set the size of tiles in pixels
void CpyToVertex ( ); // Copy the data to the vertexbuffer
void DestroyGraphicObject( ); // Destroys the object
private:
sVertex *m_pVerts; // Vertex structure
float m_fSize; // Size of the tiles in pixels
int m_iNrTiles; // Numbers of tiles which has been defined
int m_iNrVerts; // Numbers of verts needed
//DX8 PARAMS
D3DPRESENT_PARAMETERS m_d3dpp;
D3DDISPLAYMODE m_d3ddm;
};
bool GraphicObject::InitializeDirectX()
{
if( ( ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) == NULL ) )
return false;
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &m_d3ddm ) ) )
return false;
ZeroMemory( &m_d3dpp, sizeof(m_d3dpp) );
m_d3dpp.Windowed = true;
m_d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
m_d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
m_d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
m_d3dpp.BackBufferFormat = m_d3ddm.Format;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_d3dpp, &g_pD3DDevice ) ) )
return false;
if( FAILED( g_pD3DDevice->CreateVertexBuffer( sizeof(sVertex) * ( m_iNrTiles * 4 ), 0, VertexFVF,
D3DPOOL_MANAGED, &g_pVB ) ) )
return false;
g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex) * ( m_iNrTiles * 4 ), 0,
VERTEXFVF, D3DPOOL_DEFAULT, &g_pVB);
D3DXCreateTextureFromFile(g_pD3DDevice, "Texture.bmp", &g_pTexture);
return true;
}
float GraphicObject::CallNumVerts()
{
return m_iNrVerts;
}
void GraphicObject::SetNumVerts( int NumberOfVertexPoints )
{
m_iNrVerts = NumberOfVertexPoints;
m_pVerts = new sVertex [ NumberOfVertexPoints ];
}
void GraphicObject::SetUpVertex( float x, float y )
{
m_pVerts[m_iNrVerts+0].x = x; m_pVerts[m_iNrVerts+1].x = x+m_fSize;
m_pVerts[m_iNrVerts+0].y = y; m_pVerts[m_iNrVerts+1].y = y;
m_pVerts[m_iNrVerts+0].z = 1.0f; m_pVerts[m_iNrVerts+1].z = 1.0f;
m_pVerts[m_iNrVerts+0].rhw = 1.0f; m_pVerts[m_iNrVerts+1].rhw = 1.0f;
m_pVerts[m_iNrVerts+0].u = 0.0f; m_pVerts[m_iNrVerts+1].u = 1.0f;
m_pVerts[m_iNrVerts+0].v = 0.0f; m_pVerts[m_iNrVerts+1].v = 0.0f;
m_pVerts[m_iNrVerts+2].x = x; m_pVerts[m_iNrVerts+3].x = x+m_fSize;
m_pVerts[m_iNrVerts+2].y = y+m_fSize; m_pVerts[m_iNrVerts+3].y = y+m_fSize;
m_pVerts[m_iNrVerts+2].z = 1.0f; m_pVerts[m_iNrVerts+3].z = 1.0f;
m_pVerts[m_iNrVerts+2].rhw = 1.0f; m_pVerts[m_iNrVerts+3].rhw = 1.0f;
m_pVerts[m_iNrVerts+2].u = 0.0f; m_pVerts[m_iNrVerts+3].u = 1.0f;
m_pVerts[m_iNrVerts+2].v = 1.0f; m_pVerts[m_iNrVerts+3].v = 1.0f;
m_iNrVerts += 4;
}
void GraphicObject::SetTileSize( float TileSize )
{
m_fSize = TileSize;
}
void GraphicObject::CpyToVertex()
{
BYTE* *Ptr;
g_pVB->Lock(0,0, (BYTE**)&Ptr, 0);
memcpy(Ptr, m_pVerts, sizeof(m_pVerts));
g_pVB->Unlock();
}
void GraphicObject::DestroyGraphicObject( int DestroyCode )
{
delete [] m_pVerts;
}
// Called here! if I remove this all is well.
bool DoInit()
{
GraphicObject *p_GO;
p_GO->SetNumVerts( 4 );
p_GO->InitializeDirectX();
p_GO->SetUpVertex( 10.0f, 10.0f );
p_GO->CpyToVertex();
p_GO->DestroyGraphicObject( DEST_ALL );
return true;
}
help with half finished class
I am currently learning DX and I thought I might do a small 2d game, here is my half finished class that should handle the graphics that I need.
The think is that every time I run the program I get a Debug error that states
"variable p_GO (thats the class) is being used without being defined."
I can anyone see what I did wrong, because I can''t.
quote:GraphicObject *p_GO; p_GO->SetNumVerts( 4 );
You should do this
GraphicObject *p_GO = new GraphicObject;
and you should remember to delete the pointer when you 're done with it, like this
delete p_GO;
Code::Blocks IDE
[edited by - mandrav on June 7, 2004 2:30:02 AM]
Are you sure you feel comfortable enough with C++ before you start working with DX and writing a game? Because dynamic memory allocation is fundamental.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement