From e503cfb7188e29748c992e82ec96ddd4a9e7e7a2 Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 27 Mar 2023 16:48:55 -0400 Subject: [PATCH] remove old display size constants as well as cargo feature flags we'll be introducing variable display size features next, so these screen dimension compile-time constants are not going to be useful. the "low_res" and "wide" features were also tied to the idea of compile-time constants for display sizing so these need to go too, and will be brought back via runtime configuration --- examples/balls/src/main.rs | 12 +++---- examples/balls_v2/src/entities.rs | 8 ++--- examples/slimed/src/entities/systems.rs | 8 ++--- examples/template_complicated/src/main.rs | 4 +-- examples/template_minimal/src/main.rs | 4 +-- ggdt/Cargo.toml | 4 --- ggdt/benches/bitmap.rs | 7 ++-- ggdt/src/graphics/palette.rs | 3 +- ggdt/src/lib.rs | 43 ----------------------- ggdt/src/system/res/dos_like.rs | 11 +++--- ggdt/src/system/res/standard.rs | 11 +++--- ggdt/tests/graphics_indexed.rs | 3 ++ ggdt/tests/graphics_rgba.rs | 3 ++ 13 files changed, 45 insertions(+), 76 deletions(-) diff --git a/examples/balls/src/main.rs b/examples/balls/src/main.rs index e6a7662..66dbc20 100644 --- a/examples/balls/src/main.rs +++ b/examples/balls/src/main.rs @@ -47,8 +47,8 @@ fn main() -> Result<()> { for _ in 0..NUM_BALLS { let speed = rnd_value(1, 3); let ball = Ball { - x: rnd_value(0, SCREEN_WIDTH as i32 - 1), - y: rnd_value(0, SCREEN_HEIGHT as i32 - 1), + x: rnd_value(0, system.res.video.width() as i32 - 1), + y: rnd_value(0, system.res.video.height() as i32 - 1), dir_x: if rnd_value(0, 1) == 0 { -speed } else { speed }, dir_y: if rnd_value(0, 1) == 0 { -speed } else { speed }, sprite: rnd_value(0, NUM_BALL_SPRITES - 1), @@ -73,9 +73,9 @@ fn main() -> Result<()> { ball.x = 0; } } else { - if ball.x >= (SCREEN_WIDTH - BALL_WIDTH) as i32 { + if ball.x >= (system.res.video.width() - BALL_WIDTH) as i32 { ball.dir_x = -ball.dir_x; - ball.x = (SCREEN_WIDTH - BALL_WIDTH) as i32; + ball.x = (system.res.video.width() - BALL_WIDTH) as i32; } } @@ -85,9 +85,9 @@ fn main() -> Result<()> { ball.y = 0; } } else { - if ball.y >= (SCREEN_HEIGHT - BALL_HEIGHT) as i32 { + if ball.y >= (system.res.video.height() - BALL_HEIGHT) as i32 { ball.dir_y = -ball.dir_y; - ball.y = (SCREEN_HEIGHT - BALL_HEIGHT) as i32; + ball.y = (system.res.video.height() - BALL_HEIGHT) as i32; } } } diff --git a/examples/balls_v2/src/entities.rs b/examples/balls_v2/src/entities.rs index dcaeae8..d16d815 100644 --- a/examples/balls_v2/src/entities.rs +++ b/examples/balls_v2/src/entities.rs @@ -73,8 +73,8 @@ fn new_bounce_particles(entities: &mut Entities, x: f32, y: f32) { fn new_ball_entity(entities: &mut Entities) { let id = entities.new_entity(); - let x: i32 = rnd_value(SCREEN_LEFT as i32 + 1, SCREEN_RIGHT as i32 - BALL_SIZE - 1); - let y: i32 = rnd_value(SCREEN_TOP as i32 + 1, SCREEN_BOTTOM as i32 - BALL_SIZE - 1); + let x: i32 = rnd_value(1, 319 - BALL_SIZE - 1); + let y: i32 = rnd_value(1, 239 - BALL_SIZE - 1); let speed = rnd_value(1, 3) * BALL_BASE_SPEED; let vx = if rnd_value(0, 1) == 0 { -speed } else { speed }; @@ -110,12 +110,12 @@ fn update_system_collision(context: &mut Context) { let mut velocity = velocities.get_mut(&entity).unwrap(); let mut bounced = false; - if position.0.x as i32 <= SCREEN_LEFT as i32 || position.0.x as i32 + BALL_SIZE >= SCREEN_RIGHT as i32 { + if position.0.x as i32 <= 0 || position.0.x as i32 + BALL_SIZE >= context.system.res.video.right() as i32 { position.0.x -= velocity.0.x * context.delta; velocity.0.x = -velocity.0.x; bounced = true; } - if position.0.y as i32 <= SCREEN_TOP as i32 || position.0.y as i32 + BALL_SIZE >= SCREEN_BOTTOM as i32 { + if position.0.y as i32 <= 0 || position.0.y as i32 + BALL_SIZE >= context.system.res.video.bottom() as i32 { position.0.y -= velocity.0.y * context.delta; velocity.0.y = -velocity.0.y; bounced = true; diff --git a/examples/slimed/src/entities/systems.rs b/examples/slimed/src/entities/systems.rs index 8e166c2..a446610 100644 --- a/examples/slimed/src/entities/systems.rs +++ b/examples/slimed/src/entities/systems.rs @@ -529,14 +529,14 @@ fn update_system_camera_follows_player(context: &mut Core) { let positions = context.entities.components::().unwrap(); let position = positions.get(player_entity).unwrap(); - let camera_x = position.0.x as i32 - (SCREEN_WIDTH as i32 / 2) + 8; - let camera_y = position.0.y as i32 - (SCREEN_HEIGHT as i32 / 2) + 8; + let camera_x = position.0.x as i32 - (context.system.res.video.width() as i32 / 2) + 8; + let camera_y = position.0.y as i32 - (context.system.res.video.height() as i32 / 2) + 8; // clamp camera position to the map boundaries let map_pixel_width = context.tilemap.width() * TILE_WIDTH; let map_pixel_height = context.tilemap.height() * TILE_HEIGHT; - let max_camera_x = map_pixel_width - SCREEN_WIDTH; - let max_camera_y = map_pixel_height - SCREEN_HEIGHT; + let max_camera_x = map_pixel_width - context.system.res.video.width(); + let max_camera_y = map_pixel_height - context.system.res.video.height(); camera.x = camera_x.clamp(0, max_camera_x as i32); camera.y = camera_y.clamp(0, max_camera_y as i32); diff --git a/examples/template_complicated/src/main.rs b/examples/template_complicated/src/main.rs index 718f20a..24e4ae9 100644 --- a/examples/template_complicated/src/main.rs +++ b/examples/template_complicated/src/main.rs @@ -18,8 +18,8 @@ pub fn event_listener(event: &Event, context: &mut Core) -> bool { Event::SpawnPixel => { let speed = rnd_value(1, 10) as f32 * 10.0; let angle = (rnd_value(0, 359) as f32).to_radians(); - let x = (SCREEN_WIDTH / 2) as f32; - let y = (SCREEN_HEIGHT / 2) as f32; + let x = (context.system.res.video.width() / 2) as f32; + let y = (context.system.res.video.height() / 2) as f32; let color = rnd_value(0, 255); let id = context.entities.new_entity(); context.entities.add_component(id, Position(Vector2::new(x, y))); diff --git a/examples/template_minimal/src/main.rs b/examples/template_minimal/src/main.rs index 723b568..0048431 100644 --- a/examples/template_minimal/src/main.rs +++ b/examples/template_minimal/src/main.rs @@ -19,8 +19,8 @@ fn main() -> Result<()> { system.update()?; - let x = rnd_value(0, SCREEN_RIGHT) as i32; - let y = rnd_value(0, SCREEN_BOTTOM) as i32; + let x = rnd_value(0, system.res.video.right()) as i32; + let y = rnd_value(0, system.res.video.bottom()) as i32; let color = rnd_value(0, 255); system.res.video.set_pixel(x, y, color); diff --git a/ggdt/Cargo.toml b/ggdt/Cargo.toml index 8152c3a..92c6452 100644 --- a/ggdt/Cargo.toml +++ b/ggdt/Cargo.toml @@ -5,10 +5,6 @@ version = "0.1.0" authors = ["Gered King "] edition = "2021" -[features] -low_res = [] -wide = [] - [dependencies] byte-slice-cast = "1.2.1" byteorder = "1.4.3" diff --git a/ggdt/benches/bitmap.rs b/ggdt/benches/bitmap.rs index cb51ef9..8b00887 100644 --- a/ggdt/benches/bitmap.rs +++ b/ggdt/benches/bitmap.rs @@ -3,8 +3,11 @@ use criterion::{black_box, Criterion, criterion_group, criterion_main}; use ggdt::prelude::*; pub fn criterion_benchmark(c: &mut Criterion) { - let mut source = IndexedBitmap::new(SCREEN_WIDTH, SCREEN_HEIGHT).unwrap(); - let mut dest = vec![0u32; (SCREEN_WIDTH * SCREEN_HEIGHT * 4) as usize].into_boxed_slice(); + let width = 320; + let height = 240; + + let mut source = IndexedBitmap::new(width, height).unwrap(); + let mut dest = vec![0u32; (width * height * 4) as usize].into_boxed_slice(); let palette = Palette::new_vga_palette().unwrap(); c.bench_function("deindex_bitmap_pixels", |b| { diff --git a/ggdt/src/graphics/palette.rs b/ggdt/src/graphics/palette.rs index 2e9c6a6..28f1069 100644 --- a/ggdt/src/graphics/palette.rs +++ b/ggdt/src/graphics/palette.rs @@ -9,9 +9,10 @@ use thiserror::Error; use crate::graphics::bitmap::indexed::IndexedBitmap; use crate::graphics::color::{from_rgb32, lerp_rgb32, to_argb32, to_rgb32}; -use crate::NUM_COLORS; use crate::utils::abs_diff; +const NUM_COLORS: usize = 256; + /// Common trait to represent a range of indexed colour values. pub trait ColorRange: RangeBounds + Iterator {} impl ColorRange for T where T: RangeBounds + Iterator {} diff --git a/ggdt/src/lib.rs b/ggdt/src/lib.rs index b1785e9..19a0c13 100644 --- a/ggdt/src/lib.rs +++ b/ggdt/src/lib.rs @@ -13,49 +13,6 @@ pub mod utils; pub mod prelude; -pub const LOW_RES: bool = if cfg!(feature = "low_res") { - true -} else { - false -}; -pub const WIDE_SCREEN: bool = if cfg!(feature = "wide") { - true -} else { - false -}; - -pub const SCREEN_WIDTH: u32 = if cfg!(feature = "low_res") { - if cfg!(feature = "wide") { - 214 - } else { - 160 - } -} else { - if cfg!(feature = "wide") { - 428 - } else { - 320 - } -}; -pub const SCREEN_HEIGHT: u32 = if cfg!(feature = "low_res") { - 120 -} else { - 240 -}; - -pub const SCREEN_TOP: u32 = 0; -pub const SCREEN_LEFT: u32 = 0; -pub const SCREEN_RIGHT: u32 = SCREEN_WIDTH - 1; -pub const SCREEN_BOTTOM: u32 = SCREEN_HEIGHT - 1; - -pub const DEFAULT_SCALE_FACTOR: u32 = if cfg!(feature = "low_res") { - 6 -} else { - 3 -}; - -pub const NUM_COLORS: usize = 256; // i mean ... the number of colors is really defined by the size of u8 ... - // using this to hold common unit test helper functions // (since apparently rust doesn't really have a great alternative ... ?) #[cfg(test)] diff --git a/ggdt/src/system/res/dos_like.rs b/ggdt/src/system/res/dos_like.rs index c59472f..1afa6f7 100644 --- a/ggdt/src/system/res/dos_like.rs +++ b/ggdt/src/system/res/dos_like.rs @@ -28,7 +28,6 @@ use byte_slice_cast::AsByteSlice; -use crate::{DEFAULT_SCALE_FACTOR, SCREEN_HEIGHT, SCREEN_WIDTH}; use crate::audio::{Audio, TARGET_AUDIO_CHANNELS, TARGET_AUDIO_FREQUENCY}; use crate::audio::queue::AudioQueue; use crate::graphics::font::BitmaskFont; @@ -41,6 +40,10 @@ use crate::system::input_devices::mouse::cursor::CustomMouseCursor; use crate::system::input_devices::mouse::Mouse; use crate::system::res::{SystemResources, SystemResourcesConfig, SystemResourcesError}; +const DEFAULT_SCREEN_WIDTH: u32 = 320; +const DEFAULT_SCREEN_HEIGHT: u32 = 240; +const DEFAULT_SCALE_FACTOR: u32 = 3; + /// Configuration / builder for configuring and constructing an instance of [`DosLike`]. pub struct DosLikeConfig { screen_width: u32, @@ -53,8 +56,8 @@ impl DosLikeConfig { /// Returns a new [`DosLikeConfig`] with a default configuration. pub fn new() -> Self { DosLikeConfig { - screen_width: SCREEN_WIDTH, - screen_height: SCREEN_HEIGHT, + screen_width: DEFAULT_SCREEN_WIDTH, + screen_height: DEFAULT_SCREEN_HEIGHT, initial_scale_factor: DEFAULT_SCALE_FACTOR, integer_scaling: false, } @@ -140,7 +143,7 @@ impl SystemResourcesConfig for DosLikeConfig { // create the Bitmap object that will be exposed to the application acting as the system // backbuffer - let framebuffer = match IndexedBitmap::new(SCREEN_WIDTH, SCREEN_HEIGHT) { + let framebuffer = match IndexedBitmap::new(self.screen_width, self.screen_height) { Ok(bmp) => bmp, Err(error) => return Err(SystemResourcesError::SDLError(error.to_string())), }; diff --git a/ggdt/src/system/res/standard.rs b/ggdt/src/system/res/standard.rs index 040d0e3..7dc8e41 100644 --- a/ggdt/src/system/res/standard.rs +++ b/ggdt/src/system/res/standard.rs @@ -1,6 +1,5 @@ use byte_slice_cast::AsByteSlice; -use crate::{DEFAULT_SCALE_FACTOR, SCREEN_HEIGHT, SCREEN_WIDTH}; use crate::audio::{Audio, TARGET_AUDIO_CHANNELS, TARGET_AUDIO_FREQUENCY}; use crate::audio::queue::AudioQueue; use crate::graphics::bitmap::rgb::RgbaBitmap; @@ -12,6 +11,10 @@ use crate::system::input_devices::mouse::cursor::CustomMouseCursor; use crate::system::input_devices::mouse::Mouse; use crate::system::res::{SystemResources, SystemResourcesConfig, SystemResourcesError}; +const DEFAULT_SCREEN_WIDTH: u32 = 320; +const DEFAULT_SCREEN_HEIGHT: u32 = 240; +const DEFAULT_SCALE_FACTOR: u32 = 3; + pub struct StandardConfig { screen_width: u32, screen_height: u32, @@ -23,8 +26,8 @@ impl StandardConfig { /// Returns a new [`DosLikeConfig`] with a default configuration. pub fn new() -> Self { StandardConfig { - screen_width: SCREEN_WIDTH, - screen_height: SCREEN_HEIGHT, + screen_width: DEFAULT_SCREEN_WIDTH, + screen_height: DEFAULT_SCREEN_HEIGHT, initial_scale_factor: DEFAULT_SCALE_FACTOR, integer_scaling: false, } @@ -103,7 +106,7 @@ impl SystemResourcesConfig for StandardConfig { // create the Bitmap object that will be exposed to the application acting as the system // backbuffer - let framebuffer = match RgbaBitmap::new(SCREEN_WIDTH, SCREEN_HEIGHT) { + let framebuffer = match RgbaBitmap::new(self.screen_width, self.screen_height) { Ok(bmp) => bmp, Err(error) => return Err(SystemResourcesError::SDLError(error.to_string())), }; diff --git a/ggdt/tests/graphics_indexed.rs b/ggdt/tests/graphics_indexed.rs index 5a21ec2..cd001a3 100644 --- a/ggdt/tests/graphics_indexed.rs +++ b/ggdt/tests/graphics_indexed.rs @@ -6,6 +6,9 @@ use helpers::test_assets_file; pub mod helpers; +const SCREEN_WIDTH: u32 = 320; +const SCREEN_HEIGHT: u32 = 240; + const BASE_PATH: &str = "./tests/ref/indexed/"; fn reference_file(file: &Path) -> PathBuf { diff --git a/ggdt/tests/graphics_rgba.rs b/ggdt/tests/graphics_rgba.rs index 52f0ce5..f8ec3c8 100644 --- a/ggdt/tests/graphics_rgba.rs +++ b/ggdt/tests/graphics_rgba.rs @@ -24,6 +24,9 @@ pub const COLOR_BRIGHT_MAGENTA_HALF_ALPHA: u32 = 0x7fff55ff; pub const COLOR_BRIGHT_YELLOW_HALF_ALPHA: u32 = 0x7fffff55; pub const COLOR_BRIGHT_WHITE_HALF_ALPHA: u32 = 0x7fffffff; +const SCREEN_WIDTH: u32 = 320; +const SCREEN_HEIGHT: u32 = 240; + const BASE_PATH: &str = "./tests/ref/rgba/"; fn reference_file(file: &Path) -> PathBuf {