Files
dxdesktop/src/renderer.hpp
2026-04-16 21:30:35 +01:00

80 lines
1.4 KiB
C++

#pragma once
#include <stdio.h>
#include <SDL3/SDL.h>
#include "math.hpp"
namespace dxd
{
class Renderer
{
private:
SDL_Renderer *_sdl;
int _width, _height;
vec2 _center;
float _scale;
float to_screenf(float f)
{
//FIXME: Aspect ratio
return (f/2) * _height;
}
vec2 to_screenv2(vec2 p)
{
//TODO: Handle aspect ratio
return {
.x = ((p.x/2) + .5f) * _height,
.y = (.5f - (p.y/2)) * _height,
};
}
float to_viewf(float f)
{
return f * _scale;
}
vec2 to_viewv2(vec2 p)
{
return {
.x = (p.x - _center.x) * _scale,
.y = (p.y - _center.y) * _scale,
};
}
public:
Renderer(SDL_Renderer *sdl, int width, int height) : _sdl(sdl), _width(width), _height(height)
{
_center = { 0, 0 };
_scale = 1;
}
void color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
SDL_SetRenderDrawColor(_sdl, r, g, b, a);
}
void line(vec2 a, vec2 b)
{
vec2 as = to_screenv2(to_viewv2(a)), bs = to_screenv2(to_viewv2(b));
SDL_RenderLine(_sdl, as.x, as.y, bs.x, bs.y);
}
void dot(vec2 center, float size)
{
vec2 view = to_viewv2(center - vec2_one * (size/2));
vec2 screen = to_screenv2(view);
float scr_size = to_screenf(to_viewf(size));
SDL_FRect rect = {
.x = screen.x,
.y = screen.y - scr_size, //HACK: Do this somewhere else? Rect function?
.w = scr_size,
.h = scr_size,
};
SDL_RenderFillRect(_sdl, &rect);
}
};
} // namespace dxd