Skip to content

Instantly share code, notes, and snippets.

@richwandell
Created February 6, 2022 14:54
Show Gist options
  • Save richwandell/b5d8c36132dfd1c900cdf690f02a02fd to your computer and use it in GitHub Desktop.
Save richwandell/b5d8c36132dfd1c900cdf690f02a02fd to your computer and use it in GitHub Desktop.
not sure how to do this
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use glutin_window::GlutinWindow as Window;
use graphics::{clear, Transformed};
use graphics::Context;
use image::{ImageBuffer, Rgba};
use opengl_graphics::{GlGraphics, GlyphCache, OpenGL, Texture, TextureSettings};
use piston::{Button, PressEvent};
use piston::event_loop::{Events, EventSettings};
use piston::input::{RenderArgs, RenderEvent, UpdateArgs, UpdateEvent};
use piston::window::WindowSettings;
use crate::{Cpu, State};
use crate::display::{EMU_HEIGHT, EMU_WIDTH};
use crate::display::draw_debug::draw_debug;
use crate::display::draw_pixels::draw_pixels;
fn get_scaled_context(c: Context) -> Context {
let size = c.get_view_size();
let x_scaler = size[0] / EMU_WIDTH as f64;
let y_scaler = size[1] / EMU_HEIGHT as f64;
c.scale(x_scaler, y_scaler)
}
pub struct NesSystem {
window: Window,
gl: GlGraphics,
state: Rc<RefCell<State>>,
cpu: Rc<RefCell<Cpu>>,
debug: bool
}
pub trait Game {
fn new(state: Rc<RefCell<State>>, cpu: Rc<RefCell<Cpu>>, debug: bool) -> NesSystem {
let opengl = OpenGL::V3_2;
let window: Window = WindowSettings::new("IronNES", [EMU_WIDTH * 3, EMU_HEIGHT * 3])
.graphics_api(opengl)
.exit_on_esc(true)
.build()
.unwrap();
let gl = GlGraphics::new(opengl);
NesSystem {
window,
gl,
state,
cpu,
debug
}
}
fn render(&mut self,
args: RenderArgs,
d_img: &mut ImageBuffer<Rgba<u8>, Vec<u8>>,
texture: &mut Texture,
disassembly: &mut HashMap<u16, String>
);
fn start<U, F>(&mut self,settings: EventSettings, update: U, button: F)
where U: Fn(UpdateArgs), F: Fn(Button);
}
impl Game for NesSystem {
fn render(&mut self,
args: RenderArgs,
mut d_img: &mut ImageBuffer<Rgba<u8>, Vec<u8>>,
mut texture: &mut Texture,
disassembly: &mut HashMap<u16, String>
) {
let state = self.state.as_ref().borrow();
let cpu = self.cpu.as_ref().borrow();
let mut glyphs: GlyphCache = GlyphCache::new("assets/PixelEmulator-xq08.ttf", (), TextureSettings::new()).unwrap();
let debug = self.debug;
self.gl.draw(args.viewport(), |c, gl| {
//Clear the screen
clear([0.0, 0.0, 1.0, 1.0], gl);
let context = get_scaled_context(c);
if debug {
draw_debug(&*state, &*cpu, c, &mut glyphs, disassembly, gl);
} else {
draw_pixels(&*state, d_img, texture, context, gl);
}
});
}
fn start<U, F>(&mut self,settings: EventSettings, update: U, button: F)
where U: Fn(UpdateArgs), F: Fn(Button) {
let mut events = Events::new(settings);
let mut d_img = ImageBuffer::from_fn(EMU_WIDTH, EMU_HEIGHT, |x, y| {
image::Rgba([255, 255, 255, 255])
});
let mut texture = Texture::from_image(&d_img, &TextureSettings::new());
let mut disassembly = self.cpu.as_ref().borrow_mut().disassemble();
// Main loop
while let Some(e) = events.next(&mut self.window) {
if let Some(args) = e.press_args() {
button(args);
}
if let Some(args) = e.render_args() {
self.render(args, &mut d_img, &mut texture, &mut disassembly);
} else if let Some(args) = e.update_args() {
update(args);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment