mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-13 04:22:05 +01:00
Refactoring
This commit is contained in:
parent
cd6db439e4
commit
92b2e0d948
@ -65,17 +65,20 @@ func (i *ids) CreateTexture(img image.Image, filter graphics.Filter) (
|
|||||||
|
|
||||||
func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
|
func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
|
||||||
graphics.RenderTargetId, error) {
|
graphics.RenderTargetId, error) {
|
||||||
renderTarget, texture, err := rendertarget.Create(width, height, filter)
|
|
||||||
|
texture, err := texture.Create(width, height, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
renderTargetId := graphics.RenderTargetId(<-i.counts)
|
renderTarget := texture.CreateRenderTarget()
|
||||||
|
|
||||||
textureId := graphics.TextureId(<-i.counts)
|
textureId := graphics.TextureId(<-i.counts)
|
||||||
|
renderTargetId := graphics.RenderTargetId(<-i.counts)
|
||||||
|
|
||||||
i.lock.Lock()
|
i.lock.Lock()
|
||||||
defer i.lock.Unlock()
|
defer i.lock.Unlock()
|
||||||
i.renderTargets[renderTargetId] = renderTarget
|
|
||||||
i.textures[textureId] = texture
|
i.textures[textureId] = texture
|
||||||
|
i.renderTargets[renderTargetId] = renderTarget
|
||||||
i.renderTargetToTexture[renderTargetId] = textureId
|
i.renderTargetToTexture[renderTargetId] = textureId
|
||||||
|
|
||||||
return renderTargetId, nil
|
return renderTargetId, nil
|
||||||
@ -84,5 +87,11 @@ func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
|
|||||||
func (i *ids) DeleteRenderTarget(id graphics.RenderTargetId) {
|
func (i *ids) DeleteRenderTarget(id graphics.RenderTargetId) {
|
||||||
renderTarget := i.renderTargets[id]
|
renderTarget := i.renderTargets[id]
|
||||||
renderTarget.Dispose()
|
renderTarget.Dispose()
|
||||||
|
|
||||||
|
i.lock.Lock()
|
||||||
|
defer i.lock.Unlock()
|
||||||
delete(i.renderTargets, id)
|
delete(i.renderTargets, id)
|
||||||
|
|
||||||
|
// TODO: Remove the related texture
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
package offscreen
|
package offscreen
|
||||||
|
|
||||||
// #cgo LDFLAGS: -framework OpenGL
|
|
||||||
//
|
|
||||||
// #include <stdlib.h>
|
|
||||||
// #include <OpenGL/gl.h>
|
|
||||||
import "C"
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
||||||
@ -25,24 +20,14 @@ func New(screenWidth, screenHeight, screenScale int) *Offscreen {
|
|||||||
screenScale: screenScale,
|
screenScale: screenScale,
|
||||||
}
|
}
|
||||||
|
|
||||||
mainFramebuffer := C.GLint(0)
|
offscreen.mainFramebufferTexture = rendertarget.NewWithCurrentFramebuffer(
|
||||||
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
|
|
||||||
|
|
||||||
var err error
|
|
||||||
offscreen.mainFramebufferTexture, err = rendertarget.CreateWithFramebuffer(
|
|
||||||
screenWidth*screenScale,
|
screenWidth*screenScale,
|
||||||
screenHeight*screenScale,
|
screenHeight*screenScale)
|
||||||
texture.Framebuffer(mainFramebuffer))
|
|
||||||
if err != nil {
|
|
||||||
panic("creating main framebuffer failed: " + err.Error())
|
|
||||||
}
|
|
||||||
offscreen.currentRenderTarget = offscreen.mainFramebufferTexture
|
|
||||||
|
|
||||||
return offscreen
|
return offscreen
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Offscreen) Set(rt *rendertarget.RenderTarget) {
|
func (o *Offscreen) Set(rt *rendertarget.RenderTarget) {
|
||||||
C.glFlush()
|
|
||||||
o.currentRenderTarget = rt
|
o.currentRenderTarget = rt
|
||||||
rt.SetAsViewport()
|
rt.SetAsViewport()
|
||||||
}
|
}
|
||||||
|
@ -7,31 +7,55 @@ import "C"
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type NativeTexture C.GLuint
|
||||||
|
|
||||||
type RenderTarget struct {
|
type RenderTarget struct {
|
||||||
framebuffer texture.Framebuffer
|
framebuffer C.GLuint
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
}
|
}
|
||||||
|
|
||||||
func Create(width, height int, filter graphics.Filter) (
|
func NewWithCurrentFramebuffer(width, height int) *RenderTarget {
|
||||||
*RenderTarget, *texture.Texture, error) {
|
framebuffer := C.GLint(0)
|
||||||
tex, err := texture.Create(width, height, filter)
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &framebuffer)
|
||||||
if err != nil {
|
return &RenderTarget{C.GLuint(framebuffer), width, height}
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
framebuffer := tex.CreateFramebuffer()
|
|
||||||
return &RenderTarget{framebuffer, width, height}, tex, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateWithFramebuffer(width, height int, framebuffer texture.Framebuffer) (
|
func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
|
||||||
*RenderTarget, error) {
|
framebuffer := C.GLuint(0)
|
||||||
return &RenderTarget{framebuffer, width, height}, nil
|
C.glGenFramebuffers(1, &framebuffer)
|
||||||
|
|
||||||
|
origFramebuffer := C.GLint(0)
|
||||||
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer)
|
||||||
|
|
||||||
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
|
||||||
|
defer C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
|
||||||
|
|
||||||
|
C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0,
|
||||||
|
C.GL_TEXTURE_2D, nativeTexture, 0)
|
||||||
|
if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) !=
|
||||||
|
C.GL_FRAMEBUFFER_COMPLETE {
|
||||||
|
panic("creating framebuffer failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set this framebuffer opaque because alpha values on a target might be
|
||||||
|
// confusing.
|
||||||
|
C.glClearColor(0, 0, 0, 1)
|
||||||
|
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
||||||
|
|
||||||
|
return framebuffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateFromTexture(native NativeTexture, width, height int) *RenderTarget {
|
||||||
|
framebuffer := createFramebuffer(C.GLuint(native))
|
||||||
|
return &RenderTarget{framebuffer, width, height}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RenderTarget) SetAsViewport() {
|
func (r *RenderTarget) SetAsViewport() {
|
||||||
|
C.glFlush()
|
||||||
|
|
||||||
C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(r.framebuffer))
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(r.framebuffer))
|
||||||
err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER)
|
err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER)
|
||||||
if err != C.GL_FRAMEBUFFER_COMPLETE {
|
if err != C.GL_FRAMEBUFFER_COMPLETE {
|
||||||
|
@ -7,21 +7,20 @@ import "C"
|
|||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
||||||
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/shader"
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/shader"
|
||||||
"image"
|
"image"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Framebuffer C.GLuint
|
|
||||||
|
|
||||||
type Texture struct {
|
type Texture struct {
|
||||||
native shader.NativeTexture
|
native C.GLuint
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
|
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
|
||||||
filter graphics.Filter) shader.NativeTexture {
|
filter graphics.Filter) C.GLuint {
|
||||||
nativeTexture := C.GLuint(0)
|
nativeTexture := C.GLuint(0)
|
||||||
|
|
||||||
C.glGenTextures(1, (*C.GLuint)(&nativeTexture))
|
C.glGenTextures(1, (*C.GLuint)(&nativeTexture))
|
||||||
@ -52,7 +51,7 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
|
|||||||
C.GLsizei(textureWidth), C.GLsizei(textureHeight),
|
C.GLsizei(textureWidth), C.GLsizei(textureHeight),
|
||||||
0, C.GL_RGBA, C.GL_UNSIGNED_BYTE, ptr)
|
0, C.GL_RGBA, C.GL_UNSIGNED_BYTE, ptr)
|
||||||
|
|
||||||
return shader.NativeTexture(nativeTexture)
|
return nativeTexture
|
||||||
}
|
}
|
||||||
|
|
||||||
func Create(width, height int, filter graphics.Filter) (*Texture, error) {
|
func Create(width, height int, filter graphics.Filter) (*Texture, error) {
|
||||||
@ -69,38 +68,14 @@ func CreateFromImage(img image.Image, filter graphics.Filter) (*Texture, error)
|
|||||||
return &Texture{native, size.X, size.Y}, nil
|
return &Texture{native, size.X, size.Y}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createFramebuffer(nativeTexture C.GLuint) Framebuffer {
|
func (t *Texture) CreateRenderTarget() *rendertarget.RenderTarget {
|
||||||
framebuffer := C.GLuint(0)
|
return rendertarget.CreateFromTexture(
|
||||||
C.glGenFramebuffers(1, &framebuffer)
|
rendertarget.NativeTexture(t.native), t.width, t.height)
|
||||||
|
|
||||||
origFramebuffer := C.GLint(0)
|
|
||||||
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer)
|
|
||||||
|
|
||||||
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
|
|
||||||
defer C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
|
|
||||||
|
|
||||||
C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0,
|
|
||||||
C.GL_TEXTURE_2D, nativeTexture, 0)
|
|
||||||
if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) !=
|
|
||||||
C.GL_FRAMEBUFFER_COMPLETE {
|
|
||||||
panic("creating framebuffer failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set this framebuffer opaque because alpha values on a target might be
|
|
||||||
// confusing.
|
|
||||||
C.glClearColor(0, 0, 0, 1)
|
|
||||||
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
|
||||||
|
|
||||||
return Framebuffer(framebuffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Texture) CreateFramebuffer() Framebuffer {
|
|
||||||
return createFramebuffer(C.GLuint(t.native))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Texture) Draw(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
func (t *Texture) Draw(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
quad := graphics.TextureQuadForTexture(t.width, t.height)
|
quad := graphics.TextureQuadForTexture(t.width, t.height)
|
||||||
shader.DrawTexture(t.native,
|
shader.DrawTexture(shader.NativeTexture(t.native),
|
||||||
projectionMatrix, []graphics.TextureQuad{quad},
|
projectionMatrix, []graphics.TextureQuad{quad},
|
||||||
geometryMatrix, colorMatrix)
|
geometryMatrix, colorMatrix)
|
||||||
}
|
}
|
||||||
@ -108,7 +83,7 @@ func (t *Texture) Draw(projectionMatrix [16]float32, geometryMatrix matrix.Geome
|
|||||||
func (t *Texture) DrawParts(parts []graphics.TexturePart, projectionMatrix [16]float32,
|
func (t *Texture) DrawParts(parts []graphics.TexturePart, projectionMatrix [16]float32,
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
quads := graphics.TextureQuadsForTextureParts(parts, t.width, t.height)
|
quads := graphics.TextureQuadsForTextureParts(parts, t.width, t.height)
|
||||||
shader.DrawTexture(t.native,
|
shader.DrawTexture(shader.NativeTexture(t.native),
|
||||||
projectionMatrix, quads,
|
projectionMatrix, quads,
|
||||||
geometryMatrix, colorMatrix)
|
geometryMatrix, colorMatrix)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user