#include "camera.h"
#include "debug.h"
#include "render.h"

Camera g_camera;

void camera_init() {
    LOG_INFO("camera_init");
    g_camera.transform = IdentityTransform;
    g_camera.fov = 10;
}

static inline
float _camera_height(Camera* self) {
    return self->fov * ((float)g_render_resolution.y / (float)g_render_resolution.x);
}

SDL_FRect camera_world_to_pixel_rect(Camera* self, SDL_FRect* world_space) {
    Transform t = self->transform;
    t.rotation = 0;
    t.scale = OneVector;
    t = transform_invert(t);

    Vector tl = {world_space->x + (self->fov / 2.0f), world_space->y + (_camera_height(self) / 2.0f)};
    Vector size = {world_space->w, world_space->h};

    tl = vmulff(transform_point(&t, tl), g_render_resolution.x / self->fov);
    size = vmulff(vmulf(t.scale, size), g_render_resolution.x / self->fov);


    return (SDL_FRect){
        tl.x, tl.y,
        size.x, size.y
    };
}

Vector camera_world_to_pixel_point(Camera* self, Vector point) {
    Transform t = self->transform;
    t.rotation = 0;
    t.scale = OneVector;
    t = transform_invert(t);

    point = (Vector){point.x + (self->fov / 2.0f), point.y + (_camera_height(self) / 2.0f)};

    return vmulff(transform_point(&t, point), g_render_resolution.x / self->fov);
}
