use i16 for coordinates instead of isize, yielding better cache locality etc

This commit is contained in:
Crispy 2024-12-07 20:49:23 +01:00
parent 28213da9f3
commit 42a355d387
3 changed files with 18 additions and 15 deletions

View file

@ -13,7 +13,7 @@ use crate::{
level::Level, level::Level,
marble_engine::{ marble_engine::{
board::Board, board::Board,
pos::Pos, pos::{Pos, PosInt},
tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile, WireType}, tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile, WireType},
Machine, Machine,
}, },
@ -28,7 +28,7 @@ const HEADER_HEIGHT: i32 = 40;
const FOOTER_HEIGHT: i32 = 95; const FOOTER_HEIGHT: i32 = 95;
const SIDEBAR_WIDTH: i32 = 200 + 32 * 2 + 5 * 4; const SIDEBAR_WIDTH: i32 = 200 + 32 * 2 + 5 * 4;
const MAX_ZOOM_IN: i32 = 3; const MAX_ZOOM_IN: i32 = 3;
const BOARD_MARGIN: isize = 3; const BOARD_MARGIN: PosInt = 3;
const MAX_SPEED_POWER: u8 = 16; const MAX_SPEED_POWER: u8 = 16;
const SPEED_DIGITS: u8 = 5; const SPEED_DIGITS: u8 = 5;
const MAX_FRAME_TIME_MICROS: u128 = 1_000_000 / 30; const MAX_FRAME_TIME_MICROS: u128 = 1_000_000 / 30;

View file

@ -2,6 +2,7 @@ use crate::{draw_scaled_texture, Textures};
use super::tile::*; use super::tile::*;
use super::Pos; use super::Pos;
use super::PosInt;
use raylib::prelude::*; use raylib::prelude::*;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -73,7 +74,7 @@ impl Board {
} }
fn in_bounds(&self, p: Pos) -> bool { fn in_bounds(&self, p: Pos) -> bool {
p.x >= 0 && p.y >= 0 && p.x < self.width as isize && p.y < self.height as isize p.x >= 0 && p.y >= 0 && p.x < self.width as PosInt && p.y < self.height as PosInt
} }
pub fn get(&self, p: Pos) -> Option<Tile> { pub fn get(&self, p: Pos) -> Option<Tile> {
@ -116,11 +117,11 @@ impl Board {
} }
} }
pub fn grow_to_include(&mut self, p: Pos) -> (isize, isize) { pub fn grow_to_include(&mut self, p: Pos) -> (PosInt,PosInt) {
let mut offset_x = 0; let mut offset_x = 0;
let mut offset_y = 0; let mut offset_y = 0;
if p.x < 0 { if p.x < 0 {
let len = p.x.unsigned_abs(); let len = p.x.unsigned_abs() as usize;
for row in &mut self.rows { for row in &mut self.rows {
let mut new_row = vec![Tile::Blank; len]; let mut new_row = vec![Tile::Blank; len];
new_row.append(row); new_row.append(row);
@ -137,7 +138,7 @@ impl Board {
} }
if p.y < 0 { if p.y < 0 {
let len = p.y.unsigned_abs(); let len = p.y.unsigned_abs() as usize;
let mut new_rows = vec![vec![Tile::Blank; self.width]; len]; let mut new_rows = vec![vec![Tile::Blank; self.width]; len];
new_rows.append(&mut self.rows); new_rows.append(&mut self.rows);
self.rows = new_rows; self.rows = new_rows;
@ -148,7 +149,7 @@ impl Board {
self.rows.resize(new_height, vec![Tile::Blank; self.width]); self.rows.resize(new_height, vec![Tile::Blank; self.width]);
self.height = new_height; self.height = new_height;
} }
(offset_x as isize, offset_y as isize) (offset_x as PosInt, offset_y as PosInt)
} }
pub fn trim_size(&mut self, margin: usize) -> (usize, usize) { pub fn trim_size(&mut self, margin: usize) -> (usize, usize) {

View file

@ -2,10 +2,12 @@ use std::ops::Add;
use raylib::prelude::*; use raylib::prelude::*;
pub type PosInt = i16;
#[derive(Debug, Default, Clone, Copy, PartialEq)] #[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Pos { pub struct Pos {
pub x: isize, pub x: PosInt,
pub y: isize, pub y: PosInt,
} }
impl Pos { impl Pos {
@ -34,8 +36,8 @@ impl Pos {
impl From<(usize, usize)> for Pos { impl From<(usize, usize)> for Pos {
fn from(value: (usize, usize)) -> Self { fn from(value: (usize, usize)) -> Self {
Self { Self {
x: value.0 as isize, x: value.0 as PosInt,
y: value.1 as isize, y: value.1 as PosInt,
} }
} }
} }
@ -43,8 +45,8 @@ impl From<(usize, usize)> for Pos {
impl From<(i32, i32)> for Pos { impl From<(i32, i32)> for Pos {
fn from(value: (i32, i32)) -> Self { fn from(value: (i32, i32)) -> Self {
Self { Self {
x: value.0 as isize, x: value.0 as PosInt,
y: value.1 as isize, y: value.1 as PosInt,
} }
} }
} }
@ -52,8 +54,8 @@ impl From<(i32, i32)> for Pos {
impl From<Vector2> for Pos { impl From<Vector2> for Pos {
fn from(vec: Vector2) -> Self { fn from(vec: Vector2) -> Self {
Self { Self {
x: vec.x as isize, x: vec.x as PosInt,
y: vec.y as isize, y: vec.y as PosInt,
} }
} }
} }