mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Refactoring
This commit is contained in:
parent
cfff7bf081
commit
d570a397c5
@ -23,29 +23,26 @@ type Context struct {
|
||||
screenId graphics.RenderTargetId
|
||||
defaultId graphics.RenderTargetId
|
||||
currentId graphics.RenderTargetId
|
||||
ids *ids
|
||||
screenWidth int
|
||||
screenHeight int
|
||||
screenScale int
|
||||
}
|
||||
|
||||
func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
|
||||
func NewContext(screenWidth, screenHeight, screenScale int) *Context {
|
||||
context := &Context{
|
||||
ids: ids,
|
||||
screenWidth: screenWidth,
|
||||
screenHeight: screenHeight,
|
||||
screenScale: screenScale,
|
||||
}
|
||||
defaultRenderTarget := &RenderTarget{
|
||||
framebuffer: C.GLuint(0),
|
||||
width: screenWidth * screenScale,
|
||||
height: screenHeight * screenScale,
|
||||
flipY: true,
|
||||
}
|
||||
context.defaultId = context.ids.addRenderTarget(defaultRenderTarget)
|
||||
context.defaultId = idsInstance.addRenderTarget(defaultRenderTarget)
|
||||
|
||||
var err error
|
||||
context.screenId, err = ids.createRenderTarget(
|
||||
context.screenId, err = idsInstance.createRenderTarget(
|
||||
screenWidth, screenHeight, graphics.FilterNearest)
|
||||
if err != nil {
|
||||
panic("initializing the offscreen failed: " + err.Error())
|
||||
@ -60,7 +57,7 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
|
||||
|
||||
func (c *Context) Dispose() {
|
||||
// TODO: remove the default framebuffer?
|
||||
c.ids.deleteRenderTarget(c.screenId)
|
||||
idsInstance.deleteRenderTarget(c.screenId)
|
||||
}
|
||||
|
||||
func (c *Context) Update(draw func(graphics.Context)) {
|
||||
@ -90,7 +87,7 @@ func (c *Context) Clear() {
|
||||
}
|
||||
|
||||
func (c *Context) Fill(r, g, b uint8) {
|
||||
c.ids.fillRenderTarget(c.currentId, r, g, b)
|
||||
idsInstance.fillRenderTarget(c.currentId, r, g, b)
|
||||
}
|
||||
|
||||
func (c *Context) Texture(id graphics.TextureId) graphics.Drawer {
|
||||
@ -98,7 +95,7 @@ func (c *Context) Texture(id graphics.TextureId) graphics.Drawer {
|
||||
}
|
||||
|
||||
func (c *Context) RenderTarget(id graphics.RenderTargetId) graphics.Drawer {
|
||||
return &textureWithContext{c.ids.toTexture(id), c}
|
||||
return &textureWithContext{idsInstance.toTexture(id), c}
|
||||
}
|
||||
|
||||
func (c *Context) ResetOffscreen() {
|
||||
@ -118,5 +115,5 @@ func (t *textureWithContext) Draw(
|
||||
parts []graphics.TexturePart,
|
||||
geo matrix.Geometry,
|
||||
color matrix.Color) {
|
||||
t.context.ids.drawTexture(t.context.currentId, t.id, parts, geo, color)
|
||||
idsInstance.drawTexture(t.context.currentId, t.id, parts, geo, color)
|
||||
}
|
||||
|
@ -13,10 +13,30 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var idsInstance *ids = newIds()
|
||||
func glMatrix(matrix [4][4]float64) [16]float32 {
|
||||
result := [16]float32{}
|
||||
for j := 0; j < 4; j++ {
|
||||
for i := 0; i < 4; i++ {
|
||||
result[i+j*4] = float32(matrix[i][j])
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func NewContext(screenWidth, screenHeight, screenScale int) *Context {
|
||||
return newContext(idsInstance, screenWidth, screenHeight, screenScale)
|
||||
type ids struct {
|
||||
textures map[graphics.TextureId]*Texture
|
||||
renderTargets map[graphics.RenderTargetId]*RenderTarget
|
||||
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
||||
lastId int
|
||||
currentRenderTargetId graphics.RenderTargetId
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
var idsInstance = &ids{
|
||||
textures: map[graphics.TextureId]*Texture{},
|
||||
renderTargets: map[graphics.RenderTargetId]*RenderTarget{},
|
||||
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
||||
currentRenderTargetId: -1,
|
||||
}
|
||||
|
||||
func CreateRenderTarget(
|
||||
@ -31,26 +51,6 @@ func CreateTexture(
|
||||
return idsInstance.createTexture(img, filter)
|
||||
}
|
||||
|
||||
type ids struct {
|
||||
textures map[graphics.TextureId]*Texture
|
||||
renderTargets map[graphics.RenderTargetId]*RenderTarget
|
||||
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
||||
lastId int
|
||||
currentRenderTargetId graphics.RenderTargetId
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func newIds() *ids {
|
||||
ids := &ids{
|
||||
textures: map[graphics.TextureId]*Texture{},
|
||||
renderTargets: map[graphics.RenderTargetId]*RenderTarget{},
|
||||
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
||||
lastId: 0,
|
||||
currentRenderTargetId: -1,
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func (i *ids) textureAt(id graphics.TextureId) *Texture {
|
||||
i.RLock()
|
||||
defer i.RUnlock()
|
||||
@ -94,7 +94,11 @@ func (i *ids) createRenderTarget(width, height int, filter graphics.Filter) (
|
||||
framebuffer := createFramebuffer(texture.native)
|
||||
// The current binded framebuffer can be changed.
|
||||
i.currentRenderTargetId = -1
|
||||
renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false}
|
||||
renderTarget := &RenderTarget{
|
||||
framebuffer: framebuffer,
|
||||
width: texture.width,
|
||||
height: texture.height,
|
||||
}
|
||||
|
||||
i.Lock()
|
||||
defer i.Unlock()
|
||||
|
@ -9,16 +9,6 @@ import (
|
||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||
)
|
||||
|
||||
func glMatrix(matrix [4][4]float64) [16]float32 {
|
||||
result := [16]float32{}
|
||||
for j := 0; j < 4; j++ {
|
||||
for i := 0; i < 4; i++ {
|
||||
result[i+j*4] = float32(matrix[i][j])
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type RenderTarget struct {
|
||||
framebuffer C.GLuint
|
||||
width int
|
||||
|
Loading…
Reference in New Issue
Block a user