This repository was archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
119 lines (98 loc) · 3.29 KB
/
main.cpp
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
//
#include "EnumEntityName.h" // Enum: Miner_Bob, Wife_Elsa
#include "Miner.h"
// #include "Locations.h"
#include "SingletonMessage.h"
#define SMI SingletonMessage::Instance()
#include <string>
#include <vector>
// Only this "main.cpp" code requires "raylib.h"
#include "raylib.h"
using namespace std;
#define RECT_WIDTH 800
#define RECT_HEIGHT 150
void DrawDialogueBox(const char *message, int frames_counter, bool enabled,
Texture2D texture);
int main() {
// create a Miner instance
Miner miner(Miner_Bob);
int window_width = 1000;
int window_height = 450;
// SetTraceLogLevel(LOG_NONE);
// SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(window_width, window_height, "Miner - Finite State Machine");
int frames_counter = 0;
int len = 0;
bool enabled_to_skip = false;
// vector<string> vec{};
string str{"( Press Space Key to Continue )"};
Image image_miner =
LoadImage("resources/miner.png"); // Load image in CPU memory (RAM)
ImageResize(&image_miner, 128, 128); // Resize flipped-cropped image
// Image converted to texture, GPU memory (RAM -> VRAM)
Texture2D texture = LoadTextureFromImage(image_miner);
// Unload image data from CPU memory (RAM)
UnloadImage(image_miner);
// FPS=200
SetTargetFPS(200);
while (!WindowShouldClose()) {
frames_counter++;
// press space key to play each message
if (IsKeyPressed(KEY_SPACE) && enabled_to_skip) {
frames_counter = 0;
enabled_to_skip = false;
// if message buffer is empty, then allow to Update
if (SMI->CheckEmpty() == true) {
miner.Update(); // call Update()
}
str = SMI->GetVal(); // get string vector from SingletonMessage buffer
SMI->DelFirstVal();
// SMI->ClearVector(); // clear SingletonMessage buffer
}
// control message play speed
len = str.length();
if (frames_counter >= len * 3.5) {
enabled_to_skip = true;
}
// Draw message box
BeginDrawing();
ClearBackground(BLACK);
DrawDialogueBox(str.c_str(), frames_counter, enabled_to_skip, texture);
EndDrawing();
// }
}
CloseWindow();
return 0;
}
void DrawDialogueBox(const char *message, int frames_counter, bool enabled,
Texture2D texture) {
Rectangle box = {(float)(GetScreenWidth() / 2 - RECT_WIDTH / 2),
(float)(GetScreenHeight() - (RECT_HEIGHT + 50)),
RECT_WIDTH,
RECT_HEIGHT};
// Draw message box background
DrawRectangleRec(box, DARKBLUE);
// Draw message box's frame
DrawRectangleLinesEx(box, 5, LIGHTGRAY);
// Draw character's avatar
DrawTexture(texture,
GetScreenWidth() / 6 - texture.width / 2,
GetScreenHeight() * 0.73 - texture.height / 2,
WHITE);
// Draw message
DrawText(TextSubtext(message, 0, frames_counter / 3), box.x + 120, box.y + 40,
20, WHITE);
// Draw message ending mark
if (enabled) {
// Draw rectangle
// DrawRectangle(box.x + RECT_WIDTH - 48, box.y + 105, 32, 32, WHITE);
// Draw triangle
DrawTriangle(
// triangle vertices
{box.x + RECT_WIDTH - 60, box.y + 105},
{box.x + RECT_WIDTH - 60 + 15, box.y + 105 + 30},
{box.x + RECT_WIDTH - 60 + 30, box.y + 105},
// triangle color
WHITE);
}
}