ebiten/example/blocks/textures.go

137 lines
2.7 KiB
Go
Raw Normal View History

2014-12-09 15:16:04 +01:00
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2014-12-08 17:35:35 +01:00
package blocks
2014-05-03 08:25:41 +02:00
import (
2014-12-09 14:09:22 +01:00
"github.com/hajimehoshi/ebiten"
2014-05-03 08:25:41 +02:00
"image"
2014-12-07 10:25:28 +01:00
_ "image/png"
2014-05-03 08:25:41 +02:00
"os"
"sync"
)
type namePath struct {
name string
path string
}
type nameSize struct {
name string
2014-12-06 17:09:59 +01:00
size Size
2014-05-03 08:25:41 +02:00
}
type Textures struct {
texturePaths chan namePath
renderTargetSizes chan nameSize
2014-12-09 14:09:22 +01:00
textures map[string]ebiten.TextureID
renderTargets map[string]ebiten.RenderTargetID
2014-05-03 08:25:41 +02:00
sync.RWMutex
}
2014-12-14 09:28:19 +01:00
func NewTextures() *Textures {
2014-05-03 08:25:41 +02:00
textures := &Textures{
texturePaths: make(chan namePath),
renderTargetSizes: make(chan nameSize),
2014-12-09 14:09:22 +01:00
textures: map[string]ebiten.TextureID{},
renderTargets: map[string]ebiten.RenderTargetID{},
2014-05-03 08:25:41 +02:00
}
2014-05-03 17:40:53 +02:00
go func() {
for {
textures.loopMain()
}
}()
2014-05-03 08:25:41 +02:00
return textures
}
func loadImage(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return img, nil
}
2014-05-03 17:40:53 +02:00
func (t *Textures) loopMain() {
select {
case p := <-t.texturePaths:
name := p.name
path := p.path
go func() {
img, err := loadImage(path)
if err != nil {
panic(err)
}
2014-12-14 09:28:19 +01:00
id, err := ebiten.NewTextureID(img, ebiten.FilterNearest)
2014-05-03 17:40:53 +02:00
if err != nil {
panic(err)
}
t.Lock()
defer t.Unlock()
t.textures[name] = id
}()
case s := <-t.renderTargetSizes:
name := s.name
size := s.size
go func() {
2014-12-14 09:28:19 +01:00
id, err := ebiten.NewRenderTargetID(size.Width, size.Height, ebiten.FilterNearest)
2014-05-03 17:40:53 +02:00
if err != nil {
panic(err)
}
t.Lock()
defer t.Unlock()
t.renderTargets[name] = id
}()
2014-05-03 08:25:41 +02:00
}
}
func (t *Textures) RequestTexture(name string, path string) {
t.texturePaths <- namePath{name, path}
}
2014-12-06 17:09:59 +01:00
func (t *Textures) RequestRenderTarget(name string, size Size) {
2014-05-03 08:25:41 +02:00
t.renderTargetSizes <- nameSize{name, size}
}
func (t *Textures) Has(name string) bool {
t.RLock()
defer t.RUnlock()
_, ok := t.textures[name]
if ok {
return true
}
_, ok = t.renderTargets[name]
return ok
}
2014-12-09 14:09:22 +01:00
func (t *Textures) GetTexture(name string) ebiten.TextureID {
2014-05-03 08:25:41 +02:00
t.RLock()
defer t.RUnlock()
return t.textures[name]
}
2014-12-09 14:09:22 +01:00
func (t *Textures) GetRenderTarget(name string) ebiten.RenderTargetID {
2014-05-03 08:25:41 +02:00
t.RLock()
defer t.RUnlock()
return t.renderTargets[name]
}