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

📄 main_dialog.cpp

📁 通用网络游戏开发框架
💻 CPP
字号:
// ============================================================================
// Settings dialog
//
// (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 "resource.h"
#include "universal.h"
#include "control.h"

using namespace std;

static BOOL exit_game        (HWND hdlg, INT_PTR result);
static BOOL start_game       (HWND hdlg, INT_PTR result);
static BOOL stop_game        (HWND hdlg, INT_PTR result);
static void dispatch_command (HWND dialog, int id);
static void initialise       (HWND dialog);

extern DWORD WINAPI update_handler(LPVOID parameter);
extern DWORD WINAPI game_handler(LPVOID parameter);

struct Command {
   int  id;
   BOOL (* function)(HWND a, INT_PTR b);
};

namespace {
   Control attrition_button (IDC_ATTRITION);
   Control bases_button     (IDC_BUMP_BASES);
   Control disrupt_button   (IDC_DISRUPT);
   Control explore_button   (IDC_EXPLORE);
   Control game_status      (IDC_STATUS);
   Control group_button     (IDC_GROUP);
   Control hidden_button    (IDC_HIDDEN);
   Control start_button     (IDC_START);
   Control stop_button      (IDC_STOP);

   Command command_list[] = {
      {IDOK,       exit_game},
      {IDC_START,  start_game},
      {IDC_STOP,   stop_game},
   };

   const int number_of_commands( sizeof(command_list) / sizeof(Command) );
}


// ============================================================================
// Main dialog message handler
// ============================================================================

BOOL CALLBACK DlgProc (HWND dialog, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch (message) {
   case WM_INITDIALOG:
      initialise(dialog);
      break;

   case WM_COMMAND:
      dispatch_command(dialog, LOWORD (wParam));
      break;

   case WM_CLOSE:
      exit_game(dialog, 0);
      break;

   default:
      return FALSE;
   }

   return TRUE;
}


// ============================================================================
// Initialisation
//
// Set the various controls to their default values and start a thread to
// handle client connections and one to deal with updates to the game.
// ============================================================================

static void initialise(HWND dialog)
{
   player_count = GetDlgItem(dialog, IDC_PLAYERS);
   register_controls(dialog);

   attrition_button .checked   (true);
   bases_button     .set_range (1, 9);
   bases_button     .set_value (2);
   explore_button   .checked   (true);
   group_button     .checked   (true);
   hidden_button    .checked   (true);
   stop_button      .enable    (false);

   DWORD thread_id (0);

   CreateThread(0, 0, game_handler,   0, 0, &thread_id);
   CreateThread(0, 0, update_handler, 0, 0, &thread_id);
}


// ============================================================================
// Execute the appropriate function when a command is issued from the dialog
// ============================================================================

static void dispatch_command(HWND dialog, int control_id)
{
   for (int i = 0; i < number_of_commands; i++) {
      if (control_id == command_list[i].id) {
         command_list[i].function(dialog, control_id);
         break;
      }
   }
}


// ============================================================================
// Exit button pressed
// ============================================================================

static BOOL exit_game(HWND dialog, INT_PTR p)
{
   game_running = false;

   vector<Client>::iterator i;

   for (i = clients.begin(); i != clients.end(); i++) {
      if (i->in_use) {
         *(i->socket) << "shut-down\n";
      }
   }

   EndDialog(dialog, p);

   return TRUE;
}


// ============================================================================
// Start button pressed
// ============================================================================

static BOOL start_game(HWND hdlg, INT_PTR result)
{
   start_button.enable(false);
   stop_button .enable(true);

   SetWindowText(game_status, "Running");

   bool   group_bases;
   bool   exploring;
   bool   hidden;
   bool   attrition;
   bool   disrupt;
   int    number_of_bases;

   exploring       = explore_button  .get_state();
   hidden          = hidden_button   .get_state();
   group_bases     = group_button    .get_state();
   attrition       = attrition_button.get_state();
   disrupt         = disrupt_button  .get_state();
   number_of_bases = bases_button    .get_value();

   board.initialise(exploring, hidden, attrition, disrupt);

   for (int i = 0; i < max_players; i++) {
      if (clients[i].in_use) {
         board.setup_player(i, number_of_bases, group_bases);
      }
   }

   for (int k = 0; k < max_players; k++) {
      if (clients[k].in_use) {
         Socket * socket = (clients[k].socket);
         (*socket) << "new-board\n";
         board.transmit(socket);
      }
   }

   game_running = true;

   return TRUE;
}


// ============================================================================
// Stop button pressed
// ============================================================================

static BOOL stop_game(HWND hdlg, INT_PTR result)
{
   SetWindowText(game_status, "Stopped");

   start_button.enable(true);
   stop_button .enable(false);

   game_running = false;

   return TRUE;
}

⌨️ 快捷键说明

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