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
|
#include "app.h"
#include "physics.h"
#include "res.h"
#include "ent.h"
#include "game.h"
#include "env.h"
#include "cam.h"
#include "editor.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
App app = {0};
static void
app_prepare_view(void) {
if (app.scr_tex.texture.width != app.rw ||
app.scr_tex.texture.height != app.rh) {
if (app.scr_tex.id) {
UnloadRenderTexture(app.scr_tex);
}
app.scr_tex = LoadRenderTexture(app.rw, app.rh);
SetTextureFilter(app.scr_tex.texture, TEXTURE_FILTER_POINT);
}
f32 width = MAX(GetScreenWidth(), 1);
f32 height = MAX(GetScreenHeight(), 1);
f32 scale = fminf(
width / app.rw, height / app.rh);
app.scr_rec = (Rect){
.x = (width - app.rw * scale) * 0.5f,
.y = (height - app.rh * scale) * 0.5f,
.width = app.rw * scale,
.height = app.rh * scale,
};
}
Ray
app_screen_ray(Vec2 position, Camera camera) {
position.x = (position.x - app.scr_rec.x) *
app.rw / app.scr_rec.width;
position.y = (position.y - app.scr_rec.y) *
app.rh / app.scr_rec.height;
return GetScreenToWorldRayEx(
position, camera, app.rw, app.rh);
}
Vec2
app_world_to_screen(Vec3 position, Camera camera) {
Vec2 point = GetWorldToScreenEx(
position, camera, app.rw, app.rh);
return (Vec2){
app.scr_rec.x + point.x * app.scr_rec.width / app.rw,
app.scr_rec.y + point.y * app.scr_rec.height / app.rh,
};
}
static void
app_clear_depth(void) {
rlDrawRenderBatchActive();
rlEnableDepthMask();
rlColorMask(false, false, false, false);
rlClearScreenBuffers();
rlColorMask(true, true, true, true);
}
static void
app_init(void) {
SetTraceLogLevel(LOG_WARNING);
SetConfigFlags(
// FLAG_VSYNC_HINT |
FLAG_WINDOW_RESIZABLE);
InitWindow(1280, 720, "Fun Game");
app.rw = GetScreenWidth() / 2;
app.rh = GetScreenHeight() / 2;
SetExitKey(KEY_NULL);
// SetTargetFPS(60);
app.run = true;
FLAG_SET(app.flags, BIT(APP_FLAG_EDIT));
FLAG_SET(app.flags, BIT(APP_FLAG_DEBUG));
physics_init();
res_init();
env_init();
entity_system_init();
game_init();
editor_init();
cam_init();
}
static void
app_exit(void) {
UnloadRenderTexture(app.scr_tex);
editor_exit();
game_exit();
entity_system_exit();
env_exit();
res_exit();
physics_exit();
}
static void
app_loop(void) {
app.run = !WindowShouldClose();
f64 frame_start = GetTime();
app_prepare_view();
entity_system_update();
/* this is some hypothetical bullshit, probably will change */
if (app.mode == APP_MODE_EDIT) {
editor_update();
}
physics_update();
res_update();
env_update();
BeginDrawing();
ClearBackground(BLACK);
BeginTextureMode(app.scr_tex);
ClearBackground(RAYWHITE);
env_draw_sky(main_cam.cam);
BeginMode3D(main_cam.cam);
entity_system_draw_3d();
if (FLAG_CHECK(app.flags, BIT(APP_FLAG_DEBUG)) ||
FLAG_CHECK(app.flags, BIT(APP_FLAG_EDIT))) {
app_clear_depth();
rlEnableDepthTest();
editor_draw_3d();
}
EndMode3D();
EndTextureMode();
DrawTexturePro(app.scr_tex.texture,
(Rect){
0, 0,
(f32)app.scr_tex.texture.width,
-(f32)app.scr_tex.texture.height,
},
app.scr_rec, (Vec2){0}, 0, WHITE);
editor_draw_ui();
// app.frame_time_ms = (GetTime() - frame_start) * 1000.0f;
EndDrawing();
app.frame_time_ms = (GetTime() - frame_start) * 1000.0f;
}
int
main(void) {
app_init();
while (app.run) {
app_loop();
}
app_exit();
return 0;
}
|