From e158dd3bc5da18576c1eb884a8a65bcec241edf1 Mon Sep 17 00:00:00 2001 From: Ry Date: Tue, 21 Jun 2022 18:08:16 -0700 Subject: [PATCH] Add more generic tile drawing routines --- background.asm | 19 ++++++++++++ draw_tile.asm | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ fox32rom.def | 21 +++++++------ main.asm | 4 +++ overlay.asm | 29 ++++++++++++++++++ 5 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 draw_tile.asm diff --git a/background.asm b/background.asm index aac0fae..f2deda3 100644 --- a/background.asm +++ b/background.asm @@ -62,6 +62,25 @@ draw_filled_rectangle_to_background: pop r5 ret +; draw a single tile to the background +; inputs: +; r0: tile number +; r1: X coordinate +; r2: Y coordinate +; outputs: +; none +draw_tile_to_background: + push r8 + push r9 + + mov r8, BACKGROUND_FRAMEBUFFER + mov r9, 640 + call draw_tile_generic + + pop r9 + pop r8 + ret + ; draw a single font tile to the background ; inputs: ; r0: tile number diff --git a/draw_tile.asm b/draw_tile.asm new file mode 100644 index 0000000..c617fab --- /dev/null +++ b/draw_tile.asm @@ -0,0 +1,80 @@ +; generic tile drawing routines + +const TILEMAP_POINTER: 0x01FFFFF0 +const TILEMAP_WIDTH: 0x01FFFFF4 +const TILEMAP_HEIGHT: 0x01FFFFF8 + +; set the current tilemap +; inputs: +; r0: pointer to tilemap data +; r1: tile width +; r2: tile height +set_tilemap: + mov [TILEMAP_POINTER], r0 + mov [TILEMAP_WIDTH], r1 + mov [TILEMAP_HEIGHT], r2 + + ret + +; draw a single tile to a framebuffer +; inputs: +; r0: tile number +; r1: X coordinate +; r2: Y coordinate +; r8: pointer to framebuffer +; r9: framebuffer width (pixels) +; outputs: +; none +draw_tile_generic: + push r0 + push r1 + push r2 + push r5 + push r6 + push r7 + push r8 + push r9 + + ; calculate pointer to the tile data + mov r6, [TILEMAP_WIDTH] + mov r7, [TILEMAP_HEIGHT] + push r6 + mul r6, r7 + mul r0, r6 + mul r0, 4 ; 4 bytes per pixel + add r0, [TILEMAP_POINTER] ; r0: pointer to tile data + pop r6 + + ; calculate pointer to the framebuffer + mul r9, 4 ; 4 bytes per pixel + mul r2, r9 ; y * width * 4 + mul r1, 4 ; x * 4 + add r1, r2 ; y * width * 4 + (x * 4) + add r1, r8 ; r1: pointer to framebuffer + + ; r8: tile width in bytes + mov r8, r6 + mul r8, 4 + +draw_tile_generic_y_loop: + mov r5, r6 ; x counter +draw_tile_generic_x_loop: + mov [r1], [r0] ; draw pixel + add r0, 4 ; increment tile pointer + add r1, 4 ; increment framebuffer pointer + dec r5 + ifnz jmp draw_tile_generic_x_loop ; loop if there are still more X pixels to draw + sub r1, r8 ; return to the beginning of this line + add r1, r9 ; increment to the next line by adding the framebuffer width in bytes + dec r7 ; decrement height counter + ifnz jmp draw_tile_generic_y_loop ; loop if there are still more Y pixels to draw + + pop r9 + pop r8 + pop r7 + pop r6 + pop r5 + pop r2 + pop r1 + pop r0 + ret diff --git a/fox32rom.def b/fox32rom.def index 4936e8e..eb01e0d 100644 --- a/fox32rom.def +++ b/fox32rom.def @@ -15,8 +15,9 @@ draw_format_str_generic: jmp [0xF0041004] draw_decimal_generic: jmp [0xF0041008] draw_hex_generic: jmp [0xF004100C] draw_font_tile_generic: jmp [0xF0041010] -draw_pixel_generic: jmp [0xF0041014] -draw_filled_rectangle_generic: jmp [0xF0041018] +draw_tile_generic: jmp [0xF0041014] +draw_pixel_generic: jmp [0xF0041018] +draw_filled_rectangle_generic: jmp [0xF004101C] ; background jump table fill_background: jmp [0xF0042000] @@ -25,8 +26,9 @@ draw_format_str_to_background: jmp [0xF0042008] draw_decimal_to_background: jmp [0xF004200C] draw_hex_to_background: jmp [0xF0042010] draw_font_tile_to_background: jmp [0xF0042014] -draw_pixel_to_background: jmp [0xF0042018] -draw_filled_rectangle_to_background: jmp [0xF004201C] +draw_tile_to_background: jmp [0xF0042018] +draw_pixel_to_background: jmp [0xF004201C] +draw_filled_rectangle_to_background: jmp [0xF0042020] ; overlay jump table fill_overlay: jmp [0xF0043000] @@ -35,11 +37,12 @@ draw_format_str_to_overlay: jmp [0xF0043008] draw_decimal_to_overlay: jmp [0xF004300C] draw_hex_to_overlay: jmp [0xF0043010] draw_font_tile_to_overlay: jmp [0xF0043014] -draw_pixel_to_overlay: jmp [0xF0043018] -draw_filled_rectangle_to_overlay: jmp [0xF004301C] -find_overlay_covering_position: jmp [0xF0043020] -check_if_overlay_covers_position: jmp [0xF0043024] -check_if_enabled_overlay_covers_position: jmp [0xF0043028] +draw_tile_to_overlay: jmp [0xF0043018] +draw_pixel_to_overlay: jmp [0xF004301C] +draw_filled_rectangle_to_overlay: jmp [0xF0043020] +find_overlay_covering_position: jmp [0xF0043024] +check_if_overlay_covers_position: jmp [0xF0043028] +check_if_enabled_overlay_covers_position: jmp [0xF004302C] ; menu bar jump table enable_menu_bar: jmp [0xF0044000] diff --git a/main.asm b/main.asm index f7f6285..f058851 100644 --- a/main.asm +++ b/main.asm @@ -121,6 +121,7 @@ get_rom_version: #include "draw_pixel.asm" #include "draw_rectangle.asm" #include "draw_text.asm" + #include "draw_tile.asm" #include "event.asm" #include "integer.asm" #include "keyboard.asm" @@ -157,6 +158,7 @@ get_rom_version: data.32 draw_format_str_generic data.32 draw_decimal_generic data.32 draw_font_tile_generic + data.32 draw_tile_generic data.32 draw_pixel_generic data.32 draw_filled_rectangle_generic @@ -168,6 +170,7 @@ get_rom_version: data.32 draw_decimal_to_background data.32 draw_hex_to_background data.32 draw_font_tile_to_background + data.32 draw_tile_to_background data.32 draw_pixel_to_background data.32 draw_filled_rectangle_to_background @@ -179,6 +182,7 @@ get_rom_version: data.32 draw_decimal_to_overlay data.32 draw_hex_to_overlay data.32 draw_font_tile_to_overlay + data.32 draw_tile_to_overlay data.32 draw_pixel_to_overlay data.32 draw_filled_rectangle_to_overlay data.32 find_overlay_covering_position diff --git a/overlay.asm b/overlay.asm index 6b26f7b..993c7af 100644 --- a/overlay.asm +++ b/overlay.asm @@ -85,6 +85,35 @@ draw_filled_rectangle_to_overlay: pop r5 ret +; draw a single tile to an overlay +; inputs: +; r0: tile number +; r1: X coordinate +; r2: Y coordinate +; r3: overlay number +; outputs: +; none +draw_tile_to_overlay: + push r3 + push r4 + push r8 + push r9 + + mov r4, r3 + or r4, 0x80000100 ; bitwise or the overlay number with the command to get the overlay size + or r3, 0x80000200 ; bitwise or the overlay number with the command to get the framebuffer pointer + in r8, r3 ; r8: overlay framebuffer poiner + in r9, r4 + and r9, 0x0000FFFF ; r9: overlay width + + call draw_tile_generic + + pop r9 + pop r8 + pop r4 + pop r3 + ret + ; draw a single font tile to an overlay ; inputs: ; r0: tile number