[FIXED]Im stumped =/

Posts 1–9 of 9 · Page 1 of 1
[FIXED]Im stumped =/
This is my first time messing around with DirectX, and so I really don't understand what's going on here. The project compiled fine, but then there's this runtime error. It says hWnd isn't initialized. When it clearly is in my code!
Code:
Run-Time Check Failure #3 - The variable 'hWnd' is being used without being initialized.
That is the exact message. You can look at my code, but since there are no errors in it that the compiler picked up I doubt it's a code issue. It is quite a lot of code, so I will add it as an attachment.

PS: on another note I need to learn better how to properly debug things. I really have never had to deal with such a large amount of code that I know next to nothing about. Do you guys have any tips? o_O
D3D_Game_base.rarattachment deleted · 4 downloads before removal
Code:
/*
Beginning Game Programming
Chapter 5
D3D_Game_Template
*/

//header files to include
#include <d3d9.h>
#include <time.h>

//application title
#define APPTITLE (LPCSTR)"Direct3D_Windowed"

//forward declarations
LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
ATOM MyRegisterClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int, HWND*);
int Game_Init(HWND);
void Game_Run(HWND);
void Game_End(HWND);

//Direct3D objects
LPDIRECT3D9 d3d = NULL; 
LPDIRECT3DDEVICE9 d3ddev = NULL; 


//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // declare variables & register the class & initialize application & creates new window
	MSG msg;
	MyRegisterClass(hInstance);
    HWND hWnd;
    if(!InitInstance(hInstance, nCmdShow, &hWnd))return FALSE;
	
	//initialize the game
    if (!Game_Init(hWnd))return 0;


    // main message loop
    int done = 0;
	while (!done)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
	    {
            if (msg.message == WM_QUIT)
            {
                MessageBox(hWnd, (LPCSTR)"Received WM_QUIT message", (LPCSTR)"WinMain", MB_OK);
                done = 1;
            }
		    TranslateMessage(&msg);
		    DispatchMessage(&msg);
	    }
        else
            Game_Run(hWnd);
    }
	return msg.wParam;
}

///////////////////////////////////////////////////////////////////////////////
// WinProc & WinMain helper function///////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

LRESULT WINAPI WinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Game_End(hwnd);
            PostQuitMessage(0);
            return 0;
    }

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

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    //create the window class structure
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX); 

    //fill the struct with info
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = (WNDPROC)WinProc;
    wc.cbClsExtra	 = 0;
    wc.cbWndExtra	 = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = APPTITLE;
    wc.hIconSm       = NULL;

    return RegisterClassEx(&wc);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, HWND *hwnd)
{

	//create new window
	*hwnd = CreateWindow(
		APPTITLE,				//window class
		APPTITLE,				//title bar
		WS_OVERLAPPEDWINDOW,	//window style
		CW_USEDEFAULT,			//x position of window
		CW_USEDEFAULT,			//y position of window
		500,					//width of window
		400,					//height of window
		NULL,					//parent window
		NULL,					//menu
		hInstance,				//application interface
		NULL);					//window parameters

	//was there an error creating the window
	if(!*hwnd)return FALSE;

	//display the window
	ShowWindow(*hwnd, nCmdShow);
	UpdateWindow(*hwnd);

	return TRUE;
}




/////////////////////////////////////////////////////////////////////////////////////////////////////
//GAME FUNCTIONS/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////

int Game_Init(HWND hwnd)
{
    //display init message
    MessageBox(hwnd, (LPCSTR)"Program is about to start", (LPCSTR)"Game_Init", MB_OK);

    //initialize Direct3D
    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    if (d3d == NULL)
    {
        MessageBox(hwnd, (LPCSTR)"Error initializing Direct3D", (LPCSTR)"Error", MB_OK);
        return 0;
    }

    //set Direct3D presentation parameters
    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

    //create Direct3D device
    d3d->CreateDevice(
        D3DADAPTER_DEFAULT, 
        D3DDEVTYPE_HAL, 
        hwnd,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dpp, 
        &d3ddev);

    if (d3ddev == NULL)
    {
        MessageBox(hwnd, (LPCSTR)"Error creating Direct3D device", (LPCSTR)"Error", MB_OK);
        return 0;
    }

	//set random number seed
	srand((unsigned int)time(NULL));

    //return okay
    return 1;
}

void Game_Run(HWND hwnd)
{
    //make sure the Direct3D device is valid
    if (d3ddev == NULL)return;

    //clear the backbuffer to black
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,255), 1.0f, 0);
    
    //start rendering
    if (d3ddev->BeginScene())
    {
        
    
        //stop rendering
        d3ddev->EndScene();
    }

    //display the back buffer on the screen
    d3ddev->Present(NULL, NULL, NULL, NULL);

}

void Game_End(HWND hwnd)
{
    //display close message
    MessageBox(hwnd, (LPCSTR)"Program is about to end", (LPCSTR)"Game_End", MB_OK);

    //release the Direct3D device
    if (d3ddev != NULL) 
        d3ddev->Release();

    //release the Direct3D object
    if (d3d != NULL)
        d3d->Release();
}
Yes thank you I should have checked back on this site 35 earlier. But your right. I needed to use a reference to hWnd not just create a new hwnd. lol. Pointers and handles will be the death of me... haha xD. Anyway it works now check it:

See I made a pretty lil screen :P

Now the next step is to make it run full screen. Im close though, but still running into quite a few bugs... hopefully I can figure them out by tonight... if not it will just have to wait untill this weekend so I can study for my tests. And oh yeh Halloween too so lol.

EDIT: WOOT! FULLSCREEN!

Lol. ok, now that I got the basics down I promise not to spam the forum until I make something actually worth while. Unless I get completely stumped again :P
Pretty.rarattachment deleted · 4 downloads before removal
FullScreen.rarattachment deleted · 3 downloads before removal
hey did u guys paid for the book or got it free
if u got it free can u PM me the link
Quote Originally Posted by Matrix_NEO006 View Post
hey did u guys paid for the book or got it free
if u got it free can u PM me the link
Lol. I buy all my books from the bookstore. I prefer to read offline TBH =/ ... Ofcourse if I could get it on my Sony ereader I would too, but Sony's selection sucks. It's all fiction... and crappy fiction too. And I can't by Kindle books because of DRM crap... and the fact that you need a kindle in order to buy them ;l.
Quote Originally Posted by Matrix_NEO006 View Post
hey did u guys paid for the book or got it free
if u got it free can u PM me the link
Almost every book I own is taken freely. Honestly, just google for it or search free ebooks...
Not true i picked my c++ book at barns and noble ..... i dont know if its the best but it fits me well...
"c++ for dummies"
lowl..
noobfriendly..
Exactly lol. im learning alot though.
Posts 1–9 of 9 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us