fix syntax
This commit is contained in:
186
graphics.cm
186
graphics.cm
@@ -42,7 +42,7 @@ function calc_image_size(img) {
|
||||
|
||||
function decorate_rect_px(img) {
|
||||
// default UV rect is the whole image if none supplied
|
||||
img.rect ??= {x:0, y:0, width:1, height:1} // [u0,v0,uw,vh] in 0-1
|
||||
if (!img.rect) img.rect = {x:0, y:0, width:1, height:1} // [u0,v0,uw,vh] in 0-1
|
||||
|
||||
var width = 0, height = 0;
|
||||
if (img.texture) {
|
||||
@@ -87,27 +87,32 @@ function wrapFrames(arr){ /* [{surface,time,rect}, …] → [{image,t
|
||||
}
|
||||
});
|
||||
}
|
||||
function makeAnim(frames, loop=true){
|
||||
return { frames, loop }
|
||||
function makeAnim(frames, loop){
|
||||
var local_loop = loop != null ? loop : true
|
||||
return { frames: frames, loop: local_loop }
|
||||
}
|
||||
|
||||
function decode_image(bytes, ext)
|
||||
{
|
||||
switch(ext) {
|
||||
case 'gif': return decode_gif(gif.decode(bytes))
|
||||
case 'ase':
|
||||
case 'aseprite': return decode_aseprite(aseprite.decode(bytes))
|
||||
case 'qoi': return qoi.decode(bytes) // returns single surface
|
||||
case 'png': return png.decode(bytes) // returns single surface
|
||||
case 'jpg':
|
||||
case 'jpeg': return png.decode(bytes) // png.decode handles jpg too via stb_image
|
||||
case 'bmp': return png.decode(bytes) // png.decode handles bmp too via stb_image
|
||||
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 png decoder for other formats (uses stb_image)
|
||||
return png.decode(bytes)
|
||||
var qoi_result = null
|
||||
if (ext == 'gif') {
|
||||
return decode_gif(gif.decode(bytes))
|
||||
} else if (ext == 'ase' || ext == 'aseprite') {
|
||||
return decode_aseprite(aseprite.decode(bytes))
|
||||
} else if (ext == 'qoi') {
|
||||
return qoi.decode(bytes) // returns single surface
|
||||
} else if (ext == 'png') {
|
||||
return png.decode(bytes) // returns single surface
|
||||
} else if (ext == 'jpg' || ext == 'jpeg') {
|
||||
return png.decode(bytes) // png.decode handles jpg too via stb_image
|
||||
} else if (ext == 'bmp') {
|
||||
return png.decode(bytes) // png.decode handles bmp too via stb_image
|
||||
} else {
|
||||
// Try QOI first since it's fast to check
|
||||
qoi_result = qoi.decode(bytes)
|
||||
if (qoi_result) return qoi_result
|
||||
// Fall back to png decoder for other formats (uses stb_image)
|
||||
return png.decode(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,52 +156,49 @@ function decode_aseprite(decoded) {
|
||||
}
|
||||
|
||||
function create_image(path){
|
||||
try{
|
||||
def bytes = io.slurp(path);
|
||||
def bytes = io.slurp(path);
|
||||
|
||||
var ext = pop(array(path, '.'))
|
||||
var raw = decode_image(bytes, ext);
|
||||
|
||||
/* ── Case A: single surface (from make_texture) ────────────── */
|
||||
if(raw && raw.width && raw.pixels && !is_array(raw)) {
|
||||
return graphics.Image(raw)
|
||||
}
|
||||
var ext = pop(array(path, '.'))
|
||||
var raw = decode_image(bytes, ext);
|
||||
var anims = null
|
||||
var keys = null
|
||||
|
||||
/* ── Case B: array of surfaces (from make_gif) ────────────── */
|
||||
if(is_array(raw)) {
|
||||
// Single frame GIF returns array with one surface
|
||||
if(length(raw) == 1 && !raw[0].time) {
|
||||
return graphics.Image(raw[0])
|
||||
}
|
||||
// Multiple frames - create animation
|
||||
return makeAnim(wrapFrames(raw), true);
|
||||
}
|
||||
|
||||
if(is_object(raw) && !raw.width) {
|
||||
if(raw.surface)
|
||||
return graphics.Image(raw.surface)
|
||||
|
||||
if(raw.frames && is_array(raw.frames) && raw.loop != null)
|
||||
return makeAnim(wrapFrames(raw.frames), !!raw.loop);
|
||||
|
||||
def anims = {};
|
||||
var keys = array(raw)
|
||||
arrfor(keys, function(name) {
|
||||
var anim = raw[name]
|
||||
if(anim && is_array(anim.frames))
|
||||
anims[name] = makeAnim(wrapFrames(anim.frames), !!anim.loop);
|
||||
else if(anim && anim.surface)
|
||||
anims[name] = graphics.Image(anim.surface);
|
||||
})
|
||||
if(length(array(anims))) return anims;
|
||||
}
|
||||
|
||||
throw Error('Unsupported image structure from decoder');
|
||||
|
||||
}catch(e){
|
||||
log.error(`Error loading image ${path}: ${e.message}`);
|
||||
throw e;
|
||||
/* ── Case A: single surface (from make_texture) ────────────── */
|
||||
if(raw && raw.width && raw.pixels && !is_array(raw)) {
|
||||
return graphics.Image(raw)
|
||||
}
|
||||
|
||||
/* ── Case B: array of surfaces (from make_gif) ────────────── */
|
||||
if(is_array(raw)) {
|
||||
// Single frame GIF returns array with one surface
|
||||
if(length(raw) == 1 && !raw[0].time) {
|
||||
return graphics.Image(raw[0])
|
||||
}
|
||||
// Multiple frames - create animation
|
||||
return makeAnim(wrapFrames(raw), true);
|
||||
}
|
||||
|
||||
if(is_object(raw) && !raw.width) {
|
||||
if(raw.surface)
|
||||
return graphics.Image(raw.surface)
|
||||
|
||||
if(raw.frames && is_array(raw.frames) && raw.loop != null)
|
||||
return makeAnim(wrapFrames(raw.frames), !!raw.loop);
|
||||
|
||||
anims = {};
|
||||
keys = array(raw)
|
||||
arrfor(keys, function(name) {
|
||||
var anim = raw[name]
|
||||
if(anim && is_array(anim.frames))
|
||||
anims[name] = makeAnim(wrapFrames(anim.frames), !!anim.loop);
|
||||
else if(anim && anim.surface)
|
||||
anims[name] = graphics.Image(anim.surface);
|
||||
})
|
||||
if(length(array(anims))) return anims;
|
||||
}
|
||||
|
||||
log.error(`Error loading image ${path}: Unsupported image structure from decoder`);
|
||||
return null
|
||||
}
|
||||
|
||||
var image = {}
|
||||
@@ -212,7 +214,7 @@ image.dimensions = function() {
|
||||
return [width, height].scale([this.rect[2], this.rect[3]])
|
||||
}
|
||||
|
||||
var spritesheet
|
||||
var spritesheet = null
|
||||
var sheet_frames = []
|
||||
var sheetsize = 1024
|
||||
|
||||
@@ -249,8 +251,10 @@ graphics.from_surface = function(surf)
|
||||
|
||||
graphics.from = function(id, data)
|
||||
{
|
||||
if (!is_text(id))
|
||||
throw Error('Expected a string ID')
|
||||
if (!is_text(id)) {
|
||||
log.error('Expected a string ID')
|
||||
return null
|
||||
}
|
||||
|
||||
if (is_blob(data))
|
||||
return graphics.texture_from_data(data)
|
||||
@@ -258,14 +262,20 @@ graphics.from = function(id, data)
|
||||
|
||||
graphics.texture = function texture(path) {
|
||||
if (is_proto(path, graphics.Image)) return path
|
||||
|
||||
if (!is_text(path))
|
||||
throw Error('need a string for graphics.texture')
|
||||
|
||||
if (!is_text(path)) {
|
||||
log.error('need a string for graphics.texture')
|
||||
return null
|
||||
}
|
||||
|
||||
var parts = array(path, ':')
|
||||
var id = parts[0]
|
||||
var animName = parts[1]
|
||||
var frameIndex = parts[2]
|
||||
var ipath = null
|
||||
var result = null
|
||||
var idx = null
|
||||
var anim = null
|
||||
|
||||
// Handle the case where animName is actually a frame index (e.g., "gears:0")
|
||||
if (animName != null && frameIndex == null && !is_null(number(animName))) {
|
||||
@@ -274,14 +284,14 @@ graphics.texture = function texture(path) {
|
||||
}
|
||||
|
||||
if (!cache[id]) {
|
||||
var ipath = res.find_image(id)
|
||||
|
||||
ipath = res.find_image(id)
|
||||
|
||||
if (!ipath) {
|
||||
// If still not found, return notex
|
||||
return graphics.texture('notex')
|
||||
}
|
||||
|
||||
var result = create_image(ipath)
|
||||
|
||||
result = create_image(ipath)
|
||||
cache[id] = result
|
||||
}
|
||||
|
||||
@@ -294,7 +304,7 @@ graphics.texture = function texture(path) {
|
||||
if (!animName && frameIndex != null) {
|
||||
// If cached is a single animation (has .frames property)
|
||||
if (cached.frames && is_array(cached.frames)) {
|
||||
var idx = number(frameIndex)
|
||||
idx = number(frameIndex)
|
||||
if (idx == null) return cached
|
||||
// Wrap the index
|
||||
idx = idx % length(cached.frames)
|
||||
@@ -319,8 +329,8 @@ graphics.texture = function texture(path) {
|
||||
// If cached is a single animation (has .frames property)
|
||||
if (cached.frames && is_array(cached.frames)) {
|
||||
if (frameIndex != null) {
|
||||
var idx = number(frameIndex)
|
||||
if (isNaN(idx)) return cached
|
||||
idx = number(frameIndex)
|
||||
if (idx == null) return cached
|
||||
// Wrap the index
|
||||
idx = idx % length(cached.frames)
|
||||
return cached.frames[idx].image
|
||||
@@ -331,13 +341,14 @@ graphics.texture = function texture(path) {
|
||||
|
||||
// If cached is an object of multiple animations
|
||||
if (is_object(cached) && !cached.frames) {
|
||||
var anim = cached[animName]
|
||||
anim = cached[animName]
|
||||
if (!anim)
|
||||
throw Error(`animation ${animName} not found in ${id}`)
|
||||
log.error(`animation ${animName} not found in ${id}`)
|
||||
return null
|
||||
|
||||
if (frameIndex != null) {
|
||||
var idx = number(frameIndex)
|
||||
if (isNaN(idx)) return anim
|
||||
idx = number(frameIndex)
|
||||
if (idx == null) return anim
|
||||
|
||||
if (is_proto(anim, graphics.Image)) {
|
||||
// Single image animation - any frame index returns the image
|
||||
@@ -412,19 +423,25 @@ var datas = []
|
||||
|
||||
graphics.get_font = function get_font(path) {
|
||||
if (is_object(path)) return path
|
||||
if (!is_text(path))
|
||||
throw Error(`Can't find font with path: ${path}`)
|
||||
if (!is_text(path)) {
|
||||
log.error(`Can't find font with path: ${path}`)
|
||||
return null
|
||||
}
|
||||
|
||||
var parts = array(path, '.')
|
||||
var size = 16 // default size
|
||||
var font_path = path
|
||||
parts[1] = number(parts[1])
|
||||
if (parts[1]) {
|
||||
path = parts[0]
|
||||
font_path = parts[0]
|
||||
size = parts[1]
|
||||
}
|
||||
|
||||
var fullpath = res.find_font(path)
|
||||
if (!fullpath) throw Error(`Cannot load font ${path}`)
|
||||
var fullpath = res.find_font(font_path)
|
||||
if (!fullpath) {
|
||||
log.error(`Cannot load font ${path}`)
|
||||
return null
|
||||
}
|
||||
|
||||
var fontstr = `${fullpath}.${size}`
|
||||
if (fontcache[fontstr]) return fontcache[fontstr]
|
||||
@@ -441,7 +458,8 @@ graphics.queue_sprite_mesh = function(queue) {
|
||||
var sprites = filter(queue, x => x.type == 'sprite')
|
||||
if (length(sprites) == 0) return []
|
||||
var mesh = graphics.make_sprite_mesh(sprites)
|
||||
for (var i = 0; i < length(sprites); i++) {
|
||||
var i = 0
|
||||
for (i = 0; i < length(sprites); i++) {
|
||||
sprites[i].mesh = mesh
|
||||
sprites[i].first_index = i*6
|
||||
sprites[i].num_indices = 6
|
||||
|
||||
Reference in New Issue
Block a user