📄 client_interface_win.hpp
字号:
/* Copyright(c) Ben Bear 2003-2004 */
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
#ifndef __client_interface_win_hpp
#define __client_interface_win_hpp
#include <cstdio>
#include <cstring>
#include <windows.h>
// oh, i wish your WIN32 compiler has this header file
#include <conio.h>
#include "info.hpp"
#include "honey_hole.hpp"
const int COLOR_TEXT = LIGHTGRAY;
const int COLOR_NONE = DARKGRAY;
const int COLOR_PLAYER[6] = {LIGHTCYAN, LIGHTMAGENTA, WHITE,
LIGHTRED, LIGHTBLUE, LIGHTGREEN};
const int COLOR_HELP = BROWN;
const int COLOR_JUMP = YELLOW;
const int COLOR_INSIDE = RED;
const char SIGN_NONE = '.';
const char SIGN_PLAYER = 'o';
const char SIGN_HELP = 'e';
const char SIGN_JUMP = '*';
const char SIGN_INSIDE = SIGN_NONE;
const int X_CENTER = 40;
const int Y_CENTER = 12;
class client_interface
{
protected:
HANDLE std_out;
public:
client_interface ();
~client_interface ();
bool move (int y, int x);
bool kbhit ();
int command ();
void text_color (int color);
void show_chess (int h, int ch);
void show_name (int p, int g, const char* name);
void clear ();
void out_text (int y, int x, const char* str);
};
client_interface::client_interface ()
{
std_out = GetStdHandle (STD_OUTPUT_HANDLE);
COORD coord = {80, 25};
SetConsoleScreenBufferSize (std_out, coord);
move (0, 0);
}
client_interface::~client_interface ()
{
COORD coord = {80, 300};
SetConsoleScreenBufferSize (std_out, coord);
}
bool
client_interface::move (int y, int x)
{
COORD c;
c.X = x;
c.Y = y;
return SetConsoleCursorPosition (std_out, c);
}
bool
client_interface::kbhit ()
{
return ::kbhit ();
}
int
client_interface::command ()
{
int ch = getch();
if (ch == 0)
{
ch = getch();
}
else
{
switch (ch)
{
case '1': ch = KB_L_DOWN; break;
case '2': ch = KB_DOWN; break;
case '3': ch = KB_R_DOWN; break;
case '4': ch = KB_LEFT; break;
case '5': ch = KB_MODE; break;
case '6': ch = KB_RIGHT; break;
case '7': ch = KB_L_UP; break;
case '8': ch = KB_UP; break;
case '9': ch = KB_R_UP; break;
case 'O':
case 'o': ch = KB_FINISH; break;
case 'Q':
case 'q': ch = KB_QUIT; break;
default: ch += 0xFF00; break;
}
}
return ch;
}
void
client_interface::text_color (int color)
{
SetConsoleTextAttribute (std_out, color);
}
void
client_interface::show_chess (int h, int ch)
{
int x, y;
honey_hole hole (h);
hole.get (x, y);
int c;
char s;
if (ch == CHESS_NONE)
{
c = COLOR_NONE;
s = SIGN_NONE;
}
else if ((ch >= 0) && (ch < 6))
{
c = COLOR_PLAYER[ch];
s = SIGN_PLAYER;
}
else if (ch == CHESS_HELP)
{
c = COLOR_HELP;
s = SIGN_HELP;
}
else if (ch == CHESS_JUMP)
{
c = COLOR_JUMP;
s = SIGN_JUMP;
}
else if (ch == CHESS_INSIDE)
{
c = COLOR_INSIDE;
s = SIGN_INSIDE;
}
text_color (c);
move (Y_CENTER + y, X_CENTER + x * 2 - y);
std::printf ("%c", s);
}
void
client_interface::show_name (int p, int g, const char* name)
{
int x, y, c;
c = std::strlen (name)+2;
if (p == 0)
{
x = 65;
y = 18;
c = x;
}
else if (p == 1)
{
x = 35;
y = 23;
c = X_CENTER - c / 2;
}
else if (p == 2)
{
x = 5;
y = 18;
c = PLAYER_NAME_LEN + x - c;
}
else if (p == 3)
{
x = 5;
y = 5;
c = PLAYER_NAME_LEN + x - c;
}
else if (p == 4)
{
x = 65;
y = 1;
c = X_CENTER - c / 2;
}
else if (p == 5)
{
x = 65;
y = 5;
c = x;
}
move (y, x);
std::printf (" ");
text_color (COLOR_PLAYER[g]);//COLOR_TEXT);
move (y, c);
std::printf ("%d:%s", g, name);
}
void
client_interface::clear ()
{
DWORD written;
COORD c = { 0, 0 };
int length = 80 * 25;
FillConsoleOutputCharacter( std_out, ' ', length, c, &written );
}
void
client_interface::out_text (int y, int x, const char* str)
{
text_color (COLOR_TEXT);
move (y, x);
std::printf ("%s", str);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -