Original Post
ok, I can''t figure this out at all. All I want to do is make a simple object(ie square) using a class. I''m using the basic.cpp framework included with dx8. It works perfectly when kept in that file.
So, this is the object i''m making:
square.cpp
When i run this code, i get an error message that says this:
Unhandled exception in basic.exe: 0xC0000005: Access Violation
pointing at &m_pVB in
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &m_pVB ) ) )
Anyone know how to fix this?
HRESULT CMyD3DApplication::InitSquare()
{
// Initialize to render a quad
CUSTOMVERTEX cvVertices[] =
{
{ 0.0f, 0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{ 100.0f, 0.0f, 0.5f, 1.0f, 0xffff0000, },
{ 100.0f, 100.0f, 0.5f, 1.0f, 0xff0000ff, },
{ 0.0f, 100.0f, 0.5f, 1.0f, 0xff00ff00, },
};
m_dwSizeofVertices = sizeof(cvVertices);
// Create the vertex buffer
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pVB ) ) )
return E_FAIL;
BYTE* pVertices;
if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, &pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, cvVertices, m_dwSizeofVertices);
m_pVB->Unlock();
return S_OK;
}
HRESULT CMyD3DApplication::DrawSquare()
{
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
return S_OK;
}
[source]
Now, this works perfectly and i get a nice square on the screen. However, its not very usefull in making multiple square without a lot of code, so I want to make my own square class to make it easier.
I make my square.h and square.cpp files and put in the following code:
square.h
[source]
#ifndef Square_H
#define Square_H
#include <tchar.h>
#include <math.h>
#include <stdio.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "resource.h"
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex
DWORD color; // The vertex color
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
// -------------Square CLASS-----------------
class Square : public CD3DApplication
{
protected:
LPDIRECT3DVERTEXBUFFER8 m_pVB; // Buffer to hold vertices
DWORD m_dwSizeofVertices;
public:
Square();
HRESULT Init();
HRESULT Draw();
~Square();
};
#endif
|
#include "Square.h"
Square::Square()
{
m_pVB = NULL;
}
HRESULT Square::Init()
{
// Initialize to render a quad
CUSTOMVERTEX cvVertices[] =
{
{ 0.0f, 0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{ 100.0f, 0.0f, 0.5f, 1.0f, 0xffff0000, },
{ 100.0f, 100.0f, 0.5f, 1.0f, 0xff0000ff, },
{ 0.0f, 100.0f, 0.5f, 1.0f, 0xff00ff00, },
};
m_dwSizeofVertices = sizeof(cvVertices);
// Create the vertex buffer
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pVB ) ) )
return E_FAIL;
BYTE* pVertices;
if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, &pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, cvVertices, m_dwSizeofVertices);
m_pVB->Unlock();
return S_OK;
}
HRESULT Square::Draw()
{
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
return S_OK;
}
Square::~Square()
{
SAFE_RELEASE( m_pVB );
}
|