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) }