techkaczynski.dev / code

summaryrefslogtreecommitdiff
path: root/app.c
diff options
context:
space:
mode:
Diffstat (limited to 'app.c')
-rw-r--r--app.c91
1 files changed, 82 insertions, 9 deletions
diff --git a/app.c b/app.c
index c1010d4..b82c588 100644
--- a/app.c
+++ b/app.c
@@ -13,15 +13,73 @@
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_MSAA_4X_HINT |
- FLAG_VSYNC_HINT |
+ 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);
+ // SetTargetFPS(60);
app.run = true;
FLAG_SET(app.flags, BIT(APP_FLAG_EDIT));
@@ -38,6 +96,7 @@ app_init(void) {
static void
app_exit(void) {
+ UnloadRenderTexture(app.scr_tex);
editor_exit();
game_exit();
entity_system_exit();
@@ -51,6 +110,7 @@ app_loop(void) {
app.run = !WindowShouldClose();
f64 frame_start = GetTime();
+ app_prepare_view();
entity_system_update();
/* this is some hypothetical bullshit, probably will change */
@@ -63,25 +123,38 @@ app_loop(void) {
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();
- EndMode3D();
if (FLAG_CHECK(app.flags, BIT(APP_FLAG_DEBUG)) ||
- FLAG_CHECK(app.flags, BIT(APP_FLAG_EDIT))) {
- BeginMode3D(main_cam.cam);
- rlDisableDepthTest();
+ FLAG_CHECK(app.flags, BIT(APP_FLAG_EDIT))) {
+ app_clear_depth();
+ rlEnableDepthTest();
editor_draw_3d();
- EndMode3D();
}
+ 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;
+ // app.frame_time_ms = (GetTime() - frame_start) * 1000.0f;
EndDrawing();
+ app.frame_time_ms = (GetTime() - frame_start) * 1000.0f;
}
int