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
Init(tf graphics.TextureFactory)
Update(input InputState)
Draw(g graphics.GraphicsContext, offscreen graphics.Texture)
Draw(g graphics.Context, offscreen graphics.Texture)
}
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) 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
}
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})
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) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) {
func (game *Rects) Draw(g graphics.Context, offscreen graphics.Texture) {
g.SetOffscreen(game.rectsTexture.ID)
x := rand.Intn(game.ScreenWidth())

View File

@ -52,7 +52,7 @@ func (game *Rotating) Update(input ebiten.InputState) {
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})
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})
// Draw the sprites

View File

@ -185,10 +185,12 @@ func main() {
case <-update:
game.Update(inputState)
inputState = ebiten.InputState{}
case gameDraw := <-draw:
case drawing := <-draw:
ch := make(chan interface{})
s := &SyncDrawable{game, ch}
gameDraw <- s
drawing <- func(g graphics.Context, offscreen graphics.Texture) {
game.Draw(g, offscreen)
close(ch)
}
<-ch
}
}
@ -197,12 +199,18 @@ func main() {
currentUI.Run()
}
type SyncDrawable struct {
drawable graphics.Drawable
ch chan interface{}
type FuncInitializer struct {
f func(graphics.TextureFactory)
}
func (s *SyncDrawable) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) {
s.drawable.Draw(g, offscreen)
close(s.ch)
func (i *FuncInitializer) Initialize(tf graphics.TextureFactory) {
i.f(tf)
}
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"
)
type Drawable interface {
Draw(g GraphicsContext, offscreen Texture)
}
type Device interface {
Initializing() <-chan chan func(TextureFactory)
TextureFactory() TextureFactory
Drawing() <-chan chan Drawable
Drawing() <-chan chan func(g Context, offscreen Texture)
}
type Rect struct {
@ -28,7 +25,7 @@ type TexturePart struct {
Source Rect
}
type GraphicsContext interface {
type Context interface {
Clear()
Fill(clr color.Color)
DrawRect(rect Rect, clr color.Color)

View File

@ -15,7 +15,7 @@ import (
"unsafe"
)
type GraphicsContext struct {
type Context struct {
screenWidth int
screenHeight int
screenScale int
@ -29,8 +29,8 @@ type GraphicsContext struct {
}
// This method should be called on the UI thread.
func newGraphicsContext(screenWidth, screenHeight, screenScale int) *GraphicsContext {
context := &GraphicsContext{
func newContext(screenWidth, screenHeight, screenScale int) *Context {
context := &Context{
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
@ -48,12 +48,12 @@ func newGraphicsContext(screenWidth, screenHeight, screenScale int) *GraphicsCon
return context
}
func (context *GraphicsContext) Clear() {
func (context *Context) Clear() {
C.glClearColor(0, 0, 0, 0)
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()
max := float64(math.MaxUint16)
C.glClearColor(
@ -64,7 +64,7 @@ func (context *GraphicsContext) Fill(clr color.Color) {
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)
height := float32(context.currentOffscreenHeight)
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,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
texture := context.textures[textureID]
@ -123,7 +123,7 @@ func (context *GraphicsContext) DrawTexture(
geometryMatrix, colorMatrix)
}
func (context *GraphicsContext) DrawTextureParts(
func (context *Context) DrawTextureParts(
textureID graphics.TextureID, locations []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
@ -182,7 +182,7 @@ func abs(x int) int {
return x
}
func (context *GraphicsContext) SetOffscreen(textureID graphics.TextureID) {
func (context *Context) SetOffscreen(textureID graphics.TextureID) {
texture := context.textures[textureID]
context.currentOffscreenWidth = texture.width
context.currentOffscreenHeight = texture.height
@ -195,7 +195,7 @@ func (context *GraphicsContext) SetOffscreen(textureID graphics.TextureID) {
texture.textureWidth, texture.textureHeight)
}
func (context *GraphicsContext) setOffscreenFramebuffer(framebuffer C.GLuint,
func (context *Context) setOffscreenFramebuffer(framebuffer C.GLuint,
textureWidth, textureHeight int) {
if framebuffer == context.mainFramebuffer {
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.currentOffscreenWidth = context.screenWidth * context.screenScale
context.currentOffscreenHeight = context.screenHeight * context.screenScale
}
// This method should be called on the UI thread.
func (context *GraphicsContext) flush() {
func (context *Context) flush() {
C.glFlush()
}
// This method should be called on the UI thread.
func (context *GraphicsContext) setShaderProgram(
func (context *Context) setShaderProgram(
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
program := C.GLuint(0)
if colorMatrix.IsIdentity() {
@ -308,7 +308,7 @@ func (context *GraphicsContext) setShaderProgram(
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]
if ok {
return framebuffer
@ -331,7 +331,7 @@ func (context *GraphicsContext) getFramebuffer(textureID C.GLuint) C.GLuint {
return newFramebuffer
}
func (context *GraphicsContext) deleteFramebuffer(textureID C.GLuint) {
func (context *Context) deleteFramebuffer(textureID C.GLuint) {
framebuffer, ok := context.framebuffers[textureID]
if !ok {
// TODO: panic?
@ -341,7 +341,7 @@ func (context *GraphicsContext) deleteFramebuffer(textureID C.GLuint) {
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)
id := graphics.TextureID(texture.id)
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)
if err != nil {
return graphics.Texture{}, err

View File

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