-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrax.hpp
More file actions
354 lines (298 loc) · 10.2 KB
/
Frax.hpp
File metadata and controls
354 lines (298 loc) · 10.2 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/********************************************************************************************************************************************************
*
* Frax Framework v2.0 - A simple and easy-to-use framework.
*
* FEATURES:
* - NO forced external dependencies, expect raylib and standard library.
*
* NOTES:
* - In just a SINGLE file before including #define FRAX_IMPL
* - Macros are prefixed with FRAX
*
* DEPENDENCIES:
* [raylib] for almost everything.
*
* LICENSE: MIT
*
* MIT License
*
* Copyright (c) 2025 Cube Nerd (@mastercuber55)
*
* Permission is hereby granted, free of charge, to any person obtaining a
*copy of this software and associated documentation files (the "Software"), to
*deal in the Software without restriction, including without limitation the
*rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
*sell copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
* The above copyright notice, this permission notice shall be included in
*all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS
*OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
*OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*
********************************************************************************************************************************************************/
#ifndef FRAX_FRAMEWORK
#define FRAX_FRAMEWORK "2.0"
#ifdef ENABLE_FRAX_DEBUG
#define FRAX_DEBUG(...) TraceLog(LOG_INFO, __VA_ARGS__)
#else
#define FRAX_DEBUG(...) \
while (false) { \
}
#endif
#include <any>
#include <memory>
#include <raylib.h>
#include <string>
namespace Frax {
extern Vector2 ScreenSize;
extern bool KeepRunning;
//----------------------------------------------------------------------------------
// Structures Definitions (Module: Structures)
//----------------------------------------------------------------------------------
struct Scene {
std::any ReturnData;
Color BackgroundColor = BLACK;
Scene();
virtual bool ShouldClose();
virtual std::any Run();
virtual void Close(std::any returnData);
virtual void Update(const float& dt);
virtual void Draw();
~Scene();
bool KeepRunning;
};
// Used for easier rectangle manipulation
struct Rect {
// Variables
float x, y, w, h;
double Rotation;
std::shared_ptr<Texture> TexturePtr;
Rectangle Source;
Color Tint;
// De-Constructers
Rect(float x, float y, float w, float h, Color tint = WHITE);
Rect(Rectangle dest, Color tint = WHITE);
Rect(Rectangle dest, const std::string &textureFileStr,
Rectangle Source = {0, 0, 0, 0});
~Rect();
// Setters
void SetCenter(Vector2 cenpos);
void operator=(Vector2 pos);
void SetTextureFile(const std::string &textureFileStr);
// Getters
Vector2 GetCenter();
operator Vector2() const;
operator Rectangle() const;
// Utilities
void Draw();
};
//------------------------------------------------------------------------------------
// Initialization And Closing Functions (Module: Core)
//------------------------------------------------------------------------------------
void Init(const std::string &windowTitle, Vector2 scrnSize = {0, 0});
bool ShouldClose();
void Close();
//------------------------------------------------------------------------------------
// Tool Functions (Module: Tools)
//------------------------------------------------------------------------------------
void WASDMovement(Rect *obj, float speed);
bool AreColorSame(Color A, Color B);
Vector2 GetRandomPositionInside(Camera2D cam);
Vector2 GetRandomPositionOutside(Camera2D cam, float margin = 0.0f);
} // namespace Frax
#endif // FRAX_FRAMEWORK
//------------------------------------------------------------------------------------
// FRAX Implementation (Module: Implementation)
// No need to go any further, you know.
//------------------------------------------------------------------------------------
// #define FRAX_IMPL
#ifdef FRAX_IMPL
#undef FRAX_IMPL
#include <unordered_map>
//------------------------------------------------------------------------------------
// Variables (Module: Variables)
//------------------------------------------------------------------------------------
namespace Frax {
Vector2 ScreenSize;
bool KeepRunning = true;
// Might change the bottom two into pointer based system.
// Currently, if a texture is no longer needed it remains here, which needs to
// be solved.
std::hash<std::string> HashTextureKey;
std::unordered_map<size_t, std::weak_ptr<Texture>> Textures;
//----------------------------------------------------------------------------------
// Structures Definitions (Module: Structures)
//----------------------------------------------------------------------------------
Scene::Scene() { KeepRunning = true; }
void Scene::Update(const float& dt) { (void)dt; }
void Scene::Draw() {}
void Scene::Close(std::any returnData) {
KeepRunning = false;
ReturnData = returnData;
}
std::any Scene::Run() {
while (!this->ShouldClose()) {
//------------------------------------------------------------------------------------
// Event And Logic Handling
//------------------------------------------------------------------------------------
float dt = GetFrameTime();
this->Update(dt);
//------------------------------------------------------------------------------------
// Drawing
//------------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(BackgroundColor);
Draw();
EndDrawing();
}
return ReturnData;
}
bool Scene::ShouldClose() { return !KeepRunning || Frax::ShouldClose(); }
Scene::~Scene() {}
Rect::Rect(float x, float y, float w, float h, Color tint) {
this->x = x, this->y = y;
this->w = w, this->h = h;
this->Tint = tint;
this->Source = {0, 0, 0, 0}, this->Rotation = 0.0;
}
Rect::Rect(Rectangle dest, Color tint)
: Rect(dest.x, dest.y, dest.width, dest.height, tint) {}
Rect::Rect(Rectangle dest, const std::string &textureFileStr, Rectangle Source)
: Rect(dest, WHITE) {
this->Source = Source;
this->SetTextureFile(textureFileStr);
}
Rect::~Rect() {
if (TexturePtr)
TexturePtr.reset(); // Reduce reference count.
}
void Rect::SetTextureFile(const std::string &textureFileStr) {
auto hash = HashTextureKey(textureFileStr);
auto it = Textures.find(hash);
if (it != Textures.end()) {
TexturePtr = it->second.lock();
return;
}
TexturePtr = std::shared_ptr<Texture>(
new Texture(LoadTexture(textureFileStr.c_str())), [hash](Texture *t) {
UnloadTexture(*t);
delete t;
Textures.erase(hash);
});
Textures[hash] = TexturePtr;
}
void Rect::Draw() {
if (TexturePtr) {
Rectangle currSource =
(Source.height == 0 && Source.width == 0)
? Rectangle{0, 0, static_cast<float>(TexturePtr->width),
static_cast<float>(TexturePtr->height)}
: Source;
DrawTexturePro(*TexturePtr, currSource, {x + w / 2.0f, y + h / 2.0f, w, h},
{w / 2.0f, h / 2.0f}, Rotation, Tint);
} else {
DrawRectangle(x, y, w, h, this->Tint);
}
}
void Rect::operator=(Vector2 pos) {
x = pos.x;
y = pos.y;
}
void Rect::SetCenter(Vector2 cenpos) {
x = cenpos.x - w / 2;
y = cenpos.y - h / 2;
}
Vector2 Rect::GetCenter() { return {x + w / 2.0f, y + h / 2.0f}; }
Rect::operator Vector2() const { return {this->x, this->y}; }
Rect::operator Rectangle() const { return {x, y, w, h}; }
void Init(const std::string &title, Vector2 scrnSize) {
TraceLog(LOG_INFO, "Initializing Frax %s", FRAX_FRAMEWORK);
// Convinently, raylib allows passing 0, 0 to get maximum screen size possible
// on current monitor.
InitWindow(scrnSize.x, scrnSize.y, title.c_str());
InitAudioDevice();
if (scrnSize.x == 0 && scrnSize.y == 0) {
// Changed from GetMonitor...() to add android compability
scrnSize.x = GetScreenWidth();
scrnSize.y = GetScreenHeight();
}
ScreenSize = scrnSize;
SetExitKey(KEY_NULL);
}
bool ShouldClose() { return WindowShouldClose() || !KeepRunning; }
void Close() {
CloseAudioDevice();
CloseWindow();
TraceLog(LOG_INFO, "Frax closed successfully");
}
//------------------------------------------------------------------------------------
// Tool Functions (Module: Tools)
//------------------------------------------------------------------------------------
void WASDMovement(Rect *obj, float spd) {
if (IsKeyDown(KEY_W))
obj->y += -spd;
if (IsKeyDown(KEY_A))
obj->x += -spd;
if (IsKeyDown(KEY_S))
obj->y += +spd;
if (IsKeyDown(KEY_D))
obj->x += +spd;
}
bool AreColorSame(Color A, Color B) { return ColorToInt(A) == ColorToInt(B); }
Vector2 GetRandomPositionInside(Camera2D cam) {
return {(float)(GetRandomValue(cam.target.x - cam.offset.x,
cam.target.x + cam.offset.x)),
(float)(GetRandomValue(cam.target.y - cam.offset.y,
cam.target.y + cam.offset.y))};
}
Vector2 GetRandomPositionOutside(Camera2D cam, float margin) {
(void)margin; // TO CODE LATER
// 0 1, 2
// 3, 4, 5
// 6, 7, 8
int section = 4;
while (section == 4) {
section = GetRandomValue(0, 8);
}
Vector2 pos = GetRandomPositionInside(cam);
switch (section) {
case 0:
pos.x -= ScreenSize.x;
pos.y -= ScreenSize.y;
break;
case 1:
pos.y -= ScreenSize.y;
break;
case 2:
pos.x += ScreenSize.x;
pos.y -= ScreenSize.y;
break;
case 3:
pos.x -= ScreenSize.x;
break;
case 5:
pos.x += ScreenSize.x;
break;
case 6:
pos.x -= ScreenSize.x;
pos.y += ScreenSize.y;
break;
case 7:
pos.y += ScreenSize.y;
break;
case 8:
pos.x += ScreenSize.x;
pos.y += ScreenSize.y;
break;
}
return pos;
}
} // namespace Frax
#endif // FRAX_IMPL