Archived

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

LonelyTower

Questions about Donuts3D sample - CMyApplication::Run()

Recommended Posts

Hi, I have tried searching through the archives for more information about Donuts3D but has failed to come across one that answers my question. The class defintion for the CMyApplication class in the game is:
  
class CMyApplication 
{
public:
    HRESULT Create( HINSTANCE hInstance );
    INT     Run();
    LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

.
.
// snipped

.
.
  
And the Run() function contains the following...
  
INT CMyApplication::Run()
{
    // Load keyboard accelerators

    HACCEL hAccel = LoadAccelerators( NULL, MAKEINTRESOURCE(IDR_MAIN_ACCEL) );

    // Now we''re ready to recieve and process Windows messages.

    BOOL bGotMsg;
    MSG  msg;
    PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );

    while( WM_QUIT != msg.message  )
    {
        // Use PeekMessage() if the app is active, so we can use idle time to

        // render the scene. Else, use GetMessage() to avoid eating CPU time.

        if( m_bIsActive )
            bGotMsg = PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE );
        else
            bGotMsg = GetMessage( &msg, NULL, 0U, 0U );

        if( bGotMsg )
        {
            // Translate and dispatch the message

            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        else
        {
            // Render a frame during idle time (no messages are waiting)

            if( m_bDisplayReady )
            {
                FrameMove();
                RenderFrame();
            }
        }
    }

    return (int)msg.wParam;
}
  
Now the question is - is CMyApplication a seperate thread? If not, how is the Run() function being called each time to handle window messages? What''s the nature of the Run() function? Thanks in advance!

Share this post


Link to post
Share on other sites
I have seen no evidence of multithreading in the D3D Common Files. I ve been considering writing a multithreaded version lately...

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share this post


Link to post
Share on other sites
quote:
Original post by Wreakon
Thats because that Run is called every time a message received with certain parameters. No its not threaded.

-------
Happy Coding!
Homepage: www.pcwebvia.f2s.com


What causes this to happen?

Usually the code snippet from PeekMessage() onward is put in an endless loop:

  
while (TRUE)
{
PeekMessage(....)
.
.
.
}


But in Donuts3D, this is put into the Run() function. So what actually caused the Run() function to be called? Is it handeld by API? Is the Run() function a special function reconigsed by Windows?

Many thanks in advance.

Share this post


Link to post
Share on other sites
You''re forgetting something: the code in Winmain(). Winmain() is in one of the D3DX libraries (i never use it so i dunno which one). Winmain() will do some kind of loop, calling Application.Run().

HTH, Steve

Share this post


Link to post
Share on other sites
quote:
Original post by Steveyboy
You''re forgetting something: the code in Winmain(). Winmain() is in one of the D3DX libraries (i never use it so i dunno which one). Winmain() will do some kind of loop, calling Application.Run().

HTH, Steve



Er sorry, I thought WinMain is part of the Windows API?

Share this post


Link to post
Share on other sites
quote:

It is, but the D3DX librarys use their own Winmain, don''t they? :s Try adding a Winmain() function in an application that uses D3DX, and you should get a "Winamin() - redefinition" error.


...that shouldn''t happen. The D3DX lib is a static lib that does no such thing.

If you had actually looked at the source for run, its code goes something like this(typed from memory)
  
int Run()
{
//Some sort of initialization


while(bRun) //This is of course, not accurate; look at d3dapp.cpp

{
//Do some more stuff

if( PeekMessage(...) )
{
GetMessage(...);
TranslateMessage(&msg);
DispatchMessage(&msg)
}
else
Render3DEnvironment();
}
}


I guess you could consider that the Psuedo code. It is a standard Game Loop. if we have a message, process it, else render our frame.

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share this post


Link to post
Share on other sites
No no no no NO!

WinMain() is the Application''s entry point. This is the first place that is hit when you run the app.

In WinMain() the application class is declared, initialised (with Create() or similar) and Run (with Run() or whatever).

All the Run() does is enter into a standard windows message loop (if you don''t know what one of these is then read up on it). The message loop is what handles all the message from windows (Keypresses, screen redraws, etc).

Where Render(), etc, sits is after you''ve said "Okay, we''ve processed window''s messages, let''s get on with doing our own thing" and this is where you''d dump in your "Mainloop" type code (game loops, etc).

After you''ve done your thing, the message loop goes back round again to check for more windows messages. It''s not threading, it''s just a loop.

Does this help anyone?

Oli



All the best problems start with C

The Most Boring Website Ever?

Share this post


Link to post
Share on other sites
quote:
Original post by downgraded
No no no no NO!

WinMain() is the Application''s entry point. This is the first place that is hit when you run the app.

In WinMain() the application class is declared, initialised (with Create() or similar) and Run (with Run() or whatever).

All the Run() does is enter into a standard windows message loop (if you don''t know what one of these is then read up on it). The message loop is what handles all the message from windows (Keypresses, screen redraws, etc).

Where Render(), etc, sits is after you''ve said "Okay, we''ve processed window''s messages, let''s get on with doing our own thing" and this is where you''d dump in your "Mainloop" type code (game loops, etc).

After you''ve done your thing, the message loop goes back round again to check for more windows messages. It''s not threading, it''s just a loop.

Does this help anyone?

Oli



All the best problems start with C

The Most Boring Website Ever?


Oh oops! My bad...I failed to take in

WHILE (WM_MESSAGE != QUIT)
{ ....... }

That explains everything. Thanks all!

Share this post


Link to post
Share on other sites
quote:

Oh oops! My bad...I failed to take in

WHILE (WM_MESSAGE != QUIT)
{ ....... }


Sorry, but that''s still wrong. That wont work because WM_Message is non-existent. Try:
while( msg.message != WM_QUIT
{...}


-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share this post


Link to post
Share on other sites
quote:

Oh oops! My bad...I failed to take in

WHILE (WM_MESSAGE != QUIT)
{ ....... }


Sorry, but that''s still wrong. That wont work because WM_Message is non-existent. Try:
while( msg.message != WM_QUIT )
{...}


-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share this post


Link to post
Share on other sites
CSpot::Run();
JohnCan->LookAt(Jane(CSpot::Run()));

I need to stop.

All of you arguing over WinMain obviously don''t pay attention to capitalization (ie Winmain != WinMain - note the M).

[ 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
Heh, did any of you guys actually look at winmain in donuts?


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow )
{
CMyApplication app;

g_hInst = hInstance;

if( FAILED( app.Create( hInstance ) ) )
return 0;

return app.Run();
}


it''s the same for all the apps created by ms''s directx template that came with the v8 sdk, regardless of weather or not it''s using d3dx. it''s doing the exact same thing but the people who write the samples pride themselves on making them confusing as possible.

Share this post


Link to post
Share on other sites