prog8/examples/cx16/imgviewer/koalaviewer.p8

145 lines
4.3 KiB
Plaintext
Raw Normal View History

%target cx16
%import graphics
%import textio
%import diskio
%import c64colors
main {
2020-12-11 22:32:47 +00:00
const uword load_location = $6000
sub start() {
2020-12-11 22:00:58 +00:00
graphics.enable_bitmap_mode()
; set a better C64 color palette, the Cx16's default is too saturated
c64colors.set_palette_pepto()
2020-12-11 22:00:58 +00:00
2020-12-11 22:32:47 +00:00
if strlen(diskio.status(8)) ; trick to check if we're running on sdcard or host system shared folder
show_pics_sdcard()
else
show_file_list()
repeat {
2020-12-11 22:00:58 +00:00
;
}
}
2020-12-11 22:32:47 +00:00
sub show_file_list() {
; listing a directory doesn't work with a shared host directory in the emulator...
str[] pictures = [
"i01-blubb-sphinx.koa",
"i02-bugjam-jsl.koa",
"i03-dinothawr-ar.koa",
"i04-fox-leon.koa",
"i05-hunter-agod.koa",
"i06-jazzman-jds.koa",
"i07-katakis-jegg.koa"
]
uword nameptr
for nameptr in pictures {
uword size = diskio.load(8, nameptr, load_location)
if size==10001 {
convert_koalapic()
} else {
txt.print_uw(size)
txt.print("\nload error\n")
txt.print(diskio.status(8))
}
load_image_from_disk(nameptr)
2020-12-12 02:32:15 +00:00
cx16.wait(60)
2020-12-11 22:32:47 +00:00
}
}
sub show_pics_sdcard() {
2020-12-11 22:00:58 +00:00
; load and show all *.koa pictures on the disk.
; this only works in the emulator V38 with an sd-card image with the files on it.
str[20] filename_ptrs
ubyte num_files = diskio.list_files(8, ".koa", true, &filename_ptrs, len(filename_ptrs))
if num_files {
while num_files {
num_files--
load_image_from_disk(filename_ptrs[num_files])
2020-12-12 02:32:15 +00:00
cx16.wait(60)
}
2020-12-11 22:00:58 +00:00
} else {
2020-12-12 00:17:30 +00:00
txt.print("no *.koa files found\n")
}
}
2020-12-11 22:00:58 +00:00
sub load_image_from_disk(uword filenameptr) {
; special load routine that uses per-byte loading so it works from an sd-card image
if diskio.f_open(8, filenameptr) {
uword size = diskio.f_read(load_location, 2) ; skip the first 2 bytes (load address)
if size==2 {
size = diskio.f_read(load_location, 10001)
if size == 10001 {
convert_koalapic()
} else {
txt.print_uw(size)
txt.print("\nload error\n")
txt.print(diskio.status(8))
}
}
diskio.f_close()
}
}
sub convert_koalapic() {
ubyte cy
2020-12-06 06:23:57 +00:00
ubyte @zp cx
uword @zp cy_times_forty = 0
ubyte @zp d
2020-12-06 17:38:27 +00:00
uword bitmap_ptr = load_location
; theoretically you could put the 8-pixel array in zeropage to squeeze out another tiny bit of performance
ubyte[8] pixels
2020-12-06 06:23:57 +00:00
for cy in 0 to 24*8 step 8 {
for cx in 0 to 39 {
for d in 0 to 7 {
cx16.r0 = cx as uword * 8
cx16.r1 = cy as uword + d
cx16.FB_cursor_position()
get_8_pixels()
cx16.r0 = &pixels
cx16.r1 = 8
cx16.FB_set_pixels()
}
}
cy_times_forty += 40
}
sub get_8_pixels() {
2020-12-06 17:38:27 +00:00
ubyte bm = @(bitmap_ptr)
ubyte @zp m = mcol(bm)
pixels[7] = m
pixels[6] = m
2020-12-06 06:23:57 +00:00
bm >>= 2
m = mcol(bm)
pixels[5] = m
pixels[4] = m
2020-12-06 06:23:57 +00:00
bm >>= 2
m = mcol(bm)
pixels[3] = m
pixels[2] = m
2020-12-06 06:23:57 +00:00
bm >>= 2
m = mcol(bm)
pixels[1] = m
pixels[0] = m
2020-12-06 17:38:27 +00:00
bitmap_ptr++
sub mcol(ubyte b) -> ubyte {
2020-12-06 06:23:57 +00:00
ubyte @zp color
when b & 3 {
2020-12-06 17:38:27 +00:00
0 -> color = @(load_location + 8000 + 1000 + 1000) & 15
1 -> color = @(load_location + 8000 + cy_times_forty + cx) >>4
2 -> color = @(load_location + 8000 + cy_times_forty + cx) & 15
else -> color = @(load_location + 8000 + 1000 + cy_times_forty + cx) & 15
}
return color
}
}
}
}