mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Add global function to create textures / render targets
This commit is contained in:
parent
18141ecb6f
commit
53cb2fbce8
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user