techkaczynski.dev / code

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app.c91
-rw-r--r--app.h6
-rw-r--r--cam.c3
-rw-r--r--editor.c44
-rw-r--r--env.c11
5 files changed, 123 insertions, 32 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
diff --git a/app.h b/app.h
index 1f0ff23..a635923 100644
--- a/app.h
+++ b/app.h
@@ -14,7 +14,9 @@ typedef enum {
} AppFlag;
typedef struct {
- int w, h;
+ RenderTexture scr_tex;
+ Rect scr_rec;
+ int w, h, rw, rh;
f32 frame_time_ms;
AppMode mode;
AppFlag flags;
@@ -23,6 +25,8 @@ typedef struct {
extern App app;
+Ray app_screen_ray(Vec2 position, Camera camera);
+Vec2 app_world_to_screen(Vec3 position, Camera camera);
void app_notify(cstr title, cstr msg);
#endif
diff --git a/cam.c b/cam.c
index 6827709..fb73718 100644
--- a/cam.c
+++ b/cam.c
@@ -1,4 +1,5 @@
#include "cam.h"
+#include "app.h"
#include "reasings.h"
#include <rcamera.h>
@@ -410,7 +411,7 @@ cam_ctrl_orbit(void) {
Vec3 up = Vector3Normalize(
Vector3CrossProduct(right, forward));
- f32 scale = view_height / GetScreenHeight();
+ f32 scale = view_height / app.scr_rec.height;
Vec3 offset = Vector3Add(
Vector3Scale(right, -delta.x * scale),
Vector3Scale(up, delta.y * scale));
diff --git a/editor.c b/editor.c
index 9257870..4fa4235 100644
--- a/editor.c
+++ b/editor.c
@@ -757,23 +757,23 @@ editor_focus_selection(void) {
f32 radius = fmaxf(
Vector3Distance(minimum, maximum) * 0.5f, 0.5f);
- /* Leave space for panels on either side of the view. */
- f32 width = GetScreenWidth();
- f32 height = GetScreenHeight();
+ /* Leave space for panels covering the displayed scene. */
+ Rect viewport = app.scr_rec;
f32 margin = 0;
if (ed.panel[ED_PNL_LEFT].enabled) {
Rect bounds = ed.panel[ED_PNL_LEFT].bounds;
- margin = fmaxf(margin, bounds.x + bounds.width);
+ margin = fmaxf(margin,bounds.x + bounds.width - viewport.x);
}
if (ed.panel[ED_PNL_RIGHT].enabled) {
Rect bounds = ed.panel[ED_PNL_RIGHT].bounds;
- margin = fmaxf(margin, width - bounds.x);
+ margin = fmaxf(margin, viewport.x + viewport.width - bounds.x);
}
- f32 visible_width = fmaxf(width - 2.0f * margin, 1.0f);
- f32 aspect = visible_width / height;
+ f32 visible_width = fmaxf(viewport.width - 2.0f * margin, 1.0f);
+ f32 aspect = visible_width / viewport.height;
+
f32 half_fov = main_cam.perspective_fov * DEG2RAD * 0.5f;
f32 horizontal = atanf(tanf(half_fov) * aspect);
f32 angle = fminf(half_fov, horizontal);
@@ -922,7 +922,9 @@ case ED_INTERACT_NONE:
}
}
- if (ed.focus != 0) {
+ if (ed.focus != 0 ||
+ !CheckCollisionPointRec(GetMousePosition(),
+ app.scr_rec)) {
break;
}
@@ -1107,7 +1109,11 @@ default:
/* tools */
static void
sel_ray(void) {
- Ray ray = GetScreenToWorldRay(end_point, main_cam.cam);
+ if (!CheckCollisionPointRec(end_point, app.scr_rec)) {
+ return;
+ }
+
+ Ray ray = app_screen_ray(end_point, main_cam.cam);
Vec3 dir = Vector3Scale(ray.direction, 1000.0f);
b3QueryFilter filter = {
@@ -1154,7 +1160,7 @@ physics_pick(void) {
return;
}
- Ray ray = GetScreenToWorldRay(GetMousePosition(), main_cam.cam);
+ Ray ray = app_screen_ray(GetMousePosition(), main_cam.cam);
Vec3 dir = Vector3Scale(ray.direction, 1000.0f);
b3Pos origin = {
@@ -1212,7 +1218,7 @@ sel_area(void) {
continue;
}
- Vec2 pos = GetWorldToScreen(world_pos, main_cam.cam);
+ Vec2 pos = app_world_to_screen(world_pos, main_cam.cam);
if (CheckCollisionPointRec(pos, sel_rec)) {
ent->ed.selected = true;
}
@@ -1301,7 +1307,7 @@ editor_select(void) {
static bool
ray_plane_hit(Vec3 plane_pos, Vec3 plane_dir, Vec3 *out) {
- Ray ray = GetScreenToWorldRay(GetMousePosition(), main_cam.cam);
+ Ray ray = app_screen_ray(GetMousePosition(), main_cam.cam);
f32 denom = Vector3DotProduct(ray.direction, plane_dir);
if (fabsf(denom) < 0.0001f) {
@@ -1481,7 +1487,11 @@ editor_move(void) {
Vec3 delta = {0};
if (surface) {
- Ray ray = GetScreenToWorldRay(
+ if (!CheckCollisionPointRec(GetMousePosition(), app.scr_rec)) {
+ return;
+ }
+
+ Ray ray = app_screen_ray(
GetMousePosition(), main_cam.cam);
Vec3 direction = Vector3Scale(ray.direction, 1000.0f);
@@ -1530,8 +1540,8 @@ editor_move(void) {
return;
}
- Vec2 a = GetWorldToScreen(pivot, main_cam.cam);
- Vec2 b = GetWorldToScreen(
+ Vec2 a = app_world_to_screen(pivot, main_cam.cam);
+ Vec2 b = app_world_to_screen(
Vector3Add(pivot, axis), main_cam.cam);
Vec2 screen_axis = Vector2Subtract(b, a);
f32 length_squared = Vector2LengthSqr(screen_axis);
@@ -5502,7 +5512,7 @@ gizmo_rotate(void) {
tanf(main_cam.cam.fovy * DEG2RAD * 0.5f);
}
- f32 radius = view_height * 65.0f / GetScreenHeight();
+ f32 radius = view_height * 65.0f / app.scr_rec.height;
if (ed.rotate_group) {
DrawSphereEx(pivot, radius * 0.04f, 4, 6, WHITE);
@@ -5593,7 +5603,7 @@ gizmo_scale(void) {
tanf(main_cam.cam.fovy * DEG2RAD * 0.5f);
}
- f32 pixel = view_height / GetScreenHeight();
+ f32 pixel = view_height / app.scr_rec.height;
if (!ed.scale_group || i == 0) {
DrawSphereEx(pivot, pixel * 3.0f, 4, 6, WHITE);
diff --git a/env.c b/env.c
index efcdfc2..b55bb09 100644
--- a/env.c
+++ b/env.c
@@ -53,14 +53,15 @@ sky_draw_symbol(Texture tex, Vec3 dir, Camera3D cam, f32 angular_diameter,
// distance is arbitrary. Only the direction matters because result is
// projected into screen space.
Vec3 world_pos = Vector3Add(cam.position, Vector3Scale(dir, 1000.0f));
- Vec2 scr_pos = GetWorldToScreen(world_pos, cam);
+ Vec2 scr_pos = GetWorldToScreenEx(world_pos, cam,
+ rlGetFramebufferWidth(), rlGetFramebufferHeight());
// Convert the requested angular diameter into pixels. This makes the
// symbols respond correctly when camera FOV changes.
f32 vfov = cam.fovy * DEG2RAD;
f32 diameter =
- (f32)GetScreenHeight() *
+ (f32)rlGetFramebufferHeight() *
tanf(angular_diameter * DEG2RAD * 0.5f) /
tanf(vfov * 0.5f);
@@ -275,8 +276,10 @@ env_update(void) {
void
env_draw_sky(Camera cam) {
- int scr_w = GetScreenWidth();
- int scr_h = GetScreenHeight();
+ // int scr_w = GetScreenWidth();
+ // int scr_h = GetScreenHeight();
+ int scr_w = rlGetFramebufferWidth();
+ int scr_h = rlGetFramebufferHeight();
if (scr_w <= 0 || scr_h <= 0) {
return;