Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Win32 message loop and CPU usage

Win32 message loop and CPU usage

Scheduled Pinned Locked Moved C / C++ / MFC
game-devtutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hanan888
    wrote on last edited by
    #1

    I'm developing an animated game on Win32. my message-loop looks like this:

    MSG msg;
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
    if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
    {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
    }
    else //"idle" state. Call graphic rendering functions. Not that they are simple and speedy.
    {
    CPTWindowManager::GetInstance()->RenderAllGames();
    }
    }

    I have noticed that takes 50% of the CPU. I wonder if this 50% are "for real". If so, it is not desirable. I tried to use GetMessage() but I don't know how to figure out the "idle" state when using GetMessage(). Note that the rendering stuff are relatively simple rendering and thus the time they take is unnoticeable.

    C 1 Reply Last reply
    0
    • H Hanan888

      I'm developing an animated game on Win32. my message-loop looks like this:

      MSG msg;
      ZeroMemory( &msg, sizeof(msg) );
      while( msg.message!=WM_QUIT )
      {
      if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
      {
      TranslateMessage( &msg );
      DispatchMessage( &msg );
      }
      else //"idle" state. Call graphic rendering functions. Not that they are simple and speedy.
      {
      CPTWindowManager::GetInstance()->RenderAllGames();
      }
      }

      I have noticed that takes 50% of the CPU. I wonder if this 50% are "for real". If so, it is not desirable. I tried to use GetMessage() but I don't know how to figure out the "idle" state when using GetMessage(). Note that the rendering stuff are relatively simple rendering and thus the time they take is unnoticeable.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      No matter how long the "rendering stuff" is taking, you have a busy loop all the time: there's nowhere a 'pause' in the loop (sleep or wait for something...). But don't worry too much, that's usually how games are written: you want to achieve the best possible performance, so it's normal that you will eat up all remaining CPU time. In your case it is probably taking 50% of the CPU because you have a dual-core, am I right ?

      Cédric Moonen Software developer
      Charting control [v1.4 - Updated]

      H 1 Reply Last reply
      0
      • C Cedric Moonen

        No matter how long the "rendering stuff" is taking, you have a busy loop all the time: there's nowhere a 'pause' in the loop (sleep or wait for something...). But don't worry too much, that's usually how games are written: you want to achieve the best possible performance, so it's normal that you will eat up all remaining CPU time. In your case it is probably taking 50% of the CPU because you have a dual-core, am I right ?

        Cédric Moonen Software developer
        Charting control [v1.4 - Updated]

        H Offline
        H Offline
        Hanan888
        wrote on last edited by
        #3

        Yes I have a dual-core. Also, I don't 'feel' the 50% slowing down my windows experience. I have, however, saw a similar game that when run only eats several %. Perhaps they did some scheme with the WM_PAINT messages - they cause the window to refersh, by InvalideRect() I guess, and render on the WM_PAINTs. Sound like something I should try ? Thanks

        C 1 Reply Last reply
        0
        • H Hanan888

          Yes I have a dual-core. Also, I don't 'feel' the 50% slowing down my windows experience. I have, however, saw a similar game that when run only eats several %. Perhaps they did some scheme with the WM_PAINT messages - they cause the window to refersh, by InvalideRect() I guess, and render on the WM_PAINTs. Sound like something I should try ? Thanks

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          It depends which kind of game it is. For example, if it is something static (like solitaire, mine sweeper, ...) then you don't need to repaint the view dynamically. You can simply repaint the view when something changed. So, I can't really answer your question, it depends of the kind of game you are trying to write.

          Cédric Moonen Software developer
          Charting control [v1.4 - Updated]

          H 1 Reply Last reply
          0
          • C Cedric Moonen

            It depends which kind of game it is. For example, if it is something static (like solitaire, mine sweeper, ...) then you don't need to repaint the view dynamically. You can simply repaint the view when something changed. So, I can't really answer your question, it depends of the kind of game you are trying to write.

            Cédric Moonen Software developer
            Charting control [v1.4 - Updated]

            H Offline
            H Offline
            Hanan888
            wrote on last edited by
            #5

            Thanks. It has dynamic rendering, to be specific, it plays series of pictures according to time (what might be called animation). So it must repaint all the time.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups