diff --git a/ebiten.go b/ebiten.go index e1166bbf2..d40879977 100644 --- a/ebiten.go +++ b/ebiten.go @@ -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 { diff --git a/example/game/blank/blank.go b/example/game/blank/blank.go index ed9ae85c6..d0d430eb0 100644 --- a/example/game/blank/blank.go +++ b/example/game/blank/blank.go @@ -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) { } diff --git a/example/game/monochrome/monochrome.go b/example/game/monochrome/monochrome.go index 92db81ea4..b4e4e21de 100644 --- a/example/game/monochrome/monochrome.go +++ b/example/game/monochrome/monochrome.go @@ -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() diff --git a/example/game/rects/rects.go b/example/game/rects/rects.go index 575cf3d66..95cc56e91 100644 --- a/example/game/rects/rects.go +++ b/example/game/rects/rects.go @@ -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()) diff --git a/example/game/rotating/rotating.go b/example/game/rotating/rotating.go index 8f5eff339..12e8cb4e0 100644 --- a/example/game/rotating/rotating.go +++ b/example/game/rotating/rotating.go @@ -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() diff --git a/example/game/sprites/sprites.go b/example/game/sprites/sprites.go index aeb4135d9..caf4a327f 100644 --- a/example/game/sprites/sprites.go +++ b/example/game/sprites/sprites.go @@ -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 diff --git a/example/glut/main.go b/example/glut/main.go index ce05128d3..8484d0236 100644 --- a/example/glut/main.go +++ b/example/glut/main.go @@ -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) } diff --git a/graphics/graphics.go b/graphics/graphics.go index 2ebb355a8..79c0a8c18 100644 --- a/graphics/graphics.go +++ b/graphics/graphics.go @@ -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) diff --git a/graphics/opengl/graphics_context.go b/graphics/opengl/context.go similarity index 90% rename from graphics/opengl/graphics_context.go rename to graphics/opengl/context.go index 2184fdd58..c1bfa438a 100644 --- a/graphics/opengl/graphics_context.go +++ b/graphics/opengl/context.go @@ -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 diff --git a/graphics/opengl/device.go b/graphics/opengl/device.go index 14050f1a4..d08ad9db2 100644 --- a/graphics/opengl/device.go +++ b/graphics/opengl/device.go @@ -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 }