Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Custom chapter image load error #330

Merged
merged 2 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 43 additions & 40 deletions scripts/scr_image/scr_image.gml
Original file line number Diff line number Diff line change
Expand Up @@ -550,44 +550,47 @@ function scr_image(path, image_id, x1, y1, width, height) {

/// @description Use this to load the image at given path and id into the image cache so it can be
/// referenced in a different function to scr_image. Obtain the image later with `obj_img.image_cache[$path][image_id]`
function scr_image_cache(path, image_id){
var drawing_sprite;
var cache_arr_exists = struct_exists(obj_img.image_cache, path);
if(!cache_arr_exists){
var empty_arr = array_create(100, -1);
variable_struct_set(obj_img.image_cache, path, empty_arr);
}
// Start with 100 slots but allow it to expand if needed
if(cache_arr_exists && image_id > 100){
for(var i = 100; i <= image_id; i++){
array_push(obj_img.image_cache, -1);
}
}
function scr_image_cache(path, image_id) {
try {
var drawing_sprite;
var cache_arr_exists = struct_exists(obj_img.image_cache, path);
if (!cache_arr_exists) {
var empty_arr = array_create(100, -1);
variable_struct_set(obj_img.image_cache, path, empty_arr);
}
// Start with 100 slots but allow it to expand if needed
if (cache_arr_exists && image_id > 100) {
for (var i = 100; i <= image_id; i++) {
EttyKitty marked this conversation as resolved.
Show resolved Hide resolved
EttyKitty marked this conversation as resolved.
Show resolved Hide resolved
array_push(obj_img.image_cache, -1);
}
}

var existing_sprite = -1;
try {
existing_sprite = array_get(obj_img.image_cache[$path], image_id);
} catch (_ex){
debugl($"error trying to fetch image {path}/{image_id}.png from cache: {_ex}");
existing_sprite = -1;
}

if(sprite_exists(existing_sprite)){
drawing_sprite = existing_sprite;
} else if (image_id>-1){
var folders = string_replace_all(path, "/", "\\");
var dir = $"{working_directory}\\images\\{folders}\\{string(image_id)}.png";
if(file_exists(dir)){
drawing_sprite = sprite_add(dir,1, false,false,0,0);
if (image_id >= array_length(obj_img.image_cache[$path])) {
array_resize(obj_img.image_cache[$path], image_id + 1);
}
array_set(obj_img.image_cache[$path], image_id, drawing_sprite);

} else {
drawing_sprite = -1;
// debugl($"No directory/file found matching {dir}"); // too much noise
}
}
return drawing_sprite;
}
var existing_sprite = -1;
try {
existing_sprite = array_get(obj_img.image_cache[$ path], image_id);
} catch (_ex) {
debugl($"error trying to fetch image {path}/{image_id}.png from cache: {_ex}");
existing_sprite = -1;
}

if (sprite_exists(existing_sprite)) {
drawing_sprite = existing_sprite;
} else if (image_id > -1) {
var folders = string_replace_all(path, "/", "\\");
var dir = $"{working_directory}\\images\\{folders}\\{string(image_id)}.png";
if (file_exists(dir)) {
drawing_sprite = sprite_add(dir, 1, false, false, 0, 0);
if (image_id >= array_length(obj_img.image_cache[$ path])) {
array_resize(obj_img.image_cache[$ path], image_id + 1);
}
array_set(obj_img.image_cache[$ path], image_id, drawing_sprite);
} else {
drawing_sprite = -1;
// debugl($"No directory/file found matching {dir}"); // too much noise
}
}
return drawing_sprite;
} catch (_exception) {
handle_exception(_exception);
}
}
2 changes: 1 addition & 1 deletion scripts/scr_load/scr_load.gml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function scr_load(save_part, save_id) {
global.chapter_icon_filename = ini_read_real("Ini", "global_chapter_icon_filename", 0);


if(global.chapter_icon_path != "Error"){
if(global.chapter_icon_path != "Error" && global.chapter_icon_path != "") {
global.chapter_icon_sprite = scr_image_cache(global.chapter_icon_path, global.chapter_icon_filename);
} else {
global.chapter_icon_sprite = spr_icon_chapters;
Expand Down
Loading