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 (
"github.com/hajimehoshi/ebiten/graphics"
_ "image/png"
"sync"
)
type Size struct {
@ -32,20 +33,11 @@ func NewGame() *Game {
game := &Game{
sceneManager: NewSceneManager(NewTitleScene()),
input: NewInput(),
textures: NewTextures(),
}
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 {
for name := range texturePaths {
if !game.textures.Has(name) {
@ -60,7 +52,17 @@ func (game *Game) isInitialized() bool {
return true
}
var once sync.Once
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() {
return
}

View File

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

View File

@ -17,7 +17,27 @@ type TextureId int
// all alpha of a render target is maximum.
type RenderTargetId int
var currentTextureFactory TextureFactory
type TextureFactory interface {
CreateRenderTarget(width, height int, filter Filter) (RenderTargetId, 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 {
Draw(context graphics.Context)
Update()
SetTextureFactory(textureFactory graphics.TextureFactory)
}
func Run(u UI, game Game, width, height, scale int, title string, fps int) {
canvas := u.Start(width, height, scale, title)
game.SetTextureFactory(canvas)
frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime)

View File

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