From 85b6cce5b891382d4c6fc7a4138a763f720fcbfb Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sun, 14 Jul 2024 00:31:10 +0200 Subject: [PATCH] make viewer fields private and use getters --- src/main.rs | 39 +++++----------------- src/rendering/views.rs | 76 +++++++++++++++++++++++++++++++++--------- 2 files changed, 70 insertions(+), 45 deletions(-) diff --git a/src/main.rs b/src/main.rs index 88ab608..b2d9d1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use macroquad::{prelude::*, ui::root_ui}; use mmu::MemoryOperations; use rendering::{ line_rendering::{self}, - tiles::*, + tiles::{self, *}, views::*, }; use rfd::FileDialog; @@ -49,34 +49,13 @@ async fn main() { ]; const SCALING: f32 = 4.0; - let mut gb_display = GbDisplay { - offset_x: 5.0, - offset_y: 5.0, - scaling: SCALING, - gb_image: Image::gen_image_color(160, 144, GREEN) - }; - let gb_display_size = gb_display.size(); - - let mut background_viewer = BackgroundViewer { - offset_x: gb_display_size.x + 10.0, - offset_y: 5.0, - scaling: SCALING / 2.0, - image: Image::gen_image_color(32 * 8, 32 * 8, PINK) - }; - - let background_viewer_size = background_viewer.size(); - - let mut tile_viewer = TileViewer { - offset_x: gb_display_size.x + background_viewer_size.x + 15.0, - offset_y: 5.0, - scaling: SCALING, - atlas: Image::gen_image_color(8 * 16, 8 * 24, WHITE) - }; - let tile_viewer_size = tile_viewer.size(); + let mut gb_display = GbDisplay::new(5.0, 5.0, SCALING); + let mut background_viewer = BackgroundViewer::new(gb_display.size().x + 10.0, 5.0, SCALING / 2.0); + let mut tile_viewer = TileViewer::new(gb_display.size().x + background_viewer.size().x + 15.0, 5.0, SCALING); request_new_screen_size( - background_viewer_size.x + tile_viewer_size.x + gb_display_size.x + 20.0, - tile_viewer_size.y + 10.0, + background_viewer.size().x + tile_viewer.size().x + gb_display.size().x + 20.0, + tile_viewer.size().y + 10.0, ); let filedialog = FileDialog::new() @@ -141,7 +120,7 @@ async fn main() { log::debug!("🔢 Following Word (PC): {:#06X}", pc_following_word); for _ in 0..=cpu_cycles_taken { - ppu.step(&mut cpu, &mut gb_display.gb_image, &PALETTE); + ppu.step(&mut cpu, &mut gb_display.get_gb_image(), &PALETTE); // Alternatively Redraw UI at 30 frames per second: (ppu_time.elapsed().as_millis() as f32) >= TIME_PER_FRAME @@ -168,8 +147,8 @@ async fn main() { last_frame_time = time::Instant::now(); // Update Debugging Views - update_atlas_from_memory(&cpu, 16 * 24, &mut tile_viewer.atlas, &PALETTE); - update_background_from_memory(&cpu, &mut background_viewer.image, &PALETTE, false, true); + update_atlas_from_memory(&cpu, 16 * 24, &mut tile_viewer.get_atlas(), &PALETTE); + update_background_from_memory(&cpu, &mut background_viewer.get_image(), &PALETTE, false, true); background_viewer.draw(); tile_viewer.draw(); diff --git a/src/rendering/views.rs b/src/rendering/views.rs index 4f7d216..f40a913 100644 --- a/src/rendering/views.rs +++ b/src/rendering/views.rs @@ -6,10 +6,25 @@ pub trait Draw { } pub struct GbDisplay { - pub offset_x: f32, - pub offset_y: f32, - pub scaling: f32, - pub gb_image: Image + offset_x: f32, + offset_y: f32, + scaling: f32, + gb_image: Image +} + +impl GbDisplay { + pub fn new(offset_x: f32, offset_y: f32, scaling: f32) -> GbDisplay { + GbDisplay { + offset_x, + offset_y, + scaling, + gb_image: Image::gen_image_color(160, 144, GREEN) + } + } + + pub fn get_gb_image(&mut self) -> &mut Image { + &mut self.gb_image + } } impl Draw for GbDisplay { @@ -40,10 +55,26 @@ impl Draw for GbDisplay { } pub struct TileViewer { - pub offset_x: f32, - pub offset_y: f32, - pub scaling: f32, - pub atlas: Image + offset_x: f32, + offset_y: f32, + scaling: f32, + atlas: Image +} + +impl TileViewer { + pub fn new(offset_x: f32, offset_y: f32, scaling: f32) -> TileViewer { + TileViewer { + offset_x, + offset_y, + scaling, + atlas: Image::gen_image_color(8 * 16, 8 * 24, WHITE) + } + } + + pub fn get_atlas(&mut self) -> &mut Image { + &mut self.atlas + } + } impl Draw for TileViewer { @@ -100,10 +131,25 @@ impl Draw for TileViewer { } pub struct BackgroundViewer { - pub offset_x: f32, - pub offset_y: f32, - pub scaling: f32, - pub image: Image + offset_x: f32, + offset_y: f32, + scaling: f32, + image: Image +} + +impl BackgroundViewer { + pub fn new(offset_x: f32, offset_y: f32, scaling: f32) -> BackgroundViewer { + BackgroundViewer { + offset_x, + offset_y, + scaling, + image: Image::gen_image_color(32 * 8, 32 * 8, WHITE) + } + } + + pub fn get_image(&mut self) -> &mut Image { + &mut self.image + } } impl Draw for BackgroundViewer { @@ -134,9 +180,9 @@ impl Draw for BackgroundViewer { } pub struct EmulationControls { - pub offset_x: f32, - pub offset_y: f32, - pub scaling: f32, + offset_x: f32, + offset_y: f32, + scaling: f32, play_active: Texture2D, play_inactive: Texture2D,