⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 update_handler.cpp

📁 通用网络游戏开发框架
💻 CPP
字号:
// ============================================================================
// 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();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -