mirror of
https://github.com/Mercantec-GHC/h5-projekt-mst.git
synced 2026-08-26 21:27:38 +02:00
query screen size instead of constants
This commit is contained in:
parent
23a911d703
commit
9da46fe76d
@ -19,6 +19,8 @@ pub trait Renderer {
|
|||||||
fn draw_line(&mut self, from: V2, to: V2, color: Color);
|
fn draw_line(&mut self, from: V2, to: V2, color: Color);
|
||||||
fn draw_triangle(&mut self, triangle: Triangle2, color: Color);
|
fn draw_triangle(&mut self, triangle: Triangle2, color: Color);
|
||||||
fn draw_triangles(&mut self, triangles: &[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)]
|
#[derive(Clone, Copy)]
|
||||||
|
|||||||
@ -21,9 +21,6 @@ use crate::engine::{
|
|||||||
Color, Renderer, Triangle2,
|
Color, Renderer, Triangle2,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub static WIDTH: f64 = 1920.0;
|
|
||||||
pub static HEIGHT: f64 = 1080.0;
|
|
||||||
|
|
||||||
pub struct SdlIo<'me> {
|
pub struct SdlIo<'me> {
|
||||||
sdl_context: Sdl,
|
sdl_context: Sdl,
|
||||||
video_subsystem: VideoSubsystem,
|
video_subsystem: VideoSubsystem,
|
||||||
@ -86,7 +83,7 @@ impl SdlIo<'_> {
|
|||||||
let video_subsystem = sdl_context.video().unwrap();
|
let video_subsystem = sdl_context.video().unwrap();
|
||||||
let ttf_context = sdl3::ttf::init().map_err(|e| e.to_string())?;
|
let ttf_context = sdl3::ttf::init().map_err(|e| e.to_string())?;
|
||||||
let window = video_subsystem
|
let window = video_subsystem
|
||||||
.window("Game", WIDTH as u32, HEIGHT as u32)
|
.window("Game", 1920, 1080)
|
||||||
.position_centered()
|
.position_centered()
|
||||||
.fullscreen()
|
.fullscreen()
|
||||||
.build()
|
.build()
|
||||||
@ -164,12 +161,12 @@ impl SdlIo<'_> {
|
|||||||
|
|
||||||
/// world space to screen space
|
/// world space to screen space
|
||||||
fn scale_w2s(&self, v: V2) -> V2 {
|
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)
|
V2(v.0 * factor, v.1 * factor)
|
||||||
}
|
}
|
||||||
/// world space to screen space.
|
/// world space to screen space.
|
||||||
fn translate_w2s(&self, v: V2) -> V2 {
|
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)
|
V2(v.0 + middle.0, middle.1 - v.1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,6 +267,15 @@ impl Renderer for SdlIo<'_> {
|
|||||||
let texture = self.texture_handler.get(id);
|
let texture = self.texture_handler.get(id);
|
||||||
V2(texture.width() as f64, texture.height() as f64)
|
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 {
|
impl From<V2> for FPoint {
|
||||||
|
|||||||
@ -13,7 +13,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
engine::{Color, Key, Renderer, Scene, Shape, HEIGHT, V2, V3, WIDTH},
|
engine::{Color, Key, Renderer, Scene, Shape, V2, V3},
|
||||||
server::Server,
|
server::Server,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -483,7 +483,7 @@ impl<R: Renderer> engine::Game<R> for Game {
|
|||||||
Color::Green,
|
Color::Green,
|
||||||
);
|
);
|
||||||
let V2(width, _height) = r.query_texture(id);
|
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 {
|
if let Some(joever_timer) = self.joever_timer {
|
||||||
let id = r.load_text(
|
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);
|
let V2(width, height) = r.query_texture(id);
|
||||||
r.draw_texture(
|
r.draw_texture(
|
||||||
id,
|
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,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user