aboutsummaryrefslogtreecommitdiff
path: root/terminal-game-utils.hpp
blob: d7d95229de0491f38268918e38616474365ae438 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <format>
#include <iostream>
#include <map>
#include <ostream>
#include <poll.h>
#include <string>
#include <termios.h>
#include <unistd.h>

namespace tgu {

inline struct termios orig_termios;

inline std::string newline = "\n\r";

enum class GameKey {
  INVALID,
  LEFTARROW,
  DOWNARROW,
  UPARROW,
  RIGHTARROW,
  ESCAPE,
  A,
  B,
  C,
  D,
  E,
  F,
  G,
  H,
  I,
  J,
  K,
  L,
  M,
  N,
  O,
  P,
  Q,
  R,
  S,
  T,
  U,
  V,
  W,
  X,
  Y,
  Z,
  SPACE,
};

inline std::map<std::string, GameKey> KeyMap = {{"a", GameKey::A},
                                                {"b", GameKey::B},
                                                {"c", GameKey::C},
                                                {"d", GameKey::D},
                                                {"e", GameKey::E},
                                                {"f", GameKey::F},
                                                {"g", GameKey::G},
                                                {"h", GameKey::H},
                                                {"i", GameKey::I},
                                                {"j", GameKey::J},
                                                {"k", GameKey::K},
                                                {"l", GameKey::L},
                                                {"m", GameKey::M},
                                                {"n", GameKey::N},
                                                {"o", GameKey::O},
                                                {"p", GameKey::P},
                                                {"q", GameKey::Q},
                                                {"r", GameKey::R},
                                                {"s", GameKey::S},
                                                {"t", GameKey::T},
                                                {"u", GameKey::U},
                                                {"v", GameKey::V},
                                                {"w", GameKey::W},
                                                {"x", GameKey::X},
                                                {"y", GameKey::Y},
                                                {"z", GameKey::Z},
                                                {"A", GameKey::A},
                                                {"B", GameKey::B},
                                                {"C", GameKey::C},
                                                {"D", GameKey::D},
                                                {"E", GameKey::E},
                                                {"F", GameKey::F},
                                                {"G", GameKey::G},
                                                {"H", GameKey::H},
                                                {"I", GameKey::I},
                                                {"J", GameKey::J},
                                                {"K", GameKey::K},
                                                {"L", GameKey::L},
                                                {"M", GameKey::M},
                                                {"N", GameKey::N},
                                                {"O", GameKey::O},
                                                {"P", GameKey::P},
                                                {"Q", GameKey::Q},
                                                {"R", GameKey::R},
                                                {"S", GameKey::S},
                                                {"T", GameKey::T},
                                                {"U", GameKey::U},
                                                {"V", GameKey::V},
                                                {"W", GameKey::W},
                                                {"X", GameKey::X},
                                                {"Y", GameKey::Y},
                                                {"Z", GameKey::Z},
                                                {"\x1b", GameKey::ESCAPE},
                                                {"\x1b[D", GameKey::LEFTARROW},
                                                {"\x1b[B", GameKey::DOWNARROW},
                                                {"\x1b[A", GameKey::UPARROW},
                                                {"\x1b[C", GameKey::RIGHTARROW},
                                                {" ", GameKey::SPACE}};

inline void DisableRawMode() { tcsetattr(0, TCSAFLUSH, &orig_termios); }

inline void EnableRawMode() {

  tcgetattr(0, &orig_termios);

  atexit(DisableRawMode);
  struct termios raw = orig_termios;
  raw.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON);
  raw.c_oflag &= ~(OPOST);
  raw.c_cflag |= (CS8);
  raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
  raw.c_cc[VMIN] = 1;
  raw.c_cc[VTIME] = 0;

  tcsetattr(0, TCSAFLUSH, &raw);
}

inline bool InputAvailable(int timeout_ms) {
  pollfd pfd{};
  pfd.fd = STDIN_FILENO;
  pfd.events = POLLIN;

  return poll(&pfd, 1, timeout_ms) > 0;
}

inline GameKey GetKeyPress() {
  std::string keypress_string;
  char c;

  read(STDIN_FILENO, &c, 1);
  keypress_string += c;

  if (c == 27)
    while (InputAvailable(10)) {
      read(STDIN_FILENO, &c, 1);
      keypress_string += c;
    }

  if (KeyMap.find(keypress_string) != KeyMap.end())
    return KeyMap[keypress_string];
  return GameKey::INVALID;
}

// Text printers:

inline void TextGreen() { std::cout << "\033[32m"; }

inline void TextRed() { std::cout << "\033[31m"; }

inline void TextBlue() { std::cout << "\033[34m"; }

inline void TextRGB(int r, int g, int b) {
  std::cout << std::format("\033[38;2;{};{};{}m", r, g, b);
};

// Background printers:

inline void BGGreen() { std::cout << "\033[42m"; }

inline void BGRed() { std::cout << "\033[41m"; }

inline void BGBlue() { std::cout << "\033[44m"; }

inline void BGRGB(int r, int g, int b) {
  std::cout << std::format("\033[48;2;{};{};{}m", r, g, b);
}

inline void TerminalColorReset() { std::cout << "\033[0m"; }

// Game buffer printers:

inline void EnterGameBuffer() { std::cout << "\033[?1049h"; }

inline void LeaveGameBuffer() { std::cout << "\033[?1049l"; }

inline void MoveCursor(int row, int col) {
  std::cout << "\033[" << (row + 1) << ";" << (col + 1) << "H" << std::flush;
}

inline void MakeCursorVisible() { std::cout << "\033[?25h"; }

inline void MakeCursorInvisible() {
  std::cout << "\033[?25l";
  atexit(MakeCursorVisible);
}

}; // namespace tgu