mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-13 12:32:05 +01:00
Refactoring
This commit is contained in:
parent
b60db9c2f6
commit
cd6db439e4
@ -9,7 +9,6 @@ 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/rendertarget"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/shader"
|
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,7 +32,7 @@ func New(screenWidth, screenHeight, screenScale int) *Offscreen {
|
|||||||
offscreen.mainFramebufferTexture, err = rendertarget.CreateWithFramebuffer(
|
offscreen.mainFramebufferTexture, err = rendertarget.CreateWithFramebuffer(
|
||||||
screenWidth*screenScale,
|
screenWidth*screenScale,
|
||||||
screenHeight*screenScale,
|
screenHeight*screenScale,
|
||||||
rendertarget.Framebuffer(mainFramebuffer))
|
texture.Framebuffer(mainFramebuffer))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("creating main framebuffer failed: " + err.Error())
|
panic("creating main framebuffer failed: " + err.Error())
|
||||||
}
|
}
|
||||||
@ -55,20 +54,14 @@ func (o *Offscreen) SetMainFramebuffer() {
|
|||||||
func (o *Offscreen) DrawTexture(texture *texture.Texture,
|
func (o *Offscreen) DrawTexture(texture *texture.Texture,
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
projectionMatrix := o.projectionMatrix()
|
projectionMatrix := o.projectionMatrix()
|
||||||
quad := graphics.TextureQuadForTexture(texture.Width, texture.Height)
|
texture.Draw(projectionMatrix, geometryMatrix, colorMatrix)
|
||||||
shader.DrawTexture(texture.Native,
|
|
||||||
projectionMatrix, []graphics.TextureQuad{quad},
|
|
||||||
geometryMatrix, colorMatrix)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Offscreen) DrawTextureParts(texture *texture.Texture,
|
func (o *Offscreen) DrawTextureParts(texture *texture.Texture,
|
||||||
parts []graphics.TexturePart,
|
parts []graphics.TexturePart,
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
projectionMatrix := o.projectionMatrix()
|
projectionMatrix := o.projectionMatrix()
|
||||||
quads := graphics.TextureQuadsForTextureParts(parts, texture.Width, texture.Height)
|
texture.DrawParts(parts, projectionMatrix, geometryMatrix, colorMatrix)
|
||||||
shader.DrawTexture(texture.Native,
|
|
||||||
projectionMatrix, quads,
|
|
||||||
geometryMatrix, colorMatrix)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Offscreen) projectionMatrix() [16]float32 {
|
func (o *Offscreen) projectionMatrix() [16]float32 {
|
||||||
|
@ -10,37 +10,10 @@ import (
|
|||||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Framebuffer C.GLuint
|
|
||||||
|
|
||||||
type RenderTarget struct {
|
type RenderTarget struct {
|
||||||
framebuffer Framebuffer
|
framebuffer texture.Framebuffer
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
}
|
|
||||||
|
|
||||||
func createFramebuffer(nativeTexture C.GLuint) Framebuffer {
|
|
||||||
framebuffer := C.GLuint(0)
|
|
||||||
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(framebuffer)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Create(width, height int, filter graphics.Filter) (
|
func Create(width, height int, filter graphics.Filter) (
|
||||||
@ -49,11 +22,11 @@ func Create(width, height int, filter graphics.Filter) (
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
framebuffer := createFramebuffer(C.GLuint(tex.Native))
|
framebuffer := tex.CreateFramebuffer()
|
||||||
return &RenderTarget{framebuffer, tex.Width, tex.Height}, tex, nil
|
return &RenderTarget{framebuffer, width, height}, tex, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateWithFramebuffer(width, height int, framebuffer Framebuffer) (
|
func CreateWithFramebuffer(width, height int, framebuffer texture.Framebuffer) (
|
||||||
*RenderTarget, error) {
|
*RenderTarget, error) {
|
||||||
return &RenderTarget{framebuffer, width, height}, nil
|
return &RenderTarget{framebuffer, width, height}, nil
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,16 @@ 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/texture"
|
|
||||||
"sync"
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type NativeTexture C.GLuint
|
||||||
|
|
||||||
var once sync.Once
|
var once sync.Once
|
||||||
|
|
||||||
func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []graphics.TextureQuad, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
func DrawTexture(native NativeTexture, projectionMatrix [16]float32,
|
||||||
|
quads []graphics.TextureQuad, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
initialize()
|
initialize()
|
||||||
})
|
})
|
||||||
|
@ -6,20 +6,22 @@ package texture
|
|||||||
import "C"
|
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/opengl/shader"
|
||||||
"image"
|
"image"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Native C.GLuint
|
type Framebuffer C.GLuint
|
||||||
|
|
||||||
type Texture struct {
|
type Texture struct {
|
||||||
Native
|
native shader.NativeTexture
|
||||||
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) Native {
|
filter graphics.Filter) shader.NativeTexture {
|
||||||
nativeTexture := C.GLuint(0)
|
nativeTexture := C.GLuint(0)
|
||||||
|
|
||||||
C.glGenTextures(1, (*C.GLuint)(&nativeTexture))
|
C.glGenTextures(1, (*C.GLuint)(&nativeTexture))
|
||||||
@ -50,7 +52,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 Native(nativeTexture)
|
return shader.NativeTexture(nativeTexture)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Create(width, height int, filter graphics.Filter) (*Texture, error) {
|
func Create(width, height int, filter graphics.Filter) (*Texture, error) {
|
||||||
@ -66,3 +68,47 @@ func CreateFromImage(img image.Image, filter graphics.Filter) (*Texture, error)
|
|||||||
native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter)
|
native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter)
|
||||||
return &Texture{native, size.X, size.Y}, nil
|
return &Texture{native, size.X, size.Y}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createFramebuffer(nativeTexture C.GLuint) Framebuffer {
|
||||||
|
framebuffer := C.GLuint(0)
|
||||||
|
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(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) {
|
||||||
|
quad := graphics.TextureQuadForTexture(t.width, t.height)
|
||||||
|
shader.DrawTexture(t.native,
|
||||||
|
projectionMatrix, []graphics.TextureQuad{quad},
|
||||||
|
geometryMatrix, colorMatrix)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Texture) DrawParts(parts []graphics.TexturePart, projectionMatrix [16]float32,
|
||||||
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
|
quads := graphics.TextureQuadsForTextureParts(parts, t.width, t.height)
|
||||||
|
shader.DrawTexture(t.native,
|
||||||
|
projectionMatrix, quads,
|
||||||
|
geometryMatrix, colorMatrix)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user