fix: mouse coordinate conversion uses renderer cell_pixel_size
- Replaced hardcoded 1600/900 screen size with renderer.cell_pixel_size() - Mouse world pos: cam + (screen_pos / cell_px) — correct for any zoom level - Mouse UI pos: (screen_pos / cell_px) * UI_SCALE — correct for UI coordinate space - Shoot direction: player_screen uses cell_px instead of hardcoded 8.0 - Added cell_pixel_size() to GpuRenderer trait and both renderers
This commit is contained in:
+22
-9
@@ -73,6 +73,7 @@ trait GpuRenderer {
|
||||
fn grid_w(&self) -> usize;
|
||||
fn grid_h(&self) -> usize;
|
||||
fn adjust_zoom(&mut self, delta: i32);
|
||||
fn cell_pixel_size(&self) -> u32;
|
||||
}
|
||||
|
||||
impl GpuRenderer for verbatim::render::vulkan::VulkanRenderer {
|
||||
@@ -102,6 +103,9 @@ impl GpuRenderer for verbatim::render::vulkan::VulkanRenderer {
|
||||
fn adjust_zoom(&mut self, delta: i32) {
|
||||
verbatim::render::vulkan::VulkanRenderer::adjust_zoom(self, delta)
|
||||
}
|
||||
fn cell_pixel_size(&self) -> u32 {
|
||||
verbatim::render::vulkan::VulkanRenderer::cell_pixel_size(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl GpuRenderer for verbatim::render::graphics::GraphicsRenderer {
|
||||
@@ -131,6 +135,9 @@ impl GpuRenderer for verbatim::render::graphics::GraphicsRenderer {
|
||||
fn adjust_zoom(&mut self, delta: i32) {
|
||||
verbatim::render::graphics::GraphicsRenderer::adjust_zoom(self, delta)
|
||||
}
|
||||
fn cell_pixel_size(&self) -> u32 {
|
||||
verbatim::render::graphics::GraphicsRenderer::cell_pixel_size(self)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -264,15 +271,18 @@ fn run_gpu_mode<R: GpuRenderer>(title: &str) {
|
||||
}
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
input.on_mouse_move(position.x, position.y);
|
||||
game.mouse_ui_x = position.x as i32;
|
||||
game.mouse_ui_y = position.y as i32;
|
||||
let vw = renderer.grid_w() as i32;
|
||||
let vh = renderer.grid_h() as i32;
|
||||
let cell_px = renderer.cell_pixel_size() as f64;
|
||||
let vw = renderer.grid_w();
|
||||
let vh = renderer.grid_h();
|
||||
game.mouse_ui_x =
|
||||
(position.x / cell_px * verbatim::ui::UI_SCALE as f64) as i32;
|
||||
game.mouse_ui_y =
|
||||
(position.y / cell_px * verbatim::ui::UI_SCALE as f64) as i32;
|
||||
game.show_tooltip = true;
|
||||
game.show_crosshair = true;
|
||||
if vw > 0 && vh > 0 {
|
||||
let wx = game.cam_x + (position.x as i32 * vw / 1600);
|
||||
let wy = game.cam_y + (position.y as i32 * vh / 900);
|
||||
if vw > 0 && vh > 0 && cell_px > 0.0 {
|
||||
let wx = game.cam_x + (position.x / cell_px) as i32;
|
||||
let wy = game.cam_y + (position.y / cell_px) as i32;
|
||||
game.mouse_world_pos = (wx, wy);
|
||||
}
|
||||
}
|
||||
@@ -346,9 +356,12 @@ fn run_gpu_mode<R: GpuRenderer>(title: &str) {
|
||||
|| input.shoot_up
|
||||
|| input.shoot_down
|
||||
{
|
||||
let cell_px = renderer.cell_pixel_size() as f64;
|
||||
let (px, py) = game.player.center(&game.entities);
|
||||
let player_screen_x = ((px as i32 - game.cam_x) as f64) * 8.0 + 4.0;
|
||||
let player_screen_y = ((py as i32 - game.cam_y) as f64) * 8.0 + 4.0;
|
||||
let player_screen_x =
|
||||
((px as i32 - game.cam_x) as f64) * cell_px + cell_px / 2.0;
|
||||
let player_screen_y =
|
||||
((py as i32 - game.cam_y) as f64) * cell_px + cell_px / 2.0;
|
||||
|
||||
let (dx, dy) = if input.shoot_mouse {
|
||||
let mx = input.mouse_x - player_screen_x;
|
||||
|
||||
@@ -1130,6 +1130,10 @@ impl GraphicsRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cell_pixel_size(&self) -> u32 {
|
||||
self.char_size
|
||||
}
|
||||
|
||||
fn check_resize(&mut self) {
|
||||
let sl = ash::khr::surface::Instance::new(&self.entry, &self.instance);
|
||||
let caps = match unsafe {
|
||||
|
||||
@@ -727,6 +727,10 @@ impl VulkanRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cell_pixel_size(&self) -> u32 {
|
||||
self.char_size
|
||||
}
|
||||
|
||||
fn check_resize(&mut self) {
|
||||
let sl = ash::khr::surface::Instance::new(&self.entry, &self.instance);
|
||||
let caps = match unsafe {
|
||||
|
||||
Reference in New Issue
Block a user