query screen size instead of constants

This commit is contained in:
Theis Pieter Hollebeek 2026-04-28 16:07:27 +02:00
parent 23a911d703
commit 9da46fe76d
3 changed files with 20 additions and 9 deletions

View File

@ -19,6 +19,8 @@ pub trait Renderer {
fn draw_line(&mut self, from: V2, to: V2, color: Color);
fn draw_triangle(&mut self, triangle: Triangle2, color: Color);
fn draw_triangles(&mut self, triangles: &[Triangle2], color: Color);
fn screen_width(&self) -> f64;
fn screen_height(&self) -> f64;
}
#[derive(Clone, Copy)]

View File

@ -21,9 +21,6 @@ use crate::engine::{
Color, Renderer, Triangle2,
};
pub static WIDTH: f64 = 1920.0;
pub static HEIGHT: f64 = 1080.0;
pub struct SdlIo<'me> {
sdl_context: Sdl,
video_subsystem: VideoSubsystem,
@ -86,7 +83,7 @@ impl SdlIo<'_> {
let video_subsystem = sdl_context.video().unwrap();
let ttf_context = sdl3::ttf::init().map_err(|e| e.to_string())?;
let window = video_subsystem
.window("Game", WIDTH as u32, HEIGHT as u32)
.window("Game", 1920, 1080)
.position_centered()
.fullscreen()
.build()
@ -164,12 +161,12 @@ impl SdlIo<'_> {
/// world space to screen space
fn scale_w2s(&self, v: V2) -> V2 {
let factor = WIDTH / 2.0;
let factor = self.screen_width() / 2.0;
V2(v.0 * factor, v.1 * factor)
}
/// world space to screen space.
fn translate_w2s(&self, v: V2) -> V2 {
let middle = V2(WIDTH / 2.0, HEIGHT / 2.0);
let middle = V2(self.screen_width() / 2.0, self.screen_height() / 2.0);
V2(v.0 + middle.0, middle.1 - v.1)
}
@ -270,6 +267,15 @@ impl Renderer for SdlIo<'_> {
let texture = self.texture_handler.get(id);
V2(texture.width() as f64, texture.height() as f64)
}
fn screen_width(&self) -> f64 {
let (size, _) = self.canvas.window().size();
size as f64
}
fn screen_height(&self) -> f64 {
let (_, size) = self.canvas.window().size();
size as f64
}
}
impl From<V2> for FPoint {

View File

@ -13,7 +13,7 @@ use std::{
};
use crate::{
engine::{Color, Key, Renderer, Scene, Shape, HEIGHT, V2, V3, WIDTH},
engine::{Color, Key, Renderer, Scene, Shape, V2, V3},
server::Server,
};
@ -483,7 +483,7 @@ impl<R: Renderer> engine::Game<R> for Game {
Color::Green,
);
let V2(width, _height) = r.query_texture(id);
r.draw_texture(id, V2(WIDTH / 2.0 - width / 2.0, 50.0));
r.draw_texture(id, V2(r.screen_width() / 2.0 - width / 2.0, 50.0));
}
if let Some(joever_timer) = self.joever_timer {
let id = r.load_text(
@ -494,7 +494,10 @@ impl<R: Renderer> engine::Game<R> for Game {
let V2(width, height) = r.query_texture(id);
r.draw_texture(
id,
V2(WIDTH / 2.0 - width / 2.0, HEIGHT / 2.0 - height / 2.0),
V2(
r.screen_width() / 2.0 - width / 2.0,
r.screen_height() / 2.0 - height / 2.0,
),
);
}
}