Skip to main content
gamedev.net
🔒 Locked

unexpected errors

Started by stenny Jul 14, 2006 at 8:38 AM 1 replies 600+ views
Original Post
stenny
stenny
Aye, I've got a simple question. I'm trying to set up a basic direct3D system (or whatever it's called), by following a tutorial, but I get 2 unexpected errors:

#include <iostream>
#include <windows.h>
#include <d3d9.h>

// Include the Direct3D library file
#pragma comment (lib, "d3d9.lib")

// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D inferface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class

// 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

// Windows Procedure:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}

	return 0;
}

// instead of the normal main, we have WinMain
int WINAPI WinMain(
				   HINSTANCE hInstance, // instance handle of the application
				   HINSTANCE hPrevInstance, // unused
				   LPSTR lpCmdLine, // command line options (if any)
				   int nCmdShow) // show window flag (mini-/maximized)
{
	// registering a windows class
	WNDCLASSEX wcex = {
		sizeof(WNDCLASSEX), // size of this structure
		CS_CLASSDC,			// Style of the window
		WindowProc,			// Window message procedure
		0L,					// 0
		0L,					// 0
		hInstance,			// instance handle from windmain
		NULL,				// handle to application icon
		NULL,				// handle to application cursor
		NULL,			    // handle to application brush
		NULL,				// handle to application menu
		L"GameClass",		// this class' name
		NULL				// handle to small application icon
	};

	// register the just made class and exit on error
	if (!RegisterClassEx(&wcex)) // parameter is the WNDCLASSEX class just created
	{
		return FALSE;
	};

	// creating a window
	HWND hWnd;
	hWnd = CreateWindow(
		L"GameClass",			// name of the gameclass
		L"My Direct3D program",	// Window caption
		WS_OVERLAPPEDWINDOW,	// Windowstyle
	    300,					// X of the window
	    300,					// Y of the window
		640,					// Width of the window
		480,					// Height of the window
		NULL,
		NULL,
		hInstance,				// instance handle from winmain
		NULL);

	// return on error creating the window
	if (hWnd == NULL)
	{
		return FALSE;
	}

	// show the window
	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);

	// set up and initialize Direct3D
	initD3D(hWnd);

	// Enter the messagepump
	MSG msg;

	// clear out the message structure
	ZeroMemory(&msg, sizeof(MSG));

	// loop endlessly until you receive a quit message
	while(msg.message != WM_QUIT)
	{
		//find out the starting time for each loop
		DWORD starting_point = GetTickCount();

		// peek into the message queue and see if there's a message waiting
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			// yay! a message! Handle it normally!
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		};
		// render each frame
		render_frame();

		while ((GetTickCount() - starting_point) < 25);
	}

	// clean up DirectX and COM
	cleanD3D();

	// unregister class
	UnregisterClass(L"GameClass", hInstance); // parameter 1: class name, parameter 2: instance handle

	// the normal return 0, to check if the program has executed correctly
	return 0;
}

// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);    // create the Direct3D interface

    D3DPRESENT_PARAMETERS d3dpp;    // create a struct to hold various device information

    ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use
    d3dpp.Windowed = TRUE;    // program windowed, not fullscreen
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames
    d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D


    // 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);

    return;
}


// this is the function used to render a single frame
void render_frame(void)
{
    // clear the window to a deep blue
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

    d3ddev->BeginScene();    // begins the 3D scene

    // do 3D rendering on the back buffer here

    d3ddev->EndScene();    // ends the 3D scene

    d3ddev->Present(NULL, NULL, NULL, NULL);   // displays the created frame on the screen

    return;
}


// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D

    return;
}
errors: error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib Error 2 fatal error LNK1120: 1 unresolved externals G:\CPP\WinAPI\Direct3D\Debug\Direct3D.exe Does anyone know the solution? - Stenny
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel
dave
dave
Have you made sure that the project is set up to be a Windows Application rather than a Console Application?

Dave
stenny
stenny
I did, but I'll double check

(you're everywhere aren't you?)

EDIT
Okay, the exact steps (in VS):

+ file->new->project...
+ Win32->Win32 Console Application - OK
the Win32 Application wizard turns up
+ next
+ application type: Windows application, additional options: empty project -> finish
+ add->new item (resource explorer)
+ cpp file
I filled in the code
+ctrl+F5

...
Damn! you're right! And I'm sure I did use a win32 application!

How do you know all that stuff! I'm getting jealous here :P.

Thanks again!

- Stenny
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.