diff --git a/ebitenutil/debugprint.go b/ebitenutil/debugprint.go index c04dbe86d..1ada20997 100644 --- a/ebitenutil/debugprint.go +++ b/ebitenutil/debugprint.go @@ -24,17 +24,17 @@ import ( type debugPrintState struct { textTexture *ebiten.Texture - debugPrintRenderTarget ebiten.RenderTarget + debugPrintRenderTarget *ebiten.RenderTarget y int } var defaultDebugPrintState = new(debugPrintState) -func DebugPrint(r ebiten.RenderTarget, str string) { +func DebugPrint(r *ebiten.RenderTarget, str string) { defaultDebugPrintState.DebugPrint(r, str) } -func (d *debugPrintState) drawText(r ebiten.RenderTarget, str string, x, y int, clr color.Color) { +func (d *debugPrintState) drawText(r *ebiten.RenderTarget, str string, x, y int, clr color.Color) { parts := []ebiten.TexturePart{} locationX, locationY := 0, 0 for _, c := range str { @@ -60,7 +60,7 @@ func (d *debugPrintState) drawText(r ebiten.RenderTarget, str string, x, y int, r.DrawTexture(d.textTexture, parts, geom, clrm) } -func (d *debugPrintState) DebugPrint(r ebiten.RenderTarget, str string) { +func (d *debugPrintState) DebugPrint(r *ebiten.RenderTarget, str string) { if d.textTexture == nil { img, err := assets.TextImage() if err != nil { diff --git a/example/alphablending/main.go b/example/alphablending/main.go index 29bd74153..f92be4c64 100644 --- a/example/alphablending/main.go +++ b/example/alphablending/main.go @@ -31,11 +31,11 @@ const ( type Game struct { count int - tmpRenderTarget ebiten.RenderTarget + tmpRenderTarget *ebiten.RenderTarget ebitenTexture *ebiten.Texture } -func (g *Game) Update(r ebiten.RenderTarget) error { +func (g *Game) Update(r *ebiten.RenderTarget) error { g.count++ g.count %= 600 diff := float64(g.count) * 0.2 diff --git a/example/blocks/blocks/field.go b/example/blocks/blocks/field.go index 55aa4d154..c4e9b7013 100644 --- a/example/blocks/blocks/field.go +++ b/example/blocks/blocks/field.go @@ -112,7 +112,7 @@ func (f *Field) flushLine(j int) bool { return true } -func (f *Field) Draw(r ebiten.RenderTarget, textures *Textures, geo ebiten.GeometryMatrix) { +func (f *Field) Draw(r *ebiten.RenderTarget, textures *Textures, geo ebiten.GeometryMatrix) { blocks := make([][]BlockType, len(f.blocks)) for i, blockCol := range f.blocks { blocks[i] = make([]BlockType, len(blockCol)) diff --git a/example/blocks/blocks/font.go b/example/blocks/blocks/font.go index 7dcbda173..fa1eb6f95 100644 --- a/example/blocks/blocks/font.go +++ b/example/blocks/blocks/font.go @@ -32,7 +32,7 @@ func textWidth(str string) int { return charWidth * len(str) } -func drawText(r ebiten.RenderTarget, textures *Textures, str string, ox, oy, scale int, clr color.Color) { +func drawText(r *ebiten.RenderTarget, textures *Textures, str string, ox, oy, scale int, clr color.Color) { fontTextureId := textures.GetTexture("font") parts := []ebiten.TexturePart{} @@ -59,7 +59,7 @@ func drawText(r ebiten.RenderTarget, textures *Textures, str string, ox, oy, sca r.DrawTexture(fontTextureId, parts, geoMat, clrMat) } -func drawTextWithShadow(r ebiten.RenderTarget, textures *Textures, str string, x, y, scale int, clr color.Color) { +func drawTextWithShadow(r *ebiten.RenderTarget, textures *Textures, str string, x, y, scale int, clr color.Color) { drawText(r, textures, str, x+1, y+1, scale, color.RGBA{0, 0, 0, 0x80}) drawText(r, textures, str, x, y, scale, clr) } diff --git a/example/blocks/blocks/game.go b/example/blocks/blocks/game.go index 0e87e82ef..a78246e48 100644 --- a/example/blocks/blocks/game.go +++ b/example/blocks/blocks/game.go @@ -70,7 +70,7 @@ func (game *Game) isInitialized() bool { return true } -func (game *Game) Update(r ebiten.RenderTarget) error { +func (game *Game) Update(r *ebiten.RenderTarget) error { game.once.Do(func() { game.textures = NewTextures() for name, path := range texturePaths { diff --git a/example/blocks/blocks/gamescene.go b/example/blocks/blocks/gamescene.go index 89afa043b..02de0f939 100644 --- a/example/blocks/blocks/gamescene.go +++ b/example/blocks/blocks/gamescene.go @@ -109,7 +109,7 @@ func (s *GameScene) Update(state *GameState) { } } -func (s *GameScene) Draw(r ebiten.RenderTarget, textures *Textures) { +func (s *GameScene) Draw(r *ebiten.RenderTarget, textures *Textures) { r.Fill(color.White) field := textures.GetTexture("empty") diff --git a/example/blocks/blocks/piece.go b/example/blocks/blocks/piece.go index c53a0bbfc..d0a4bae97 100644 --- a/example/blocks/blocks/piece.go +++ b/example/blocks/blocks/piece.go @@ -138,7 +138,7 @@ const blockHeight = 10 const fieldBlockNumX = 10 const fieldBlockNumY = 20 -func drawBlocks(r ebiten.RenderTarget, textures *Textures, blocks [][]BlockType, geo ebiten.GeometryMatrix) { +func drawBlocks(r *ebiten.RenderTarget, textures *Textures, blocks [][]BlockType, geo ebiten.GeometryMatrix) { parts := []ebiten.TexturePart{} for i, blockCol := range blocks { for j, block := range blockCol { @@ -213,7 +213,7 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) { } } -func (p *Piece) Draw(r ebiten.RenderTarget, textures *Textures, fieldX, fieldY int, pieceX, pieceY int, angle Angle) { +func (p *Piece) Draw(r *ebiten.RenderTarget, textures *Textures, fieldX, fieldY int, pieceX, pieceY int, angle Angle) { size := len(p.blocks) blocks := make([][]BlockType, size) for i := range p.blocks { diff --git a/example/blocks/blocks/scenemanager.go b/example/blocks/blocks/scenemanager.go index 1a7d9a47f..670b0d67e 100644 --- a/example/blocks/blocks/scenemanager.go +++ b/example/blocks/blocks/scenemanager.go @@ -29,7 +29,7 @@ func init() { type Scene interface { Update(state *GameState) - Draw(r ebiten.RenderTarget, textures *Textures) + Draw(r *ebiten.RenderTarget, textures *Textures) } const transitionMaxCount = 20 @@ -60,7 +60,7 @@ func (s *SceneManager) Update(state *GameState) { } } -func (s *SceneManager) Draw(r ebiten.RenderTarget, textures *Textures) { +func (s *SceneManager) Draw(r *ebiten.RenderTarget, textures *Textures) { if s.transitionCount == -1 { s.current.Draw(r, textures) return diff --git a/example/blocks/blocks/textures.go b/example/blocks/blocks/textures.go index 7f079ef90..3fe33a426 100644 --- a/example/blocks/blocks/textures.go +++ b/example/blocks/blocks/textures.go @@ -38,7 +38,7 @@ type Textures struct { texturePaths chan namePath renderTargetSizes chan nameSize textures map[string]*ebiten.Texture - renderTargets map[string]ebiten.RenderTarget + renderTargets map[string]*ebiten.RenderTarget sync.RWMutex } @@ -47,7 +47,7 @@ func NewTextures() *Textures { texturePaths: make(chan namePath), renderTargetSizes: make(chan nameSize), textures: map[string]*ebiten.Texture{}, - renderTargets: map[string]ebiten.RenderTarget{}, + renderTargets: map[string]*ebiten.RenderTarget{}, } go func() { for { @@ -129,7 +129,7 @@ func (t *Textures) GetTexture(name string) *ebiten.Texture { return t.textures[name] } -func (t *Textures) GetRenderTarget(name string) ebiten.RenderTarget { +func (t *Textures) GetRenderTarget(name string) *ebiten.RenderTarget { t.RLock() defer t.RUnlock() return t.renderTargets[name] diff --git a/example/blocks/blocks/titlescene.go b/example/blocks/blocks/titlescene.go index cd7888d83..2b6496200 100644 --- a/example/blocks/blocks/titlescene.go +++ b/example/blocks/blocks/titlescene.go @@ -40,7 +40,7 @@ func (s *TitleScene) Update(state *GameState) { } } -func (s *TitleScene) Draw(r ebiten.RenderTarget, textures *Textures) { +func (s *TitleScene) Draw(r *ebiten.RenderTarget, textures *Textures) { drawTitleBackground(r, textures, s.count) drawLogo(r, textures, "BLOCKS") @@ -50,7 +50,7 @@ func (s *TitleScene) Draw(r ebiten.RenderTarget, textures *Textures) { drawTextWithShadow(r, textures, message, x, y, 1, color.RGBA{0x80, 0, 0, 0xff}) } -func drawTitleBackground(r ebiten.RenderTarget, textures *Textures, c int) { +func drawTitleBackground(r *ebiten.RenderTarget, textures *Textures, c int) { const textureWidth = 32 const textureHeight = 32 @@ -73,7 +73,7 @@ func drawTitleBackground(r ebiten.RenderTarget, textures *Textures, c int) { r.DrawTexture(backgroundTexture, parts, geo, clr) } -func drawLogo(r ebiten.RenderTarget, textures *Textures, str string) { +func drawLogo(r *ebiten.RenderTarget, textures *Textures, str string) { scale := 4 textWidth := textWidth(str) * scale x := (ScreenWidth - textWidth) / 2 diff --git a/example/image/main.go b/example/image/main.go index 62c31d46e..1d1b9fd03 100644 --- a/example/image/main.go +++ b/example/image/main.go @@ -36,7 +36,7 @@ type Game struct { gophersTexture *ebiten.Texture } -func (g *Game) Update(r ebiten.RenderTarget) error { +func (g *Game) Update(r *ebiten.RenderTarget) error { g.count++ if ebiten.IsKeyPressed(ebiten.KeyLeft) { g.horizontalCount-- diff --git a/example/mosaic/main.go b/example/mosaic/main.go index ccbab1676..c747e4c77 100644 --- a/example/mosaic/main.go +++ b/example/mosaic/main.go @@ -32,10 +32,10 @@ const mosaicRatio = 16 type Game struct { gophersTexture *ebiten.Texture - gophersRenderTarget ebiten.RenderTarget + gophersRenderTarget *ebiten.RenderTarget } -func (g *Game) Update(r ebiten.RenderTarget) error { +func (g *Game) Update(r *ebiten.RenderTarget) error { geo := ebiten.ScaleGeometry(1.0/mosaicRatio, 1.0/mosaicRatio) ebiten.DrawWholeTexture(g.gophersRenderTarget, g.gophersTexture, geo, ebiten.ColorMatrixI()) diff --git a/example/paint/main.go b/example/paint/main.go index 9ed6b2fed..ec55cc692 100644 --- a/example/paint/main.go +++ b/example/paint/main.go @@ -33,11 +33,11 @@ const ( type Game struct { inited bool count int - brushRenderTarget ebiten.RenderTarget - canvasRenderTarget ebiten.RenderTarget + brushRenderTarget *ebiten.RenderTarget + canvasRenderTarget *ebiten.RenderTarget } -func (g *Game) Update(r ebiten.RenderTarget) error { +func (g *Game) Update(r *ebiten.RenderTarget) error { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { g.count++ } diff --git a/example/perspective/main.go b/example/perspective/main.go index ec06aa46c..cb9058632 100644 --- a/example/perspective/main.go +++ b/example/perspective/main.go @@ -32,7 +32,7 @@ type Game struct { gophersTexture *ebiten.Texture } -func (g *Game) Update(r ebiten.RenderTarget) error { +func (g *Game) Update(r *ebiten.RenderTarget) error { parts := []ebiten.TexturePart{} w, h := g.gophersTexture.Size() for i := 0; i < h; i++ { diff --git a/gamecontext.go b/gamecontext.go index a572d95de..ad88bf0b2 100644 --- a/gamecontext.go +++ b/gamecontext.go @@ -48,7 +48,7 @@ func glFilter(f Filter) int { } // NewRenderTarget returns a new RenderTarget. -func NewRenderTarget(width, height int, filter Filter) (RenderTarget, error) { +func NewRenderTarget(width, height int, filter Filter) (*RenderTarget, error) { return currentUI.newRenderTarget(width, height, glFilter(filter)) } diff --git a/graphics.go b/graphics.go index 0f79a5dc0..f60557ffb 100644 --- a/graphics.go +++ b/graphics.go @@ -34,8 +34,12 @@ type TexturePart struct { Src Rect } +type TextureDrawer interface { + DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error +} + // DrawWholeTexture draws the whole texture. -func DrawWholeTexture(r RenderTarget, texture *Texture, geo GeometryMatrix, color ColorMatrix) error { +func DrawWholeTexture(r TextureDrawer, texture *Texture, geo GeometryMatrix, color ColorMatrix) error { w, h := texture.Size() parts := []TexturePart{ {Rect{0, 0, float64(w), float64(h)}, Rect{0, 0, float64(w), float64(h)}}, diff --git a/internal/opengl/internal/shader/shader.go b/internal/opengl/internal/shader/shader.go index c16ad794a..bc969b244 100644 --- a/internal/opengl/internal/shader/shader.go +++ b/internal/opengl/internal/shader/shader.go @@ -86,6 +86,7 @@ void main(void) { vec4 color1 = texture2D(texture1, vertex_out_tex_coord1); color0 = (color_matrix * color0) + color_matrix_translation; + // Photoshop-like RGBA blending gl_FragColor.a = color0.a + color1.a - color0.a * color1.a; gl_FragColor.rgb = (color0.a * color0.rgb + (1.0 - color0.a) * color1.a * color1.rgb) / gl_FragColor.a; } diff --git a/rendertarget.go b/rendertarget.go index d83707061..8d75e802f 100644 --- a/rendertarget.go +++ b/rendertarget.go @@ -25,15 +25,6 @@ import ( "math" ) -// TODO: Rename -type RenderTarget interface { - Texture() *Texture - Size() (width, height int) - Clear() error - Fill(clr color.Color) error - DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error -} - type renderTarget struct { glRenderTarget *opengl.RenderTarget texture *Texture @@ -127,36 +118,36 @@ type syncer interface { Sync(func()) } -type syncRenderTarget struct { +type RenderTarget struct { syncer syncer - inner RenderTarget + inner *renderTarget } -func (c *syncRenderTarget) Texture() *Texture { - return c.inner.Texture() +func (r *RenderTarget) Texture() *Texture { + return r.inner.Texture() } -func (c *syncRenderTarget) Size() (width, height int) { - return c.inner.Size() +func (r *RenderTarget) Size() (width, height int) { + return r.inner.Size() } -func (c *syncRenderTarget) Clear() (err error) { - c.syncer.Sync(func() { - err = c.inner.Clear() +func (r *RenderTarget) Clear() (err error) { + r.syncer.Sync(func() { + err = r.inner.Clear() }) return } -func (c *syncRenderTarget) Fill(clr color.Color) (err error) { - c.syncer.Sync(func() { - err = c.inner.Fill(clr) +func (r *RenderTarget) Fill(clr color.Color) (err error) { + r.syncer.Sync(func() { + err = r.inner.Fill(clr) }) return } -func (c *syncRenderTarget) DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) (err error) { - c.syncer.Sync(func() { - err = c.inner.DrawTexture(texture, parts, geo, color) +func (r *RenderTarget) DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) (err error) { + r.syncer.Sync(func() { + err = r.inner.DrawTexture(texture, parts, geo, color) }) return } diff --git a/run.go b/run.go index f0a7ba450..7a48532fa 100644 --- a/run.go +++ b/run.go @@ -18,7 +18,7 @@ package ebiten // Run runs the game. // This function must be called from the main thread. -func Run(f func(RenderTarget) error, width, height, scale int, title string) error { +func Run(f func(*RenderTarget) error, width, height, scale int, title string) error { err := startUI(width, height, scale, title) if err != nil { return err diff --git a/ui.go b/ui.go index cebf7bb0b..b5fba1f33 100644 --- a/ui.go +++ b/ui.go @@ -110,14 +110,14 @@ func (u *ui) Sync(f func()) { u.use(f) } -func (u *ui) draw(f func(RenderTarget) error) (err error) { +func (u *ui) draw(f func(*RenderTarget) error) (err error) { u.use(func() { err = u.graphicsContext.preUpdate() }) if err != nil { return } - err = f(&syncRenderTarget{syncer: u, inner: u.graphicsContext.screen}) + err = f(&RenderTarget{syncer: u, inner: u.graphicsContext.screen}) if err != nil { return } @@ -144,13 +144,13 @@ func (u *ui) newTexture(img image.Image, filter int) (*Texture, error) { return texture, err } -func (u *ui) newRenderTarget(width, height int, filter int) (RenderTarget, error) { - var renderTarget RenderTarget +func (u *ui) newRenderTarget(width, height int, filter int) (*RenderTarget, error) { + var renderTarget *renderTarget var err error u.use(func() { renderTarget, err = newRenderTarget(width, height, filter) }) - return &syncRenderTarget{u, renderTarget}, err + return &RenderTarget{u, renderTarget}, err } func (u *ui) run() {