Click here to Skip to main content
15,894,546 members
Articles / Desktop Programming / MFC

WinBattle

Rate me:
Please Sign up or sign in to vote.
4.46/5 (14 votes)
22 Dec 200320 min read 63.6K   2.8K   43  
A multi-player game tutorial and reusable framework
// ============================================================================
// Deal with game updates
//
// (c) 2003 Ken Reed
//
// This is free software. You can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
// ============================================================================

#include "stdafx.h"

#include "global.h"
#include "socket.h"
#include "report_error.h"

#include <mmsystem.h>

using namespace std;

static void process_updates();
static void update_clients();


// ============================================================================
// Entry point (New Thread)
// ============================================================================

DWORD WINAPI update_handler(LPVOID parameter)
{
   try {
      enable_fp_exceptions();
      process_updates();
   }
   catch (Exception & e) {
      report_text(e.get_error().c_str());
   }
   catch (...) {
      report_text("Exception thrown from update handler");
   }

   return 0;
}


// ============================================================================
// Process game updates
// ============================================================================

static void process_updates()
{
   const double update_period = 0.4;

   // Get the time in milliseconds. The value returned by timeGetTime will
   // reset to zero every 47 days or so, so trap the roll-over.

   while (true) {
      double now (timeGetTime());
      now /= 1000.0;

      static double last_update = now;
      if (now < last_update) {
         last_update = now;
      }

      double elapsed (now - last_update);

      if (elapsed >= update_period) {
         if (game_running) {
            board.update(elapsed);
            update_clients();
         }
         last_update = now;
      } 
      else {
         double sleep_time ((update_period - elapsed) * 1000.0); 
         Sleep(static_cast<DWORD>(sleep_time));
      }
   }
}


// ============================================================================
// Update all the clients.
// ============================================================================

static void update_clients()
{
   vector<Client>::iterator k;

   for (k = clients.begin(); k != clients.end(); k++) {
      if (k->in_use) {
         Socket * socket = k->socket;

         (*socket) << "update-board\n";
         board.send_updates(socket);
      }
   }

   board.end_of_updates();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Systems Engineer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions