mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
opengl: Move ensureFramebuffer to opengl.Image
This commit is contained in:
parent
95a925ec9a
commit
2a7caf7755
@ -228,21 +228,18 @@ func (c *drawImageCommand) String() string {
|
||||
|
||||
// Exec executes the drawImageCommand.
|
||||
func (c *drawImageCommand) Exec(indexOffsetInBytes int) error {
|
||||
f, err := c.dst.ensureFramebuffer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// On some environments, viewport size must be within the framebuffer size.
|
||||
// e.g. Edge (#71), Chrome on GPD Pocket (#420), macOS Mojave (#691).
|
||||
// Use the same size of the framebuffer here.
|
||||
opengl.GetContext().SetViewport(f)
|
||||
if err := c.dst.image.SetViewport(); err != nil {
|
||||
return err
|
||||
}
|
||||
opengl.GetContext().BlendFunc(c.mode)
|
||||
|
||||
if c.nindices == 0 {
|
||||
return nil
|
||||
}
|
||||
proj := f.ProjectionMatrix()
|
||||
proj := c.dst.image.ProjectionMatrix()
|
||||
dw, dh := c.dst.Size()
|
||||
sw, sh := c.src.Size()
|
||||
opengl.UseProgram(proj, c.src.image, dw, dh, sw, sh, c.color, c.filter)
|
||||
@ -340,12 +337,7 @@ type pixelsCommand struct {
|
||||
|
||||
// Exec executes a pixelsCommand.
|
||||
func (c *pixelsCommand) Exec(indexOffsetInBytes int) error {
|
||||
f, err := c.img.ensureFramebuffer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w, h := c.img.Size()
|
||||
p, err := opengl.GetContext().FramebufferPixels(f, w, h)
|
||||
p, err := c.img.image.Pixels()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ package graphicscommand
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||
"github.com/hajimehoshi/ebiten/internal/math"
|
||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||
)
|
||||
|
||||
@ -115,16 +114,3 @@ func (i *Image) ReplacePixels(p []byte, x, y, width, height int) {
|
||||
func (i *Image) IsInvalidated() bool {
|
||||
return i.image.IsInvalidated()
|
||||
}
|
||||
|
||||
func (i *Image) ensureFramebuffer() (*opengl.Framebuffer, error) {
|
||||
if i.image.Framebuffer != nil {
|
||||
return i.image.Framebuffer, nil
|
||||
}
|
||||
w, h := i.image.Size()
|
||||
f, err := opengl.NewFramebufferFromTexture(i.image.Texture, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
i.image.Framebuffer = f
|
||||
return i.image.Framebuffer, nil
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ func (c *Context) bindFramebuffer(f framebufferNative) {
|
||||
c.lastFramebuffer = f
|
||||
}
|
||||
|
||||
func (c *Context) SetViewport(f *Framebuffer) {
|
||||
func (c *Context) setViewport(f *Framebuffer) {
|
||||
c.bindFramebuffer(f.native)
|
||||
if c.lastViewportWidth != f.width || c.lastViewportHeight != f.height {
|
||||
c.setViewportImpl(f.width, f.height)
|
||||
|
@ -46,12 +46,12 @@ func NewScreenFramebuffer(width, height int) *Framebuffer {
|
||||
}
|
||||
}
|
||||
|
||||
// ProjectionMatrix returns a projection matrix of the framebuffer.
|
||||
// projectionMatrix returns a projection matrix of the framebuffer.
|
||||
//
|
||||
// A projection matrix converts the coodinates on the framebuffer
|
||||
// (0, 0) - (viewport width, viewport height)
|
||||
// to the normalized device coodinates (-1, -1) - (1, 1) with adjustment.
|
||||
func (f *Framebuffer) ProjectionMatrix() []float32 {
|
||||
func (f *Framebuffer) projectionMatrix() []float32 {
|
||||
if f.proMatrix != nil {
|
||||
return f.proMatrix
|
||||
}
|
||||
@ -59,7 +59,7 @@ func (f *Framebuffer) ProjectionMatrix() []float32 {
|
||||
return f.proMatrix
|
||||
}
|
||||
|
||||
func (f *Framebuffer) Delete() {
|
||||
func (f *Framebuffer) delete() {
|
||||
if f.native != theContext.getScreenFramebuffer() {
|
||||
theContext.deleteFramebuffer(f.native)
|
||||
}
|
||||
|
@ -14,6 +14,10 @@
|
||||
|
||||
package opengl
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/math"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
Texture Texture
|
||||
Framebuffer *Framebuffer
|
||||
@ -38,9 +42,49 @@ func (i *Image) IsInvalidated() bool {
|
||||
|
||||
func (i *Image) Delete() {
|
||||
if i.Framebuffer != nil {
|
||||
i.Framebuffer.Delete()
|
||||
i.Framebuffer.delete()
|
||||
}
|
||||
if i.Texture != *new(Texture) {
|
||||
theContext.deleteTexture(i.Texture)
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Image) SetViewport() error {
|
||||
if err := i.ensureFramebuffer(); err != nil {
|
||||
return err
|
||||
}
|
||||
theContext.setViewport(i.Framebuffer)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Image) Pixels() ([]byte, error) {
|
||||
if err := i.ensureFramebuffer(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w, h := i.Size()
|
||||
p, err := theContext.FramebufferPixels(i.Framebuffer, w, h)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (i *Image) ProjectionMatrix() []float32 {
|
||||
if i.Framebuffer == nil {
|
||||
panic("not reached")
|
||||
}
|
||||
return i.Framebuffer.projectionMatrix()
|
||||
}
|
||||
|
||||
func (i *Image) ensureFramebuffer() error {
|
||||
if i.Framebuffer != nil {
|
||||
return nil
|
||||
}
|
||||
w, h := i.Size()
|
||||
f, err := NewFramebufferFromTexture(i.Texture, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.Framebuffer = f
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user