Refactoring

This commit is contained in:
Hajime Hoshi 2013-06-28 00:33:03 +09:00
parent b0de4acaf4
commit f06e541f48
10 changed files with 60 additions and 55 deletions

View File

@ -15,7 +15,7 @@ type Game interface {
Fps() int Fps() int
Init(tf graphics.TextureFactory) Init(tf graphics.TextureFactory)
Update(input InputState) Update(input InputState)
Draw(g graphics.GraphicsContext, offscreen graphics.Texture) Draw(g graphics.Context, offscreen graphics.Texture)
} }
type UI interface { type UI interface {

View File

@ -30,5 +30,5 @@ func (game *Blank) Init(tf graphics.TextureFactory) {
func (game *Blank) Update(input ebiten.InputState) { func (game *Blank) Update(input ebiten.InputState) {
} }
func (game *Blank) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { func (game *Blank) Draw(g graphics.Context, offscreen graphics.Texture) {
} }

View File

@ -100,7 +100,7 @@ func (game *Monochrome) Update(input ebiten.InputState) {
<-game.ch <-game.ch
} }
func (game *Monochrome) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { func (game *Monochrome) Draw(g graphics.Context, offscreen graphics.Texture) {
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255}) g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
geometryMatrix := matrix.IdentityGeometry() geometryMatrix := matrix.IdentityGeometry()

View File

@ -36,7 +36,7 @@ func (game *Rects) Init(tf graphics.TextureFactory) {
func (game *Rects) Update(input ebiten.InputState) { func (game *Rects) Update(input ebiten.InputState) {
} }
func (game *Rects) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { func (game *Rects) Draw(g graphics.Context, offscreen graphics.Texture) {
g.SetOffscreen(game.rectsTexture.ID) g.SetOffscreen(game.rectsTexture.ID)
x := rand.Intn(game.ScreenWidth()) x := rand.Intn(game.ScreenWidth())

View File

@ -52,7 +52,7 @@ func (game *Rotating) Update(input ebiten.InputState) {
game.x++ game.x++
} }
func (game *Rotating) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { func (game *Rotating) Draw(g graphics.Context, offscreen graphics.Texture) {
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255}) g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
geometryMatrix := matrix.IdentityGeometry() geometryMatrix := matrix.IdentityGeometry()

View File

@ -111,7 +111,7 @@ func (game *Sprites) Update(input ebiten.InputState) {
} }
} }
func (game *Sprites) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { func (game *Sprites) Draw(g graphics.Context, offscreen graphics.Texture) {
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255}) g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
// Draw the sprites // Draw the sprites

View File

@ -185,10 +185,12 @@ func main() {
case <-update: case <-update:
game.Update(inputState) game.Update(inputState)
inputState = ebiten.InputState{} inputState = ebiten.InputState{}
case gameDraw := <-draw: case drawing := <-draw:
ch := make(chan interface{}) ch := make(chan interface{})
s := &SyncDrawable{game, ch} drawing <- func(g graphics.Context, offscreen graphics.Texture) {
gameDraw <- s game.Draw(g, offscreen)
close(ch)
}
<-ch <-ch
} }
} }
@ -197,12 +199,18 @@ func main() {
currentUI.Run() currentUI.Run()
} }
type SyncDrawable struct { type FuncInitializer struct {
drawable graphics.Drawable f func(graphics.TextureFactory)
ch chan interface{}
} }
func (s *SyncDrawable) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { func (i *FuncInitializer) Initialize(tf graphics.TextureFactory) {
s.drawable.Draw(g, offscreen) i.f(tf)
close(s.ch) }
type FuncDrawable struct {
f func(graphics.Context, graphics.Texture)
}
func (d *FuncDrawable) Draw(g graphics.Context, offscreen graphics.Texture) {
d.f(g, offscreen)
} }

View File

@ -6,13 +6,10 @@ import (
"image/color" "image/color"
) )
type Drawable interface {
Draw(g GraphicsContext, offscreen Texture)
}
type Device interface { type Device interface {
Initializing() <-chan chan func(TextureFactory)
TextureFactory() TextureFactory TextureFactory() TextureFactory
Drawing() <-chan chan Drawable Drawing() <-chan chan func(g Context, offscreen Texture)
} }
type Rect struct { type Rect struct {
@ -28,7 +25,7 @@ type TexturePart struct {
Source Rect Source Rect
} }
type GraphicsContext interface { type Context interface {
Clear() Clear()
Fill(clr color.Color) Fill(clr color.Color)
DrawRect(rect Rect, clr color.Color) DrawRect(rect Rect, clr color.Color)

View File

@ -15,7 +15,7 @@ import (
"unsafe" "unsafe"
) )
type GraphicsContext struct { type Context struct {
screenWidth int screenWidth int
screenHeight int screenHeight int
screenScale int screenScale int
@ -29,8 +29,8 @@ type GraphicsContext struct {
} }
// This method should be called on the UI thread. // This method should be called on the UI thread.
func newGraphicsContext(screenWidth, screenHeight, screenScale int) *GraphicsContext { func newContext(screenWidth, screenHeight, screenScale int) *Context {
context := &GraphicsContext{ context := &Context{
screenWidth: screenWidth, screenWidth: screenWidth,
screenHeight: screenHeight, screenHeight: screenHeight,
screenScale: screenScale, screenScale: screenScale,
@ -48,12 +48,12 @@ func newGraphicsContext(screenWidth, screenHeight, screenScale int) *GraphicsCon
return context return context
} }
func (context *GraphicsContext) Clear() { func (context *Context) Clear() {
C.glClearColor(0, 0, 0, 0) C.glClearColor(0, 0, 0, 0)
C.glClear(C.GL_COLOR_BUFFER_BIT) C.glClear(C.GL_COLOR_BUFFER_BIT)
} }
func (context *GraphicsContext) Fill(clr color.Color) { func (context *Context) Fill(clr color.Color) {
r, g, b, a := clr.RGBA() r, g, b, a := clr.RGBA()
max := float64(math.MaxUint16) max := float64(math.MaxUint16)
C.glClearColor( C.glClearColor(
@ -64,7 +64,7 @@ func (context *GraphicsContext) Fill(clr color.Color) {
C.glClear(C.GL_COLOR_BUFFER_BIT) C.glClear(C.GL_COLOR_BUFFER_BIT)
} }
func (context *GraphicsContext) DrawRect(rect graphics.Rect, clr color.Color) { func (context *Context) DrawRect(rect graphics.Rect, clr color.Color) {
width := float32(context.currentOffscreenWidth) width := float32(context.currentOffscreenWidth)
height := float32(context.currentOffscreenHeight) height := float32(context.currentOffscreenHeight)
textureWidth := float32(clp2(uint64(width))) textureWidth := float32(clp2(uint64(width)))
@ -112,7 +112,7 @@ func (context *GraphicsContext) DrawRect(rect graphics.Rect, clr color.Color) {
} }
} }
func (context *GraphicsContext) DrawTexture( func (context *Context) DrawTexture(
textureID graphics.TextureID, textureID graphics.TextureID,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
texture := context.textures[textureID] texture := context.textures[textureID]
@ -123,7 +123,7 @@ func (context *GraphicsContext) DrawTexture(
geometryMatrix, colorMatrix) geometryMatrix, colorMatrix)
} }
func (context *GraphicsContext) DrawTextureParts( func (context *Context) DrawTextureParts(
textureID graphics.TextureID, locations []graphics.TexturePart, textureID graphics.TextureID, locations []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
@ -182,7 +182,7 @@ func abs(x int) int {
return x return x
} }
func (context *GraphicsContext) SetOffscreen(textureID graphics.TextureID) { func (context *Context) SetOffscreen(textureID graphics.TextureID) {
texture := context.textures[textureID] texture := context.textures[textureID]
context.currentOffscreenWidth = texture.width context.currentOffscreenWidth = texture.width
context.currentOffscreenHeight = texture.height context.currentOffscreenHeight = texture.height
@ -195,7 +195,7 @@ func (context *GraphicsContext) SetOffscreen(textureID graphics.TextureID) {
texture.textureWidth, texture.textureHeight) texture.textureWidth, texture.textureHeight)
} }
func (context *GraphicsContext) setOffscreenFramebuffer(framebuffer C.GLuint, func (context *Context) setOffscreenFramebuffer(framebuffer C.GLuint,
textureWidth, textureHeight int) { textureWidth, textureHeight int) {
if framebuffer == context.mainFramebuffer { if framebuffer == context.mainFramebuffer {
textureWidth = int(clp2(uint64(context.screenWidth * context.screenScale))) textureWidth = int(clp2(uint64(context.screenWidth * context.screenScale)))
@ -235,19 +235,19 @@ func (context *GraphicsContext) setOffscreenFramebuffer(framebuffer C.GLuint,
} }
} }
func (context *GraphicsContext) resetOffscreen() { func (context *Context) resetOffscreen() {
context.setOffscreenFramebuffer(context.mainFramebuffer, 0, 0) context.setOffscreenFramebuffer(context.mainFramebuffer, 0, 0)
context.currentOffscreenWidth = context.screenWidth * context.screenScale context.currentOffscreenWidth = context.screenWidth * context.screenScale
context.currentOffscreenHeight = context.screenHeight * context.screenScale context.currentOffscreenHeight = context.screenHeight * context.screenScale
} }
// This method should be called on the UI thread. // This method should be called on the UI thread.
func (context *GraphicsContext) flush() { func (context *Context) flush() {
C.glFlush() C.glFlush()
} }
// This method should be called on the UI thread. // This method should be called on the UI thread.
func (context *GraphicsContext) setShaderProgram( func (context *Context) setShaderProgram(
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
program := C.GLuint(0) program := C.GLuint(0)
if colorMatrix.IsIdentity() { if colorMatrix.IsIdentity() {
@ -308,7 +308,7 @@ func (context *GraphicsContext) setShaderProgram(
1, (*C.GLfloat)(&glColorMatrixTranslation[0])) 1, (*C.GLfloat)(&glColorMatrixTranslation[0]))
} }
func (context *GraphicsContext) getFramebuffer(textureID C.GLuint) C.GLuint { func (context *Context) getFramebuffer(textureID C.GLuint) C.GLuint {
framebuffer, ok := context.framebuffers[textureID] framebuffer, ok := context.framebuffers[textureID]
if ok { if ok {
return framebuffer return framebuffer
@ -331,7 +331,7 @@ func (context *GraphicsContext) getFramebuffer(textureID C.GLuint) C.GLuint {
return newFramebuffer return newFramebuffer
} }
func (context *GraphicsContext) deleteFramebuffer(textureID C.GLuint) { func (context *Context) deleteFramebuffer(textureID C.GLuint) {
framebuffer, ok := context.framebuffers[textureID] framebuffer, ok := context.framebuffers[textureID]
if !ok { if !ok {
// TODO: panic? // TODO: panic?
@ -341,7 +341,7 @@ func (context *GraphicsContext) deleteFramebuffer(textureID C.GLuint) {
delete(context.framebuffers, textureID) delete(context.framebuffers, textureID)
} }
func (context *GraphicsContext) NewTexture(width, height int) graphics.Texture { func (context *Context) NewTexture(width, height int) graphics.Texture {
texture := newTexture(width, height) texture := newTexture(width, height)
id := graphics.TextureID(texture.id) id := graphics.TextureID(texture.id)
context.textures[id] = texture context.textures[id] = texture
@ -357,7 +357,7 @@ func (context *GraphicsContext) NewTexture(width, height int) graphics.Texture {
} }
} }
func (context *GraphicsContext) NewTextureFromImage(img image.Image) (graphics.Texture, error) { func (context *Context) NewTextureFromImage(img image.Image) (graphics.Texture, error) {
texture, err := newTextureFromImage(img) texture, err := newTextureFromImage(img)
if err != nil { if err != nil {
return graphics.Texture{}, err return graphics.Texture{}, err

View File

@ -14,25 +14,25 @@ type Device struct {
screenWidth int screenWidth int
screenHeight int screenHeight int
screenScale int screenScale int
graphicsContext *GraphicsContext context *Context
offscreenTexture graphics.Texture offscreenTexture graphics.Texture
deviceUpdate chan chan graphics.Drawable drawing chan chan func(graphics.Context, graphics.Texture)
updating chan chan func() updating chan chan func()
} }
func NewDevice(screenWidth, screenHeight, screenScale int, updating chan chan func()) *Device { func NewDevice(screenWidth, screenHeight, screenScale int, updating chan chan func()) *Device {
graphicsContext := newGraphicsContext(screenWidth, screenHeight, screenScale) context := newContext(screenWidth, screenHeight, screenScale)
device := &Device{ device := &Device{
screenWidth: screenWidth, screenWidth: screenWidth,
screenHeight: screenHeight, screenHeight: screenHeight,
screenScale: screenScale, screenScale: screenScale,
deviceUpdate: make(chan chan graphics.Drawable), drawing: make(chan chan func(graphics.Context, graphics.Texture)),
graphicsContext: graphicsContext, context: context,
updating: updating, updating: updating,
} }
device.offscreenTexture = device.offscreenTexture =
device.graphicsContext.NewTexture(screenWidth, screenHeight) device.context.NewTexture(screenWidth, screenHeight)
go func() { go func() {
for { for {
@ -44,8 +44,8 @@ func NewDevice(screenWidth, screenHeight, screenScale int, updating chan chan fu
return device return device
} }
func (device *Device) Drawing() <-chan chan graphics.Drawable { func (device *Device) Drawing() <-chan chan func(graphics.Context, graphics.Texture) {
return device.deviceUpdate return device.drawing
} }
func (device *Device) OffscreenTexture() graphics.Texture { func (device *Device) OffscreenTexture() graphics.Texture {
@ -53,17 +53,17 @@ func (device *Device) OffscreenTexture() graphics.Texture {
} }
func (device *Device) Update() { func (device *Device) Update() {
g := device.graphicsContext g := device.context
C.glEnable(C.GL_TEXTURE_2D) C.glEnable(C.GL_TEXTURE_2D)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST)
g.SetOffscreen(device.offscreenTexture.ID) g.SetOffscreen(device.offscreenTexture.ID)
g.Clear() g.Clear()
ch := make(chan graphics.Drawable) ch := make(chan func(graphics.Context, graphics.Texture))
device.deviceUpdate <- ch device.drawing <- ch
drawable := <-ch drawable := <-ch
drawable.Draw(g, device.offscreenTexture) drawable(g, device.offscreenTexture)
g.flush() g.flush()
@ -85,5 +85,5 @@ func (device *Device) Update() {
} }
func (device *Device) TextureFactory() graphics.TextureFactory { func (device *Device) TextureFactory() graphics.TextureFactory {
return device.graphicsContext return device.context
} }