I get an error that says KEY_DOWN and main_window_handle are not declared. Are they suppost to be???Code:if (KEY_DOWN(VK_ESCAPR))
{
PostMessage(main_window_handle, WM_DESTROY,0,0);
}
Printable View
I get an error that says KEY_DOWN and main_window_handle are not declared. Are they suppost to be???Code:if (KEY_DOWN(VK_ESCAPR))
{
PostMessage(main_window_handle, WM_DESTROY,0,0);
}
Got any other code or is that it?
Yes! Define them...
Code:#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
HWND main_window_handle; //make sure you set this to hwnd passed to WinMain later...
Code:if (KEY_DOWN(VK_ESCAPR))
//wrong
if (KEY_DOWN(VK_ESCAPE))
//right
so how would I do that? Thoe following is my WinMainCode://make sure you set this to hwnd passed to WinMain later...
Code:int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg;// generic message
// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, "G3D.ICO");
// register the window class
if (!RegisterClassEx(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindowEx(0, // extended style
WINDOW_CLASS_NAME, // class
"Geo 3D Game Engine V1.0 Alpha -- By Ryan ****** ©", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // initial x,y
800,600, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
return(0);
//Calls Game Initilizastion
int Game_Init();
// enter main event loop
while(1)
{
if (PeekMessage(&msg, NULL,0,0,PM_REMOVE))
{
//Test if this is a quit.
if (msg.message == WM_QUIT)
break;
//Translate any accelerator keys
TranslateMessage(&msg);
//Send message to window proc.
DispatchMessage(&msg);
} //end IF(PeekMessage);
// Calls Game_Shutdown
int Game_Shutdown();
}// end while(1);
//Call Game_Shutdown
int Game_Shutdown();
//Return to windows
return(msg.wParam);
}//end WinMain
Where is the code that causes the error?
look at Jawibs post above.
~Trooper
No, I mean, you haven't actually posted up the code and it's context that actually causes the error. Posting up a few lines isn't enough, I need to know what function it's in and what other code is there. Just post your entire code.
okay here's the .cpp file
~Trooper
That's where I'd put it. I think WM_CREATE is just about the earliest place you can put the code, except maybe WM_NCCREATE.Code:case WM_CREATE:
{
// do initialization stuff here
main_window_handle=hwnd;
return(0);
}
You could probably put it right after you create the windowCode:if (!(hwnd = CreateWindowEx(0, // extended style
WINDOW_CLASS_NAME, // class
"Geo 3D Game Engine V1.0 Alpha -- By Ryan ****** ©", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // initial x,y
800,600, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
return(0);
Placing it in the WM_NCCREATE handler would come before that, Jawib. A lot happens before CreateWindowEx() returns.
But that would be even before the message pump begins, handling your WM_NCCREATE message. Unless CreateWindowEx() calls your message handler on its own?
Ummmmmmmmmm.........
you could put it here
if (!(main_window_handle = CreateWindowEx(0, WINDOW_CLASS_NAME,
That's probably the best place to put it really. If you also initialize the variable with NULL in the first place, then you can be assured that whenever you use the handle, it will either point to a window that has been created and is valid, or it will be NULL, in which case it'll just get flagged as invalid. If you were to place the assignment in WM_NCCREATE, and by chance some function used the handle before WM_CREATE returned, you might have some problems.
So I guess novacain is right. Do the following:
This way, main_window_handle does not have the window handle until WM_CREATE returns, which is a good thing IMO.Code://at the top of the file
HWND main_window_handle=NULL;
.
.
.
int WINAPI WinApi(...)
{
.
.
.
main_window_handle=CreateWindowEx(...);
if (!main_window_handle)
exit(1);