Thread: Game updating

  1. #1
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104

    Game updating

    Hi, I am planning to make a old strategy game, in the red alert style. I am planing to use C++, pure Win32API and OpenGL(though maybe even allegro or SDL would be enough, i wont learn those when OpenGL can do the same).

    I havent developed a game which is this big before, and I am wondering: Where should I update the world database
    (Like what is happening, where is all the units placed, shall there be a hole in the ground after a certain explosion, update the infantry-troops positions since they are moving, etc..)?

    Should I update it in the WinMain?
    Or in WM_TIMER messages?

    The answer is probably in WinMain, but that means I have to some time-management. So if you mean WM_TIMER isnt good enough, could you get me started, finding a good libary and function set for dealing with accurate, low-value,millisecond management, so I can compute when a update of worlddatabase would be okay or not?

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    It's common to see the message loop in WinMain ripped apart to look like this:

    Code:
    while(true)
    {
       if( PeekMessage() ) 
       {
            if(WM_QUIT == msg)
                break;
    
            TranslateMessage();
            DispacthMessage();
       }
       else
       {
            game.Run();
       }
    }
    I forget the params to the message functions off hand, but you get the idea. The game object in this example has the Run function, which captures input, updates the game state (all that stuff you were talking about) then draws to the screen. It wouldn't be a good idea to cram all that into one big function of course, but I hope this gives you an idea on how you would like to set things up.

    However, running your game off of windows messages is not a bad idea, in fact some people wouldn't sleep easily at night if they saw the above loop in WinMain. As long as it gets the job done, you're doing it right!

  3. #3
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    Yes, I know this is a common way of doing it.
    BUT:
    Lets say, the above function moves the soldiers 1 square on the battlescreen. Then (on mulitplayer), the computer with the quickest computer would be having the fastest units!

    Which is why I want to have some usefull functions for getting, and controlling and comparing time. In secounds, miniutes, and most importantly: milliseconds.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    What you want is timebased movement. Do a board search, if i remember correctly there was a thread about that just a month or so ago.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  5. #5
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    One good way I've heard of that will cause everyone to move at the same speed works like this:

    Get the fps.
    Infantry moves 1 square if the fps is ...60 ( 60 is the 'goal fps' )
    infantry moves 2 squares if the fpr is 30
    infantry moves .5 square if the fps is 120
    infantry moves .1 square if the fps is 600
    infantry moves 10 squares if the fps is 6


    etc.

    So even if someone is going through the loop at a MUCH greater fps, they'll still be moving the same amount.
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by elnerdo
    One good way I've heard of that will cause everyone to move at the same speed works like this:
    Assume you get a lag spike of several seconds, your units will move a huge distance moving through walls n stuff unless you have a very clever movement system.

    My personal preference is a discrete set of update intervals. It uses a deadloop but with a sleep-system used in some real-time OS'es. basically, when you have updated the game you sleep *almost* as long as you need until the next update. This relieves the CPU a bit.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Lisen to Shakti becouse there was a recent post on this topic, i believe i was the one asking, and plz do not do what elnerdo told you, that is a sloppy mess.
    You just need a velocity that your units move at and then you can base your movment on real time. Useing real time will eliminate any lag issues that you would encountor.

    Last, what Magos said dose not really make sence to me becouse that would only happen if you had no collision area detections, and, if you did not, the units would still move through things any way.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    What magos said does make sense if you aren't doing swept volume collision detection calculation. Depending on how complex you make it, there are often complex collision detections that can only really be solved by performing overlap tests, done by incrementing the object's position and/or orientation and checking for the collisions. The problem of when two objects move 'too much' in a single move and pass through each other in a single step is called tunneling.

    If this is just single player you should be able to find about a bazillion resources for doing time based movement. Doing this over a network is, as I understand, many times more complex.

    Go onto msdn.microsoft.com and look up queryperformancecounter and queryperformancefrequency. You'll need those for the most accurate/precise timing on xWindows.
    See you in 13

  9. #9
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Thanks for clearing that up for me Darkness

  10. #10
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    Magos, the method you mentioned is the same method I've been using on all my games, but you can still notice a difference of speed on two computers with that.

    (It's especially good because it prevents your games from using 100% of the cpu)
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

  11. #11
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    When i made my Space Shooter, i have the user set how many FPS he wants, then leave it all to SDL_Delay(TIMER);

    How that worked is i'd wait for a set amount of milliseconds, and I could limit it to 60 FPS, or untimed FPS, even .000000000000000000001 FPS.... It's not as flexible, but it works.
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. So you want to be a game programmer?
    By dxfoo in forum Game Programming
    Replies: 23
    Last Post: 09-26-2006, 08:38 AM
  4. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM