summary refs log tree commit diff
path: root/sdl_gpu_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'sdl_gpu_test.c')
-rw-r--r--sdl_gpu_test.c48
1 files changed, 37 insertions, 11 deletions
diff --git a/sdl_gpu_test.c b/sdl_gpu_test.c
index 8bc6b73..30e6b83 100644
--- a/sdl_gpu_test.c
+++ b/sdl_gpu_test.c
@@ -3,8 +3,11 @@
 #define SDL_MAIN_USE_CALLBACKS 1
 #include <SDL3/SDL_main.h>
 #include <SDL3/SDL_gpu.h>
+#include <SDL3/SDL_timer.h>
+#include <cglm/cglm.h>
 #include <math.h>
 #include <stdio.h>
+#include <string.h>
 #include <err.h>
 
 SDL_Window *win;
@@ -202,6 +205,14 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
 	return SDL_APP_CONTINUE;
 }
 
+void build_cam_mat(mat4 dest, float width, float height, float x, float y, float scale, float rot) {
+	glm_mat4_identity(dest);
+	glm_scale(dest, (vec3) { height < width ? 1 : height / width, height < width ? width / height : 1, 1 });
+	glm_scale_uni(dest, scale);
+	glm_rotate_z(dest, rot, dest);
+	glm_translate(dest, (vec3) { -x, -y, 0 });
+}
+
 SDL_AppResult SDL_AppIterate(void *appstate) {
 	(void)appstate;
 
@@ -213,17 +224,32 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
 	CHECK(SDL_WaitAndAcquireGPUSwapchainTexture(cmdbuf, win, &swapchain, &swc_width, &swc_height), "swapchain texture acquisition");
 	CHECK(swapchain, "nil swapchain texture");
 
-	float how = (float)swc_height / (float)swc_width;
-	float woh = (float)swc_width / (float)swc_height;
-	float scale = (32 * 2) / fminf(swc_width, swc_height);
-	float scale_x = (swc_height < swc_width ? how : 1) * scale;
-	float scale_y = (swc_height < swc_width ? 1 : woh) * scale;
-	float cam_mat[4][4] = {
-		{ scale_x, 0, 0, 0, },
-		{ 0, scale_y, 0, 0, },
-		{ 0, 0, 1, 0, },
-		{ 0, 0, 0, 1, },
-	};
+	static float t = 0;
+	static Uint64 last = 0;
+	Uint64 now = SDL_GetTicksNS();
+	float dt = (now - last) / 1e9;
+	last = now;
+	t += dt;
+
+	float mx, my;
+	SDL_GetMouseState(&mx, &my);
+	mx -= swc_width * 0.5f;
+	my -= swc_height * 0.5f;
+	mx *= 1.0f / fminf(swc_width, swc_height);
+	my *= 1.0f / fminf(swc_width, swc_height);
+
+	float rot = t;
+
+	mat4 cam_mat;
+	static float x = 0, y = 0;
+
+	float s = sinf(-rot), c = cosf(-rot);
+
+	float dx = mx * dt * 10;
+	float dy = -my * dt * 10;
+	x += dx * c - dy * s;
+	y += dy * c + dx * s;
+	build_cam_mat(cam_mat, swc_width, swc_height, x, y, 0.1, rot);
 	SDL_PushGPUVertexUniformData(cmdbuf, 0, &cam_mat, sizeof(cam_mat));
 	SDL_GPURenderPass *render_pass = SDL_BeginGPURenderPass(
 		cmdbuf,