mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 12:08:58 +01:00
Add ids.go
This commit is contained in:
parent
f6523b28f4
commit
e90f6d3222
@ -23,23 +23,18 @@ type Context struct {
|
||||
screenWidth int
|
||||
screenHeight int
|
||||
screenScale int
|
||||
textures map[graphics.TextureId]*gtexture.Texture
|
||||
renderTargets map[graphics.RenderTargetId]*grendertarget.RenderTarget
|
||||
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
||||
ids *ids
|
||||
mainFramebufferTexture *grendertarget.RenderTarget
|
||||
projectionMatrix [16]float32
|
||||
}
|
||||
|
||||
func newContext(screenWidth, screenHeight, screenScale int) *Context {
|
||||
context := &Context{
|
||||
return &Context{
|
||||
screenWidth: screenWidth,
|
||||
screenHeight: screenHeight,
|
||||
screenScale: screenScale,
|
||||
textures: map[graphics.TextureId]*gtexture.Texture{},
|
||||
renderTargets: map[graphics.RenderTargetId]*grendertarget.RenderTarget{},
|
||||
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
||||
ids: newIds(),
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
func (context *Context) Init() {
|
||||
@ -57,7 +52,7 @@ func (context *Context) Init() {
|
||||
panic("creating main framebuffer failed: " + err.Error())
|
||||
}
|
||||
|
||||
context.screenId, err = context.newRenderTarget(
|
||||
context.screenId, err = context.createRenderTarget(
|
||||
context.screenWidth, context.screenHeight, texture.FilterNearest)
|
||||
if err != nil {
|
||||
panic("initializing the offscreen failed: " + err.Error())
|
||||
@ -65,7 +60,7 @@ func (context *Context) Init() {
|
||||
}
|
||||
|
||||
func (context *Context) ToTexture(renderTargetId graphics.RenderTargetId) graphics.TextureId {
|
||||
return context.renderTargetToTexture[renderTargetId]
|
||||
return context.ids.ToTexture(renderTargetId)
|
||||
}
|
||||
|
||||
func (context *Context) Clear() {
|
||||
@ -85,10 +80,7 @@ func (context *Context) Fill(r, g, b uint8) {
|
||||
func (context *Context) DrawTexture(
|
||||
textureId graphics.TextureId,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
tex, ok := context.textures[textureId]
|
||||
if !ok {
|
||||
panic("invalid texture ID")
|
||||
}
|
||||
tex := context.ids.TextureAt(textureId)
|
||||
tex.Draw(func(native interface{}, quads []gtexture.Quad) {
|
||||
shader.DrawTexture(native.(texture.Native),
|
||||
context.projectionMatrix, quads,
|
||||
@ -99,10 +91,7 @@ func (context *Context) DrawTexture(
|
||||
func (context *Context) DrawTextureParts(
|
||||
textureId graphics.TextureId, parts []graphics.TexturePart,
|
||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||
tex, ok := context.textures[textureId]
|
||||
if !ok {
|
||||
panic("invalid texture ID")
|
||||
}
|
||||
tex := context.ids.TextureAt(textureId)
|
||||
tex.DrawParts(parts, func(native interface{}, quads []gtexture.Quad) {
|
||||
shader.DrawTexture(native.(texture.Native),
|
||||
context.projectionMatrix, quads,
|
||||
@ -115,7 +104,7 @@ func (context *Context) ResetOffscreen() {
|
||||
}
|
||||
|
||||
func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
|
||||
renderTarget := context.renderTargets[renderTargetId]
|
||||
renderTarget := context.ids.RenderTargetAt(renderTargetId)
|
||||
context.setOffscreen(renderTarget)
|
||||
}
|
||||
|
||||
@ -175,49 +164,21 @@ func (context *Context) flush() {
|
||||
C.glFlush()
|
||||
}
|
||||
|
||||
func (context *Context) newRenderTarget(width, height int, filter texture.Filter) (
|
||||
func (context *Context) createRenderTarget(width, height int, filter texture.Filter) (
|
||||
graphics.RenderTargetId, error) {
|
||||
renderTarget, texture, err := rendertarget.New(width, height, filter)
|
||||
renderTargetId, err := context.ids.CreateRenderTarget(width, height, filter)
|
||||
if err != nil {
|
||||
return 0, nil
|
||||
return 0, err
|
||||
}
|
||||
renderTargetId := graphics.RenderTargetId(<-newId)
|
||||
textureId := graphics.TextureId(<-newId)
|
||||
context.renderTargets[renderTargetId] = renderTarget
|
||||
context.textures[textureId] = texture
|
||||
context.renderTargetToTexture[renderTargetId] = textureId
|
||||
|
||||
context.setOffscreen(renderTarget)
|
||||
context.Clear()
|
||||
// TODO: Is it OK to revert he main framebuffer?
|
||||
context.setMainFramebufferOffscreen()
|
||||
|
||||
return renderTargetId, nil
|
||||
}
|
||||
|
||||
func (context *Context) NewRenderTarget(width, height int) (
|
||||
graphics.RenderTargetId, error) {
|
||||
return context.newRenderTarget(width, height, texture.FilterLinear)
|
||||
return context.createRenderTarget(width, height, texture.FilterLinear)
|
||||
}
|
||||
|
||||
func (context *Context) NewTextureFromImage(img image.Image) (
|
||||
graphics.TextureId, error) {
|
||||
texture, err := texture.NewFromImage(img)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
textureId := graphics.TextureId(<-newId)
|
||||
context.textures[textureId] = texture
|
||||
return textureId, nil
|
||||
}
|
||||
|
||||
var newId chan int
|
||||
|
||||
func init() {
|
||||
newId = make(chan int)
|
||||
go func() {
|
||||
for i := 0; ; i++ {
|
||||
newId <- i
|
||||
}
|
||||
}()
|
||||
return context.ids.CreateTextureFromImage(img)
|
||||
}
|
||||
|
70
graphics/opengl/ids.go
Normal file
70
graphics/opengl/ids.go
Normal file
@ -0,0 +1,70 @@
|
||||
package opengl
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
||||
grendertarget "github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
|
||||
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
|
||||
"image"
|
||||
)
|
||||
|
||||
type ids struct {
|
||||
textures map[graphics.TextureId]*gtexture.Texture
|
||||
renderTargets map[graphics.RenderTargetId]*grendertarget.RenderTarget
|
||||
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
||||
counts chan int
|
||||
}
|
||||
|
||||
func newIds() *ids {
|
||||
ids := &ids{
|
||||
textures: map[graphics.TextureId]*gtexture.Texture{},
|
||||
renderTargets: map[graphics.RenderTargetId]*grendertarget.RenderTarget{},
|
||||
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
||||
counts: make(chan int),
|
||||
}
|
||||
go func() {
|
||||
for i := 1; ; i++ {
|
||||
ids.counts <- i
|
||||
}
|
||||
}()
|
||||
return ids
|
||||
}
|
||||
|
||||
func (i *ids) TextureAt(id graphics.TextureId) *gtexture.Texture {
|
||||
return i.textures[id]
|
||||
}
|
||||
|
||||
func (i *ids) RenderTargetAt(id graphics.RenderTargetId) *grendertarget.RenderTarget {
|
||||
return i.renderTargets[id]
|
||||
}
|
||||
|
||||
func (i *ids) ToTexture(id graphics.RenderTargetId) graphics.TextureId {
|
||||
return i.renderTargetToTexture[id]
|
||||
}
|
||||
|
||||
func (i *ids) CreateTextureFromImage(img image.Image) (
|
||||
graphics.TextureId, error) {
|
||||
texture, err := texture.NewFromImage(img)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
textureId := graphics.TextureId(<-i.counts)
|
||||
i.textures[textureId] = texture
|
||||
return textureId, nil
|
||||
}
|
||||
|
||||
func (i *ids) CreateRenderTarget(width, height int, filter texture.Filter) (
|
||||
graphics.RenderTargetId, error) {
|
||||
renderTarget, texture, err := rendertarget.New(width, height, filter)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
renderTargetId := graphics.RenderTargetId(<-i.counts)
|
||||
textureId := graphics.TextureId(<-i.counts)
|
||||
i.renderTargets[renderTargetId] = renderTarget
|
||||
i.textures[textureId] = texture
|
||||
i.renderTargetToTexture[renderTargetId] = textureId
|
||||
|
||||
return renderTargetId, nil
|
||||
}
|
@ -6,8 +6,8 @@ package rendertarget
|
||||
import "C"
|
||||
import (
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
||||
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
|
||||
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
|
||||
)
|
||||
|
||||
type Framebuffer C.GLuint
|
||||
@ -29,6 +29,11 @@ func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
|
||||
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
|
||||
}
|
||||
|
||||
@ -38,7 +43,7 @@ func New(width, height int, filter texture.Filter) (
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
f := func(native interface{}) interface{}{
|
||||
f := func(native interface{}) interface{} {
|
||||
return createFramebuffer(C.GLuint(native.(texture.Native)))
|
||||
}
|
||||
framebuffer := tex.CreateFramebuffer(f)
|
||||
|
@ -54,8 +54,10 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
|
||||
return Native(nativeTexture)
|
||||
}
|
||||
|
||||
func create(textureWidth, textureHeight int, filter Filter) (interface{}, error) {
|
||||
return createNativeTexture(textureWidth, textureHeight, nil, filter), nil
|
||||
func create(textureWidth, textureHeight int, filter Filter) (
|
||||
interface{}, error) {
|
||||
return createNativeTexture(textureWidth, textureHeight,
|
||||
nil, filter), nil
|
||||
}
|
||||
|
||||
func createFromImage(img *image.NRGBA) (interface{}, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user