Skip to content

Commit

Permalink
Load error sprite if file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Feb 15, 2025
1 parent 3a0b820 commit 73ab93f
Showing 1 changed file with 61 additions and 17 deletions.
78 changes: 61 additions & 17 deletions src/engine/Component/Sprite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,10 @@ module SpriteModule
return this
end

fullPath = joinpath(BasePath, "assets", "images", imagePath)

this.image = load_image_sdl(this, fullPath, imagePath)
Component.load_image(this::InternalSprite, imagePath::String)
if this.image == C_NULL
error = unsafe_string(SDL2.SDL_GetError())

println(fullPath)
println(string("Couldn't open image! SDL Error: ", error))
@error(string("Couldn't open image! path: $(fullPath) SDL Error: ", error))
Base.show_backtrace(stdout, catch_backtrace())
return
end
Expand Down Expand Up @@ -183,22 +179,71 @@ module SpriteModule
this.isFlipped = !this.isFlipped
end

const FALLBACK_IMAGE_BYTES = UInt8[
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x00, 0x20, 0x08, 0x06, 0x00, 0x00, 0x00, 0x73, 0x7a, 0x7a, 0xf4, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, 0x01, 0x02, 0x49, 0x44, 0x41, 0x54, 0x58, 0x85, 0xdd, 0x96, 0x4b, 0x0e,
0x83, 0x30, 0x0c, 0x44, 0xed, 0xaa, 0x57, 0x61, 0xc9, 0x02, 0x72, 0x14, 0xae, 0x59, 0x8e, 0x12, 0x75, 0xd1, 0x25, 0x87,
0x71, 0x37, 0x0d, 0xa2, 0x40, 0xc3, 0xd8, 0x71, 0x68, 0xd5, 0x59, 0x81, 0x64, 0x65, 0x5e, 0x7e, 0x9e, 0x30, 0x01, 0x12,
0x11, 0x41, 0xea, 0x98, 0x99, 0x91, 0xba, 0xa5, 0xae, 0x88, 0xf9, 0x18, 0x02, 0x34, 0x58, 0x02, 0xd5, 0x80, 0x1c, 0x16,
0xde, 0xfa, 0x7e, 0x33, 0xfb, 0xb6, 0xe9, 0x36, 0x75, 0x8f, 0xe9, 0x3e, 0x7f, 0x0f, 0x31, 0xc2, 0x10, 0xd9, 0x22, 0x64,
0xf6, 0x6b, 0x98, 0x04, 0x82, 0x42, 0x7c, 0x2c, 0xd0, 0x2c, 0xfd, 0x1a, 0x44, 0x03, 0x71, 0x78, 0x06, 0x50, 0x2d, 0xb7,
0xa0, 0x6d, 0xba, 0xb7, 0xff, 0x9c, 0x2e, 0x5e, 0x00, 0x56, 0xfd, 0x27, 0x00, 0xba, 0xfc, 0x59, 0x00, 0x66, 0xe6, 0x21,
0x46, 0x17, 0x20, 0x13, 0x40, 0xa9, 0xd0, 0x6b, 0xf8, 0xdb, 0x67, 0xe0, 0x8c, 0x6d, 0xa8, 0xb2, 0x02, 0x9a, 0x56, 0xec,
0x0e, 0xa0, 0x31, 0x27, 0x02, 0xc2, 0x88, 0x08, 0x6f, 0xcb, 0x5a, 0x73, 0x18, 0x00, 0x81, 0xb0, 0x98, 0xab, 0x00, 0x72,
0x10, 0x56, 0x73, 0x35, 0x40, 0x82, 0x20, 0x22, 0x4a, 0x20, 0x25, 0xe6, 0x45, 0x92, 0x97, 0x4e, 0x37, 0xf6, 0x96, 0x79,
0x0b, 0x76, 0x07, 0xab, 0xf1, 0x28, 0x5d, 0x9b, 0xe7, 0x6e, 0x82, 0x88, 0x88, 0x16, 0x02, 0x06, 0xc8, 0x99, 0xa7, 0xe7,
0xd8, 0x18, 0x82, 0x1a, 0xc2, 0xa5, 0x13, 0xa6, 0xfc, 0x6f, 0x9b, 0x6e, 0x86, 0x38, 0x15, 0x60, 0x09, 0xa1, 0x95, 0x6b,
0x16, 0x58, 0x20, 0x60, 0x80, 0x5a, 0xd1, 0x6c, 0xba, 0x86, 0x9e, 0x99, 0x60, 0x6a, 0xa1, 0x9e, 0x99, 0x60, 0xee, 0xe1,
0x7b, 0x27, 0xfd, 0x2b, 0x99, 0x50, 0xaa, 0x27, 0x9d, 0x07, 0x96, 0x9b, 0xca, 0xab, 0x4b, 0x6c, 0x00, 0x00, 0x00, 0x00,
0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
] # This is a 1x1 transparent PNG image.

function load_fallback_image()
rwops = SDL2.SDL_RWFromMem(pointer(FALLBACK_IMAGE_BYTES), length(FALLBACK_IMAGE_BYTES))
if rwops == C_NULL
@error("Failed to create SDL_RWops for fallback image.")
return C_NULL
end
image = SDL2.IMG_Load_RW(rwops, 1) # Load directly from memory and free rwops after use
return image
end

function Component.load_image(this::InternalSprite, imagePath::String)
SDL2.SDL_ClearError()
this.image = load_image_sdl(this, joinpath(BasePath, "assets", "images", imagePath), imagePath)

fullPath = joinpath(BasePath, "assets", "images", imagePath)
this.image = load_image_sdl(this, fullPath, imagePath)
error = unsafe_string(SDL2.SDL_GetError())
if !isempty(error)
println(string("Couldn't open image! SDL Error: ", error))

if !isempty(error) || this.image == C_NULL
@error("Couldn't open image '$imagePath'! SDL Error: ", error)
SDL2.SDL_ClearError()
this.image = C_NULL
return

# Load from byte array
this.image = load_fallback_image()
this.imagePath = "fallback.png"
this.pixelsPerUnit = 0
if this.image == C_NULL
@error("Fallback image also failed to load! $(unsafe_string(SDL2.SDL_GetError()))")
return
end
else
this.imagePath = imagePath
end


# Get image size
surface = unsafe_wrap(Array, this.image, 10; own = false)
this.size = Math.Vector2(surface[1].w, surface[1].h)
this.imagePath = imagePath

# Create texture
this.texture = SDL2.SDL_CreateTextureFromSurface(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, this.image)

if this.texture == C_NULL
@error("Failed to create texture from image.")
return
end

Component.set_color(this)
end

Expand All @@ -209,10 +254,10 @@ module SpriteModule
if rw != C_NULL
@debug("loading image from cache")
@debug("comma separated path: ", get_comma_separated_path(imagePath))
return SDL2.IMG_Load_RW(rw, 1)
SDL2.IMG_Load_RW(rw, 1)
end
end
@debug "Loading image from disk for sprite, there are $(length(JulGame.IMAGES_CACHE)) images in cache"
@debug "Loading image from disk $(fullPath) for sprite, there are $(length(JulGame.IMAGE_CACHE)) images in cache"

return SDL2.IMG_Load(fullPath)
end
Expand All @@ -235,7 +280,6 @@ module SpriteModule
end

SDL2.SDL_DestroyTexture(this.texture)
SDL2.SDL_FreeSurface(this.image)
this.image = C_NULL
this.texture = C_NULL
end
Expand Down

0 comments on commit 73ab93f

Please sign in to comment.