Change Texture

This commit is contained in:
Hajime Hoshi 2013-07-13 01:36:01 +09:00
parent 984d572cb0
commit 15c76624f1
9 changed files with 60 additions and 41 deletions

View File

@ -107,6 +107,6 @@ func (game *Input) drawText(g graphics.Context, text string, x, y int) {
geometryMatrix := matrix.IdentityGeometry() geometryMatrix := matrix.IdentityGeometry()
geometryMatrix.Translate(float64(x), float64(y)) geometryMatrix.Translate(float64(x), float64(y))
colorMatrix := matrix.IdentityColor() colorMatrix := matrix.IdentityColor()
g.DrawTextureParts(game.textTexture.ID, parts, g.DrawTextureParts(game.textTexture.ID(), parts,
geometryMatrix, colorMatrix) geometryMatrix, colorMatrix)
} }

View File

@ -124,9 +124,9 @@ func (game *Monochrome) Draw(g graphics.Context) {
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255}) g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
geometryMatrix := matrix.IdentityGeometry() geometryMatrix := matrix.IdentityGeometry()
tx := game.ScreenWidth()/2 - game.ebitenTexture.Width/2 tx := game.ScreenWidth()/2 - game.ebitenTexture.Width()/2
ty := game.ScreenHeight()/2 - game.ebitenTexture.Height/2 ty := game.ScreenHeight()/2 - game.ebitenTexture.Height()/2
geometryMatrix.Translate(float64(tx), float64(ty)) geometryMatrix.Translate(float64(tx), float64(ty))
g.DrawTexture(game.ebitenTexture.ID, g.DrawTexture(game.ebitenTexture.ID(),
geometryMatrix, game.colorMatrix) geometryMatrix, game.colorMatrix)
} }

View File

@ -57,7 +57,7 @@ func (game *Rects) Update(context ebiten.GameContext) {
} }
func (game *Rects) Draw(g graphics.Context) { func (game *Rects) Draw(g graphics.Context) {
g.SetOffscreen(game.rectsTexture.ID) g.SetOffscreen(game.rectsTexture.ID())
x := rand.Intn(game.ScreenWidth()) x := rand.Intn(game.ScreenWidth())
y := rand.Intn(game.ScreenHeight()) y := rand.Intn(game.ScreenHeight())
@ -74,8 +74,8 @@ func (game *Rects) Draw(g graphics.Context) {
&color.RGBA{red, green, blue, alpha}, &color.RGBA{red, green, blue, alpha},
) )
g.SetOffscreen(g.Screen().ID) g.SetOffscreen(g.Screen().ID())
g.DrawTexture(game.rectsTexture.ID, g.DrawTexture(game.rectsTexture.ID(),
matrix.IdentityGeometry(), matrix.IdentityGeometry(),
matrix.IdentityColor()) matrix.IdentityColor())
} }

View File

@ -76,7 +76,7 @@ func (game *Rotating) Draw(g graphics.Context) {
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255}) g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
geometryMatrix := matrix.IdentityGeometry() geometryMatrix := matrix.IdentityGeometry()
tx, ty := float64(game.ebitenTexture.Width), float64(game.ebitenTexture.Height) tx, ty := float64(game.ebitenTexture.Width()), float64(game.ebitenTexture.Height())
geometryMatrix.Translate(-tx/2, -ty/2) geometryMatrix.Translate(-tx/2, -ty/2)
geometryMatrix.Rotate(float64(game.x) * 2 * math.Pi / float64(game.Fps()*10)) geometryMatrix.Rotate(float64(game.x) * 2 * math.Pi / float64(game.Fps()*10))
geometryMatrix.Translate(tx/2, ty/2) geometryMatrix.Translate(tx/2, ty/2)
@ -84,7 +84,7 @@ func (game *Rotating) Draw(g graphics.Context) {
centerY := float64(game.ScreenHeight()) / 2 centerY := float64(game.ScreenHeight()) / 2
geometryMatrix.Translate(centerX-tx/2, centerY-ty/2) geometryMatrix.Translate(centerX-tx/2, centerY-ty/2)
g.DrawTexture(game.ebitenTexture.ID, g.DrawTexture(game.ebitenTexture.ID(),
geometryMatrix, geometryMatrix,
matrix.IdentityColor()) matrix.IdentityColor())
} }

View File

@ -119,8 +119,8 @@ func (game *Sprites) Init(tf graphics.TextureFactory) {
sprite := newSprite( sprite := newSprite(
game.ScreenWidth(), game.ScreenWidth(),
game.ScreenHeight(), game.ScreenHeight(),
game.ebitenTexture.Width, game.ebitenTexture.Width(),
game.ebitenTexture.Height) game.ebitenTexture.Height())
game.sprites = append(game.sprites, sprite) game.sprites = append(game.sprites, sprite)
} }
} }
@ -142,13 +142,13 @@ func (game *Sprites) Draw(g graphics.Context) {
LocationX: sprite.x, LocationX: sprite.x,
LocationY: sprite.y, LocationY: sprite.y,
Source: graphics.Rect{ Source: graphics.Rect{
0, 0, texture.Width, texture.Height, 0, 0, texture.Width(), texture.Height(),
}, },
} }
locations = append(locations, location) locations = append(locations, location)
} }
geometryMatrix := matrix.IdentityGeometry() geometryMatrix := matrix.IdentityGeometry()
g.DrawTextureParts(texture.ID, locations, g.DrawTextureParts(texture.ID(), locations,
geometryMatrix, matrix.IdentityColor()) geometryMatrix, matrix.IdentityColor())
} }

View File

@ -65,10 +65,10 @@ type TextureFactory interface {
NewTextureFromImage(img image.Image) (Texture, error) NewTextureFromImage(img image.Image) (Texture, error)
} }
type Texture struct { type Texture interface {
ID TextureID ID() TextureID
Width int Width() int
Height int Height() int
} }
type TextureID int type TextureID int

View File

@ -37,8 +37,6 @@ import (
type Context struct { type Context struct {
screen graphics.Texture screen graphics.Texture
screenWidth int
screenHeight int
screenScale int screenScale int
textures map[graphics.TextureID]*Texture textures map[graphics.TextureID]*Texture
currentOffscreenWidth int currentOffscreenWidth int
@ -46,14 +44,13 @@ type Context struct {
projectionMatrix [16]float32 projectionMatrix [16]float32
currentShaderProgram C.GLuint currentShaderProgram C.GLuint
mainFramebuffer C.GLuint mainFramebuffer C.GLuint
mainFramebufferTexture *Texture
framebuffers map[C.GLuint]C.GLuint 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{
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale, screenScale: screenScale,
textures: map[graphics.TextureID]*Texture{}, textures: map[graphics.TextureID]*Texture{},
mainFramebuffer: 0, mainFramebuffer: 0,
@ -64,6 +61,10 @@ func newContext(screenWidth, screenHeight, screenScale int) *Context {
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer) C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
context.mainFramebuffer = C.GLuint(mainFramebuffer) context.mainFramebuffer = C.GLuint(mainFramebuffer)
context.mainFramebufferTexture = newVirtualTexture(
screenWidth * screenScale,
screenHeight * screenScale)
initializeShaders() initializeShaders()
context.screen = context.NewTexture(screenWidth, screenHeight) context.screen = context.NewTexture(screenWidth, screenHeight)
@ -227,8 +228,8 @@ func (context *Context) SetOffscreen(textureID graphics.TextureID) {
func (context *Context) setOffscreenFramebuffer(framebuffer C.GLuint, func (context *Context) setOffscreenFramebuffer(framebuffer C.GLuint,
textureWidth, textureHeight int) { textureWidth, textureHeight int) {
if framebuffer == context.mainFramebuffer { if framebuffer == context.mainFramebuffer {
textureWidth = int(clp2(uint64(context.screenWidth * context.screenScale))) textureWidth = context.mainFramebufferTexture.textureWidth
textureHeight = int(clp2(uint64(context.screenHeight * context.screenScale))) textureHeight = context.mainFramebufferTexture.textureHeight
} }
C.glFlush() C.glFlush()
@ -249,7 +250,7 @@ func (context *Context) setOffscreenFramebuffer(framebuffer C.GLuint,
e41 = -1 e41 = -1
e42 = -1 e42 = -1
} else { } else {
height := float32(context.screenHeight) * float32(context.screenScale) height := float32(context.mainFramebufferTexture.Height())
e11 = float32(2) / float32(textureWidth) e11 = float32(2) / float32(textureWidth)
e22 = -1 * float32(2) / float32(textureHeight) e22 = -1 * float32(2) / float32(textureHeight)
e41 = -1 e41 = -1
@ -266,8 +267,8 @@ func (context *Context) setOffscreenFramebuffer(framebuffer C.GLuint,
func (context *Context) resetOffscreen() { func (context *Context) resetOffscreen() {
context.setOffscreenFramebuffer(context.mainFramebuffer, 0, 0) context.setOffscreenFramebuffer(context.mainFramebuffer, 0, 0)
context.currentOffscreenWidth = context.screenWidth * context.screenScale context.currentOffscreenWidth = context.mainFramebufferTexture.Width()
context.currentOffscreenHeight = context.screenHeight * context.screenScale context.currentOffscreenHeight = context.mainFramebufferTexture.Height()
} }
// This method should be called on the UI thread. // This method should be called on the UI thread.
@ -375,27 +376,19 @@ func (context *Context) NewTexture(width, height int) graphics.Texture {
id := graphics.TextureID(texture.id) id := graphics.TextureID(texture.id)
context.textures[id] = texture context.textures[id] = texture
context.SetOffscreen(id) context.SetOffscreen(texture.ID())
context.Clear() context.Clear()
context.resetOffscreen() context.resetOffscreen()
return graphics.Texture{ return texture
ID: id,
Width: texture.width,
Height: texture.height,
}
} }
func (context *Context) NewTextureFromImage(img image.Image) (graphics.Texture, error) { func (context *Context) NewTextureFromImage(img image.Image) (graphics.Texture, error) {
texture, err := newTextureFromImage(img) texture, err := newTextureFromImage(img)
if err != nil { if err != nil {
return graphics.Texture{}, err return nil, err
} }
id := graphics.TextureID(texture.id) id := graphics.TextureID(texture.id)
context.textures[id] = texture context.textures[id] = texture
return graphics.Texture{ return texture, nil
ID: id,
Width: texture.width,
Height: texture.height,
}, nil
} }

View File

@ -67,7 +67,7 @@ func (device *Device) Update() {
C.glEnable(C.GL_TEXTURE_2D) C.glEnable(C.GL_TEXTURE_2D)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST)
context.SetOffscreen(context.Screen().ID) context.SetOffscreen(context.Screen().ID())
context.Clear() context.Clear()
ch := make(chan func(graphics.Context)) ch := make(chan func(graphics.Context))
@ -89,7 +89,7 @@ func (device *Device) Update() {
{0, scale, 0}, {0, scale, 0},
}, },
} }
context.DrawTexture(context.Screen().ID, context.DrawTexture(context.Screen().ID(),
geometryMatrix, matrix.IdentityColor()) geometryMatrix, matrix.IdentityColor())
context.flush() context.flush()
} }

View File

@ -25,6 +25,7 @@ package opengl
// #include <OpenGL/gl.h> // #include <OpenGL/gl.h>
import "C" import "C"
import ( import (
"github.com/hajimehoshi/go.ebiten/graphics"
"image" "image"
"unsafe" "unsafe"
) )
@ -62,6 +63,19 @@ type Texture struct {
height int height int
textureWidth int textureWidth int
textureHeight int textureHeight int
isVirtual bool
}
func (texture *Texture) ID() graphics.TextureID {
return graphics.TextureID(texture.id)
}
func (texture *Texture) Width() int {
return texture.width
}
func (texture *Texture) Height() int {
return texture.height
} }
func createTexture(width, height int, pixels []uint8) *Texture { func createTexture(width, height int, pixels []uint8) *Texture {
@ -76,11 +90,12 @@ func createTexture(width, height int, pixels []uint8) *Texture {
height: height, height: height,
textureWidth: textureWidth, textureWidth: textureWidth,
textureHeight: textureHeight, textureHeight: textureHeight,
isVirtual: false,
} }
textureID := C.GLuint(0) textureID := C.GLuint(0)
C.glGenTextures(1, (*C.GLuint)(&textureID)) C.glGenTextures(1, (*C.GLuint)(&textureID))
if textureID == 0 { if textureID < 0 {
panic("glGenTexture failed") panic("glGenTexture failed")
} }
C.glPixelStorei(C.GL_UNPACK_ALIGNMENT, 4) C.glPixelStorei(C.GL_UNPACK_ALIGNMENT, 4)
@ -126,3 +141,14 @@ func newTextureFromImage(img image.Image) (*Texture, error) {
size := img.Bounds().Size() size := img.Bounds().Size()
return createTexture(size.X, size.Y, pix), nil return createTexture(size.X, size.Y, pix), nil
} }
func newVirtualTexture(width, height int) *Texture {
return &Texture{
id: 0,
width: width,
height: height,
textureWidth: int(clp2(uint64(width))),
textureHeight: int(clp2(uint64(height))),
isVirtual: true,
}
}