Change GraphicsContext.Fill to use color.Color

This commit is contained in:
Hajime Hoshi 2014-12-20 06:18:28 +09:00
parent ccef1b5e65
commit c49937519e
7 changed files with 48 additions and 103 deletions

View File

@ -110,7 +110,7 @@ func (s *GameScene) Update(state *GameState) {
} }
func (s *GameScene) Draw(context ebiten.GraphicsContext, textures *Textures) { func (s *GameScene) Draw(context ebiten.GraphicsContext, textures *Textures) {
context.Fill(0xff, 0xff, 0xff) context.Fill(color.White)
field := textures.GetTexture("empty") field := textures.GetTexture("empty")
w, h := field.Size() w, h := field.Size()

View File

@ -43,10 +43,10 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error {
} }
if !g.inited { if !g.inited {
gr.PushRenderTarget(g.brushRenderTarget) gr.PushRenderTarget(g.brushRenderTarget)
gr.Fill(0xff, 0xff, 0xff) gr.Fill(color.White)
gr.PopRenderTarget() gr.PopRenderTarget()
gr.PushRenderTarget(g.canvasRenderTarget) gr.PushRenderTarget(g.canvasRenderTarget)
gr.Fill(0xff, 0xff, 0xff) gr.Fill(color.White)
gr.PopRenderTarget() gr.PopRenderTarget()
g.inited = true g.inited = true
} }

View File

@ -1,63 +0,0 @@
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
_ "image/jpeg"
"log"
)
const (
screenWidth = 320
screenHeight = 240
)
type Game struct {
gophersTexture *ebiten.Texture
textRenderTarget *ebiten.RenderTarget
}
func (g *Game) Update(gr ebiten.GraphicsContext) error {
gr.PushRenderTarget(g.textRenderTarget)
gr.Fill(0x80, 0x40, 0x40)
ebitenutil.DebugPrint(gr, "Hello, World!")
gr.PopRenderTarget()
gr.Fill(0x40, 0x40, 0x80)
geo := ebiten.GeometryMatrixI()
clr := ebiten.ColorMatrixI()
ebiten.DrawWholeTexture(gr, g.textRenderTarget.Texture(), geo, clr)
return nil
}
func main() {
g := new(Game)
var err error
g.gophersTexture, _, err = ebitenutil.NewTextureFromFile("images/gophers.jpg", ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
g.textRenderTarget, err = ebiten.NewRenderTarget(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}

View File

@ -17,9 +17,8 @@ limitations under the License.
package ebiten package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/internal"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/opengl/internal/shader" "image/color"
) )
// A Rect represents a rectangle. // A Rect represents a rectangle.
@ -48,7 +47,7 @@ func DrawWholeTexture(g GraphicsContext, texture *Texture, geo GeometryMatrix, c
// A GraphicsContext is the interface that means a context of rendering. // A GraphicsContext is the interface that means a context of rendering.
type GraphicsContext interface { type GraphicsContext interface {
Clear() error Clear() error
Fill(r, g, b uint8) error Fill(clr color.Color) error
DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error
// TODO: ScreenRenderTarget() Drawer // TODO: ScreenRenderTarget() Drawer
PushRenderTarget(id *RenderTarget) PushRenderTarget(id *RenderTarget)
@ -91,28 +90,3 @@ func (r *RenderTarget) Texture() *Texture {
func (r *RenderTarget) Size() (width int, height int) { func (r *RenderTarget) Size() (width int, height int) {
return r.glRenderTarget.Width(), r.glRenderTarget.Height() return r.glRenderTarget.Width(), r.glRenderTarget.Height()
} }
func u(x float64, width int) float32 {
return float32(x) / float32(internal.AdjustSizeForTexture(width))
}
func v(y float64, height int) float32 {
return float32(y) / float32(internal.AdjustSizeForTexture(height))
}
func textureQuads(parts []TexturePart, width, height int) []shader.TextureQuad {
quads := make([]shader.TextureQuad, 0, len(parts))
for _, part := range parts {
x1 := float32(part.Dst.X)
x2 := float32(part.Dst.X + part.Dst.Width)
y1 := float32(part.Dst.Y)
y2 := float32(part.Dst.Y + part.Dst.Height)
u1 := u(part.Src.X, width)
u2 := u(part.Src.X+part.Src.Width, width)
v1 := v(part.Src.Y, height)
v2 := v(part.Src.Y+part.Src.Height, height)
quad := shader.TextureQuad{x1, x2, y1, y2, u1, u2, v1, v2}
quads = append(quads, quad)
}
return quads
}

View File

@ -19,6 +19,7 @@ package ebiten
import ( import (
"github.com/go-gl/gl" "github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
"image/color"
) )
func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) { func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) {
@ -41,7 +42,7 @@ func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsCo
screenScale: screenScale, screenScale: screenScale,
} }
idsInstance.fillRenderTarget(c.screen, 0, 0, 0) idsInstance.fillRenderTarget(c.screen, color.RGBA{0, 0, 0, 0})
return c, nil return c, nil
} }
@ -68,11 +69,11 @@ func (c *graphicsContext) dispose() {
} }
func (c *graphicsContext) Clear() error { func (c *graphicsContext) Clear() error {
return c.Fill(0, 0, 0) return c.Fill(color.RGBA{0, 0, 0, 0})
} }
func (c *graphicsContext) Fill(r, g, b uint8) error { func (c *graphicsContext) Fill(clr color.Color) error {
return idsInstance.fillRenderTarget(c.currents[len(c.currents)-1], r, g, b) return idsInstance.fillRenderTarget(c.currents[len(c.currents)-1], clr)
} }
func (c *graphicsContext) DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error { func (c *graphicsContext) DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error {

35
ids.go
View File

@ -18,8 +18,10 @@ package ebiten
import ( import (
"github.com/go-gl/gl" "github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/internal"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/opengl/internal/shader" "github.com/hajimehoshi/ebiten/internal/opengl/internal/shader"
"image/color"
"math" "math"
) )
@ -45,17 +47,19 @@ func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget,
} }
texture := &Texture{glTexture} texture := &Texture{glTexture}
// TODO: Is |texture| necessary?
renderTarget := &RenderTarget{glRenderTarget, texture} renderTarget := &RenderTarget{glRenderTarget, texture}
return renderTarget, nil return renderTarget, nil
} }
func (i *ids) fillRenderTarget(renderTarget *RenderTarget, r, g, b uint8) error { func (i *ids) fillRenderTarget(renderTarget *RenderTarget, clr color.Color) error {
if err := i.setViewportIfNeeded(renderTarget); err != nil { if err := i.setViewportIfNeeded(renderTarget); err != nil {
return err return err
} }
const max = float64(math.MaxUint8) const max = math.MaxUint16
gl.ClearColor(gl.GLclampf(float64(r)/max), gl.GLclampf(float64(g)/max), gl.GLclampf(float64(b)/max), 1) r, g, b, a := clr.RGBA()
gl.ClearColor(gl.GLclampf(float64(r)/max), gl.GLclampf(float64(g)/max), gl.GLclampf(float64(b)/max), gl.GLclampf(float64(a)/max))
gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT)
return nil return nil
} }
@ -82,3 +86,28 @@ func (i *ids) setViewportIfNeeded(renderTarget *RenderTarget) error {
} }
return nil return nil
} }
func u(x float64, width int) float32 {
return float32(x) / float32(internal.AdjustSizeForTexture(width))
}
func v(y float64, height int) float32 {
return float32(y) / float32(internal.AdjustSizeForTexture(height))
}
func textureQuads(parts []TexturePart, width, height int) []shader.TextureQuad {
quads := make([]shader.TextureQuad, 0, len(parts))
for _, part := range parts {
x1 := float32(part.Dst.X)
x2 := float32(part.Dst.X + part.Dst.Width)
y1 := float32(part.Dst.Y)
y2 := float32(part.Dst.Y + part.Dst.Height)
u1 := u(part.Src.X, width)
u2 := u(part.Src.X+part.Src.Width, width)
v1 := v(part.Src.Y, height)
v2 := v(part.Src.Y+part.Src.Height, height)
quad := shader.TextureQuad{x1, x2, y1, y2, u1, u2, v1, v2}
quads = append(quads, quad)
}
return quads
}

View File

@ -16,6 +16,10 @@ limitations under the License.
package ebiten package ebiten
import (
"image/color"
)
type syncer interface { type syncer interface {
Sync(f func()) Sync(f func())
} }
@ -34,9 +38,9 @@ func (c *syncGraphicsContext) Clear() (err error) {
return return
} }
func (c *syncGraphicsContext) Fill(r, g, b uint8) (err error) { func (c *syncGraphicsContext) Fill(clr color.Color) (err error) {
c.syncer.Sync(func() { c.syncer.Sync(func() {
err = c.innerGraphicsContext.Fill(r, g, b) err = c.innerGraphicsContext.Fill(clr)
}) })
return return
} }