Assertion failing...

Started by
5 comments, last by djoseph74 19 years, 11 months ago
Hi, I''ve got a simple 3 part program (main, h, cpp) that does nothing more than create a window, initialize directx, begin the scene, clear the viewport, and sit there. Everything seems to be ok in most places. The problems I''m having are an Assert failing in win_main.cpp, and it appears to be running really slow. When I got to move the window, it hesitates. It never does clear the viewport and turn the background black or any other color. I''ve been looking at this comparing it to various tutorials on sites and some sample code I have in my book and from another web site. Can someone point me in the right direction of: 1. How to fix the viewport problem. 2. Fixing the assert in win_main.cpp. Here is my source: win_main.cpp

#include <windows.h>
#include "init_d3d.h"

#pragma comment( lib, "d3d9.lib" )

#define class_name "SpaceInvaders"

const int iWinWidth  = 800;
const int iWinHeight = 800;

LRESULT CALLBACK WinProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );

int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow )
{
	HWND hwnd;
	MSG  msg;

	WNDCLASSEX wndclassex = {0};

	wndclassex.cbSize           = sizeof( WNDCLASSEX );
	wndclassex.style            = CS_HREDRAW | CS_VREDRAW;
	wndclassex.lpfnWndProc      = WinProc;
	wndclassex.hInstance        = hinstance;
	wndclassex.hbrBackground    = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclassex.lpszClassName    = class_name;
	wndclassex.hCursor          = (HCURSOR)LoadImage( NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED );

	RegisterClassEx( &wndclassex );

	RECT rect = { 0, 0, iWinWidth, iWinHeight };

	DWORD winStyleEx = WS_EX_CLIENTEDGE;
	DWORD winStyle   = WS_CAPTION | WS_SYSMENU;

	AdjustWindowRectEx( &rect, winStyle, false, winStyleEx );

	hwnd = CreateWindowEx( winStyleEx,
						   class_name,
						   "Space Invaders Clone Build 0.01.06092004 [alpha]",
						   winStyle,
						   CW_USEDEFAULT,
						   CW_USEDEFAULT,
						   iWinWidth,
						   iWinHeight,
						   NULL,
						   NULL,
						   hinstance,
						   NULL                 );

	if ( g_D3D->init( hwnd ) == false )
		return EXIT_FAILURE;

	GetClientRect( hwnd, &rect );

	//assert(rect.right == iWinWidth && rect.bottom == rect.bottom);


	ShowWindow( hwnd, ishow );
	UpdateWindow( hwnd );

	while ( 1 )
	{
		if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			if ( msg.message == WM_QUIT )
				break;

			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{
			g_D3D->beginScene();
			g_D3D->clearViewPort( 0xFF000000 );		// D3DCOLOR_ARGB( 255, 125, 155, 255 )

			g_D3D->endScene();
		}
	}

	UnregisterClass( class_name, hinstance );

	return EXIT_SUCCESS;
}

LRESULT CALLBACK WinProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
	switch ( message )
	{
		/*case WM_RBUTTONDOWN:
			MessageBox( 0, "Hello DirectX 9!", "Hello", MB_OK );
			return 0;*/

		case WM_DESTROY:
			PostQuitMessage( 0 );

			return 0;

		case WM_KEYDOWN:
			if( wparam == VK_ESCAPE )
				SendMessage( hwnd, WM_CLOSE, 0, 0 );

			return 0;
	}

	return DefWindowProc( hwnd, message, wparam, lparam );
}
init_d3d.cpp

#include "init_d3d.h"

CD3D_obj::CD3D_obj()
{
	d3d_interface = NULL;
	d3d_device    = NULL;
}

void CD3D_obj::beginScene()
{
	HRESULT bs_result = d3d_device->BeginScene();

	assert( bs_result == D3D_OK );
}

void CD3D_obj::endScene()
{
	HRESULT es_result = d3d_device->EndScene();

	assert( es_result == D3D_OK );
}

bool CD3D_obj::init( HWND hwnd )
{
	HRESULT d3d_result;
	D3DPRESENT_PARAMETERS d3d_params = {0};

	d3d_interface = Direct3DCreate9( D3D_SDK_VERSION );

	if ( d3d_interface == NULL )
		return false;

	d3d_params.Windowed         = true;						// Windowed mode

	d3d_params.SwapEffect       = D3DSWAPEFFECT_DISCARD;
	d3d_params.BackBufferFormat = D3DFMT_UNKNOWN;

	/*
		This is some code I found.  It would be more ideal
		for our app to eventually have a configurations
		window and save the settings for resultion, and 
		windowed mode (y/n).  This is good education though.

		if( is_app_fullscreen )
		{
			pp.Windowed          = FALSE;
			pp.BackBufferWidth   = 640;
			pp.BackBufferHeight  = 480;
		}
		else
		{
			pp.Windowed          = TRUE;
		}
	*/

	d3d_result = d3d_interface->CreateDevice( D3DADAPTER_DEFAULT,
											  D3DDEVTYPE_HAL,
									 		  hwnd,
											  D3DCREATE_HARDWARE_VERTEXPROCESSING,		// There is also SOFTWARE processing...

											  &d3d_params,
											  &d3d_device                          );

	if ( d3d_result != D3D_OK )
	{
		d3d_result = d3d_interface->CreateDevice( D3DADAPTER_DEFAULT,
												  D3DDEVTYPE_HAL,
									 			  hwnd,
												  D3DCREATE_SOFTWARE_VERTEXPROCESSING,		// There is also SOFTWARE processing...

												  &d3d_params,
												  &d3d_device                          );

		if ( d3d_result != D3D_OK )
			return false;
	}

	return true;
}

bool CD3D_obj::clearViewPort( int iVPColor )
{
	HRESULT cvp_result = d3d_device->Clear( 0, NULL, D3DCLEAR_TARGET, iVPColor, 1.0f, 0 );

	return ( cvp_result == D3D_OK );
}

CD3D_obj::~CD3D_obj()
{
	if( d3d_device != NULL )
		d3d_device->Release();

    if( d3d_interface != NULL )
		d3d_interface->Release();

	d3d_device    = NULL;
	d3d_interface = NULL;
}

CD3D_obj theD3D_obj;
CD3D_obj *g_D3D = &theD3D_obj;
init_d3d.h

#ifndef INIT_D3D_H
#define INIT_D3D_H

#include <d3d9.h>
#include <assert.h>

class CD3D_obj
{
	public:
		CD3D_obj();		// constructor

		~CD3D_obj();	// deconstructor


		void beginScene();
		void endScene();

		bool init( HWND hwnd );
		bool clearViewPort( int iVPColor );

	private:
		IDirect3D9 *d3d_interface;
		IDirect3DDevice9 *d3d_device;

		CD3D_obj( const CD3D_obj &obj ) {}
		CD3D_obj& operator =(CD3D_obj &obj) { return *this; }
};

extern CD3D_obj *g_D3D;

#endif
Advertisement
My first suggestion would be to not do this:

D3DObject TheObject;
D3DObject *PointerObject = &TheObject

But instead, do this:

D3DObject *PointerObject = NULL;

And in the D3DObject constructor:

if ( PointerObject == NULL ){    PointerObject = this;}


Which sets the pointer to the address of the class. That way, you can only have one D3DObject (because of the if block), and the pointer gets set as well. It works for me .

Also, try to find out where the assert is failing. It would help us if you posted the assert that was failing.


-- Fyhuang, president, Altitude Technologies

Altitude Technologies: http://www.hytetech.com/altitude
Altitude Forums: http://s8.invisionfree.com/Altitude_Forums/

[edited by - fyhuang on June 10, 2004 8:29:33 PM]
- fyhuang [ site ]
Hi,

Thanks for the reply.

I changed the suggested to:

CD3D_obj *g_D3D = NULL;

I''m getting a new error now is in the init_d3d.cpp in the bool CD3D_obj::init function. Its the line that is:

d3d_interface = Direct3DCreate9( D3D_SDK_VERSION );

The error:

Unhandled exception at 0x00411f70 in Space Invaders 3D.exe: 0xC0000005: Access violation writing location 0x00000000.

Also, the assert that fails is:

assert(rect.right == iWinWidth && rect.bottom == rect.bottom); in the win_main.cpp file. Its commented out in my post.

-Dan Joseph
The assert is failing appropriately. You''re comparing the width of the window to that of the client area, which is an obvious inequality. Use GetWindowRect instead.
Using GetWindowRect didn''t help the assert either. Do you see anywhere else that would cause it to fail?

-Dan Joseph
Ahh, actually, I see what you are saying now. I took the iWinHeight and Width out of the CreateWindowEx... I put in:

rect.right - rect.left,
rect.bottom - rect.top,

The assert() is ok now.

I''m still getting my original problem with the g_D3D->Clear() in the beginScene() function in init_d3d.cpp, and the app is running slow, hesitating, appears bogged down by something. I don''t see it? anyone else see what''s wrong?

-Dan Joseph
Alright, I''m voluntarily closing this case... I got it fixed. I forgot to present it after I had the scene ended... Thanks again for the previous help. Take care!

-Dan Joseph

This topic is closed to new replies.

Advertisement