Skip to content

Commit 5d0f557

Browse files
committed
Full button support
1 parent 1d1a2f0 commit 5d0f557

File tree

12 files changed

+188
-27
lines changed

12 files changed

+188
-27
lines changed

gamebook/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ project(client VERSION ${PROJECT_VERSION})
66

77
option(client -G)
88

9-
set(CMAKE_C_STANDARD 17)
10-
set(CMAKE_CXX_STANDARD 17)
9+
set(CMAKE_C_STANDARD 23)
10+
set(CMAKE_CXX_STANDARD 23)
1111

12-
file(GLOB sources "source/*.cpp" "source/filesystem/*.cpp" "source/logicstr/*.cpp" "source/io/*.cpp")
12+
file(GLOB sources "source/*.cpp" "source/filesystem/*.cpp" "source/logicstr/*.cpp" "source/io/*.cpp" "source/render/*.cpp")
1313

1414
message("\nCode sources found: ${sources}\n")
1515

gamebook/cmake/client.pdb

56 KB
Binary file not shown.

gamebook/source/io/buttonmanager.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,64 @@
11
#include "buttonmanager.h"
2+
#include "iomath.h"
23

34
#ifdef __EMSCRIPTEN__
4-
#include <emscripten.h>
5+
#include <emscripten.h>
6+
#else
7+
#include <conio.h>
58
#endif
69

710
string ButtonManager::GetLastPressed() {
811
#ifdef __EMSCRIPTEN__
9-
return emscripten_run_script_string("lastPressed");
12+
return emscripten_run_script_string("lastPressed");
13+
#else
14+
return this->lastPressed;
1015
#endif
11-
return "none";
1216
}
1317

1418
void ButtonManager::CreateButton(string button) {
1519
#ifdef __EMSCRIPTEN__
16-
EM_ASM({
17-
createButton(UTF8ToString($0));
18-
}, button.c_str());
20+
EM_ASM({
21+
createButton(UTF8ToString($0));
22+
}, button.c_str());
23+
#else
24+
this->buffer.push_back(button);
1925
#endif
2026
}
2127

2228
void ButtonManager::ResetButtons() {
2329
#ifdef __EMSCRIPTEN__
2430
emscripten_run_script("resetButtons();");
25-
#endif
31+
#else
32+
this->buffer = vector<string>();
33+
this->index = 0;
34+
this->lastPressed = "none";
35+
#endif
2636
}
2737

2838
int ButtonManager::GetButtonCount() {
2939
#ifdef __EMSCRIPTEN__
3040
return emscripten_run_script_int("buttons.length");
41+
#else
42+
return this->buffer.size() - 1;
43+
#endif
44+
}
45+
46+
void ButtonManager::Update() {
47+
#ifdef __EMSCRIPTEN__
48+
#else
49+
auto input = _getch();
50+
51+
if (input==0 || input==0xE0) {
52+
input = _getch();
53+
54+
if (input == KEY_UP)
55+
this->index++;
56+
else if (input == KEY_DOWN)
57+
this->index--;
58+
59+
} else if (input == KEY_ENTER && !this->buffer.empty()) {
60+
this->lastPressed = this->buffer[this->index];
61+
}
62+
clamp_int(&this->index, 0, this->GetButtonCount());
3163
#endif
32-
return 0;
3364
}

gamebook/source/io/buttonmanager.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
#include <vector>
44
#include <string>
55

6+
#include "iomath.h"
7+
68
using namespace std;
79

10+
#define KEY_ENTER 13
11+
#define KEY_UP 72
12+
#define KEY_DOWN 80
13+
814
/// Work only on the web
915
class ButtonManager {
1016
public:
@@ -13,4 +19,11 @@ class ButtonManager {
1319
void CreateButton(string button);
1420
string GetLastPressed();
1521
void ResetButtons();
22+
#ifdef __EMSCRIPTEN__
23+
#else
24+
private:
25+
string lastPressed = "none";
26+
int index = 0;
27+
vector<string> buffer;
28+
#endif
1629
};

gamebook/source/io/iomath.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "iomath.h"
2+
3+
#include <iostream>
4+
5+
void clamp_int(int* base, int min, int max) {
6+
if (*base < min)
7+
*base = min;
8+
else if (*base > max)
9+
*base = max;
10+
}

gamebook/source/io/iomath.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void clamp_int(int* base, int min, int max);

gamebook/source/main.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <iostream>
22

3+
#include "render/console.h"
34
#include "io/buttonmanager.h"
5+
46
#include <string>
57

68
#ifdef __EMSCRIPTEN__
@@ -9,19 +11,30 @@
911

1012
using namespace std;
1113

12-
void game() {
13-
auto buttonManager = ButtonManager();
14-
if (buttonManager.GetButtonCount() < 5)
15-
buttonManager.CreateButton("myButton");
16-
cout << buttonManager.GetButtonCount() << endl;
14+
long long int counter = 0;
15+
16+
auto buttonManager = ButtonManager();
17+
auto console = Console();
18+
19+
void GameLoop() {
1720
if (buttonManager.GetLastPressed() != "none") {
18-
cout << buttonManager.GetLastPressed() << endl;
19-
}
21+
cout << "You: " << buttonManager.GetLastPressed() << endl;
22+
buttonManager.ResetButtons();
23+
buttonManager.CreateButton("Hello !");
24+
} else if (buttonManager.GetButtonCount() != 1)
25+
buttonManager.CreateButton("Hello !");
2026
}
2127

2228
int main() {
29+
console.SetWindow(600, 600);
30+
cout << "Game: Hello !" << endl;
31+
2332
#ifdef __EMSCRIPTEN__
24-
emscripten_set_main_loop(game, 0, 1);
33+
emscripten_set_main_loop(GameLoop, 0, 1);
34+
#else
35+
while(true) {
36+
GameLoop();
37+
}
2538
#endif
2639
return 0;
2740
}

gamebook/source/render/console.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,77 @@
11
#include "console.h"
2+
3+
#ifdef _WIN32
4+
#include <windows.h>
5+
#else
6+
#include <sys/ioctl.h>
7+
#include <unistd.h>
8+
#endif
9+
10+
#ifdef __EMSCRIPTEN__
11+
#include <emscripten.h>
12+
#endif
13+
14+
Console::Console() {
15+
// Optimize the console
16+
#ifdef _WIN32
17+
std::ios_base::sync_with_stdio(false);
18+
setvbuf(stdout, NULL, _IONBF, 0);
19+
#endif
20+
}
21+
22+
Console::~Console() {
23+
#ifdef _WIN32
24+
HWND hwnd = GetConsoleWindow();
25+
26+
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
27+
GetConsoleScreenBufferInfo(hwnd, &consoleInfo);
28+
WORD defaultAttribute = consoleInfo.wAttributes;
29+
SetConsoleTextAttribute(hwnd, defaultAttribute);
30+
#elif __EMSCRIPTEN__
31+
#else
32+
std::cout << COLOR_DEFAULT << std::endl;
33+
#endif
34+
}
35+
36+
void Console::SetWindow(int x, int y) {
37+
#ifdef _WIN32
38+
HWND hwnd = GetConsoleWindow();
39+
RECT rect = {100, 100, x, y};
40+
MoveWindow(hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,TRUE);
41+
LONG dwStyle = GetWindowLong(hwnd, GWL_STYLE);
42+
dwStyle &= ~(WS_MAXIMIZEBOX | WS_SIZEBOX);
43+
SetWindowLong(hwnd, GWL_STYLE, dwStyle);
44+
45+
WORD colorAttribute = FOREGROUND_GREEN;
46+
SetConsoleTextAttribute(hwnd, colorAttribute);
47+
#elif __EMSCRIPTEN__
48+
#else
49+
std::cout << COLOR_FOREGROUND_GREEN << COLOR_BACKGROUND_BLACK << std::endl;
50+
51+
struct winsize w;
52+
w.ws_row = x;
53+
w.ws_col = y;
54+
ioctl(STDOUT_FILENO, TIOCSWINSZ, &w);
55+
#endif
56+
}
57+
58+
void Console::WaitAny() {
59+
std::cin.get();
60+
}
61+
62+
void Console::Clear() {
63+
#ifdef __EMSCRIPTEN__
64+
emscripten_run_script("document.getElementById(\"output\").value = \"\";");
65+
#else
66+
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
67+
CONSOLE_SCREEN_BUFFER_INFO csbi;
68+
DWORD count;
69+
COORD coord = { 0, 0 };
70+
71+
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
72+
{
73+
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
74+
SetConsoleCursorPosition(hStdOut, coord);
75+
}
76+
#endif
77+
}

gamebook/source/render/console.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#pragma once
22

33
#include <string>
4+
#include <iostream>
45

5-
using namespace std;
6+
#define COLOR_FOREGROUND_GREEN "\033[32m"
7+
#define COLOR_BACKGROUND_BLACK "\033[40m"
8+
#define COLOR_DEFAULT "\033[0m"
69

710
class Console {
811
public:
9-
void PrintText(string input);
10-
void PrintLine();
11-
void MoveCursor();
12+
Console();
13+
~Console();
14+
void SetWindow(int x, int y);
15+
void WaitAny();
1216
void Clear();
13-
private:
14-
float printSpeed = 1;
15-
bool proceduralTextPrint = false;
1617
};

gamebook/web/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@
199199

200200
<div id="buttons">
201201
<script>
202-
203202
var buttons = []
204203

205204
function createButton(id) {

0 commit comments

Comments
 (0)