Get video_sdl2 compiling

This commit is contained in:
Reid 'arrdem' McKenzie 2023-01-01 23:37:10 -07:00
parent f52fe61359
commit 45bffd1dcf
2 changed files with 41 additions and 30 deletions

View file

@ -2,6 +2,7 @@ pub mod buff;
pub mod console; pub mod console;
pub mod null; pub mod null;
pub mod system; pub mod system;
pub mod video_sdl2;
use crate::vm::Uxn; use crate::vm::Uxn;

View file

@ -1,6 +1,9 @@
use crate::uxn::device::Device; use crate::{
device::{Device, DeviceError},
vm::Uxn,
};
static blending: [[u8; 16]; 5] = [ static BLENDING: [[u8; 16]; 5] = [
[0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0], [0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0],
[0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],
[1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1], [1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1],
@ -8,17 +11,19 @@ static blending: [[u8; 16]; 5] = [
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
]; ];
static palette_mono: [u32; 2] = [0x0f000000u32, 0x0fffffffu32]; static PALETTE_MONO: [u32; 2] = [0x0f000000u32, 0x0fffffffu32];
#[derive(Debug)]
struct Layer { struct Layer {
pixels: Vec<u8>, pixels: Vec<u8>,
changed: bool, changed: bool,
} }
#[derive(Debug)]
pub struct Sdl2VideoDevice { pub struct Sdl2VideoDevice {
vector: u16, vector: u16,
palette: [u32; 4], palette: [u32; 4],
pixels: &[u32], pixels: Vec<u32>,
width: u16, width: u16,
height: u16, height: u16,
fg: Layer, fg: Layer,
@ -37,7 +42,7 @@ impl Sdl2VideoDevice {
} }
fn clear(&mut self) { fn clear(&mut self) {
for i in 0..self.width * self.height { for i in 0..(self.width * self.height) as usize {
// FIXME: Is there some clear/set operation on vector? You can write your own seemingly, but it's this loop. // FIXME: Is there some clear/set operation on vector? You can write your own seemingly, but it's this loop.
self.fg.pixels[i] = 0x00; self.fg.pixels[i] = 0x00;
self.bg.pixels[i] = 0x00; self.bg.pixels[i] = 0x00;
@ -50,8 +55,8 @@ impl Sdl2VideoDevice {
fn write_pixel(&mut self, layer: bool, x: u16, y: u16, color: u8) { fn write_pixel(&mut self, layer: bool, x: u16, y: u16, color: u8) {
let layer = if layer { &mut self.fg } else { &mut self.bg }; let layer = if layer { &mut self.fg } else { &mut self.bg };
if x < self.width && y < self.height { if x < self.width && y < self.height {
let idx = x + (y * self.height) as usize; let idx = (x + (y * self.height)) as usize;
if color != layer.pixes[idx] { if color != layer.pixels[idx] {
layer.pixels[idx] = color; layer.pixels[idx] = color;
layer.changed |= true; layer.changed |= true;
} }
@ -60,30 +65,35 @@ impl Sdl2VideoDevice {
/** /**
* Roll through the pixels associated with a sprite, calling write_pixel for each one. * Roll through the pixels associated with a sprite, calling write_pixel for each one.
*
* Known as `screen_blit` in the C implementation.
*/ */
fn write_sprite( fn write_sprite(
&mut self, &mut self,
layer: bool, layer: bool,
x: u16, x: u16,
y: u16, y: u16,
sprite_ptr: (), sprite: &[u8],
color: u8,
flipx: bool, flipx: bool,
flipy: bool, flipy: bool,
twobpp: bool, twobpp: bool,
) { ) {
let mut v = h = opaque = blending[4][color]; let v = BLENDING[4][color as usize];
let h = v;
let opaque = v;
for v in 0..8 { for v in 0..8 {
let c: u8 = sprite[v] | (if twobpp { sprite[v + 8] } else { 0 }) << 8; let mut c: u8 = sprite[v] | (if twobpp { sprite[v + 8] } else { 0 }) << 8;
for h in 7..0 { for h in 7..0 {
c = c >>= 1; c = c >> 1;
let ch = (c & 1) | ((c >> 7) & 2); let ch = ((c & 1) | ((c >> 7) & 2)) as usize;
if (opaque || (ch != 0)) { if opaque != 0 || ch != 0 {
self.write_pixel( self.write_pixel(
layer, layer,
x + (if flipx { 7 - h } else { h }), x + (if flipx { 7 - h } else { h }) as u16,
y + (if flipy { 7 - v } else { v }), y + (if flipy { 7 - v } else { v }) as u16,
blending[ch][color], BLENDING[ch][color as usize],
); );
} }
} }
@ -104,7 +114,7 @@ impl Sdl2VideoDevice {
let g = (colors[2 + i / 2] >> shift) & 0x0f; let g = (colors[2 + i / 2] >> shift) & 0x0f;
let b = (colors[4 + i / 2] >> shift) & 0x0f; let b = (colors[4 + i / 2] >> shift) & 0x0f;
self.palette[i] = 0x0f000000 | r << 16 | g << 8 | b; self.palette[i] = 0x0f000000u32 | (r as u32) << 16 | (g as u32) << 8 | b as u32;
self.palette[i] |= self.palette[i] << 4; self.palette[i] |= self.palette[i] << 4;
shift ^= 4; shift ^= 4;
@ -117,23 +127,23 @@ impl Sdl2VideoDevice {
fn redraw(&mut self) { fn redraw(&mut self) {
let size = self.width * self.height; let size = self.width * self.height;
let mut pallet = [0u32; 16]; let mut palette = [0u32; 16];
for i in 0..16 { for i in 0..16 {
let idx = if (i >> 2) != 0 { i >> 2 } else { i & 3 }; let idx = if (i >> 2) != 0 { i >> 2 } else { i & 3 };
palette[i] = self.palette[idx]; palette[i] = self.palette[idx];
} }
if (self.mono) { if self.mono != 0 {
for i in i..size { for i in 0..size as usize {
let idx = (if self.fg.pixels[i] != 0 { let idx = (if self.fg.pixels[i] != 0 {
self.fg.pixels[i] self.fg.pixels[i]
} else { } else {
self.bg.pixels[i] self.bg.pixels[i]
}) & 0x1; }) & 0x1;
pixels[i] = palette_mono[idx]; self.pixels[i] = PALETTE_MONO[idx as usize];
} }
} else { } else {
for i in 0..size { for i in 0..size as usize {
pixels[i] = palette[self.fg.pixels[i] << 2 | self.bg.pixels[i]]; self.pixels[i] = palette[(self.fg.pixels[i] << 2 | self.bg.pixels[i]) as usize];
} }
} }
self.fg.changed = false; self.fg.changed = false;
@ -146,12 +156,12 @@ impl Device for Sdl2VideoDevice {
let slot = port & 0xF; let slot = port & 0xF;
// FIXME: this is sorta copypasta, but LOTS of source byte order assumptions here. // FIXME: this is sorta copypasta, but LOTS of source byte order assumptions here.
match slot { match slot {
0x0 => Ok(self.vector >> 8), 0x0 => Ok((self.vector >> 8) as u8),
0x1 => Ok(self.vector & 0xFF), 0x1 => Ok((self.vector & 0xFF) as u8),
0x2 => Ok(self.width >> 8), 0x2 => Ok((self.width >> 8) as u8),
0x3 => Ok(self.width & 0xFF), 0x3 => Ok((self.width & 0xFF) as u8),
0x4 => Ok(self.height >> 8), 0x4 => Ok((self.height >> 8) as u8),
0x5 => Ok(self.height & 0xFF), 0x5 => Ok((self.height & 0xFF) as u8),
_ => Ok(0), _ => Ok(0),
} }
} }
@ -182,6 +192,6 @@ impl Device for Sdl2VideoDevice {
_ => (), _ => (),
} }
Ok() Ok(())
} }
} }