mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-20 15:00:08 +01:00
Refactoring
This commit is contained in:
parent
15c76624f1
commit
70723e1211
@ -36,38 +36,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
screen graphics.Texture
|
screen *Texture
|
||||||
screenScale int
|
screenScale int
|
||||||
textures map[graphics.TextureID]*Texture
|
textures map[graphics.TextureID]*Texture
|
||||||
currentOffscreenWidth int
|
currentOffscreen *Texture
|
||||||
currentOffscreenHeight int
|
|
||||||
projectionMatrix [16]float32
|
projectionMatrix [16]float32
|
||||||
currentShaderProgram C.GLuint
|
currentShaderProgram C.GLuint
|
||||||
mainFramebuffer C.GLuint
|
|
||||||
mainFramebufferTexture *Texture
|
mainFramebufferTexture *Texture
|
||||||
framebuffers map[C.GLuint]C.GLuint
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method should be called on the UI thread.
|
// This method should be called on the UI thread.
|
||||||
func newContext(screenWidth, screenHeight, screenScale int) *Context {
|
func newContext(screenWidth, screenHeight, screenScale int) *Context {
|
||||||
context := &Context{
|
context := &Context{
|
||||||
screenScale: screenScale,
|
screenScale: screenScale,
|
||||||
textures: map[graphics.TextureID]*Texture{},
|
textures: map[graphics.TextureID]*Texture{},
|
||||||
mainFramebuffer: 0,
|
|
||||||
framebuffers: map[C.GLuint]C.GLuint{},
|
|
||||||
}
|
}
|
||||||
// main framebuffer should be created sooner than any other framebuffers!
|
// main framebuffer should be created sooner than any other framebuffers!
|
||||||
mainFramebuffer := C.GLint(0)
|
mainFramebuffer := C.GLint(0)
|
||||||
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
|
||||||
context.mainFramebuffer = C.GLuint(mainFramebuffer)
|
|
||||||
|
|
||||||
context.mainFramebufferTexture = newVirtualTexture(
|
context.mainFramebufferTexture = newVirtualTexture(
|
||||||
screenWidth * screenScale,
|
screenWidth*screenScale,
|
||||||
screenHeight * screenScale)
|
screenHeight*screenScale)
|
||||||
|
context.mainFramebufferTexture.framebuffer = C.GLuint(mainFramebuffer)
|
||||||
|
|
||||||
initializeShaders()
|
initializeShaders()
|
||||||
|
|
||||||
context.screen = context.NewTexture(screenWidth, screenHeight)
|
context.screen = context.NewTexture(screenWidth, screenHeight).(*Texture)
|
||||||
|
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
@ -93,8 +88,8 @@ func (context *Context) Fill(clr color.Color) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (context *Context) DrawRect(rect graphics.Rect, clr color.Color) {
|
func (context *Context) DrawRect(rect graphics.Rect, clr color.Color) {
|
||||||
width := float32(context.currentOffscreenWidth)
|
width := float32(context.currentOffscreen.Width())
|
||||||
height := float32(context.currentOffscreenHeight)
|
height := float32(context.currentOffscreen.Height())
|
||||||
textureWidth := float32(clp2(uint64(width)))
|
textureWidth := float32(clp2(uint64(width)))
|
||||||
textureHeight := float32(clp2(uint64(height)))
|
textureHeight := float32(clp2(uint64(height)))
|
||||||
|
|
||||||
@ -214,47 +209,38 @@ func abs(x int) int {
|
|||||||
|
|
||||||
func (context *Context) SetOffscreen(textureID graphics.TextureID) {
|
func (context *Context) SetOffscreen(textureID graphics.TextureID) {
|
||||||
texture := context.textures[textureID]
|
texture := context.textures[textureID]
|
||||||
context.currentOffscreenWidth = texture.width
|
if texture.framebuffer == 0 {
|
||||||
context.currentOffscreenHeight = texture.height
|
texture.framebuffer = createFramebuffer(texture.id)
|
||||||
|
|
||||||
framebuffer := context.getFramebuffer(texture.id)
|
|
||||||
if framebuffer == context.mainFramebuffer {
|
|
||||||
panic("invalid framebuffer")
|
|
||||||
}
|
}
|
||||||
context.setOffscreenFramebuffer(framebuffer,
|
context.setOffscreen(texture)
|
||||||
texture.textureWidth, texture.textureHeight)
|
context.currentOffscreen = texture
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *Context) setOffscreenFramebuffer(framebuffer C.GLuint,
|
func (context *Context) setOffscreen(texture *Texture) {
|
||||||
textureWidth, textureHeight int) {
|
|
||||||
if framebuffer == context.mainFramebuffer {
|
|
||||||
textureWidth = context.mainFramebufferTexture.textureWidth
|
|
||||||
textureHeight = context.mainFramebufferTexture.textureHeight
|
|
||||||
}
|
|
||||||
|
|
||||||
C.glFlush()
|
C.glFlush()
|
||||||
|
|
||||||
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, texture.framebuffer)
|
||||||
if err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER); err != C.GL_FRAMEBUFFER_COMPLETE {
|
if err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER); err != C.GL_FRAMEBUFFER_COMPLETE {
|
||||||
panic(fmt.Sprintf("glBindFramebuffer failed: %d", err))
|
panic(fmt.Sprintf("glBindFramebuffer failed: %d", err))
|
||||||
}
|
}
|
||||||
C.glEnable(C.GL_BLEND)
|
C.glEnable(C.GL_BLEND)
|
||||||
C.glBlendFunc(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA)
|
C.glBlendFunc(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA)
|
||||||
|
|
||||||
C.glViewport(0, 0, C.GLsizei(abs(textureWidth)), C.GLsizei(abs(textureHeight)))
|
C.glViewport(0, 0, C.GLsizei(abs(texture.textureWidth)),
|
||||||
|
C.GLsizei(abs(texture.textureHeight)))
|
||||||
|
|
||||||
var e11, e22, e41, e42 float32
|
var e11, e22, e41, e42 float32
|
||||||
if framebuffer != context.mainFramebuffer {
|
if texture != context.mainFramebufferTexture {
|
||||||
e11 = float32(2) / float32(textureWidth)
|
e11 = float32(2) / float32(texture.textureWidth)
|
||||||
e22 = float32(2) / float32(textureWidth)
|
e22 = float32(2) / float32(texture.textureWidth)
|
||||||
e41 = -1
|
e41 = -1
|
||||||
e42 = -1
|
e42 = -1
|
||||||
} else {
|
} else {
|
||||||
height := float32(context.mainFramebufferTexture.Height())
|
height := float32(texture.Height())
|
||||||
e11 = float32(2) / float32(textureWidth)
|
e11 = float32(2) / float32(texture.textureWidth)
|
||||||
e22 = -1 * float32(2) / float32(textureHeight)
|
e22 = -1 * float32(2) / float32(texture.textureHeight)
|
||||||
e41 = -1
|
e41 = -1
|
||||||
e42 = -1 + height/float32(textureHeight)*2
|
e42 = -1 + height/float32(texture.textureHeight)*2
|
||||||
}
|
}
|
||||||
|
|
||||||
context.projectionMatrix = [...]float32{
|
context.projectionMatrix = [...]float32{
|
||||||
@ -266,9 +252,8 @@ func (context *Context) setOffscreenFramebuffer(framebuffer C.GLuint,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (context *Context) resetOffscreen() {
|
func (context *Context) resetOffscreen() {
|
||||||
context.setOffscreenFramebuffer(context.mainFramebuffer, 0, 0)
|
context.setOffscreen(context.mainFramebufferTexture)
|
||||||
context.currentOffscreenWidth = context.mainFramebufferTexture.Width()
|
context.currentOffscreen = context.mainFramebufferTexture
|
||||||
context.currentOffscreenHeight = context.mainFramebufferTexture.Height()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method should be called on the UI thread.
|
// This method should be called on the UI thread.
|
||||||
@ -338,18 +323,13 @@ func (context *Context) setShaderProgram(
|
|||||||
1, (*C.GLfloat)(&glColorMatrixTranslation[0]))
|
1, (*C.GLfloat)(&glColorMatrixTranslation[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *Context) getFramebuffer(textureID C.GLuint) C.GLuint {
|
func createFramebuffer(textureID C.GLuint) C.GLuint {
|
||||||
framebuffer, ok := context.framebuffers[textureID]
|
framebuffer := C.GLuint(0)
|
||||||
if ok {
|
C.glGenFramebuffers(1, &framebuffer)
|
||||||
return framebuffer
|
|
||||||
}
|
|
||||||
|
|
||||||
newFramebuffer := C.GLuint(0)
|
|
||||||
C.glGenFramebuffers(1, &newFramebuffer)
|
|
||||||
|
|
||||||
origFramebuffer := C.GLint(0)
|
origFramebuffer := C.GLint(0)
|
||||||
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer)
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer)
|
||||||
C.glBindFramebuffer(C.GL_FRAMEBUFFER, newFramebuffer)
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
|
||||||
C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0,
|
C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0,
|
||||||
C.GL_TEXTURE_2D, textureID, 0)
|
C.GL_TEXTURE_2D, textureID, 0)
|
||||||
C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
|
||||||
@ -357,18 +337,7 @@ func (context *Context) getFramebuffer(textureID C.GLuint) C.GLuint {
|
|||||||
panic("creating framebuffer failed")
|
panic("creating framebuffer failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
context.framebuffers[textureID] = newFramebuffer
|
return framebuffer
|
||||||
return newFramebuffer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) deleteFramebuffer(textureID C.GLuint) {
|
|
||||||
framebuffer, ok := context.framebuffers[textureID]
|
|
||||||
if !ok {
|
|
||||||
// TODO: panic?
|
|
||||||
return
|
|
||||||
}
|
|
||||||
C.glDeleteFramebuffers(1, &framebuffer)
|
|
||||||
delete(context.framebuffers, textureID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *Context) NewTexture(width, height int) graphics.Texture {
|
func (context *Context) NewTexture(width, height int) graphics.Texture {
|
||||||
|
@ -63,6 +63,7 @@ type Texture struct {
|
|||||||
height int
|
height int
|
||||||
textureWidth int
|
textureWidth int
|
||||||
textureHeight int
|
textureHeight int
|
||||||
|
framebuffer C.GLuint
|
||||||
isVirtual bool
|
isVirtual bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user