icon_pool.go 978 B

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