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.Translate(float64(x), float64(y))
colorMatrix := matrix.IdentityColor()
g.DrawTextureParts(game.textTexture.ID, parts,
g.DrawTextureParts(game.textTexture.ID(), parts,
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})
geometryMatrix := matrix.IdentityGeometry()
tx := game.ScreenWidth()/2 - game.ebitenTexture.Width/2
ty := game.ScreenHeight()/2 - game.ebitenTexture.Height/2
tx := game.ScreenWidth()/2 - game.ebitenTexture.Width()/2
ty := game.ScreenHeight()/2 - game.ebitenTexture.Height()/2
geometryMatrix.Translate(float64(tx), float64(ty))
g.DrawTexture(game.ebitenTexture.ID,
g.DrawTexture(game.ebitenTexture.ID(),
geometryMatrix, game.colorMatrix)
}

View File

@ -57,7 +57,7 @@ func (game *Rects) Update(context ebiten.GameContext) {
}
func (game *Rects) Draw(g graphics.Context) {
g.SetOffscreen(game.rectsTexture.ID)
g.SetOffscreen(game.rectsTexture.ID())
x := rand.Intn(game.ScreenWidth())
y := rand.Intn(game.ScreenHeight())
@ -74,8 +74,8 @@ func (game *Rects) Draw(g graphics.Context) {
&color.RGBA{red, green, blue, alpha},
)
g.SetOffscreen(g.Screen().ID)
g.DrawTexture(game.rectsTexture.ID,
g.SetOffscreen(g.Screen().ID())
g.DrawTexture(game.rectsTexture.ID(),
matrix.IdentityGeometry(),
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})
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.Rotate(float64(game.x) * 2 * math.Pi / float64(game.Fps()*10))
geometryMatrix.Translate(tx/2, ty/2)
@ -84,7 +84,7 @@ func (game *Rotating) Draw(g graphics.Context) {
centerY := float64(game.ScreenHeight()) / 2
geometryMatrix.Translate(centerX-tx/2, centerY-ty/2)
g.DrawTexture(game.ebitenTexture.ID,
g.DrawTexture(game.ebitenTexture.ID(),
geometryMatrix,
matrix.IdentityColor())
}

View File

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

View File

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

View File

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

View File

@ -67,7 +67,7 @@ func (device *Device) Update() {
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_MAG_FILTER, C.GL_NEAREST)
context.SetOffscreen(context.Screen().ID)
context.SetOffscreen(context.Screen().ID())
context.Clear()
ch := make(chan func(graphics.Context))
@ -89,7 +89,7 @@ func (device *Device) Update() {
{0, scale, 0},
},
}
context.DrawTexture(context.Screen().ID,
context.DrawTexture(context.Screen().ID(),
geometryMatrix, matrix.IdentityColor())
context.flush()
}

View File

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