icon_pool.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package UI
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "strings"
  7. "github.com/veandco/go-sdl2/sdl"
  8. "github.com/cuu/gogame/image"
  9. )
  10. type IconPool struct {
  11. GameShellIconPath string
  12. Icons map[string]*sdl.Surface
  13. }
  14. func NewIconPool() *IconPool {
  15. i := &IconPool{}
  16. i.GameShellIconPath = SkinMap("sysgo/gameshell/icons/")
  17. i.Icons = make( map[string]*sdl.Surface )
  18. return i
  19. }
  20. func (self *IconPool) Init() {
  21. files,err := ioutil.ReadDir(self.GameShellIconPath)
  22. if err != nil {
  23. log.Fatal(err)
  24. return
  25. }
  26. for _,f := range files {
  27. if f.IsDir() {
  28. //pass
  29. }else {
  30. if strings.HasSuffix(f.Name(),".png") == true {
  31. keyname := strings.Split(f.Name(),".")
  32. if len(keyname) > 1 {
  33. self.Icons[ keyname[0] ] = image.Load( self.GameShellIconPath+ "/"+f.Name() )
  34. }
  35. }
  36. }
  37. }
  38. }
  39. func (self *IconPool) GetImgSurf(keyname string) *sdl.Surface {
  40. if _,ok := self.Icons[keyname]; ok {
  41. return self.Icons[keyname]
  42. } else {
  43. fmt.Println("IconPool GetImgSurf ", keyname, " failed")
  44. return nil
  45. }
  46. }
  47. var MyIconPool *IconPool
  48. // = NewIconPool()