summaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-02 09:28:28 +0100
committerskal <pascal.massimino@gmail.com>2026-02-02 09:28:28 +0100
commit61139c8d9d655e07964d63ec1f5a091a7e8ab7d0 (patch)
tree4cf3694702f24e96c1c76a339f7b3e4d4fdf5e70 /src/tests
parent0b0067cb0a8db5ea5178501a12aacdef436a9845 (diff)
refactor(platform): Encapsulate state in PlatformState struct
- Replaced all global static variables in the platform layer with a single PlatformState struct. - Updated all platform function signatures to accept a pointer to this struct, making the implementation stateless and more modular. - Refactored main.cc, tests, and tools to instantiate and pass the PlatformState struct. - This improves code organization and removes scattered global state.
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/test_3d_render.cc37
1 files changed, 16 insertions, 21 deletions
diff --git a/src/tests/test_3d_render.cc b/src/tests/test_3d_render.cc
index 0a809c4..e14ed93 100644
--- a/src/tests/test_3d_render.cc
+++ b/src/tests/test_3d_render.cc
@@ -29,18 +29,16 @@ static WGPUQueue g_queue = nullptr;
static WGPUSurface g_surface = nullptr;
static WGPUAdapter g_adapter = nullptr;
static WGPUTextureFormat g_format = WGPUTextureFormat_Undefined;
-static int g_width = 1280;
-static int g_height = 720;
// ... (init_wgpu implementation same as before)
-void init_wgpu() {
+void init_wgpu(PlatformState* platform_state) {
WGPUInstance instance = wgpuCreateInstance(nullptr);
if (!instance) {
std::cerr << "Failed to create WGPU instance." << std::endl;
exit(1);
}
- g_surface = platform_create_wgpu_surface(instance);
+ g_surface = platform_create_wgpu_surface(instance, platform_state);
if (!g_surface) {
std::cerr << "Failed to create WGPU surface." << std::endl;
exit(1);
@@ -128,8 +126,8 @@ void init_wgpu() {
config.device = g_device;
config.format = g_format;
config.usage = WGPUTextureUsage_RenderAttachment;
- config.width = g_width;
- config.height = g_height;
+ config.width = platform_state->width;
+ config.height = platform_state->height;
config.presentMode = WGPUPresentMode_Fifo;
config.alphaMode = WGPUCompositeAlphaMode_Opaque;
wgpuSurfaceConfigure(g_surface, &config);
@@ -176,22 +174,21 @@ void gen_periodic_noise(uint8_t* buffer, int w, int h, const float* params,
int main() {
printf("Running 3D Renderer Test...\n");
- platform_init_window(false, nullptr, nullptr);
- gpu_init(platform_get_window(), platform_get_width(), platform_get_height());
-
- // Create Renderer and Scene
- Renderer3D renderer;
+ PlatformState platform_state = {};
+ platform_init(&platform_state, false, nullptr, nullptr);
+
+ // The test's own WGPU init sequence
+ init_wgpu(&platform_state);
g_renderer.init(g_device, g_queue, g_format);
- g_renderer.resize(g_width, g_height);
+ g_renderer.resize(platform_state.width, platform_state.height);
g_textures.init(g_device, g_queue);
ProceduralTextureDef noise_def;
noise_def.width = 256;
noise_def.height = 256;
noise_def.gen_func = gen_periodic_noise;
- noise_def.params = {1234.0f,
- 16.0f}; // Seed, Frequency (Increased for more detail)
+ noise_def.params = {1234.0f, 16.0f};
g_textures.create_procedural_texture("noise", noise_def);
g_renderer.set_noise_texture(g_textures.get_texture_view("noise"));
@@ -202,15 +199,16 @@ int main() {
g_camera.target = vec3(0, 0, 0);
float time = 0.0f;
- while (!platform_should_close()) {
- platform_poll();
- time += 0.016f;
+ while (!platform_should_close(&platform_state)) {
+ platform_poll(&platform_state);
+ time = (float)platform_get_time();
float cam_radius = 10.0f + std::sin(time * 0.3f) * 4.0f;
float cam_height = 5.0f + std::cos(time * 0.4f) * 3.0f;
g_camera.set_look_at(vec3(std::sin(time * 0.5f) * cam_radius, cam_height,
std::cos(time * 0.5f) * cam_radius),
vec3(0, 0, 0), vec3(0, 1, 0));
+ g_camera.aspect_ratio = platform_get_aspect_ratio(&platform_state);
for (size_t i = 1; i < g_scene.objects.size(); ++i) {
g_scene.objects[i].rotation =
@@ -225,11 +223,8 @@ int main() {
WGPUTextureViewDescriptor view_desc = {};
view_desc.format = g_format;
view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.baseMipLevel = 0;
view_desc.mipLevelCount = 1;
- view_desc.baseArrayLayer = 0;
view_desc.arrayLayerCount = 1;
- view_desc.aspect = WGPUTextureAspect_All;
WGPUTextureView view =
wgpuTextureCreateView(surface_tex.texture, &view_desc);
g_renderer.render(g_scene, g_camera, time, view);
@@ -241,6 +236,6 @@ int main() {
g_renderer.shutdown();
g_textures.shutdown();
- platform_shutdown();
+ platform_shutdown(&platform_state);
return 0;
}