gutter_runner/game/assets/assets.odin
2025-01-03 00:42:38 +04:00

84 lines
1.7 KiB
Odin

package assets
import "core:c"
import "core:log"
import rl "vendor:raylib"
Loaded_Texture :: struct {
texture: rl.Texture2D,
modtime: c.long,
}
Loaded_Model :: struct {
model: rl.Model,
modtime: c.long,
}
Asset_Manager :: struct {
textures: map[cstring]Loaded_Texture,
models: map[cstring]Loaded_Model,
}
get_texture :: proc(assetman: ^Asset_Manager, path: cstring) -> rl.Texture2D {
modtime := rl.GetFileModTime(path)
existing, ok := assetman.textures[path]
if ok && existing.modtime == modtime {
return existing.texture
}
if ok {
rl.UnloadTexture(existing.texture)
delete_key(&assetman.textures, path)
log.infof("deleted texture %s. New textures len: %d", path, len(assetman.textures))
}
loaded := rl.LoadTexture(path)
if rl.IsTextureValid(loaded) {
assetman.textures[path] = {
texture = loaded,
modtime = modtime,
}
return loaded
} else {
return rl.Texture2D{}
}
}
get_model :: proc(assetman: ^Asset_Manager, path: cstring) -> rl.Model {
modtime := rl.GetFileModTime(path)
existing, ok := assetman.models[path]
if ok && existing.modtime == modtime {
return existing.model
}
if ok {
rl.UnloadModel(existing.model)
delete_key(&assetman.textures, path)
log.infof("deleted model %s. New models len: %d", path, len(assetman.textures))
}
loaded := rl.LoadModel(path)
if rl.IsModelValid(loaded) {
assetman.models[path] = {
model = loaded,
modtime = modtime,
}
return loaded
} else {
return rl.Model{}
}
}
shutdown :: proc(assetman: ^Asset_Manager) {
for _, texture in assetman.textures {
rl.UnloadTexture(texture.texture)
}
for _, model in assetman.models {
rl.UnloadModel(model.model)
}
delete(assetman.textures)
delete(assetman.models)
}