📄 client.cpp
字号:
// ============================================================================
// Winbattle client
//
// (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 "report_error.h"
#include "resource.h"
#include "socket.h"
#include "universal.h"
using namespace std;
extern DWORD WINAPI server_handler(LPVOID parameter);
static LRESULT CALLBACK WndProc (HWND h, UINT m, WPARAM w, LPARAM l);
static void initialise ();
static void process_key (WPARAM key);
static void process_mouse (LPARAM lparam, Mouse_Command command);
static void shut_down ();
namespace {
char * server_name = ("localhost");
HINSTANCE instance (0);
Point mouse_position (0, 0);
Socket server;
}
// ============================================================================
// Program entry point
//
// If we have a command line (or short cut) parameter then we assume it is the
// name of the computer where the server is running.
// ============================================================================
int APIENTRY WinMain(HINSTANCE this_instance, HINSTANCE prev_instance,
LPSTR commands, int nCmdShow)
{
try {
if (*commands != '\0') {
server_name = commands;
}
instance = this_instance;
initialise();
ShowWindow(main_window, nCmdShow);
UpdateWindow(main_window);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
catch (Exception & e) {
report_text(e.get_error().c_str());
}
catch (...) {
report_text("Exception thrown from WinMain");
}
return 0;
}
// ============================================================================
// Initialise
// ============================================================================
static void initialise()
{
enable_fp_exceptions();
char * wclass = "WinBattle";
WNDCLASSEX wcex;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = instance;
wcex.hIcon = LoadIcon(instance, MAKEINTRESOURCE(IDI_XBATTLE));
wcex.hbrBackground = reinterpret_cast<HBRUSH> (COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_XBATTLE);
wcex.lpszClassName = wclass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassExA(&wcex);
UINT style (WS_OVERLAPPEDWINDOW);
UINT exstyle (0);
int px (CW_USEDEFAULT);
int py (0);
Point size(board.get_size());
RECT rectangle = {0, 0, size.x, size.y};
AdjustWindowRectEx(&rectangle, style, true, exstyle);
int wx (abs (rectangle.right - rectangle.left) + 1);
int wy (abs (rectangle.bottom - rectangle.top) + 1);
main_window = CreateWindowEx(exstyle, wclass, wclass , style,
px, py, wx, wy, 0, 0, instance, 0);
board.window = main_window;
server.connect(server_name, server_port);
DWORD thread_id (0);
CreateThread(0, 0, server_handler, 0, 0, &thread_id);
}
// ============================================================================
// Connect to the server
// ============================================================================
static void connect()
{
char hostname[MAX_COMPUTERNAME_LENGTH + 1];
DWORD length(sizeof(hostname));
GetComputerName(hostname, &length);
int player;
server << "connect " << hostname << " " << port_number << "\n";
server >> player;
board.player = player;
}
// ============================================================================
// About dialog process
// ============================================================================
static BOOL CALLBACK DlgProc (HWND dialog, UINT message, WPARAM w, LPARAM l)
{
BOOL result(FALSE);
if (message == WM_COMMAND) {
if (LOWORD(w) == IDOK) {
EndDialog(dialog, 0);
result = TRUE;
}
}
return result;
}
// ============================================================================
// Process a control or menu item
// ============================================================================
static void process_control(int control)
{
switch (control) {
case IDM_EXIT:
PostQuitMessage(0);
break;
case IDM_ABOUT:
DialogBox(instance, MAKEINTRESOURCE(IDD_ABOUTBOX), 0, DlgProc);
break;
}
}
// ============================================================================
// Process a Windows message
// ============================================================================
static LRESULT process_message(HWND w, UINT m, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
switch (m) {
case WM_PAINT:
PAINTSTRUCT ps;
hdc = BeginPaint(w, &ps);
board.draw();
EndPaint(w, &ps);
break;
case WM_DESTROY:
shut_down();
break;
case WM_LBUTTONDOWN:
if (wParam & MK_SHIFT) {
process_mouse(lParam, Shift_Left_Mouse);
}
else {
process_mouse(lParam, Left_Mouse);
}
break;
case WM_RBUTTONDOWN:
process_mouse(lParam, Right_Mouse);
break;
case WM_MOUSEMOVE:
mouse_position.x = LOWORD (lParam);
mouse_position.y = HIWORD (lParam);
break;
case WM_CHAR:
process_key(wParam);
break;
case WM_COMMAND:
process_control(wParam);
break;
case MY_THREAD_RUNNING:
connect();
break;
default:
return DefWindowProc(w, m, wParam, lParam);
}
return 0;
}
// ============================================================================
// Windows procedure
// ============================================================================
static LRESULT CALLBACK WndProc(HWND w, UINT m, WPARAM wParam, LPARAM lParam)
{
LRESULT result;
try {
result = process_message(w, m, wParam, lParam);
}
catch (Exception & e) {
report_text(e.get_error().c_str());
}
catch (...) {
report_text("Exception thrown from Process Message");
}
return result;
}
// ============================================================================
// Process a mouse click
// ============================================================================
static void process_mouse(LPARAM lParam, Mouse_Command command)
{
mouse_position.x = LOWORD (lParam);
mouse_position.y = HIWORD (lParam);
server << "mouse " << command << " "
<< mouse_position.x << " " << mouse_position.y << "\n";
}
// ============================================================================
// Process a key press
// ============================================================================
static void process_key(WPARAM key)
{
int command (tolower(key));
server << "key " << key << " "
<< mouse_position.x << " " << mouse_position.y << "\n";
}
// ============================================================================
// Shut down this client
// ============================================================================
static void shut_down()
{
if (port_number != 0) {
server << "disconnect\n";
Sleep(100);
}
server.close();
PostQuitMessage(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -