Got the basics of sprites working
This commit is contained in:
+116
-4
@@ -29,6 +29,7 @@ export type uxn = struct {
|
||||
//TODO find a way to use these below
|
||||
export def MAXWIDTH = 4096;
|
||||
export def MAXHEIGHT = 2160;
|
||||
export def SCREENRATE = 60;
|
||||
//Screen consists of two layers of 2-bit pixels
|
||||
export type uxn_screen = ([MAXWIDTH][MAXHEIGHT]u8, [MAXWIDTH][MAXHEIGHT]u8);
|
||||
|
||||
@@ -90,7 +91,7 @@ fn emu_deo(port: u8, value: u8, state: *uxn) void = {
|
||||
case 0x2e =>
|
||||
draw_pixel(value,state);
|
||||
case 0x2f =>
|
||||
yield;
|
||||
draw_sprite(value,state);
|
||||
case =>
|
||||
if( port == 0x22 ||
|
||||
port == 0x23 ||
|
||||
@@ -133,13 +134,31 @@ fn draw_pixel(value: u8, state: *uxn) void = {
|
||||
|
||||
if(!fill){
|
||||
// fmt::printfln("Pixel at x: {:x} y: {:x} color: {:x}", x, y, color)!;
|
||||
const auto = state.dev[0x26];
|
||||
let count = ((auto & 0xf0) >> 4): i8;
|
||||
const auto_x = (auto & 0b00000001) != 0;
|
||||
const auto_y = (auto & 0b00000010) != 0;
|
||||
const auto_a = (auto & 0b00000100) != 0;
|
||||
if(!layer1){
|
||||
state.screen.0[x][y] = color;
|
||||
} else {
|
||||
state.screen.1[x][y] = color;
|
||||
};
|
||||
if(auto_x) {
|
||||
const new_x = x + 1;
|
||||
state.dev[0x28] = (new_x >> 8): u8;
|
||||
state.dev[0x29] = (new_x): u8;
|
||||
|
||||
};
|
||||
if(auto_y) {
|
||||
const new_y = y + 1;
|
||||
state.dev[0x2a] = (new_y >> 8): u8;
|
||||
state.dev[0x2b] = (new_y): u8;
|
||||
|
||||
};
|
||||
}else{
|
||||
fmt::println("Doing Fill")!;
|
||||
//TODO see if auto still happens with fill
|
||||
// fmt::println("Doing Fill")!;
|
||||
const dims = get_window_size(state);
|
||||
const startx: u16 = if(flipx) {yield 0; }else{ yield x; };
|
||||
const starty: u16 = if(flipy) {yield 0; }else{ yield y; };
|
||||
@@ -162,6 +181,93 @@ fn draw_pixel(value: u8, state: *uxn) void = {
|
||||
state.screen_update = true;
|
||||
};
|
||||
|
||||
fn draw_sprite(value: u8, state: *uxn) void = {
|
||||
//TODO handle fill
|
||||
const colors = value & 0b00001111;
|
||||
const auto = state.dev[0x26];
|
||||
let count = ((auto & 0xf0) >> 4): i8;
|
||||
const auto_x = (auto & 0b00000001) != 0;
|
||||
const auto_y = (auto & 0b00000010) != 0;
|
||||
const auto_a = (auto & 0b00000100) != 0;
|
||||
const bpp2: bool = (value & 0b10000000) != 0;
|
||||
const layer1: bool = (value & 0b01000000) != 0;
|
||||
const flipy: bool = (value & 0b00100000) != 0;
|
||||
const flipx: bool = (value & 0b00010000) != 0;
|
||||
|
||||
for(let i = 0; i <= count; i+=1){
|
||||
const addr = short_from_bytes(state.dev[0x2c],state.dev[0x2d]);
|
||||
const x = short_from_bytes(state.dev[0x28],state.dev[0x29]);
|
||||
const y = short_from_bytes(state.dev[0x2a],state.dev[0x2b]);
|
||||
copy_sprite_to_screen(bpp2,colors,x,y,addr,flipx,flipy,layer1,state);
|
||||
|
||||
if(auto_x) {
|
||||
const new_x = x + 8;
|
||||
state.dev[0x28] = (new_x >> 8): u8;
|
||||
state.dev[0x29] = (new_x): u8;
|
||||
|
||||
};
|
||||
if(auto_y) {
|
||||
const new_y = y + 8;
|
||||
state.dev[0x2a] = (new_y >> 8): u8;
|
||||
state.dev[0x2b] = (new_y): u8;
|
||||
|
||||
};
|
||||
if(auto_a){
|
||||
const new_addr = if(bpp2) { yield addr + 16; } else { yield addr + 8; };
|
||||
|
||||
state.dev[0x2c] = (new_addr >> 8): u8;
|
||||
state.dev[0x2d] = (new_addr): u8;
|
||||
};
|
||||
};
|
||||
state.screen_update = true;
|
||||
};
|
||||
|
||||
|
||||
fn copy_sprite_to_screen(bpp2: bool, colors: u8, x: u16, y: u16, addr: u16, flipx: bool, flipy: bool, layer1: bool, state: *uxn) void = {
|
||||
const bytes_per_sprite = if(bpp2) {yield 16;} else {yield 8;};
|
||||
let xoff: u16 = 0;
|
||||
let yoff: u16 = 0;
|
||||
|
||||
|
||||
|
||||
// fmt::printfln("Drawing Sprite to x: {} y: {}", x, y)!;
|
||||
for(let i = 0; i < bytes_per_sprite; i+=1){
|
||||
let data: u8 = state.ram[addr + i: u16];
|
||||
const pixels: []u8 = if(bpp2){
|
||||
yield [
|
||||
(data & 0b11000000) >> 6,
|
||||
(data & 0b00110000) >> 4,
|
||||
(data & 0b00001100) >> 2,
|
||||
(data & 0b00000011) >> 0 ];
|
||||
} else {
|
||||
yield [
|
||||
(data & 0b10000000) >> 7,
|
||||
(data & 0b01000000) >> 6,
|
||||
(data & 0b00100000) >> 5,
|
||||
(data & 0b00010000) >> 4,
|
||||
(data & 0b00001000) >> 3,
|
||||
(data & 0b00000100) >> 2,
|
||||
(data & 0b00000010) >> 1,
|
||||
(data & 0b00000001) >> 0 ];
|
||||
};
|
||||
|
||||
for(let p .. pixels){
|
||||
const color = p; //TODO handle right;
|
||||
fmt::printfln("Drawing 2bpp: {} Sprite Pixel to x: {} y: {} color: {}", bpp2, x+xoff, y+yoff, p)!;
|
||||
if(layer1) { state.screen.1[x+xoff][y+yoff] = p; }
|
||||
else {state.screen.0[x+xoff][y+yoff] = p; };
|
||||
xoff += 1;
|
||||
if(xoff >= 8){
|
||||
xoff = 0;
|
||||
yoff += 1;
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
export fn get_color(x: u16, y: u16, state: *uxn) u16 = {
|
||||
// check layer 1
|
||||
let color = state.screen.1[x][y] & 0b00000011;
|
||||
@@ -404,7 +510,7 @@ fn pop_or_get(inst: normal_op, off: u8, state: *uxn) (u8 | u16) = {
|
||||
yield get_stack_val(inst.ret,inst.short, off,state);
|
||||
};
|
||||
};
|
||||
fn uxn_eval(new_pc: u16, state: *uxn) (done | error ) = {
|
||||
export fn uxn_eval(new_pc: u16, state: *uxn) (done | error ) = {
|
||||
// let a: u16 = 0, b: u16 = 0, c: u16 = 0, x: [2]u16 = [0...], y: [2]u16 = [0...], z: [2]u16 = [0...];
|
||||
state.pc = new_pc;
|
||||
state.running = true;
|
||||
@@ -833,6 +939,12 @@ export fn uxn_init(path: str) ( *uxn | error ) = {
|
||||
};
|
||||
io::close(romfile)!; //TODO, isn't there some kind of oversized roms?
|
||||
|
||||
//Initialize default size
|
||||
state.dev[0x22] = 0x02;
|
||||
state.dev[0x23] = 0x80;
|
||||
state.dev[0x24] = 0x01;
|
||||
state.dev[0x25] = 0xe0;
|
||||
|
||||
//Initialize default colors TODO look for .theme files
|
||||
state.dev[0x08] = 0xf0;
|
||||
state.dev[0x09] = 0x7f;
|
||||
@@ -844,8 +956,8 @@ export fn uxn_init(path: str) ( *uxn | error ) = {
|
||||
return state;
|
||||
};
|
||||
export fn uxn_reset(state: *uxn) void = {
|
||||
state.pc = reset_vector;
|
||||
state.running = true;
|
||||
uxn_eval(reset_vector,state)!;
|
||||
};
|
||||
export fn uxnrun() void = {
|
||||
if(len(os::args) < 2){
|
||||
|
||||
Reference in New Issue
Block a user