diff --git a/graphics/opengl/context.go b/graphics/opengl/context.go index 791a423da..2dddac242 100644 --- a/graphics/opengl/context.go +++ b/graphics/opengl/context.go @@ -10,7 +10,6 @@ import ( "github.com/hajimehoshi/go-ebiten/graphics/matrix" "github.com/hajimehoshi/go-ebiten/graphics/opengl/offscreen" "github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget" - "math" ) type Context struct { @@ -36,12 +35,14 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context { if err != nil { panic("initializing the offscreen failed: " + err.Error()) } + context.ResetOffscreen() context.Clear() return context } func (context *Context) Dispose() { + // TODO: remove main framebuffer? context.ids.DeleteRenderTarget(context.screenId) } @@ -71,13 +72,7 @@ func (context *Context) Clear() { } func (context *Context) Fill(r, g, b uint8) { - const max = float64(math.MaxUint8) - C.glClearColor( - C.GLclampf(float64(r)/max), - C.GLclampf(float64(g)/max), - C.GLclampf(float64(b)/max), - 1) - C.glClear(C.GL_COLOR_BUFFER_BIT) + context.offscreen.Fill(r, g, b) } func (context *Context) DrawTexture( diff --git a/graphics/opengl/offscreen/offscreen.go b/graphics/opengl/offscreen/offscreen.go index 962ee99db..f68d32643 100644 --- a/graphics/opengl/offscreen/offscreen.go +++ b/graphics/opengl/offscreen/offscreen.go @@ -15,6 +15,8 @@ type Texture interface { type RenderTarget interface { SetAsViewport() ProjectionMatrix() [4][4]float64 + Clear() + Fill(r, g, b uint8) } type Offscreen struct { @@ -30,7 +32,6 @@ func New(mainFramebuffer RenderTarget) *Offscreen { func (o *Offscreen) Set(rt RenderTarget) { o.currentRenderTarget = rt - rt.SetAsViewport() } func (o *Offscreen) SetMainFramebuffer() { @@ -39,6 +40,7 @@ func (o *Offscreen) SetMainFramebuffer() { func (o *Offscreen) DrawTexture(texture Texture, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { + o.currentRenderTarget.SetAsViewport() projectionMatrix := o.currentRenderTarget.ProjectionMatrix() texture.Draw(projectionMatrix, geometryMatrix, colorMatrix) } @@ -46,6 +48,15 @@ func (o *Offscreen) DrawTexture(texture Texture, func (o *Offscreen) DrawTextureParts(texture Texture, parts []graphics.TexturePart, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { + o.currentRenderTarget.SetAsViewport() projectionMatrix := o.currentRenderTarget.ProjectionMatrix() texture.DrawParts(parts, projectionMatrix, geometryMatrix, colorMatrix) } + +func (o *Offscreen) Clear() { + o.currentRenderTarget.Clear() +} + +func (o *Offscreen) Fill(r, g, b uint8) { + o.currentRenderTarget.Fill(r, g, b) +} diff --git a/graphics/opengl/rendertarget/render_target.go b/graphics/opengl/rendertarget/render_target.go index 80a11ae33..990a447e6 100644 --- a/graphics/opengl/rendertarget/render_target.go +++ b/graphics/opengl/rendertarget/render_target.go @@ -7,6 +7,7 @@ import "C" import ( "fmt" "github.com/hajimehoshi/go-ebiten/graphics" + "math" ) type NativeTexture C.GLuint @@ -21,9 +22,7 @@ type RenderTarget struct { func NewWithCurrentFramebuffer(width, height int) *RenderTarget { framebuffer := C.GLint(0) C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &framebuffer) - rt := &RenderTarget{C.GLuint(framebuffer), width, height, true} - rt.setAsViewport() - return rt + return &RenderTarget{C.GLuint(framebuffer), width, height, true} } func createFramebuffer(nativeTexture C.GLuint) C.GLuint { @@ -87,7 +86,6 @@ func (r *RenderTarget) ProjectionMatrix() [4][4]float64 { height := graphics.AdjustSizeForTexture(r.height) matrix := graphics.OrthoProjectionMatrix(0, width, 0, height) if r.flipY { - // Flip Y and move to fit with the top of the window. matrix[1][1] *= -1 matrix[1][3] += float64(r.height) / float64(graphics.AdjustSizeForTexture(r.height)) * 2 @@ -98,3 +96,18 @@ func (r *RenderTarget) ProjectionMatrix() [4][4]float64 { func (r *RenderTarget) Dispose() { C.glDeleteFramebuffers(1, &r.framebuffer) } + +func (r *RenderTarget) Clear() { + r.Fill(0, 0, 0) +} + +func (r *RenderTarget) Fill(red, green, blue uint8) { + r.SetAsViewport() + const max = float64(math.MaxUint8) + C.glClearColor( + C.GLclampf(float64(red)/max), + C.GLclampf(float64(green)/max), + C.GLclampf(float64(blue)/max), + 1) + C.glClear(C.GL_COLOR_BUFFER_BIT) +}