1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include <string>
#include <Windows.h>
const unsigned map_width = 20;
const unsigned map_height = 20;
int the_map[map_height][map_width] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
char map_rep [] = { '.', '#' };
namespace con
{
using coord = COORD;
using handle = HANDLE;
handle get_handle_in()
{
static handle h{ GetStdHandle(STD_INPUT_HANDLE) };
return h;
}
handle get_handle_out()
{
static handle h{ GetStdHandle(STD_OUTPUT_HANDLE) };
return h ;
}
void clear(coord home_pos = { 0, 0 }, char ch = ' ')
{
auto h = get_handle_out();
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(h, &info))
return;
unsigned console_size = info.dwSize.X * info.dwSize.Y;
DWORD dummy;
FillConsoleOutputCharacterA(h, ch, console_size, { 0, 0 }, &dummy);
FillConsoleOutputAttribute(h, info.wAttributes, console_size, { 0, 0 }, &dummy);
SetConsoleCursorPosition(h, home_pos);
}
void set_cursor_pos(coord pos)
{
SetConsoleCursorPosition(get_handle_out(), pos);
}
void put_char(char ch, coord pos)
{
auto h = get_handle_out();
DWORD dummy;
FillConsoleOutputCharacterA(h, ch, 1, pos, &dummy);
}
void put_str(const std::string& s, coord pos)
{
auto h = get_handle_out();
DWORD dummy;
WriteConsoleOutputCharacterA(h, s.c_str(), s.length(), pos, &dummy);
}
char read_char()
{
auto h = get_handle_in();
DWORD dummy;
INPUT_RECORD record;
for (;;)
{
// We're only going to process key released events.
do
{
ReadConsoleInputA(h, &record, 1, &dummy);
} while (record.EventType != KEY_EVENT);
if (!record.Event.KeyEvent.bKeyDown)
return record.Event.KeyEvent.uChar.AsciiChar;
}
}
}
void draw_map()
{
for (unsigned row = 0; row < map_height; ++row)
{
std::string s;
for (unsigned col = 0; col < map_width; ++col)
s += map_rep[the_map[row][col]];
con::put_str(s, { 0, row });
}
}
void draw_object(char rep, con::coord pos)
{
con::put_char(rep, pos);
}
void clip(con::coord& to_clip, con::coord a, con::coord b)
{
if (to_clip.X < a.X)
to_clip.X = a.X;
else if (to_clip.X > b.X)
to_clip.X = b.X;
if (to_clip.Y < a.Y)
to_clip.Y = a.Y;
else if (to_clip.Y > b.Y)
to_clip.Y = b.Y;
}
int main()
{
const con::coord home_pos{ 0, map_height };
con::coord player_pos{ 5, 5 };
for (;;)
{
con::clear(home_pos);
draw_map();
draw_object('@', player_pos);
switch (con::read_char())
{
case 'w': --player_pos.Y; break;
case 's': ++player_pos.Y; break;
case 'd': ++player_pos.X; break;
case 'a': --player_pos.X; break;
}
clip(player_pos, { 0, 0 }, { map_width - 1, map_height - 1 });
}
}
|