diff --git a/example/blocks/field.go b/example/blocks/field.go index 3df2482b3..7e43531eb 100644 --- a/example/blocks/field.go +++ b/example/blocks/field.go @@ -93,7 +93,7 @@ func (f *Field) Flush() { } } -func (f *Field) Draw(context graphics.Context, textures *Textures, geo matrix.Geometry) { +func (f *Field) Draw(context graphics.Context, textures Textures, geo matrix.Geometry) { blocks := make([][]BlockType, len(f.blocks)) for i, blockCol := range f.blocks { blocks[i] = make([]BlockType, len(blockCol)) diff --git a/example/blocks/font.go b/example/blocks/font.go index 506b00e0b..5acb1dc98 100644 --- a/example/blocks/font.go +++ b/example/blocks/font.go @@ -19,7 +19,7 @@ func textWidth(str string) int { func drawText( context graphics.Context, - textures *Textures, + textures Textures, str string, x, y, scale int, clr color.Color) { @@ -55,7 +55,7 @@ func drawText( func drawTextWithShadow( context graphics.Context, - textures *Textures, + textures Textures, str string, x, y, scale int, clr color.Color) { diff --git a/example/blocks/game.go b/example/blocks/game.go index bdded8273..06b1b6b24 100644 --- a/example/blocks/game.go +++ b/example/blocks/game.go @@ -23,17 +23,25 @@ type GameState struct { Input *Input } +type Textures interface { + RequestTexture(name string, path string) + RequestRenderTarget(name string, size Size) + Has(name string) bool + GetTexture(name string) graphics.TextureId + GetRenderTarget(name string) graphics.RenderTargetId +} + type Game struct { sceneManager *SceneManager input *Input - textures *Textures + textures Textures } -func NewGame(textureFactory graphics.TextureFactory) *Game { +func NewGame(textures Textures) *Game { game := &Game{ sceneManager: NewSceneManager(NewTitleScene()), input: NewInput(), - textures: NewTextures(textureFactory), + textures: textures, } for name, path := range texturePaths { game.textures.RequestTexture(name, path) diff --git a/example/blocks/game_scene.go b/example/blocks/game_scene.go index 039c35e93..c919b2074 100644 --- a/example/blocks/game_scene.go +++ b/example/blocks/game_scene.go @@ -97,7 +97,7 @@ func (s *GameScene) Update(state *GameState) { } } -func (s *GameScene) Draw(context graphics.Context, textures *Textures) { +func (s *GameScene) Draw(context graphics.Context, textures Textures) { context.Fill(0xff, 0xff, 0xff) field := textures.GetTexture("empty") diff --git a/example/blocks/piece.go b/example/blocks/piece.go index 31d8c68b0..613a741fd 100644 --- a/example/blocks/piece.go +++ b/example/blocks/piece.go @@ -125,7 +125,7 @@ const fieldBlockNumY = 20 func drawBlocks( context graphics.Context, - textures *Textures, + textures Textures, blocks [][]BlockType, geo matrix.Geometry) { parts := []graphics.TexturePart{} @@ -211,7 +211,7 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) { func (p *Piece) Draw( context graphics.Context, - textures *Textures, + textures Textures, fieldX, fieldY int, pieceX, pieceY int, angle Angle) { diff --git a/example/blocks/scene_manager.go b/example/blocks/scene_manager.go index b944cc104..ad288ee90 100644 --- a/example/blocks/scene_manager.go +++ b/example/blocks/scene_manager.go @@ -14,7 +14,7 @@ func init() { type Scene interface { Update(state *GameState) - Draw(context graphics.Context, textures *Textures) + Draw(context graphics.Context, textures Textures) } const transitionMaxCount = 20 @@ -45,7 +45,7 @@ func (s *SceneManager) Update(state *GameState) { } } -func (s *SceneManager) Draw(context graphics.Context, textures *Textures) { +func (s *SceneManager) Draw(context graphics.Context, textures Textures) { if s.transitionCount == -1 { s.current.Draw(context, textures) return diff --git a/example/blocks/title_scene.go b/example/blocks/title_scene.go index 3f1adcfba..87d2a5921 100644 --- a/example/blocks/title_scene.go +++ b/example/blocks/title_scene.go @@ -26,7 +26,7 @@ func (s *TitleScene) Update(state *GameState) { } } -func (s *TitleScene) Draw(context graphics.Context, textures *Textures) { +func (s *TitleScene) Draw(context graphics.Context, textures Textures) { drawTitleBackground(context, textures, s.count) drawLogo(context, textures, "BLOCKS") @@ -36,7 +36,7 @@ func (s *TitleScene) Draw(context graphics.Context, textures *Textures) { drawTextWithShadow(context, textures, message, x, y, 1, color.RGBA{0x80, 0, 0, 0xff}) } -func drawTitleBackground(context graphics.Context, textures *Textures, c int) { +func drawTitleBackground(context graphics.Context, textures Textures, c int) { const textureWidth = 32 const textureHeight = 32 @@ -60,7 +60,7 @@ func drawTitleBackground(context graphics.Context, textures *Textures, c int) { context.Texture(backgroundTextureId).Draw(parts, geo, clr) } -func drawLogo(context graphics.Context, textures *Textures, str string) { +func drawLogo(context graphics.Context, textures Textures, str string) { scale := 4 textWidth := textWidth(str) * scale x := (ScreenWidth - textWidth) / 2 diff --git a/example/main.go b/example/main.go index 2b867e790..8aa703d1c 100644 --- a/example/main.go +++ b/example/main.go @@ -30,7 +30,6 @@ func main() { const title = "Ebiten Demo" u := cocoa.UI() - textureFactory := cocoa.TextureFactory() window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title) drawing := make(chan struct{}) @@ -39,7 +38,8 @@ func main() { defer close(quit) windowEvents := window.Events() - var game Game = blocks.NewGame(textureFactory) + textureFactory := cocoa.TextureFactory() + var game Game = blocks.NewGame(NewTextures(textureFactory)) frameTime := time.Duration(int64(time.Second) / int64(fps)) tick := time.Tick(frameTime) for { diff --git a/example/blocks/textures.go b/example/textures.go similarity index 93% rename from example/blocks/textures.go rename to example/textures.go index 38e4ccf4d..cf7f0f6f0 100644 --- a/example/blocks/textures.go +++ b/example/textures.go @@ -1,6 +1,7 @@ -package blocks +package main import ( + "github.com/hajimehoshi/go-ebiten/example/blocks" "github.com/hajimehoshi/go-ebiten/graphics" "image" "os" @@ -14,7 +15,7 @@ type namePath struct { type nameSize struct { name string - size Size + size blocks.Size } type Textures struct { @@ -98,7 +99,7 @@ func (t *Textures) RequestTexture(name string, path string) { t.texturePaths <- namePath{name, path} } -func (t *Textures) RequestRenderTarget(name string, size Size) { +func (t *Textures) RequestRenderTarget(name string, size blocks.Size) { t.renderTargetSizes <- nameSize{name, size} } diff --git a/graphics/opengl/ids.go b/graphics/opengl/ids.go index 1c1ba2a02..d7e988aa8 100644 --- a/graphics/opengl/ids.go +++ b/graphics/opengl/ids.go @@ -35,7 +35,7 @@ type ids struct { textures map[graphics.TextureId]*Texture renderTargets map[graphics.RenderTargetId]*RenderTarget renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId - counts chan int + lastId int currentRenderTargetId graphics.RenderTargetId sync.RWMutex } @@ -45,14 +45,9 @@ func newIds() *ids { textures: map[graphics.TextureId]*Texture{}, renderTargets: map[graphics.RenderTargetId]*RenderTarget{}, renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{}, - counts: make(chan int), + lastId: 0, currentRenderTargetId: -1, } - go func() { - for i := 1; ; i++ { - ids.counts <- i - } - }() return ids } @@ -80,10 +75,11 @@ func (i *ids) createTexture(img image.Image, filter graphics.Filter) ( if err != nil { return 0, err } - textureId := graphics.TextureId(<-i.counts) i.Lock() defer i.Unlock() + i.lastId++ + textureId := graphics.TextureId(i.lastId) i.textures[textureId] = texture return textureId, nil } @@ -100,11 +96,13 @@ func (i *ids) createRenderTarget(width, height int, filter graphics.Filter) ( i.currentRenderTargetId = -1 renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false} - textureId := graphics.TextureId(<-i.counts) - renderTargetId := graphics.RenderTargetId(<-i.counts) - i.Lock() defer i.Unlock() + i.lastId++ + textureId := graphics.TextureId(i.lastId) + i.lastId++ + renderTargetId := graphics.RenderTargetId(i.lastId) + i.textures[textureId] = texture i.renderTargets[renderTargetId] = renderTarget i.renderTargetToTexture[renderTargetId] = textureId @@ -114,10 +112,10 @@ func (i *ids) createRenderTarget(width, height int, filter graphics.Filter) ( // NOTE: renderTarget can't be used as a texture. func (i *ids) addRenderTarget(renderTarget *RenderTarget) graphics.RenderTargetId { - id := graphics.RenderTargetId(<-i.counts) - i.Lock() defer i.Unlock() + i.lastId++ + id := graphics.RenderTargetId(i.lastId) i.renderTargets[id] = renderTarget return id diff --git a/graphics/opengl/shader/draw_texture.go b/graphics/opengl/shader/draw_texture.go index 536dfd8f9..77608c578 100644 --- a/graphics/opengl/shader/draw_texture.go +++ b/graphics/opengl/shader/draw_texture.go @@ -27,7 +27,6 @@ func DrawTexture(native NativeTexture, projectionMatrix [16]float32, } // TODO: Check performance shaderProgram := use(projectionMatrix, geometryMatrix, colorMatrix) - defer C.glUseProgram(0) C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(native)) defer C.glBindTexture(C.GL_TEXTURE_2D, 0) diff --git a/graphics/opengl/shader/program.go b/graphics/opengl/shader/program.go index be15a9370..fde2c4e1b 100644 --- a/graphics/opengl/shader/program.go +++ b/graphics/opengl/shader/program.go @@ -23,6 +23,7 @@ const ( ) var programs = map[programId]*program{ + // TODO: programRegular is not used for now. Remove this. programRegular: &program{ shaderIds: []shaderId{shaderVertex, shaderFragment}, }, @@ -115,11 +116,9 @@ func getUniformLocation(program C.GLuint, name string) C.GLint { func use(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) C.GLuint { - programId := programRegular - if !colorMatrix.IsIdentity() { - programId = programColorMatrix - } + programId := programColorMatrix program := programs[programId] + // TODO: Check the performance. C.glUseProgram(program.native) C.glUniformMatrix4fv(C.GLint(getUniformLocation(program.native, "projection_matrix")), @@ -143,10 +142,6 @@ func use(projectionMatrix [16]float32, C.glUniform1i(getUniformLocation(program.native, "texture"), 0) - if programId != programColorMatrix { - return program.native - } - e := [4][5]float32{} for i := 0; i < 4; i++ { for j := 0; j < 5; j++ {