mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Remove Offscreen
This commit is contained in:
parent
1772d85d58
commit
1b6bc12ead
@ -8,14 +8,14 @@ import "C"
|
||||
import (
|
||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/offscreen"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
screenId graphics.RenderTargetId
|
||||
mainId graphics.RenderTargetId
|
||||
currentId graphics.RenderTargetId
|
||||
ids *ids
|
||||
offscreen *offscreen.Offscreen
|
||||
screenScale int
|
||||
}
|
||||
|
||||
@ -25,9 +25,9 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
|
||||
screenHeight*screenScale)
|
||||
context := &Context{
|
||||
ids: ids,
|
||||
offscreen: offscreen.New(mainFramebuffer),
|
||||
screenScale: screenScale,
|
||||
}
|
||||
context.mainId = context.ids.AddRenderTarget(mainFramebuffer)
|
||||
|
||||
var err error
|
||||
context.screenId, err = ids.CreateRenderTarget(
|
||||
@ -56,7 +56,7 @@ func (context *Context) update(draw func(graphics.Context)) {
|
||||
draw(context)
|
||||
|
||||
C.glFlush()
|
||||
context.offscreen.SetMainFramebuffer()
|
||||
context.SetOffscreen(context.mainId)
|
||||
context.Clear()
|
||||
|
||||
scale := float64(context.screenScale)
|
||||
@ -72,36 +72,36 @@ func (context *Context) Clear() {
|
||||
}
|
||||
|
||||
func (context *Context) Fill(r, g, b uint8) {
|
||||
context.offscreen.Fill(r, g, b)
|
||||
context.ids.FillRenderTarget(context.currentId, r, g, b)
|
||||
}
|
||||
|
||||
func (context *Context) DrawTexture(
|
||||
id graphics.TextureId, geo matrix.Geometry, color matrix.Color) {
|
||||
context.ids.DrawTexture(id, context.offscreen, geo, color)
|
||||
context.ids.DrawTexture(context.currentId, id, geo, color)
|
||||
}
|
||||
|
||||
func (context *Context) DrawRenderTarget(
|
||||
id graphics.RenderTargetId,
|
||||
geo matrix.Geometry, color matrix.Color) {
|
||||
context.ids.DrawRenderTarget(id, context.offscreen, geo, color)
|
||||
context.ids.DrawRenderTarget(context.currentId, id, geo, color)
|
||||
}
|
||||
|
||||
func (context *Context) DrawTextureParts(
|
||||
id graphics.TextureId, parts []graphics.TexturePart,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
context.ids.DrawTextureParts(id, context.offscreen, parts, geometryMatrix, colorMatrix)
|
||||
context.ids.DrawTextureParts(context.currentId, id, parts, geometryMatrix, colorMatrix)
|
||||
}
|
||||
|
||||
func (context *Context) DrawRenderTargetParts(
|
||||
id graphics.RenderTargetId, parts []graphics.TexturePart,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
context.ids.DrawRenderTargetParts(id, context.offscreen, parts, geometryMatrix, colorMatrix)
|
||||
context.ids.DrawRenderTargetParts(context.currentId, id, parts, geometryMatrix, colorMatrix)
|
||||
}
|
||||
|
||||
func (context *Context) ResetOffscreen() {
|
||||
context.SetOffscreen(context.screenId)
|
||||
context.currentId = context.screenId
|
||||
}
|
||||
|
||||
func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
|
||||
context.ids.SetRenderTargetAsOffscreen(renderTargetId, context.offscreen)
|
||||
context.currentId = renderTargetId
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package opengl
|
||||
import (
|
||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/offscreen"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
||||
"image"
|
||||
@ -86,6 +85,17 @@ func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
|
||||
return renderTargetId, nil
|
||||
}
|
||||
|
||||
// NOTE: renderTarget can't be used as a texture.
|
||||
func (i *ids) AddRenderTarget(renderTarget *rendertarget.RenderTarget) graphics.RenderTargetId {
|
||||
id := graphics.RenderTargetId(<-i.counts)
|
||||
|
||||
i.lock.Lock()
|
||||
defer i.lock.Unlock()
|
||||
i.renderTargets[id] = renderTarget
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
func (i *ids) DeleteRenderTarget(id graphics.RenderTargetId) {
|
||||
i.lock.Lock()
|
||||
defer i.lock.Unlock()
|
||||
@ -102,28 +112,28 @@ func (i *ids) DeleteRenderTarget(id graphics.RenderTargetId) {
|
||||
delete(i.textures, textureId)
|
||||
}
|
||||
|
||||
func (i *ids) DrawTexture(id graphics.TextureId, offscreen *offscreen.Offscreen,
|
||||
func (i *ids) FillRenderTarget(id graphics.RenderTargetId, r, g, b uint8) {
|
||||
i.renderTargetAt(id).Fill(r, g, b)
|
||||
}
|
||||
|
||||
func (i *ids) DrawTexture(target graphics.RenderTargetId, id graphics.TextureId,
|
||||
geo matrix.Geometry, color matrix.Color) {
|
||||
texture := i.textureAt(id)
|
||||
offscreen.DrawTexture(texture, geo, color)
|
||||
i.renderTargetAt(target).DrawTexture(texture, geo, color)
|
||||
}
|
||||
|
||||
func (i *ids) DrawTextureParts(id graphics.TextureId, offscreen *offscreen.Offscreen,
|
||||
func (i *ids) DrawTextureParts(target graphics.RenderTargetId, id graphics.TextureId,
|
||||
parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {
|
||||
texture := i.textureAt(id)
|
||||
offscreen.DrawTextureParts(texture, parts, geo, color)
|
||||
i.renderTargetAt(target).DrawTextureParts(texture, parts, geo, color)
|
||||
}
|
||||
|
||||
func (i *ids) DrawRenderTarget(id graphics.RenderTargetId, offscreen *offscreen.Offscreen,
|
||||
func (i *ids) DrawRenderTarget(target graphics.RenderTargetId, id graphics.RenderTargetId,
|
||||
geo matrix.Geometry, color matrix.Color) {
|
||||
i.DrawTexture(i.toTexture(id), offscreen, geo, color)
|
||||
i.DrawTexture(target, i.toTexture(id), geo, color)
|
||||
}
|
||||
|
||||
func (i *ids) DrawRenderTargetParts(id graphics.RenderTargetId, offscreen *offscreen.Offscreen,
|
||||
func (i *ids) DrawRenderTargetParts(target graphics.RenderTargetId, id graphics.RenderTargetId,
|
||||
parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {
|
||||
i.DrawTextureParts(i.toTexture(id), offscreen, parts, geo, color)
|
||||
}
|
||||
|
||||
func (i *ids) SetRenderTargetAsOffscreen(id graphics.RenderTargetId, offscreen *offscreen.Offscreen) {
|
||||
offscreen.Set(i.renderTargetAt(id))
|
||||
i.DrawTextureParts(target, i.toTexture(id), parts, geo, color)
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
package offscreen
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
||||
)
|
||||
|
||||
type Texture interface {
|
||||
Draw(projectionMatrix [4][4]float64,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color)
|
||||
DrawParts(parts []graphics.TexturePart, projectionMatrix [4][4]float64,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color)
|
||||
}
|
||||
|
||||
type RenderTarget interface {
|
||||
SetAsViewport()
|
||||
ProjectionMatrix() [4][4]float64
|
||||
Clear()
|
||||
Fill(r, g, b uint8)
|
||||
}
|
||||
|
||||
type Offscreen struct {
|
||||
currentRenderTarget RenderTarget
|
||||
mainFramebuffer RenderTarget
|
||||
}
|
||||
|
||||
func New(mainFramebuffer RenderTarget) *Offscreen {
|
||||
return &Offscreen{
|
||||
mainFramebuffer: mainFramebuffer,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Offscreen) Set(rt RenderTarget) {
|
||||
o.currentRenderTarget = rt
|
||||
}
|
||||
|
||||
func (o *Offscreen) SetMainFramebuffer() {
|
||||
o.Set(o.mainFramebuffer)
|
||||
}
|
||||
|
||||
func (o *Offscreen) DrawTexture(texture Texture,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
o.currentRenderTarget.SetAsViewport()
|
||||
projectionMatrix := o.currentRenderTarget.ProjectionMatrix()
|
||||
texture.Draw(projectionMatrix, geometryMatrix, colorMatrix)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
@ -7,9 +7,17 @@ import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Texture interface {
|
||||
Draw(projectionMatrix [4][4]float64,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color)
|
||||
DrawParts(parts []graphics.TexturePart, projectionMatrix [4][4]float64,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color)
|
||||
}
|
||||
|
||||
type NativeTexture C.GLuint
|
||||
|
||||
type RenderTarget struct {
|
||||
@ -97,10 +105,6 @@ 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)
|
||||
@ -111,3 +115,18 @@ func (r *RenderTarget) Fill(red, green, blue uint8) {
|
||||
1)
|
||||
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
||||
}
|
||||
|
||||
func (r *RenderTarget) DrawTexture(texture Texture,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
r.SetAsViewport()
|
||||
projectionMatrix := r.ProjectionMatrix()
|
||||
texture.Draw(projectionMatrix, geometryMatrix, colorMatrix)
|
||||
}
|
||||
|
||||
func (r *RenderTarget) DrawTextureParts(texture Texture,
|
||||
parts []graphics.TexturePart,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
r.SetAsViewport()
|
||||
projectionMatrix := r.ProjectionMatrix()
|
||||
texture.DrawParts(parts, projectionMatrix, geometryMatrix, colorMatrix)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user