-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cube2D.hpp
370 lines (302 loc) · 10.3 KB
/
Cube2D.hpp
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/********************************************************************************************************************************************************
*
* Cube2D Game Framework v1.2 - A simple and easy-to-use game emgine.
*
* FEATURES:
* - NO forced external dependencies, expect raylib.
* - Multiplatform: My guess is that its just as good as raylib.
* - Written in plain C code (C99) in PascalCase notation.
*
* NOTES:
* - In just a SINGLE file before including #define CGE_IMPLEMENTATION
* - Macros are prefixed with CGE
*
* DEPENDENCIES:
* [raylib] for almost everything.
*
* OPTIONAL DEPENDENCIES:
* [raygui] for GuiWIndow
*
* LICENSE: MIT
*
* MIT License
*
* Copyright (c) 2023 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 CUBE2D_FRAMEWORK
#define CUBE2D_FRAMEWORK "1.2"
#ifndef CGE_WHOLE_FILE
# define CGE_WHOLE_FILE { 0, 0, -1, -1 }
#endif // CGE_WHOLE_FILE
#ifndef CGE_SCENE_DEFAULT_BACKGROUND_COLOR
# define CGE_SCENE_DEFAULT_BACKGROUND_COLOR BLACK
#endif
#ifndef CGE_DEFAULT_FPS
# define CGE_DEFAULT_FPS 60
#endif
#include <raylib.h>
#ifdef CGE_RAYGUI
# ifdef CGE_IMPL
# define RAYGUI_IMPLEMENTATION
# endif
# include <raygui.h>
# include <functional>
#endif // CGE_RAYGUI
#include <string>
namespace Engine {
//----------------------------------------------------------------------------------
// Structures Definitions (Module: Structures)
//----------------------------------------------------------------------------------
// Scene, Used for scene management.
struct Scene {
Color BackgroundColor;
bool KeepRunning;
int CodeToReturn;
Scene(Color BackgroundColor = CGE_SCENE_DEFAULT_BACKGROUND_COLOR);
virtual bool ShouldClose();
virtual int Run();
virtual void Close(int CodeToReturn = 0);
virtual void Update();
virtual void Draw();
~Scene();
};
// Rect, Used for easier rectangle manipulation
struct Rect {
// Variables
float x, y, w, h;
double Rotation;
std::string TextureFile;
Rectangle Source;
Color Tint;
// De-Constructers
Rect(float x, float y, float w, float h);
Rect(Rectangle Destination, Color Tint);
Rect(
Rectangle Destination,
std::string TextureFile,
Rectangle Source = CGE_WHOLE_FILE
);
~Rect();
// Setters
void SetPosition(Vector2 NewPosition);
void operator=(Vector2 NewPosition);
void SetTextureFile(std::string TextureFile);
// Getters
Vector2 GetCenter();
Vector2 GetPosition();
Vector2 GetRectangle();
operator Vector2() const;
operator Rectangle() const;
// Utilities
bool IsColliding(Rect * Other);
void Draw();
};
#ifdef CGE_RAYGUI
struct GuiWindow {
bool Hidden;
float x, y, w, h;
std::string Title;
GuiWindow(Rectangle Destination, std::string Title);
void Update();
void Draw(std::function<void(Vector2 Offset)> Function);
private: bool Drag;
};
#endif // CGE_RAYGUI
//------------------------------------------------------------------------------------
// Initialization And Closing Functions (Module: Core)
//------------------------------------------------------------------------------------
//
void Init(int WindowWidth, int WindowHeight, std::string WindowTitle);
void Close();
//------------------------------------------------------------------------------------
// Tool Functions (Module: Tools)
//------------------------------------------------------------------------------------
void WASDMovement(Rect * Object, float Speed);
bool AreColorSame(Color A, Color B);
Vector2 GetRandomPosition(Camera2D Cam);
}
#endif // CUBE2D_FRAMEWORK
//------------------------------------------------------------------------------------
// CGE Implementation (Module: Implementation)
// No need to go any further you know.
//------------------------------------------------------------------------------------
#ifdef CGE_IMPL
#undef CGE_IMPL
#include <unordered_map>
#include <algorithm>
//------------------------------------------------------------------------------------
// Variables (Module: Variables)
//------------------------------------------------------------------------------------
namespace Engine {
std::unordered_map<std::string, Texture> Textures;
//----------------------------------------------------------------------------------
// Structures Definitions (Module: Structures)
//----------------------------------------------------------------------------------
Scene::Scene(Color BackgroundColor) {
this->BackgroundColor = BackgroundColor;
this->CodeToReturn = 0;
this->KeepRunning = true;
}
void Scene::Update() {}
void Scene::Draw() {}
void Scene::Close(int CodeToReturn) {
this->KeepRunning = false;
this->CodeToReturn = CodeToReturn;
}
int Scene::Run() {
while(!this->ShouldClose()) {
//------------------------------------------------------------------------------------
// Event And Logic Handling
//------------------------------------------------------------------------------------
this->Update();
//------------------------------------------------------------------------------------
// Drawing
//------------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(this->BackgroundColor);
this->Draw();
EndDrawing();
}
return this->CodeToReturn;
}
bool Scene::ShouldClose() {
return !KeepRunning || WindowShouldClose();
}
Scene::~Scene() {}
Rect::Rect(float x, float y, float w, float h) {
this->x = x, this->y = y;
this->w = w, this->h = h;
this->Tint = WHITE;
this->Source = CGE_WHOLE_FILE,
this->Rotation = 0.0;
}
Rect::Rect(Rectangle Rec, Color Tint) : Rect(Rec.x, Rec.y, Rec.width, Rec.height) {
this->Tint = Tint;
}
Rect::Rect(Rectangle Destination, std::string TextureFile, Rectangle Source) : Rect(Destination, WHITE) {
this->Source = Source;
this->SetTextureFile(TextureFile);
}
Rect::~Rect() {}
void Rect::SetTextureFile(std::string TextureFile) {
this->TextureFile = TextureFile;
if(Textures.find(TextureFile) != Textures.end()) return;
Textures[TextureFile] = LoadTexture(TextureFile.c_str());
if(
this->Source.x == 0 &&
this->Source.y == 0 &&
this->Source.width == -1 &&
this->Source.height == -1
) {
this->Source.width = Textures[TextureFile].width,
this->Source.height = Textures[TextureFile].height;
}
}
void Rect::Draw() {
(this->TextureFile.empty()) ?
DrawRectangle(this->x, this->y, this->w, this->h, this->Tint)
:
DrawTexturePro(
Textures[this->TextureFile],
this->Source,
(Rectangle){ this->x + this->w / 2.0f, this->y + this->h / 2.0f, this->w, this->h },
(Vector2){ this->w / 2.0f, this->h / 2.0f },
this->Rotation,
this->Tint
);
}
bool Rect::IsColliding(Rect * Other) {
return CheckCollisionRecs(*this, *Other);
}
void Rect::SetPosition(Vector2 Position) {
this->x = Position.x, this->y = Position.y;
}
void Rect::operator=(Vector2 NewPosition) {
this->SetPosition(NewPosition);
}
Vector2 Rect::GetCenter() {
return {this->x + this->w / 2.0f, this->y + this->h / 2.0f};
}
Vector2 Rect::GetPosition() {
return *this;
}
Vector2 Rect::GetRectangle() {
return *this;
}
Rect::operator Vector2() const {
return { this->x, this->y };
}
Rect::operator Rectangle() const {
return { this->x, this->y, this->w, this->h };
}
#ifdef RAYGUI_H
GuiWindow::GuiWindow(Rectangle Destination, std::string Title) {
this->x = Destination.x,
this->y = Destination.y,
this->w = Destination.width,
this->h = Destination.height;
this->Title = Title;
this->Hidden = true;
this->Drag = false;
}
void GuiWindow::Update() {
if(this->Hidden) return;
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && !Drag && CheckCollisionPointRec(GetMousePosition(), { x, y, w, 20.0f })) Drag = true;
if(Drag) {
Vector2 MouseDelta = GetMouseDelta();
this->x += MouseDelta.x;
this->y += MouseDelta.y;
if(IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) Drag = false;
}
}
void GuiWindow::Draw(std::function<void(Vector2 Offset)> Function) {
if(this->Hidden) return;
this->Hidden = GuiWindowBox((Rectangle){ x, y, w, h }, Title.c_str());
Function({ this->x, this->y + 20.0f });
}
#endif
void Init(int WindowWidth, int WindowHeight, std::string Title) {
TraceLog(LOG_INFO, "Initializing Cube2D %s", CUBE2D_FRAMEWORK);
InitWindow(WindowWidth, WindowHeight, Title.c_str());
InitAudioDevice();
SetExitKey(KEY_NULL);
SetTargetFPS(CGE_DEFAULT_FPS);
}
void Close() {
CloseAudioDevice();
CloseWindow();
TraceLog(LOG_INFO, "Cube2D closed successfully");
}
//------------------------------------------------------------------------------------
// Tool Functions (Module: Tools)
//------------------------------------------------------------------------------------
void WASDMovement(Rect * Object, float Speed) {
if(IsKeyDown(KEY_W)) Object->y += -Speed;
if(IsKeyDown(KEY_A)) Object->x += -Speed;
if(IsKeyDown(KEY_S)) Object->y += +Speed;
if(IsKeyDown(KEY_D)) Object->x += +Speed;
}
bool AreColorSame(Color A, Color B) {
return ColorToInt(A) == ColorToInt(B);
}
Vector2 GetRandomPosition(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))
};
}
}
#endif // CGE_IMPLEMENTATION