platform.h

cross platform rendering playground

src/core/platform.h

2.08 KB
#pragma once
#include <vector>

#include "backend/rhi.h"
#include "globals.h"

struct PlatformWindow;

enum class WindowMode {
    Windowed,
    Maximized,
    Fullscreen
    // exclusive?, I haven't looked into glfw hints to see if this is somehow truly exclusive exclusive
    // I have validated indepedent flip for windowed/bordless though, so it doens't matter too much ig
};

// TODO: decouple from cam
struct PlatformInput {
    f32 dt;

    f32 mouse_dx = 0.0f;
    f32 mouse_dy = 0.0f;

    bool move_forward = false;  // W
    bool move_backward = false; // S
    bool move_left = false;     // A
    bool move_right = false;    // D
    bool move_up = false;       // E
    bool move_down = false;     // Q
    bool fast = false;          // Shift
};

struct Platform {
    PlatformWindow *window = nullptr;
    bool framebuffer_resized = false;
    bool captured = false;
    u32 width;
    u32 height;
    f32 pending_mouse_dx = 0.0f;
    f32 pending_mouse_dy = 0.0f;

    int should_close;
    f64 dt;     // max delta hitch limit baked
    f64 raw_dt; // unclamped frame delta

    f64 last_mouse_x = 0.0;
    f64 last_mouse_y = 0.0;
    bool first_mouse_sample = true;
    PlatformInput input;
};

void init_platform(std::vector<const char *> &extensions);
void create_window(rhi::Device &device, Platform &platform, const char *windowTitle, WindowMode mode, bool resizable);

void destroy_window(rhi::Device &device, Platform &platform);
bool platform_update(Platform &platform);

// Window queries. Implemented in platform.cpp; keeps GLFW out of callers.
bool platform_should_close(Platform &platform);
void platform_request_close(Platform &platform);
void platform_framebuffer_size(Platform &platform, u32 &out_w, u32 &out_h);
f32 platform_content_scale(Platform &platform);
// Blocks with wait-events until the framebuffer is non-zero (un-minimized).
// Returns false if close was requested. Syncs platform.width/height.
bool platform_wait_for_valid_framebuffer(Platform &platform);
// Opaque native handle for rhi::SwapchainDesc / imgui backend (they cast it).
void *platform_native_window(Platform &platform);