Archived

This topic is now archived and is closed to further replies.

AcidJazz

Creating a window...

Recommended Posts

Hehe im lazy can somebody copy and paste the window procedure and winmain, i dont feel like looking it up, i dont have my old programs

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well, I have it right infront of me, but I''m too lazy to paste it into this reply.

Share this post


Link to post
Share on other sites

  
#include<windows.h> //Windows headers


const int WINDOW_WIDTH=640;
const int WINDOW_HEIGHT=480;
const int WINDOW_X = 100;
const int WINDOW_Y = 100;
const char *WINDOW_NAME = "Dx";
const char *WINDOW_CLASS_NAME = "DirectX App";

HWND hWnd;


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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc; ////Fönsterklassen


//Fyll klassen

wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = WINDOW_CLASS_NAME;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW|CS_VREDRAW;


if(!(RegisterClassEx(&wc))
return 1;

hWnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
WINDOW_NAME,
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
WINDOW_X,WINDOW_Y,
WINDOW_WIDTH,WINDOW_HEIGHT,
NULL,NULL,
hInstance,
NULL);

if(hWnd==NULL)
return 1;


MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}


return 0;

}


*yaaawn*

sHaKaZeD

Share this post


Link to post
Share on other sites
Your main loop is malformed. Your app wont respond appropriately in the rare case GetMessage returns -1. Also, PeekMessage is more efficient since it doesn''t block.

You should also disconnect your main loop rate from the Windows message pump.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Would this be more correct? :

  
for (;;)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// run game loop: yes a single frame: TODO: Make a multi-threaded version.

GameFrame();
}

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
Your main loop is malformed.

It''s a windows loop, not a game loop. And under normal circumstances it will end when WM_QUIT is posted and GetMessage returns 0.

Share this post


Link to post
Share on other sites
quote:
Original post by stevenmarky
Would this be more correct? :


Almost.

    
for (;;)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// run game loop: yes a single frame: TODO: Make a multi-threaded version.

GameFrame();
}
}

Let Windows have its share of your app''s time.

Share this post


Link to post
Share on other sites
quote:
Original post by IndirectX
It''s a windows loop, not a game loop. And under normal circumstances it will end when WM_QUIT is posted and GetMessage returns 0.

I don''t understand the distinction you''re trying to make. Because of how that loop was written, it is the main loop. The framerate and all are tied to it unless multithreaded (and it''s better to use PeekMessage and WaitMessage in a pure "Windows loop").

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
In a "regular" windows app, unless you need idle processing, you can use GetMessage and Windows will sleep you until one becomes available. In games, you always use idle processing. This is why I was referring to the original loop as the "windows" loop - can''t be put in a game. The second loop is a game loop. But then most apps have idle processing anyway.
quote:
MSDN:
If there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle or lpMsg is an invalid pointer.


And what are the chances of this happening? And if GetMessage does return -1, what are YOU going to do?

I didn''t mean to be offensive. I assumed that the original poster wanted a game loop since it''s a DX forum and I was just pointing out that the first loop can''t be used in a game.

Share this post


Link to post
Share on other sites
quote:
Original post by IndirectX
In a "regular" windows app, unless you need idle processing, you can use GetMessage and Windows will sleep you until one becomes available. In games, you always use idle processing. This is why I was referring to the original loop as the "windows" loop - can''t be put in a game. The second loop is a game loop. But then most apps have idle processing anyway.

Okay, I see that. New lexicon if you will, but I agree on the semantics.

quote:
And what are the chances of this happening? And if GetMessage does return -1, what are YOU going to do?

Terminate my loop, for one thing. Using while( GetMessage(...) ) continues the loop even when GetMessage returns -1, because -1 is nonzero. That''s why it''s discouraged in MSDN.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
Using while( GetMessage(...) ) continues the loop even when GetMessage returns -1, because -1 is nonzero.

You're quite right, I missed this.

[EDIT] Stupid me, stared at the doc page and didn't see it.
What do you think of while (GetMessage(...) > 0)?

[edited by - IndirectX on March 24, 2002 9:43:19 AM]

Share this post


Link to post
Share on other sites
OH JUST SHUT IT ALL!
He asked for a Window and there it is. The main loop isn´t malformed it´s designed for my app. And he only asked for WndProc and WinMain btw. Who gives a wacaroni if he´s lazy?

sHaKaZeD

Share this post


Link to post
Share on other sites
quote:
Original post by IndirectX
What do you think of while (GetMessage(...) > 0)?

I generally write loops like this:

while( true )
{
if( PeekMessage( hwnd, &msg, 0, 0, PM_REMOVE )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
if( msg.message == WM_QUIT )
break;
}
else
GameFunc();
}


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
Original post by shakazed
Wouldn´t that code generate a lot of lag if you get tons of window messages due to....something?

Possibly. But if your app could get tons of Windows messages due to "something", then you should consider multithreading your app. Considering how many times per second that loop gets run on average, lag possibilities are generally slim, plus your app plays better with the rest of the system.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites