Move Clear/Fill to RenderTarget

This commit is contained in:
Hajime Hoshi 2014-01-11 08:20:05 +09:00
parent 1c10c45f69
commit 1772d85d58
3 changed files with 32 additions and 13 deletions

View File

@ -10,7 +10,6 @@ import (
"github.com/hajimehoshi/go-ebiten/graphics/matrix" "github.com/hajimehoshi/go-ebiten/graphics/matrix"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/offscreen" "github.com/hajimehoshi/go-ebiten/graphics/opengl/offscreen"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget" "github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
"math"
) )
type Context struct { type Context struct {
@ -36,12 +35,14 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
if err != nil { if err != nil {
panic("initializing the offscreen failed: " + err.Error()) panic("initializing the offscreen failed: " + err.Error())
} }
context.ResetOffscreen()
context.Clear() context.Clear()
return context return context
} }
func (context *Context) Dispose() { func (context *Context) Dispose() {
// TODO: remove main framebuffer?
context.ids.DeleteRenderTarget(context.screenId) context.ids.DeleteRenderTarget(context.screenId)
} }
@ -71,13 +72,7 @@ func (context *Context) Clear() {
} }
func (context *Context) Fill(r, g, b uint8) { func (context *Context) Fill(r, g, b uint8) {
const max = float64(math.MaxUint8) context.offscreen.Fill(r, g, b)
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)
} }
func (context *Context) DrawTexture( func (context *Context) DrawTexture(

View File

@ -15,6 +15,8 @@ type Texture interface {
type RenderTarget interface { type RenderTarget interface {
SetAsViewport() SetAsViewport()
ProjectionMatrix() [4][4]float64 ProjectionMatrix() [4][4]float64
Clear()
Fill(r, g, b uint8)
} }
type Offscreen struct { type Offscreen struct {
@ -30,7 +32,6 @@ func New(mainFramebuffer RenderTarget) *Offscreen {
func (o *Offscreen) Set(rt RenderTarget) { func (o *Offscreen) Set(rt RenderTarget) {
o.currentRenderTarget = rt o.currentRenderTarget = rt
rt.SetAsViewport()
} }
func (o *Offscreen) SetMainFramebuffer() { func (o *Offscreen) SetMainFramebuffer() {
@ -39,6 +40,7 @@ func (o *Offscreen) SetMainFramebuffer() {
func (o *Offscreen) DrawTexture(texture Texture, func (o *Offscreen) DrawTexture(texture Texture,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
o.currentRenderTarget.SetAsViewport()
projectionMatrix := o.currentRenderTarget.ProjectionMatrix() projectionMatrix := o.currentRenderTarget.ProjectionMatrix()
texture.Draw(projectionMatrix, geometryMatrix, colorMatrix) texture.Draw(projectionMatrix, geometryMatrix, colorMatrix)
} }
@ -46,6 +48,15 @@ func (o *Offscreen) DrawTexture(texture Texture,
func (o *Offscreen) DrawTextureParts(texture Texture, func (o *Offscreen) DrawTextureParts(texture Texture,
parts []graphics.TexturePart, parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
o.currentRenderTarget.SetAsViewport()
projectionMatrix := o.currentRenderTarget.ProjectionMatrix() projectionMatrix := o.currentRenderTarget.ProjectionMatrix()
texture.DrawParts(parts, projectionMatrix, geometryMatrix, colorMatrix) 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)
}

View File

@ -7,6 +7,7 @@ import "C"
import ( import (
"fmt" "fmt"
"github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics"
"math"
) )
type NativeTexture C.GLuint type NativeTexture C.GLuint
@ -21,9 +22,7 @@ type RenderTarget struct {
func NewWithCurrentFramebuffer(width, height int) *RenderTarget { func NewWithCurrentFramebuffer(width, height int) *RenderTarget {
framebuffer := C.GLint(0) framebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &framebuffer) C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &framebuffer)
rt := &RenderTarget{C.GLuint(framebuffer), width, height, true} return &RenderTarget{C.GLuint(framebuffer), width, height, true}
rt.setAsViewport()
return rt
} }
func createFramebuffer(nativeTexture C.GLuint) C.GLuint { func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
@ -87,7 +86,6 @@ func (r *RenderTarget) ProjectionMatrix() [4][4]float64 {
height := graphics.AdjustSizeForTexture(r.height) height := graphics.AdjustSizeForTexture(r.height)
matrix := graphics.OrthoProjectionMatrix(0, width, 0, height) matrix := graphics.OrthoProjectionMatrix(0, width, 0, height)
if r.flipY { if r.flipY {
// Flip Y and move to fit with the top of the window.
matrix[1][1] *= -1 matrix[1][1] *= -1
matrix[1][3] += float64(r.height) / matrix[1][3] += float64(r.height) /
float64(graphics.AdjustSizeForTexture(r.height)) * 2 float64(graphics.AdjustSizeForTexture(r.height)) * 2
@ -98,3 +96,18 @@ func (r *RenderTarget) ProjectionMatrix() [4][4]float64 {
func (r *RenderTarget) Dispose() { func (r *RenderTarget) Dispose() {
C.glDeleteFramebuffers(1, &r.framebuffer) 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)
}