Click here to Skip to main content
15,886,362 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.5K   2.8K   43  
A multi-player game tutorial and reusable framework
// ============================================================================
// 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;
}

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