Skip to main content
gamedev.net
🔒 Locked

[DX] Win32 Help

Started by zee_ola05 Apr 10, 2011 at 4:30 AM 7 replies 1.8k views
Original Post
zee_ola05
zee_ola05


#include <windows.h>


// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);

/*
hInstance - identifier of the "instance"/application
prevInstance - obsolete, not important
lpCmdLine - string pointer, like argv in C (getting cmdline parameters)
showCmd - not important in games
*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc; //fill up this struct and then Register

ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc; //WindowProc() is a function that handles messages (e.g. mouse and keyboard inputs)
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "Window Class 1";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
"Window Class 1 - CreateWindowEx",
"Window Name 1",
WS_OVERLAPPEDWINDOW,
300,
300,
500,
400,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hWnd, nCmdShow);

MSG msg;

while(true)
{
//Check if there is a message in the message queue. If none, continue with the game code, else process
while(PeekMessage(&msg, hWnd, 0,0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if(msg.message == WM_QUIT)
break;

/*
Run Game Code Here
*/
}

return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
//close application
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

//Hnadle any statement the switch-case didn't
return DefWindowProc(hWnd, message, wParam, lParam);
}



I have this program written using MS Visual C++ 2010. When I debug it (F5), there is no error and it runs. BUT it does not show anything. I'm expecting a blank window. Can anyone help me identifying what is wrong with this code? Thanks!

I'm following a DirectX tutorial, BTW.
zee_ola05
zee_ola05
When I run I get this [media]http://img19.imageshack.us/img19/8185/erroryx.png[/media]
SiCrane
SiCrane
You should check function return values for errors. Most Windows functions will indicate success or failure, and if they fail you can call GetLastError() to find out what the exact error is. In particular your CreateWindowEx() call is failing.
zee_ola05
zee_ola05
Thank you. Can you tell me what in particular is wrong with CreateWindowEx()?

And if I use the GetLastError(), How will I display the error?
SiCrane
SiCrane
According to the documentation for GetLastError(), you can use FormatMessage() to get a string for the error code. Alternately you can simply examine the numerical value and compare it to the list of system error codes. Doing so will tell you which parameter CreateWindowEx() is having trouble with in your function call.
zee_ola05
zee_ola05
It didn't tell what parameter was invalid.
SiCrane
SiCrane
Take a look at the CreateWindowEx() call. Which parameter is the window class parameter? Now compare that value to the name of the window class you registered.
zee_ola05
zee_ola05
I'm sorry. Thank you.

Topic Locked

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

Sign in to reply to this topic.