Continuing with my tank-game project, I managed to write my own Tank class allowing all rendering functions for an individual tank in the game to be handled by the class. Once I got the code to compile, I ran it and I was rewarded with a black screen and the tank model popping up in the middle--exactly what I wanted. However, when I added in one line to continuously increment the tank's itsAngle variable (a float value) to make it spin, I started getting this error:
Quote:Unhandled exception at 0x00411841 in TankTest1.exe: 0xC0000005: Unhandled exception at 0x00411841 in TankTest1.exe: 0xC0000005: Access violation reading location 0xcccccccc.
The last time I got an error like this, it was because my code couldn't find the mesh file. However, even when I commented out the single line that I added, I still get the error, and the program hasn't run successfully since. The debugger isn't much help, because after I get this error, Visual Studio just locks up and I have to hit Alt F4 to close it, or else my entire windows session locks and I have to log out. I'm just not sure what is causing this, since all I did was add one line to increment a float variable that the method should have access to.
The main program code in all its glory:
#include "Tank.h"
//define Tank constructors
Tank::Tank()
{
itsX = 0;
itsY = 0;
itsZ = 0;
itsAngle = 0;
itsHealth = 100;
itsTarget = NULL;
}
Tank::Tank(float x, float y, float z, float angle)
{
itsX = x;
itsY = y;
itsZ = z;
itsAngle = angle;
itsHealth = 100;
itsTarget = NULL;
}
//define Tank destructor
Tank::~Tank()
{
mesh->Release();
delete material;
}
//define Tank functions
void Tank::Fire()
{
if (itsTarget != NULL)
{
//if tank has a target, deal damage
char damage = 20;
itsTarget->SetHealth(itsTarget->GetHealth() - damage);
}
else
{/**or else do nothing**/}
}
void Tank::Init(LPDIRECT3DDEVICE9 &d3d_dev)
{
LPD3DXBUFFER bufTankMaterial;
D3DXLoadMeshFromX(L"M1A1.x",
D3DXMESH_SYSTEMMEM,
d3d_dev,
NULL,
&bufTankMaterial,
NULL,
&numMaterials,
&mesh);
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufTankMaterial->GetBufferPointer();
material = new D3DMATERIAL9[numMaterials];
for (DWORD i = 0; i < numMaterials; i++)
{
material = tempMaterials.MatD3D;
material.Ambient = material.Diffuse;
}
}
void Tank::Render(LPDIRECT3DDEVICE9 &d3d_dev)
{
D3DXMATRIX matRotateY;
D3DXMATRIX matTranslate;
//itsAngle += 0.3f; <--the line I added that broke it
D3DXMatrixRotationY(&matRotateY,itsAngle);
D3DXMatrixTranslation(&matTranslate,itsX,itsY,itsZ);
d3d_dev->SetTransform(D3DTS_WORLD, &(matRotateY*matTranslate));
for (DWORD i = 0; i < numMaterials; i++)
{
d3d_dev->SetMaterial(&material);
mesh->DrawSubset(i);
}
}
/**MAIN PROGRAM CODE STARTS HERE**/
// global variables
int const SCREEN_WIDTH = GetSystemMetrics(SM_CXSCREEN)/2;
int const SCREEN_HEIGHT = GetSystemMetrics(SM_CYSCREEN)/2;
Tank * testTank = new Tank();
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
// global DirectX objects
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL; // the pointer to the vertex buffer
LPDIRECT3DSURFACE9 z_buffer = NULL; // the pointer to the z-buffer
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam, LPARAM lParam);
// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory
void init_graphics(void); // 3D declarations
void init_light(void); // sets up the light and the material
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = L"WindowClass1";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
L"WindowClass1",
L"Tank Battle!",
WS_EX_TOPMOST | WS_POPUP,
0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
// set up and initialize Direct3D
initD3D(hWnd);
// enter the main loop:
MSG msg;
while(TRUE)
{
DWORD starting_point = GetTickCount();
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
render_frame();
// check the 'escape' key
if(KEY_DOWN(VK_ESCAPE))
PostMessage(hWnd, WM_DESTROY, 0, 0);
while ((GetTickCount() - starting_point) < 25);
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch(message)
{
// this message is read when the window is closed
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
} break;
}
// Handle any messages the switch statement didn't
return DefWindowProc (hWnd, message, wParam, lParam);
}
void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.EnableAutoDepthStencil = TRUE; // automatically run the z-buffer for us
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 16-bit pixel format for the z-buffer
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
init_graphics(); // call the function to initialize the triangle
init_light(); // call the function to initialize the light and material
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); // turn on the 3D lighting
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50)); // ambient light
return;
}
void render_frame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
// SET UP THE TRANSFORMS
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 4.0f, 8.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
SCREEN_WIDTH / SCREEN_HEIGHT, // the aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
// draw the tank
testTank->Render(d3ddev);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
return;
}
void cleanD3D(void)
{
delete testTank;
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D
return;
}
void init_graphics(void)
{
testTank->Init(d3ddev);
return;
}
void init_light(void)
{
D3DLIGHT9 light; // create the light struct
ZeroMemory(&light, sizeof(light)); // clear out the struct for use
light.Type = D3DLIGHT_DIRECTIONAL; // make the light type 'directional light'
light.Diffuse.r = 0.5f; // .5 red
light.Diffuse.g = 0.5f; // .5 green
light.Diffuse.b = 0.5f; // .5 blue
light.Diffuse.a = 1.0f; // full alpha (we'll get to that soon)
D3DVECTOR vecDirection = {-1.0f, -0.3f, -1.0f}; // the direction of the light
light.Direction = vecDirection; // set the direction
d3ddev->SetLight(0, &light); // send the light struct properties to light #0
d3ddev->LightEnable(0, TRUE); // turn on light #0
return;
}
I can post the header if people would like, but I'm fairly sure the problem is in this .cpp