Skip to content

Commit

Permalink
Update the Text Mode examples to create their own minifb window
Browse files Browse the repository at this point in the history
  • Loading branch information
ry755 committed Feb 1, 2021
1 parent 2547a92 commit 927ab44
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
3 changes: 2 additions & 1 deletion examples/high_level_text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ authors = ["ry755"]
edition = "2018"

[dependencies]
tms9918a_emu = { path = "../../" }
tms9918a_emu = { path = "../../" }
minifb = "0.19.2"
33 changes: 30 additions & 3 deletions examples/high_level_text/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
// TMS9918A Text Mode example using high-level functions

use minifb::{Scale, ScaleMode, Window, WindowOptions};
use tms9918a_emu::{TMS9918A, VideoMode};

fn main() {
// create a new TMS9918A VDP instance
let mut vdp = TMS9918A::new("TMS9918A Text Mode Example (high-level)");
let mut vdp = TMS9918A::new();

// create a new minifb window
let mut window = Window::new(
"TMS9918A Text Mode Example (high-level)",
256,
196,
WindowOptions {
resize: true,
scale_mode: ScaleMode::AspectRatioStretch,
scale: Scale::X4,
..WindowOptions::default()
},
)
.unwrap_or_else(|e| {
panic!("{}", e);
});

// limit to max ~60 fps update rate
window.limit_update_rate(Some(std::time::Duration::from_micros(16600)));

// set the name table base address to 0x0000 (base address = multiplier * 0x0400)
vdp.set_name_table_multiplier(0);
Expand Down Expand Up @@ -35,8 +55,15 @@ fn main() {
// enable video output (sets the blanking bit in register 1)
vdp.enable_video(true);

// update window contents
while vdp.window.is_open() {
// update VDP framebuffer and window contents
while window.is_open() {
vdp.update();

window.update_with_buffer(
&vdp.frame,
vdp.frame_width,
vdp.frame_height,
)
.unwrap();
}
}
3 changes: 2 additions & 1 deletion examples/low_level_text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ authors = ["ry755"]
edition = "2018"

[dependencies]
tms9918a_emu = { path = "../../" }
tms9918a_emu = { path = "../../" }
minifb = "0.19.2"
36 changes: 33 additions & 3 deletions examples/low_level_text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,31 @@
// the data byte pointed to by the (now incremented) internal address pointer is immediately read into the read-ahead register
// additional VRAM data bytes can be read from the data port without needing to send the address again

use minifb::{Scale, ScaleMode, Window, WindowOptions};
use tms9918a_emu::TMS9918A;

fn main() {
// create a new TMS9918A VDP instance
let mut vdp = TMS9918A::new("TMS9918A Text Mode Example (low-level)");
let mut vdp = TMS9918A::new();

// create a new minifb window
let mut window = Window::new(
"TMS9918A Text Mode Example (low-level)",
256,
196,
WindowOptions {
resize: true,
scale_mode: ScaleMode::AspectRatioStretch,
scale: Scale::X4,
..WindowOptions::default()
},
)
.unwrap_or_else(|e| {
panic!("{}", e);
});

// limit to max ~60 fps update rate
window.limit_update_rate(Some(std::time::Duration::from_micros(16600)));

// register 0: disable bitmap mode, disable external video input
vdp.write_control_port(0b00000000);
Expand All @@ -58,6 +78,7 @@ fn main() {
// set VDP internal address pointer to the pattern table location
vdp.write_control_port(0x00);
vdp.write_control_port(0x48); // 0x08 | 0x40

// fill pattern table with font data
let font = include_bytes!("font.bin");
for i in font.iter() {
Expand All @@ -67,6 +88,7 @@ fn main() {
// set VDP internal address pointer to the name table location
vdp.write_control_port(0x00);
vdp.write_control_port(0x40); // 0x00 | 0x40

// clear the screen
// the video memory contains random data on startup, similar to how real memory works
for _ in 0..960 { // 40x24 tiles = 960
Expand All @@ -76,14 +98,22 @@ fn main() {
// set VDP internal address pointer to the name table location + 40 to start on the second line of tiles
vdp.write_control_port(0x28);
vdp.write_control_port(0x40); // 0x00 | 0x40

// write text by iterating over a string
let text_string = "Hello, world!";
for c in text_string.chars() {
vdp.write_data_port(c as u8);
}

// update window contents
while vdp.window.is_open() {
// update VDP framebuffer and window contents
while window.is_open() {
vdp.update();

window.update_with_buffer(
&vdp.frame,
vdp.frame_width,
vdp.frame_height,
)
.unwrap();
}
}

0 comments on commit 927ab44

Please sign in to comment.