Window Size Variables are Confusing me...

Started by
5 comments, last by Zahlman 16 years, 10 months ago
I am trying to learn Direct 3D, but this issue has nothing to do with DirectX... I have a simple application that creates a device and clears the screen to a standard color each frame. I wanted to make it full screen, because I'm just playing with settings...and I'm having an issue with the CreateWindowEx function. My Code:

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

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

#define SCREEN_WIDTH 640;
#define SCREEN_HEIGHT 480;

//Global Declerations
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;

void initD3D(HWND hWnd);
void render_frame();
void cleanD3D();

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
				   int nShowCmd)
{
	HWND hWnd;
	WNDCLASSEX wndclass;
	MSG msg;

	ZeroMemory(&wndclass, sizeof(WNDCLASSEX));
	
	wndclass.cbSize			= sizeof(WNDCLASSEX);
	wndclass.style			= CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc	= WndProc;
	wndclass.hIcon			= NULL;
	wndclass.hCursor		= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wndclass.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hInstance		= hInstance;
	wndclass.cbClsExtra		= 0;
	wndclass.lpszClassName	= "WindowClass";
	//wndclass.hbrBackground  = (HBRUSH)COLOR_WINDOW;

	RegisterClassEx(&wndclass);

	hWnd = CreateWindowEx(NULL, 
		"WindowClass", 
		"D3D WEB TUTOR", 
		WS_EX_TOPMOST | WS_POPUP, 
		0, 0, 
		SCREEN_WIDTH,  //This line and
		SCREEN_HEIGHT, //this line are giving me problems...errors below
		NULL, 
		NULL, 
		hInstance, 
		NULL);

	ShowWindow(hWnd, nShowCmd);

	initD3D(hWnd);

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

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

	cleanD3D();

	return msg.wParam;
}


void initD3D(HWND hWnd)
{
	d3d = Direct3DCreate9(D3D_SDK_VERSION);

	D3DPRESENT_PARAMETERS d3dpp;

	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));

	d3dpp.Windowed = FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow = hWnd;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = SCREEN_WIDTH;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;

	d3d->CreateDevice(D3DADAPTER_DEFAULT, 
		D3DDEVTYPE_HAL, 
		hWnd, 
		D3DCREATE_HARDWARE_VERTEXPROCESSING, 
		&d3dpp, 
		&d3ddev);

	return;
}

void render_frame()
{
	//Clear Window
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, 
		D3DCOLOR_XRGB(0, 40, 100), 1.0f, 
		0);
	d3ddev->BeginScene();

	d3ddev->EndScene();

	d3ddev->Present(NULL, NULL, NULL, NULL);

	return;
}

void cleanD3D()
{
	d3ddev->Release();
	d3d->Release();
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}break;
	}

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

}

When I compile I get: Error 1 error C2143: syntax error : missing ')' before ';' d:\learning\gameprogramming\dx\docu\dxlearning\webtut1\main.cpp 47 Error 2 error C2660: 'CreateWindowExA' : function does not take 7 arguments d:\learning\gameprogramming\dx\docu\dxlearning\webtut1\main.cpp 47 Error 3 error C2143: syntax error : missing ';' before ',' d:\learning\gameprogramming\dx\docu\dxlearning\webtut1\main.cpp 47 Error 4 error C2143: syntax error : missing ';' before ',' d:\learning\gameprogramming\dx\docu\dxlearning\webtut1\main.cpp 48 Error 5 error C2059: syntax error : ')' d:\learning\gameprogramming\dx\docu\dxlearning\webtut1\main.cpp 52 The only way I have been able to get rid of the errors is to change the lines to:

//...

hWnd = CreateWindowEx(NULL, 
		"WindowClass", 
		"D3D WEB TUTOR", 
		WS_EX_TOPMOST | WS_POPUP, 
		0, 0, 
		640,   //Width and 
		480,   //Height, can't use my #define variables??
		NULL, 
		NULL, 
		hInstance, 
		NULL);
//...

This compiles and runs fine, but I don't like using "Magic Numbers" in my programs. Anyone have any idea?
Advertisement
Take the semicolons off the ends of your defines.
Better yet, use constants.
Ok, changed to constants and works fine, also works without the semicolons on the #defines, thanks a lot!

Why is it better to use constants?

  1. Constants provide type-safety

  2. Constants are defined within a scope

  3. Constants can be passed around to functions

  4. Constants can be used with pointers

  5. Constants can be debugged
I believe it's because they're type safe, correct? I don't remember with 100% accuracy, so anyone can feel free to correct me if I'm wrong... but I believe they get evaluated as any other variable were (instead of doing a straight-out text replacement in the line of code where it's found before compiling, which is what #define does if I'm not mistaken)

So basically, with #define it's just dropping that text there, and if it matches, great! And if not, you get a cryptic error.

With constants, it'll check if it's the right type for where it's being used because the compiler knows what type it's meant to be.

EDIT: Aye, ditto to what Darklighter said. =) Beat me to it =) te he ;)
- Edgar Verona"But when a long train of abuses and usurpations, pursuing invariably the same Object evinces a design to reduce them under absolute Despotism, it is their right, it is their duty, to throw off such Government, and to provide new Guards for their future security."
Quote:Original post by TheN00B
Ok, changed to constants and works fine, also works without the semicolons on the #defines, thanks a lot!

Why is it better to use constants?


In addition to what was already said:

- If you'd written it as a constant in the first place, it would have worked with the semicolons.

- If you'd made a similar bug with the constants instead of defines, by *omitting* semicolons, the compiler would have complained about *the lines where the constants were declared*, instead of everywhere the macro is used. Much easier to figure out the problem that way.

This topic is closed to new replies.

Advertisement