refactor out duplicate and dead code

This commit is contained in:
2025-08-08 14:40:59 -04:00
parent faf19db27e
commit b5496d93bc
20 changed files with 716 additions and 1957 deletions

View File

@@ -5,6 +5,8 @@ var time = use('time')
var res = use('resources')
var json = use('json')
var os = use('os')
var staef = use('staef')
var qoi = use('qoi')
var LASTUSE = Symbol()
var LOADING = Symbol()
@@ -95,7 +97,13 @@ function decode_image(bytes, ext)
case 'gif': return graphics.make_gif(bytes) // returns array of surfaces
case 'ase':
case 'aseprite': return graphics.make_aseprite(bytes)
default: return graphics.make_texture(bytes) // returns single surface
case 'qoi': return qoi.decode(bytes) // returns single surface
default:
// Try QOI first since it's fast to check
var qoi_result = qoi.decode(bytes)
if (qoi_result) return qoi_result
// Fall back to make_texture for other formats
return graphics.image_decode(bytes) // returns single surface
}
}
@@ -221,8 +229,84 @@ graphics.texture = function texture(path) {
if (!cache[id]) {
var ipath = res.find_image(id)
if (!ipath)
// If not found normally, check in accio/32k folder
if (!ipath) {
// First check if we have a cached QOI version
var cache_dir = '.prosperon/texture_cache'
var qoi_cache_path = `${cache_dir}/${id}.qoi`
if (io.exists(qoi_cache_path)) {
// Load the cached QOI file
var qoi_bytes = io.slurpbytes(qoi_cache_path)
var cached_img = qoi.decode(qoi_bytes)
if (cached_img) {
var result = new graphics.Image(cached_img)
cache[id] = result
return cache[id]
}
}
// Try to find the file in accio/32k with any extension
var accio_base = `accio/32k/${id}`
var found_path = null
// Check for common image extensions
var extensions = ['', '.qoi', '.png', '.jpg', '.jpeg', '.bmp', '.tiff', '.gif', '.ase', '.aseprite']
for (var ext of extensions) {
var test_path = accio_base + ext
if (io.exists(test_path)) {
found_path = test_path
break
}
}
if (found_path) {
// Load the 32k image
var bytes = io.slurpbytes(found_path)
var img_32k = decode_image(bytes, found_path.ext())
// Handle single surface images
if (img_32k && img_32k.width && img_32k.pixels) {
// Get the surface module for scaling
var surface = use('surface')
var surf_4k = surface.scale(img_32k, {
width: Math.floor(img_32k.width / 8),
height: Math.floor(img_32k.height / 8),
mode: 'linear'
})
log.console(img_32k.pixels.length)
log.console(surf_4k.pixels.length)
log.console(json.encode(surf_4k))
var qoi_data = surface.compress_qoi(surf_4k)
log.console(json.encode(qoi_data))
if (qoi_data && qoi_data.pixels) {
if (!io.exists(cache_dir)) {
io.mkdir(cache_dir)
}
io.slurpwrite(qoi_cache_path, qoi_data.pixels)
}
// Use the 4k version
var result = new graphics.Image(surf_4k)
cache[id] = result
return cache[id]
}
// For now, if it's not a simple surface, just return it as-is
// (animations, etc. would need more complex handling)
var result = create_image(found_path)
cache[id] = result
return cache[id]
}
// If still not found, return notex
return graphics.texture('notex')
}
var result = create_image(ipath)
cache[id] = result
@@ -380,7 +464,7 @@ graphics.get_font = function get_font(path) {
if (fontcache[fontstr]) return fontcache[fontstr]
var data = io.slurpbytes(fullpath)
var font = graphics.make_font(data, size)
var font = new staef.font(data, size)
fontcache[fontstr] = font