-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·198 lines (171 loc) · 6.59 KB
/
main.cpp
File metadata and controls
executable file
·198 lines (171 loc) · 6.59 KB
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
#pragma once
#include "../bml_includes.hpp"
#include <memory>
#include "../utils.hpp"
extern "C" {
__declspec(dllexport) IMod* BMLEntry(IBML* bml);
}
class SpriteTextHUD : public IMod {
bool init = false;
IProperty* prop_sr{}, * prop_fps{},
* prop_sr_color{}, * prop_fps_color{}, * prop_sr_font{}, * prop_fps_font{}, * prop_sr_z{}, * prop_fps_z{};
bool sr_active = false;
float sr_timer = 0;
int fps_count = 0, fps_timer = 0;
CKDWORD sr_color = 0xffffffff, fps_color = 0xffffffff;
std::unique_ptr<BGui::Text> sr_timer_title, sr_timer_score, fps_text;
utils utils;
void init_gui() {
if (prop_sr->GetBoolean()) {
sr_timer_title = std::make_unique<decltype(sr_timer_title)::element_type>("SpriteTextHUD_SR_Title");
sr_timer_title->SetSize({ 0.2f, 0.03f });
sr_timer_title->SetPosition({ 0.03f, 0.8f });
sr_timer_title->SetAlignment(CKSPRITETEXT_LEFT);
sr_timer_title->SetTextColor(sr_color);
sr_timer_title->SetZOrder(prop_sr_z->GetInteger());
sr_timer_title->SetFont(prop_sr_font->GetString(), utils.get_bgui_font_size(12), 400, false, false);
sr_timer_title->SetText("SR Timer");
sr_timer_title->SetVisible(m_bml->IsIngame());
sr_timer_score = std::make_unique<decltype(sr_timer_title)::element_type>("SpriteTextHUD_SR_Score");
sr_timer_score->SetSize({ 0.2f, 0.03f });
sr_timer_score->SetPosition({ 0.05f, 0.83f });
sr_timer_score->SetAlignment(CKSPRITETEXT_LEFT);
sr_timer_score->SetTextColor(sr_color);
sr_timer_score->SetZOrder(prop_sr_z->GetInteger());
sr_timer_score->SetFont(prop_sr_font->GetString(), utils.get_bgui_font_size(12), 400, false, false);
sr_timer_score->SetVisible(m_bml->IsIngame());
if (m_bml->IsIngame())
update_sr_text();
}
if (prop_fps->GetBoolean()) {
fps_text = std::make_unique<decltype(sr_timer_title)::element_type>("SpriteTextHUD_FPS");
fps_text->SetSize({ 0.2f, 0.03f });
fps_text->SetPosition({ 0.005f, 0.005f });
fps_text->SetAlignment(CKSPRITETEXT_LEFT);
fps_text->SetTextColor(fps_color);
fps_text->SetZOrder(prop_fps_z->GetInteger());
fps_text->SetFont(prop_fps_font->GetString(), utils.get_bgui_font_size(12), 400, false, false);
fps_text->SetVisible(true);
}
}
// color: fallback color
CKDWORD parse_prop_color(IProperty* prop, CKDWORD color) {
char color_text[8]{};
try {
color = (CKDWORD)std::stoul(prop->GetString(), nullptr, 16);
}
catch (const std::exception& e) {
GetLogger()->Warn("Error parsing the color code: %s. Resetting to %06X.", e.what(), color & 0x00FFFFFF);
}
snprintf(color_text, sizeof(color_text), "%06X", color & 0x00FFFFFF);
prop->SetString(color_text);
return color | 0xFF000000;
}
void load_config() {
sr_color = parse_prop_color(prop_sr_color, 0xFFFFFFFF);
fps_color = parse_prop_color(prop_fps_color, 0xFFFFFFFF);
sr_timer_title.reset();
sr_timer_score.reset();
fps_text.reset();
init_gui();
}
void update_sr_text() {
int counter = int(sr_timer);
int ms = counter % 1000;
counter /= 1000;
int s = counter % 60;
counter /= 60;
int m = counter % 60;
counter /= 60;
int h = counter % 100;
static char time[16];
snprintf(time, sizeof(time), "%02d:%02d:%02d.%03d", h, m, s, ms);
sr_timer_score->SetText(time);
}
public:
SpriteTextHUD(IBML* bml) : IMod(bml), utils(bml) {}
virtual iCKSTRING GetID() override { return "SpriteTextHUD"; }
virtual iCKSTRING GetVersion() override { return "0.1.2"; }
virtual iCKSTRING GetName() override { return "SpriteTextHUD"; }
virtual iCKSTRING GetAuthor() override { return "BallanceBug"; }
virtual iCKSTRING GetDescription() override { return "Replaces your FPS and SR Timer display with Sprite Texts for better framerates and customizations."; }
DECLARE_BML_VERSION;
void OnLoad() override {
auto config = GetConfig();
prop_sr = config->GetProperty("Main", "ShowSRTimer");
prop_sr->SetDefaultBoolean(true);
prop_fps = config->GetProperty("Main", "ShowFPS");
prop_fps->SetDefaultBoolean(true);
prop_sr_color = config->GetProperty("Style", "SRTimerColor");
prop_sr_color->SetDefaultString("FFFFFF");
prop_sr_color->SetComment("Color of SR Timer in HEX format.");
prop_fps_color = config->GetProperty("Style", "FPSColor");
prop_fps_color->SetDefaultString("FFFFFF");
prop_fps_color->SetComment("Color of FPS Display in HEX format.");
prop_sr_font = config->GetProperty("Style", "SRTimerFont");
prop_sr_font->SetDefaultString("Arial");
prop_sr_font->SetComment("Font of SR Timer.");
prop_fps_font = config->GetProperty("Style", "FPSFont");
prop_fps_font->SetDefaultString("Arial");
prop_fps_font->SetComment("Font of FPS Display.");
prop_sr_z = config->GetProperty("Style", "SR_ZOrder");
prop_sr_z->SetDefaultInteger(5);
prop_sr_z->SetComment("Z Order of SR Timer.");
prop_fps_z = config->GetProperty("Style", "FPS_ZOrder");
prop_fps_z->SetDefaultInteger(0);
prop_fps_z->SetComment("Z Order of FPS Display.");
}
void OnPostStartMenu() override {
if (init) return;
load_config();
init = true;
}
void OnProcess() override {
if (sr_active) {
sr_timer += m_bml->GetTimeManager()->GetLastDeltaTime();
if (sr_timer_score) update_sr_text();
}
if (fps_text) {
CKStats stats;
m_bml->GetCKContext()->GetProfileStats(&stats);
fps_count += int(1000 / stats.TotalFrameTime);
if (++fps_timer == 90) {
char text[16];
snprintf(text, sizeof(text), "FPS: %d", (fps_count + 45) / 90);
fps_text->SetText(text);
fps_timer = 0;
fps_count = 0;
}
}
}
void OnModifyConfig(CKSTRING category, CKSTRING key, IProperty* prop) {
load_config();
}
void OnStartLevel() override {
sr_timer = 0;
if (!sr_timer_score) return;
update_sr_text();
sr_timer_title->SetVisible(true);
sr_timer_score->SetVisible(true);
}
void OnPostExitLevel() override {
if (!sr_timer_score) return;
sr_timer_title->SetVisible(false);
sr_timer_score->SetVisible(false);
}
void OnPauseLevel() override {
sr_active = false;
}
void OnUnpauseLevel() override {
sr_active = true;
}
void OnCounterActive() override {
sr_active = true;
}
void OnCounterInactive() override {
sr_active = false;
}
};
IMod* BMLEntry(IBML* bml) {
return new SpriteTextHUD(bml);
}