Thread: Message loop

  1. #1
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006

    Message loop

    I am coding my framework for my DirectX applications and I don't like the message loop that is used in my book:

    int EnterMsgLoop(bool (*ptr_display)(float timeDelta))
        MSG msg;
        ::ZeroMemory(&msg, sizeof(MSG));
        static float lastTime = (float)timeGetTime();
        while(msg.message != WM_QUIT)
            if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
                float currTime = (float)timeGetTime();
                float timeDelta = (currTime - lastTime)*0.001f;
                lastTime = currTime;
        return msg.wParam;
    If you people here on CBoard help me understand this a little more(mainly around the time), then I might be able to make my own function that wraps the message loop.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Can you perhaps explain what part of this you don't like?

    What part of the time-management don't you understand?

    The general idea, from what I see, is that you loop around until the application closes, doing either: a message operation, or you display the current state of whatever you are drawing, and part of that is of course to track time so that we know how far to move things around, for example.

    Since windows is message-based, you will have to "fetch" the messages at some time or another - otherwise you won't know what's going on.

    You could of course have two threads, one that does drawing, and one that handles messages - but then how would you get the drawing thread to understand that the user clicked the mouse, hit a spacebar or a arrow-key or whatever, which means something to the drawing side (fire a bullet, move the player or whatever)? Aha, I hear you say, I send a message - so we're back at the beginning, you need a message loop.

    You may be able to write it in a different way - and depending on your definitions of better, it may well be better - and I'm sure if you are going to write a full-feature game, none of the existing code would be left - you would rewrite all of that. But you'd have something that does roughly the same thing.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    I have explained how to get around the pointer to the display function multiple times in multiple threads.

    I've also explained how to take the code in that book and create your own C++ class framework from it.

    A board search would yield many many answers to your problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate Over Ecs!
    By cookie in forum C Programming
    Replies: 17
    Last Post: 07-16-2008, 01:25 PM
  2. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 05:15 PM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. Help, the window gets killed......
    By incognito in forum Game Programming
    Replies: 2
    Last Post: 05-28-2002, 02:22 PM