diff options
Diffstat (limited to 'engine/Components/TextureManager.go')
| -rw-r--r-- | engine/Components/TextureManager.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/engine/Components/TextureManager.go b/engine/Components/TextureManager.go new file mode 100644 index 0000000..27449b1 --- /dev/null +++ b/engine/Components/TextureManager.go @@ -0,0 +1,54 @@ +package components + +import ( + "encoding/json" + "fmt" + + rl "github.com/gen2brain/raylib-go/raylib" +) + + +type TextrueManager struct { + Textures []*rl.Texture2D + NameToId map[string]uint64 +} + +// Loads the texture in the VRAM +func (base *TextrueManager) LoadTexture(path string, displayName string) { + var image = rl.LoadImage(path) + var texture = rl.LoadTextureFromImage(image) + var id = len(base.Textures) + base.Textures = append(base.Textures, &texture) + base.NameToId[displayName] = uint64(id) + rl.UnloadImage(image) +} + +// Unloads all the textures from the VRAM +func (base *TextrueManager) UnloadAll() { + for _, v := range base.Textures { + rl.UnloadTexture(*v) + } +} + +func (base *TextrueManager) GetTexture(Id uint64) (*rl.Texture2D, error) { + if Id >= uint64(len(base.Textures)) { + return nil, fmt.Errorf("No texture loaded with that ID") + } + return base.Textures[Id], nil +} + +func (base *TextrueManager) GetTextureByName(textureName string) (*rl.Texture2D, error) { + var id, exists = base.NameToId[textureName] + if !exists { + return nil, fmt.Errorf("No such texture in the system") + } + return base.GetTexture(id) +} + +func (base *TextrueManager) String() string { + var outp, jsonErr = json.Marshal(base) + if jsonErr != nil { + return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error()) + } + return string(outp) +} |
