Archived

This topic is now archived and is closed to further replies.

Class Problems

This topic is 5014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright i have a class called CCube, it contains the stuff for creating a cube in by tutorial. (setting size/position, and rendering). So im following the tutorial (Andypike 4 i believe). The way it creates and object of the class (it probably isnt an object but whatever) by doing this CCube* g_cube1 = NULL; Then in win main g_cube1 = new CCube(); then call functions g_cube1->setposition(4.0, 0.0, 0.0) etc. This all compiles fine but when a run it i get one of those windows error reports... program was forced to shut down, send report blah blah blah... So basically it doesnt work, does anyone know why?

Share this post


Link to post
Share on other sites
well heres the code



#include <d3dx9.h>

LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL; // Buffer to hold vertices


struct CUSTOMVERTEX
{
FLOAT x, y, z;
DWORD colour;
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)


#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}

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

bool SetPosition(float x, float y, float z);
bool SetSize(float rWidth, float rHeight, float rDepth);
bool RenderC();

private:
bool UpdateVertices();
HRESULT CreateVertexBuffer();

LPDIRECT3DVERTEXBUFFER9 m_pVertexBuffer;

float m_rWidth;
float m_rHeight;
float m_rDepth;
float m_rX;
float m_rY;
float m_rZ;

struct CUBIOD_CUSTOMVERTEX
{
FLOAT x, y, z;
DWORD colour;
};
};
CCuboid::CCuboid()
{
m_pVertexBuffer = NULL;

//Set a default size and position

m_rWidth = 10.0;
m_rHeight = 10.0;
m_rDepth = 10.0;
m_rX = 0.0;
m_rY = 0.0;
m_rZ = 0.0;

//Initialize Vertex Buffer

if(SUCCEEDED(CreateVertexBuffer()))
{
UpdateVertices();
}
}
CCuboid::~CCuboid()
{
SafeRelease(m_pVertexBuffer);
}
HRESULT CCuboid::CreateVertexBuffer()
{
//Create the vertex buffer from our device.

if(FAILED(g_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUBIOD_CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &m_pVertexBuffer, NULL)))
{
return E_FAIL;
}

return S_OK;
}
bool CCuboid::SetSize(float rWidth, float rHeight, float rDepth)
{
m_rWidth = rWidth;
m_rHeight = rHeight;
m_rDepth = rDepth;

UpdateVertices();

return true;
}
bool CCuboid::SetPosition(float x, float y, float z)
{
m_rX = x;
m_rY = y;
m_rZ = z;

UpdateVertices();

return true;
}
bool CCuboid::UpdateVertices()
{
VOID* pVertices;

//Store each point of the cube together with it's colour

//Make sure that the points of a polygon are specified in a clockwise direction,

//this is because anti-clockwise faces will be culled

//We will use a three triangle strips to render these polygons (Top, Sides, Bottom).

CUBIOD_CUSTOMVERTEX cvVertices[] =
{
//Top Face

{m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 0 - Blue

{m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red

{m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 2 - Red

{m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 3 - Green


//Face 1

{m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 4 - Red

{m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 5 - Blue

{m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 6 - Green

{m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 7 - Red


//Face 2

{m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 8 - Blue

{m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 9 - Green


//Face 3

{m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 10 - Green

{m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 11 - Red


//Face 4

{m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 12 - Red

{m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 13 - Blue


//Bottom Face

{m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 14 - Green

{m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 15 - Blue

{m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 16 - Red

{m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 17 - Green

};

//Get a pointer to the vertex buffer vertices and lock the vertex buffer

if(FAILED(m_pVertexBuffer->Lock(0, sizeof(cvVertices), &pVertices, 0)))
{
return false;
}

//Copy our stored vertices values into the vertex buffer

memcpy(pVertices, cvVertices, sizeof(cvVertices));

//Unlock the vertex buffer

m_pVertexBuffer->Unlock();

return true;
}
////////////////////////////////////////////////////////////////////////////////////////

//Create objects

CCuboid* g_pCuboid = NULL;
////////////////////////////////////////////////////////////////////////////////////////


HRESULT InitialiseD3D(HWND hWnd)
{
//First of all, create the main D3D object. If it is created successfully we

//should get a pointer to an IDirect3D8 interface.

g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_pD3D == NULL)
{
return E_FAIL;
}

//Get the current display mode

D3DDISPLAYMODE d3ddm;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}

//Create a structure to hold the settings for our device

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));

//Fill the structure.

d3dpp.BackBufferCount= 1; //We only need a single back buffer

d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE; //No multi-sampling

d3dpp.MultiSampleQuality=0; //No multi-sampling

d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // Throw away previous frames, we don't need them

d3dpp.hDeviceWindow=hWnd; //This is our main (and only) window

d3dpp.Flags=0; //No flags to set

d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT; //Default Refresh Rate

d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT; //Default Presentation rate

d3dpp.BackBufferFormat=d3ddm.Format; //Display format

d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.EnableAutoDepthStencil = TRUE;


d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = 640;
d3dpp.BackBufferHeight = 480;

//Create a Direct3D device.

if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
{
return E_FAIL;
}

//Turn on back face culling. This is becuase we want to hide the back of our polygons

g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

//Turn off lighting becuase we are specifying that our vertices have colour

g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

//Activate depth buffer

g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

return S_OK;
}

HRESULT InitialiseVertexBuffer()
{
VOID* pVertices;

//Store each point of the cube together with it's colour

//Make sure that the points of a polygon are specified in a clockwise direction,

//this is because anti-clockwise faces will be culled

//We will use a three triangle strips to render these polygons (Top, Sides, Bottom).

CUSTOMVERTEX cvVertices[] =
{
//Top Face

{-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 0 - Blue

{-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red

{5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 2 - Red

{5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 3 - Green


//Face 1

{-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 4 - Red

{-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 5 - Blue

{5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 6 - Green

{5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 7 - Red


//Face 2

{5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 8 - Blue

{5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 9 - Green


//Face 3

{-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 10 - Green

{-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 11 - Red


//Face 4

{-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 12 - Red

{-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 13 - Blue


//Bottom Face

{5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 14 - Green

{5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 15 - Blue

{-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 16 - Red

{-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 17 - Green

};

//Create the vertex buffer from our device.

if(FAILED(g_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL)))
{
return E_FAIL;
}


//Get a pointer to the vertex buffer vertices and lock the vertex buffer

if(FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), &pVertices, 0)))
{
return E_FAIL;
}

//Copy our stored vertices values into the vertex buffer

memcpy(pVertices, cvVertices, sizeof(cvVertices));

//Unlock the vertex buffer

g_pVertexBuffer->Unlock();

return S_OK;
}


void SetupRotation()
{
//Here we will rotate our world around the x, y and z axis.

D3DXMATRIX matWorld, matWorldX, matWorldY, matWorldZ;

//Create the transformation matrices

D3DXMatrixRotationX(&matWorldX, timeGetTime()/400.0f);
D3DXMatrixRotationY(&matWorldY, timeGetTime()/400.0f);
D3DXMatrixRotationZ(&matWorldZ, timeGetTime()/400.0f);

//Combine the transformations by multiplying them together

D3DXMatrixMultiply(&matWorld, &matWorldX, &matWorldY);
D3DXMatrixMultiply(&matWorld, &matWorld, &matWorldZ);

//Apply the tansformation

g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}

void SetupCamera()
{
//Here we will setup the camera.

//The camera has three settings: "Camera Position", "Look at Position" and "Up Direction"

//We have set the following:

//Camera Position: (0, 0, -30)

//Look at Position: (0, 0, 0)

//Up direction: Y-Axis.

D3DXMATRIX matView;
D3DXVECTOR3 eye_vector,lookat_vector,up_vector;

//View point is 8 units back on the Z-axis

eye_vector=D3DXVECTOR3( 0.0f, 0.0f,-30.0f );

//We are looking towards the origin

lookat_vector=D3DXVECTOR3( 0.0f, 0.0f, 0.0f );

//The "up" direction is the positive direction on the y-axis

up_vector=D3DXVECTOR3(0.0f,1.0f,0.0f);

D3DXMatrixLookAtLH(&matView,
&eye_vector,
&lookat_vector,
&up_vector);


g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
}

void SetupPerspective()
{
//Here we specify the field of view, aspect ration and near and far clipping planes.

D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 500.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}


void Render()
{
if(g_pD3DDevice == NULL)
{
return;
}

//Clear the backbuffer to black

g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

//Begin the scene

g_pD3DDevice->BeginScene();

//Setup the rotation, camera, and perspective matrices

SetupRotation();
SetupCamera();
SetupPerspective();


//Rendering our objects

g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, 0, sizeof(CUSTOMVERTEX));
g_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //Top

g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 8); //Sides

g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2); //Bottom


//End the scene

g_pD3DDevice->EndScene();

//Filp the back and front buffers so that whatever has been rendered on the back buffer

//will now be visible on screen (front buffer).

g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}

void CleanUp()
{
SafeRelease(g_pVertexBuffer);
SafeRelease(g_pD3DDevice);
SafeRelease(g_pD3D);
}

void GameLoop()
{
//Enter the game loop

MSG msg;
BOOL fMessage;

PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);

while(msg.message != WM_QUIT)
{
fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);

if(fMessage)
{
//Process message

TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//No message to process, so render the current scene

Render();
}

}
}

//The windows message handler

LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
switch (wParam)
{
case VK_ESCAPE:
//User has pressed the escape key, so quit

DestroyWindow(hWnd);
return 0;
break;
}
break;

}

return DefWindowProc(hWnd, msg, wParam, lParam);
}

//Application entry point

INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
WNDCLASS wc;
HINSTANCE instance=GetModuleHandle(NULL);

wc.style = CS_OWNDC;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = instance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "DX Project 3";
wc.lpfnWndProc = WinProc; //An external function to handle window messages


RegisterClass(&wc);

HWND hWnd=CreateWindow("DX Project 3", //name of our registered class

"www.andypike.com: Tutorial 4", //Window name/title

WS_OVERLAPPEDWINDOW, //Style flags

0, //X position

0, //Y position

GetSystemMetrics(SM_CXSCREEN), //width of window

GetSystemMetrics(SM_CYSCREEN), //height of window

NULL, //Parent window

NULL, //Menu

instance, //application instance handle

NULL); //pointer to window-creation


ShowCursor(FALSE);

//////////////////////////////////////////////////////////////////////////

g_pCuboid = new CCuboid();
g_pCuboid->SetPosition(27.0,0.0,0.0);
//////////////////////////////////////////////////////////////////////////


//Initialize Direct3D

if(SUCCEEDED(InitialiseD3D(hWnd)))
{
//Show our window

ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);

//Initialize Vertex Buffer

if(SUCCEEDED(InitialiseVertexBuffer()))
{
//Start game running: Enter the game loop

GameLoop();
}
}

CleanUp();

UnregisterClass("DX Project 3", hInst);

return 0;
}



i "highlited" the two spots i think are problems with comments

[edited by - Squirell on March 25, 2004 9:06:38 AM]

[edited by - Squirell on March 25, 2004 9:07:25 AM]

[edited by - Squirell on March 25, 2004 9:08:46 AM]

Share this post


Link to post
Share on other sites
I see one major problem right off the bat. You create your cuboid BEFORE calling InitializeD3D. A function in the cuboid''s constructor references g_pD3D, which is NULL until InitializeD3D is called. Thus, an access violation.

Share this post


Link to post
Share on other sites