updates people, updates! (refactoring in neovim, kill me)
This commit is contained in:
+17
-101
@@ -1,20 +1,14 @@
|
||||
use chip8_core::*;
|
||||
use raylib::prelude::*;
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::process;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::config;
|
||||
use crate::graphics::GraphicsManager;
|
||||
use crate::input::InputManager;
|
||||
use crate::audio::AudioManager;
|
||||
use crate::themes::ThemeManager;
|
||||
|
||||
const WIN_SCALE_FAC: u32 = 15;
|
||||
const WIN_WIDTH: u32 = (SCREEN_WIDTH as u32) * WIN_SCALE_FAC;
|
||||
const WIN_HEIGHT: u32 = (SCREEN_HEIGHT as u32) * WIN_SCALE_FAC;
|
||||
|
||||
const WELCOME: &str = r#"
|
||||
The Rust
|
||||
@@ -31,15 +25,11 @@ pub struct AppManager {
|
||||
emulator: Emu,
|
||||
clock_timer: f32,
|
||||
|
||||
rl: RaylibHandle,
|
||||
thread: RaylibThread,
|
||||
canvas: Image,
|
||||
|
||||
config: config::Config,
|
||||
theme_manager: ThemeManager,
|
||||
audio_manager: AudioManager,
|
||||
input_manager: InputManager,
|
||||
|
||||
graphics_manager: GraphicsManager,
|
||||
|
||||
args: Vec<String>,
|
||||
}
|
||||
|
||||
@@ -49,18 +39,8 @@ impl AppManager {
|
||||
// print welcome message
|
||||
println!("\x1b[1;31m {} \x1b[0m", WELCOME);
|
||||
|
||||
// init raylib window
|
||||
let (mut raylib_handle, raylib_thread) = raylib::init()
|
||||
.resizable()
|
||||
.size(WIN_WIDTH as i32, WIN_HEIGHT as i32)
|
||||
.title("Chip8 emulator")
|
||||
.build();
|
||||
|
||||
raylib_handle.set_trace_log(TraceLogLevel::LOG_NONE);
|
||||
raylib_handle.set_exit_key(None);
|
||||
|
||||
let args: Vec<_> = env::args().collect();
|
||||
if args.len() != 2 {
|
||||
let arguments: Vec<_> = env::args().collect();
|
||||
if arguments.len() != 2 {
|
||||
println!("ERROR: invalid args!");
|
||||
process::exit(0);
|
||||
}
|
||||
@@ -70,30 +50,26 @@ impl AppManager {
|
||||
clock_timer: 0.,
|
||||
emulator: Emu::new(),
|
||||
|
||||
rl: raylib_handle,
|
||||
thread: raylib_thread,
|
||||
canvas: Image::gen_image_color(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, Color::GREENYELLOW),
|
||||
|
||||
config: config::read_config(),
|
||||
theme_manager: ThemeManager::new(),
|
||||
input_manager: InputManager::new(),
|
||||
audio_manager: AudioManager::new(),
|
||||
graphics_manager: GraphicsManager::new(),
|
||||
|
||||
args: args,
|
||||
args: arguments,
|
||||
};
|
||||
instance.audio_manager.load_values_from_config(&instance.config);
|
||||
instance.audio_manager.play_async_beep();
|
||||
instance.input_manager.generate_keymaps_from_config(&instance.config);
|
||||
instance.theme_manager.parse_themes(&instance.config);
|
||||
instance.graphics_manager.theme_manager.parse_themes(&instance.config);
|
||||
|
||||
instance.rl.set_target_fps(instance.config.max_fps);
|
||||
instance.canvas.clear_background(instance.get_ui_col("BG".to_string()));
|
||||
instance.canvas.draw_text("LOADING..", 1, 3, 13, instance.get_ui_col("TEXT".to_string()));
|
||||
instance.graphics_manager.rl.set_target_fps(instance.config.max_fps);
|
||||
instance.graphics_manager.canvas.clear_background(instance.graphics_manager.get_ui_col("BG".to_string()));
|
||||
instance.graphics_manager.canvas.draw_text("LOADING..", 1, 3, 13, instance.graphics_manager.get_ui_col("TEXT".to_string()));
|
||||
instance
|
||||
}
|
||||
|
||||
pub fn main_loop(&mut self) {
|
||||
while !self.rl.window_should_close() {
|
||||
while !self.graphics_manager.rl.window_should_close() {
|
||||
|
||||
let mut visuals_modified = self.emulator.tick();
|
||||
|
||||
@@ -103,59 +79,17 @@ impl AppManager {
|
||||
}
|
||||
|
||||
|
||||
self.input_manager.handle_game_input(&mut self.emulator, &self.rl);
|
||||
let actions = self.input_manager.handle_emu_input(&self.rl);
|
||||
self.input_manager.handle_game_input(&mut self.emulator, &self.graphics_manager.rl);
|
||||
let actions = self.input_manager.handle_emu_input(&self.graphics_manager.rl);
|
||||
visuals_modified |= self.handle_action(actions);
|
||||
|
||||
self.render_game(visuals_modified);
|
||||
self.graphics_manager.render_game(&self.args, &self.config, &self.emulator, visuals_modified);
|
||||
}
|
||||
}
|
||||
|
||||
fn render_game(&mut self, framebuffer_modified: bool) {
|
||||
let texture: Texture2D;
|
||||
|
||||
if framebuffer_modified {
|
||||
let screenbuffer = self.emulator.get_display();
|
||||
|
||||
for x in 0..SCREEN_WIDTH {
|
||||
for y in 0..SCREEN_HEIGHT {
|
||||
let pixel_color = if screenbuffer[x + SCREEN_WIDTH * y] {
|
||||
self.get_ui_col("FG".to_string())
|
||||
} else {
|
||||
self.get_ui_col("BG".to_string())
|
||||
};
|
||||
|
||||
self.canvas.draw_pixel(x as i32, y as i32, pixel_color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
texture = self.rl.load_texture_from_image(&self.thread, &self.canvas).unwrap();
|
||||
|
||||
|
||||
let rom_path = self.args[1].clone();
|
||||
let text_col = self.get_ui_col("TEXT".to_string());
|
||||
|
||||
let mut d = self.rl.begin_drawing(&self.thread);
|
||||
|
||||
d.clear_background(Color::RED);
|
||||
|
||||
d.draw_texture_ex(&texture, Vector2::new(0.,0.), 0., (WIN_SCALE_FAC) as f32, Color::WHITE);
|
||||
|
||||
|
||||
let text_width = d.measure_text(&rom_path.as_str(), 20) as u32;
|
||||
let x_pos = ((WIN_WIDTH - text_width) as f32) * 0.5;
|
||||
if self.config.show_path {
|
||||
d.draw_text(&self.args[1], x_pos as i32, 12, 20, text_col);
|
||||
}
|
||||
if self.config.show_fps {
|
||||
d.draw_text(&format!("{}", d.get_fps()), 10, 10, 20, text_col);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn update_clocks(&mut self) -> bool{
|
||||
self.clock_timer += self.rl.get_frame_time();
|
||||
self.clock_timer += self.graphics_manager.rl.get_frame_time();
|
||||
|
||||
let mut beep = false;
|
||||
|
||||
@@ -194,7 +128,7 @@ impl AppManager {
|
||||
},
|
||||
"NEXT_THEME" => {
|
||||
println!("ACTION: Switched theme");
|
||||
self.next_theme();
|
||||
self.graphics_manager.next_theme();
|
||||
visuals_modified = true;
|
||||
},
|
||||
"HONK" => {
|
||||
@@ -213,23 +147,5 @@ impl AppManager {
|
||||
};
|
||||
visuals_modified
|
||||
}
|
||||
|
||||
fn get_ui_col(&self, color_name: String) -> Color {
|
||||
let theme: HashMap<String,String> = self.theme_manager.themes[self.theme_manager.theme_index as usize].clone();
|
||||
|
||||
let color_code = theme.get(&color_name).expect("ERROR: Invalid theme color key used").replace("#", "");
|
||||
|
||||
Color::from_hex(&color_code).expect("ERROR: invalid theme data")
|
||||
}
|
||||
|
||||
fn next_theme(&mut self) {
|
||||
if self.theme_manager.theme_index < (self.theme_manager.num_themes - 1) {
|
||||
self.theme_manager.theme_index += 1;
|
||||
}
|
||||
else {
|
||||
self.theme_manager.theme_index = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+91
-3
@@ -1,12 +1,100 @@
|
||||
use raylib::prelude::*;
|
||||
use chip8_core::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct GraphicsManager {
|
||||
|
||||
use crate::{config::Config, themes::ThemeManager};
|
||||
|
||||
const WIN_SCALE_FAC: u32 = 15;
|
||||
const WIN_WIDTH: u32 = (SCREEN_WIDTH as u32) * WIN_SCALE_FAC;
|
||||
const WIN_HEIGHT: u32 = (SCREEN_HEIGHT as u32) * WIN_SCALE_FAC;
|
||||
|
||||
pub struct GraphicsManager {
|
||||
pub rl: RaylibHandle,
|
||||
pub thread: RaylibThread,
|
||||
pub canvas: Image,
|
||||
|
||||
pub theme_manager: ThemeManager,
|
||||
}
|
||||
|
||||
impl GraphicsManager {
|
||||
pub fn new() -> Self {
|
||||
GraphicsManager {
|
||||
|
||||
let (mut raylib_handle, raylib_thread) = raylib::init()
|
||||
.resizable()
|
||||
.size(WIN_WIDTH as i32, WIN_HEIGHT as i32)
|
||||
.title("Chip8 emulator")
|
||||
.build();
|
||||
|
||||
raylib_handle.set_trace_log(TraceLogLevel::LOG_NONE);
|
||||
raylib_handle.set_exit_key(None);
|
||||
|
||||
GraphicsManager {
|
||||
rl: raylib_handle,
|
||||
thread: raylib_thread,
|
||||
canvas: Image::gen_image_color(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, Color::GREENYELLOW),
|
||||
|
||||
theme_manager: ThemeManager::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn render_game(&mut self, args: &[String], config: &Config, emulator: &Emu, framebuffer_modified: bool) {
|
||||
let texture = self.rl.load_texture_from_image(&self.thread, &self.canvas).unwrap();
|
||||
|
||||
if framebuffer_modified {
|
||||
let screenbuffer = emulator.get_display();
|
||||
|
||||
for x in 0..SCREEN_WIDTH {
|
||||
for y in 0..SCREEN_HEIGHT {
|
||||
let pixel_color = if screenbuffer[x + SCREEN_WIDTH * y] {
|
||||
self.get_ui_col("FG".to_string())
|
||||
} else {
|
||||
self.get_ui_col("BG".to_string())
|
||||
};
|
||||
|
||||
self.canvas.draw_pixel(x as i32, y as i32, pixel_color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
let rom_path = args[1].clone();
|
||||
let text_col = self.get_ui_col("TEXT".to_string());
|
||||
|
||||
let mut d = self.rl.begin_drawing(&self.thread);
|
||||
|
||||
d.clear_background(Color::RED);
|
||||
|
||||
d.draw_texture_ex(&texture, Vector2::new(0.,0.), 0., (WIN_SCALE_FAC) as f32, Color::WHITE);
|
||||
|
||||
|
||||
let text_width = d.measure_text(rom_path.as_str(), 20) as u32;
|
||||
let x_pos = ((WIN_WIDTH - text_width) as f32) * 0.5;
|
||||
if config.show_path {
|
||||
d.draw_text(&args[1], x_pos as i32, 12, 20, text_col);
|
||||
}
|
||||
if config.show_fps {
|
||||
d.draw_text(&format!("{}", d.get_fps()), 10, 10, 20, text_col);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn get_ui_col(&self, color_name: String) -> Color {
|
||||
let theme: HashMap<String,String> = self.theme_manager.themes[self.theme_manager.theme_index as usize].clone();
|
||||
|
||||
let color_code = theme.get(&color_name).expect("ERROR: Invalid theme color key used").replace("#", "");
|
||||
|
||||
Color::from_hex(&color_code).expect("ERROR: invalid theme data")
|
||||
}
|
||||
|
||||
pub fn next_theme(&mut self) {
|
||||
if self.theme_manager.theme_index < (self.theme_manager.num_themes - 1) {
|
||||
self.theme_manager.theme_index += 1;
|
||||
}
|
||||
else {
|
||||
self.theme_manager.theme_index = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,8 +108,8 @@ impl InputManager {
|
||||
}
|
||||
|
||||
pub fn generate_keymaps_from_config(&mut self, config: &Config) {
|
||||
let used_keys = self.generate_emu_keymap_from_config(&config);
|
||||
self.generate_game_keymap_from_config(&config, used_keys);
|
||||
let used_keys = self.generate_emu_keymap_from_config(config);
|
||||
self.generate_game_keymap_from_config(config, used_keys);
|
||||
|
||||
println!("INFO: Generated keymaps from config")
|
||||
}
|
||||
@@ -156,11 +156,11 @@ impl InputManager {
|
||||
pub fn handle_game_input(&mut self, emulator: &mut Emu, rl: &RaylibHandle) {
|
||||
|
||||
for (ray_key, key_id) in &self.game_keymap {
|
||||
if rl.is_key_down(ray_key.clone()) {
|
||||
emulator.keypress(key_id.clone() as usize, true);
|
||||
if rl.is_key_down(*ray_key) {
|
||||
emulator.keypress(*key_id as usize, true);
|
||||
}
|
||||
else {
|
||||
emulator.keypress(key_id.clone() as usize, false);
|
||||
emulator.keypress(*key_id as usize, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -172,7 +172,7 @@ impl InputManager {
|
||||
let mut actions_buffer = Vec::new();
|
||||
|
||||
for (action, ray_key) in emu_keymap {
|
||||
if rl.is_key_pressed(ray_key.clone()) {
|
||||
if rl.is_key_pressed(ray_key) {
|
||||
actions_buffer.push(action);
|
||||
}
|
||||
};
|
||||
@@ -180,4 +180,4 @@ impl InputManager {
|
||||
actions_buffer
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user