Add global function to create textures / render targets

This commit is contained in:
Hajime Hoshi 2014-12-07 03:28:50 +09:00
parent 18141ecb6f
commit 53cb2fbce8
5 changed files with 35 additions and 18 deletions

View File

@ -3,6 +3,7 @@ package blocks
import ( import (
"github.com/hajimehoshi/ebiten/graphics" "github.com/hajimehoshi/ebiten/graphics"
_ "image/png" _ "image/png"
"sync"
) )
type Size struct { type Size struct {
@ -32,20 +33,11 @@ func NewGame() *Game {
game := &Game{ game := &Game{
sceneManager: NewSceneManager(NewTitleScene()), sceneManager: NewSceneManager(NewTitleScene()),
input: NewInput(), input: NewInput(),
textures: NewTextures(),
} }
return game return game
} }
func (game *Game) SetTextureFactory(textureFactory graphics.TextureFactory) {
game.textures = NewTextures(textureFactory)
for name, path := range texturePaths {
game.textures.RequestTexture(name, path)
}
for name, size := range renderTargetSizes {
game.textures.RequestRenderTarget(name, size)
}
}
func (game *Game) isInitialized() bool { func (game *Game) isInitialized() bool {
for name := range texturePaths { for name := range texturePaths {
if !game.textures.Has(name) { if !game.textures.Has(name) {
@ -60,7 +52,17 @@ func (game *Game) isInitialized() bool {
return true return true
} }
var once sync.Once
func (game *Game) Update() { func (game *Game) Update() {
once.Do(func() {
for name, path := range texturePaths {
game.textures.RequestTexture(name, path)
}
for name, size := range renderTargetSizes {
game.textures.RequestRenderTarget(name, size)
}
})
if !game.isInitialized() { if !game.isInitialized() {
return return
} }

View File

@ -18,7 +18,6 @@ type nameSize struct {
} }
type Textures struct { type Textures struct {
textureFactory graphics.TextureFactory
texturePaths chan namePath texturePaths chan namePath
renderTargetSizes chan nameSize renderTargetSizes chan nameSize
textures map[string]graphics.TextureId textures map[string]graphics.TextureId
@ -26,9 +25,8 @@ type Textures struct {
sync.RWMutex sync.RWMutex
} }
func NewTextures(textureFactory graphics.TextureFactory) *Textures { func NewTextures() *Textures {
textures := &Textures{ textures := &Textures{
textureFactory: textureFactory,
texturePaths: make(chan namePath), texturePaths: make(chan namePath),
renderTargetSizes: make(chan nameSize), renderTargetSizes: make(chan nameSize),
textures: map[string]graphics.TextureId{}, textures: map[string]graphics.TextureId{},
@ -66,7 +64,7 @@ func (t *Textures) loopMain() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
id, err := t.textureFactory.CreateTexture(img, graphics.FilterNearest) id, err := graphics.CreateTexture(img, graphics.FilterNearest)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -78,7 +76,7 @@ func (t *Textures) loopMain() {
name := s.name name := s.name
size := s.size size := s.size
go func() { go func() {
id, err := t.textureFactory.CreateRenderTarget(size.Width, size.Height, graphics.FilterNearest) id, err := graphics.CreateRenderTarget(size.Width, size.Height, graphics.FilterNearest)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -17,7 +17,27 @@ type TextureId int
// all alpha of a render target is maximum. // all alpha of a render target is maximum.
type RenderTargetId int type RenderTargetId int
var currentTextureFactory TextureFactory
type TextureFactory interface { type TextureFactory interface {
CreateRenderTarget(width, height int, filter Filter) (RenderTargetId, error) CreateRenderTarget(width, height int, filter Filter) (RenderTargetId, error)
CreateTexture(img image.Image, filter Filter) (TextureId, error) CreateTexture(img image.Image, filter Filter) (TextureId, error)
} }
func SetTextureFactory(textureFactory TextureFactory) {
currentTextureFactory = textureFactory
}
func CreateRenderTarget(width, height int, filter Filter) (RenderTargetId, error) {
if currentTextureFactory == nil {
panic("graphics.CreateRenderTarget: currentTextureFactory is not set.")
}
return currentTextureFactory.CreateRenderTarget(width, height, filter)
}
func CreateTexture(img image.Image, filter Filter) (TextureId, error) {
if currentTextureFactory == nil {
panic("graphics.CreateTexture: currentTextureFactory is not set")
}
return currentTextureFactory.CreateTexture(img, filter)
}

View File

@ -11,12 +11,10 @@ import (
type Game interface { type Game interface {
Draw(context graphics.Context) Draw(context graphics.Context)
Update() Update()
SetTextureFactory(textureFactory graphics.TextureFactory)
} }
func Run(u UI, game Game, width, height, scale int, title string, fps int) { func Run(u UI, game Game, width, height, scale int, title string, fps int) {
canvas := u.Start(width, height, scale, title) canvas := u.Start(width, height, scale, title)
game.SetTextureFactory(canvas)
frameTime := time.Duration(int64(time.Second) / int64(fps)) frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime) tick := time.Tick(frameTime)

View File

@ -11,7 +11,6 @@ type UI interface {
} }
type Canvas interface { type Canvas interface {
graphics.TextureFactory
Draw(func(graphics.Context)) Draw(func(graphics.Context))
IsClosed() bool IsClosed() bool
} }