mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-27 04:08:53 +01:00
Refactoring
This commit is contained in:
parent
24430719ff
commit
2d6818c81e
@ -3,7 +3,6 @@ package opengl
|
|||||||
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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
@ -19,7 +18,7 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
|
|||||||
ids: ids,
|
ids: ids,
|
||||||
screenScale: screenScale,
|
screenScale: screenScale,
|
||||||
}
|
}
|
||||||
mainRenderTarget := rendertarget.NewWithCurrentFramebuffer(
|
mainRenderTarget := newRTWithCurrentFramebuffer(
|
||||||
screenWidth*screenScale,
|
screenWidth*screenScale,
|
||||||
screenHeight*screenScale)
|
screenHeight*screenScale)
|
||||||
context.mainId = context.ids.AddRenderTarget(mainRenderTarget)
|
context.mainId = context.ids.AddRenderTarget(mainRenderTarget)
|
||||||
|
@ -3,24 +3,22 @@ package opengl
|
|||||||
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/texture"
|
|
||||||
"image"
|
"image"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ids struct {
|
type ids struct {
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
textures map[graphics.TextureId]*texture.Texture
|
textures map[graphics.TextureId]*Texture
|
||||||
renderTargets map[graphics.RenderTargetId]*rendertarget.RenderTarget
|
renderTargets map[graphics.RenderTargetId]*RenderTarget
|
||||||
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
||||||
counts chan int
|
counts chan int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newIds() *ids {
|
func newIds() *ids {
|
||||||
ids := &ids{
|
ids := &ids{
|
||||||
textures: map[graphics.TextureId]*texture.Texture{},
|
textures: map[graphics.TextureId]*Texture{},
|
||||||
renderTargets: map[graphics.RenderTargetId]*rendertarget.RenderTarget{},
|
renderTargets: map[graphics.RenderTargetId]*RenderTarget{},
|
||||||
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
||||||
counts: make(chan int),
|
counts: make(chan int),
|
||||||
}
|
}
|
||||||
@ -32,13 +30,13 @@ func newIds() *ids {
|
|||||||
return ids
|
return ids
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *ids) textureAt(id graphics.TextureId) *texture.Texture {
|
func (i *ids) textureAt(id graphics.TextureId) *Texture {
|
||||||
i.lock.RLock()
|
i.lock.RLock()
|
||||||
defer i.lock.RUnlock()
|
defer i.lock.RUnlock()
|
||||||
return i.textures[id]
|
return i.textures[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *ids) renderTargetAt(id graphics.RenderTargetId) *rendertarget.RenderTarget {
|
func (i *ids) renderTargetAt(id graphics.RenderTargetId) *RenderTarget {
|
||||||
i.lock.RLock()
|
i.lock.RLock()
|
||||||
defer i.lock.RUnlock()
|
defer i.lock.RUnlock()
|
||||||
return i.renderTargets[id]
|
return i.renderTargets[id]
|
||||||
@ -52,7 +50,7 @@ func (i *ids) toTexture(id graphics.RenderTargetId) graphics.TextureId {
|
|||||||
|
|
||||||
func (i *ids) CreateTexture(img image.Image, filter graphics.Filter) (
|
func (i *ids) CreateTexture(img image.Image, filter graphics.Filter) (
|
||||||
graphics.TextureId, error) {
|
graphics.TextureId, error) {
|
||||||
texture, err := texture.CreateFromImage(img, filter)
|
texture, err := createTextureFromImage(img, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -67,11 +65,12 @@ 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) {
|
||||||
|
|
||||||
texture, err := texture.Create(width, height, filter)
|
texture, err := createTexture(width, height, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
renderTarget := texture.CreateRenderTarget()
|
framebuffer := createFramebuffer(texture.native)
|
||||||
|
renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false}
|
||||||
|
|
||||||
textureId := graphics.TextureId(<-i.counts)
|
textureId := graphics.TextureId(<-i.counts)
|
||||||
renderTargetId := graphics.RenderTargetId(<-i.counts)
|
renderTargetId := graphics.RenderTargetId(<-i.counts)
|
||||||
@ -86,7 +85,7 @@ func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: renderTarget can't be used as a texture.
|
// NOTE: renderTarget can't be used as a texture.
|
||||||
func (i *ids) AddRenderTarget(renderTarget *rendertarget.RenderTarget) graphics.RenderTargetId {
|
func (i *ids) AddRenderTarget(renderTarget *RenderTarget) graphics.RenderTargetId {
|
||||||
id := graphics.RenderTargetId(<-i.counts)
|
id := graphics.RenderTargetId(<-i.counts)
|
||||||
|
|
||||||
i.lock.Lock()
|
i.lock.Lock()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package rendertarget
|
package opengl
|
||||||
|
|
||||||
// #cgo LDFLAGS: -framework OpenGL
|
// #cgo LDFLAGS: -framework OpenGL
|
||||||
//
|
//
|
||||||
@ -11,15 +11,6 @@ import (
|
|||||||
"math"
|
"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 {
|
type RenderTarget struct {
|
||||||
framebuffer C.GLuint
|
framebuffer C.GLuint
|
||||||
width int
|
width int
|
||||||
@ -27,7 +18,7 @@ type RenderTarget struct {
|
|||||||
flipY bool
|
flipY bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWithCurrentFramebuffer(width, height int) *RenderTarget {
|
func newRTWithCurrentFramebuffer(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)
|
||||||
return &RenderTarget{C.GLuint(framebuffer), width, height, true}
|
return &RenderTarget{C.GLuint(framebuffer), width, height, true}
|
||||||
@ -58,11 +49,6 @@ func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
|
|||||||
return framebuffer
|
return framebuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateFromTexture(native NativeTexture, width, height int) *RenderTarget {
|
|
||||||
framebuffer := createFramebuffer(C.GLuint(native))
|
|
||||||
return &RenderTarget{framebuffer, width, height, false}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RenderTarget) setAsViewport() {
|
func (r *RenderTarget) setAsViewport() {
|
||||||
current := C.GLint(0)
|
current := C.GLint(0)
|
||||||
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, ¤t)
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, ¤t)
|
||||||
@ -113,14 +99,14 @@ func (r *RenderTarget) Fill(red, green, blue uint8) {
|
|||||||
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RenderTarget) DrawTexture(texture Texture,
|
func (r *RenderTarget) DrawTexture(texture *Texture,
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
r.setAsViewport()
|
r.setAsViewport()
|
||||||
projectionMatrix := r.projectionMatrix()
|
projectionMatrix := r.projectionMatrix()
|
||||||
texture.Draw(projectionMatrix, geometryMatrix, colorMatrix)
|
texture.Draw(projectionMatrix, geometryMatrix, colorMatrix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RenderTarget) DrawTextureParts(texture Texture,
|
func (r *RenderTarget) DrawTextureParts(texture *Texture,
|
||||||
parts []graphics.TexturePart,
|
parts []graphics.TexturePart,
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
r.setAsViewport()
|
r.setAsViewport()
|
@ -1,4 +1,4 @@
|
|||||||
package texture
|
package opengl
|
||||||
|
|
||||||
// #cgo LDFLAGS: -framework OpenGL
|
// #cgo LDFLAGS: -framework OpenGL
|
||||||
//
|
//
|
||||||
@ -7,7 +7,6 @@ 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"
|
||||||
@ -64,25 +63,20 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
|
|||||||
return nativeTexture
|
return nativeTexture
|
||||||
}
|
}
|
||||||
|
|
||||||
func Create(width, height int, filter graphics.Filter) (*Texture, error) {
|
func createTexture(width, height int, filter graphics.Filter) (*Texture, error) {
|
||||||
native := createNativeTexture(
|
native := createNativeTexture(
|
||||||
graphics.AdjustSizeForTexture(width),
|
graphics.AdjustSizeForTexture(width),
|
||||||
graphics.AdjustSizeForTexture(height), nil, filter)
|
graphics.AdjustSizeForTexture(height), nil, filter)
|
||||||
return &Texture{native, width, height}, nil
|
return &Texture{native, width, height}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateFromImage(img image.Image, filter graphics.Filter) (*Texture, error) {
|
func createTextureFromImage(img image.Image, filter graphics.Filter) (*Texture, error) {
|
||||||
adjustedImage := graphics.AdjustImageForTexture(img)
|
adjustedImage := graphics.AdjustImageForTexture(img)
|
||||||
size := adjustedImage.Bounds().Size()
|
size := adjustedImage.Bounds().Size()
|
||||||
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 (t *Texture) CreateRenderTarget() *rendertarget.RenderTarget {
|
|
||||||
return rendertarget.CreateFromTexture(
|
|
||||||
rendertarget.NativeTexture(t.native), t.width, t.height)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Texture) Draw(projectionMatrix [4][4]float64, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
func (t *Texture) Draw(projectionMatrix [4][4]float64, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
quad := graphics.TextureQuadForTexture(t.width, t.height)
|
quad := graphics.TextureQuadForTexture(t.width, t.height)
|
||||||
shader.DrawTexture(shader.NativeTexture(t.native),
|
shader.DrawTexture(shader.NativeTexture(t.native),
|
Loading…
Reference in New Issue
Block a user