Add graphics.DrawTextures (which I want to rename later)

This commit is contained in:
Hajime Hoshi 2013-06-22 00:50:55 +09:00
parent a374565e37
commit fad8fb7a98
5 changed files with 87 additions and 46 deletions

View File

@ -57,7 +57,10 @@ func (game *RotatingImage) Draw(g graphics.GraphicsContext, offscreen graphics.T
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,
graphics.Rectangle{0, 0, int(tx), int(ty)}, graphics.Rectangle{
graphics.Point{0, 0},
graphics.Size{int(tx), int(ty)},
},
geometryMatrix, geometryMatrix,
matrix.IdentityColor()) matrix.IdentityColor())
} }

View File

@ -57,16 +57,6 @@ func (sprite *Sprite) Update() {
<-sprite.ch <-sprite.ch
} }
func (sprite *Sprite) Draw(g graphics.GraphicsContext) {
geometryMatrix := matrix.IdentityGeometry()
geometryMatrix.Translate(float64(sprite.x), float64(sprite.y))
g.DrawTexture(sprite.texture.ID,
graphics.Rectangle{0, 0, sprite.texture.Width, sprite.texture.Height},
geometryMatrix,
matrix.IdentityColor())
}
type Sprites struct { type Sprites struct {
ebitenTexture graphics.Texture ebitenTexture graphics.Texture
sprites []*Sprite sprites []*Sprite
@ -97,7 +87,7 @@ func (game *Sprites) Init(tf graphics.TextureFactory) {
} }
game.ebitenTexture = tf.NewTextureFromImage(img) game.ebitenTexture = tf.NewTextureFromImage(img)
game.sprites = []*Sprite{} game.sprites = []*Sprite{}
for i := 0; i < 200; i++ { for i := 0; i < 1000; i++ {
sprite := NewSprite( sprite := NewSprite(
game.ScreenWidth(), game.ScreenWidth(),
game.ScreenHeight(), game.ScreenHeight(),
@ -114,9 +104,22 @@ func (game *Sprites) Update() {
func (game *Sprites) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { func (game *Sprites) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) {
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255}) g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
// Draw the sprites
locations := make([]graphics.TextureLocation, 0, len(game.sprites))
texture := game.ebitenTexture
for _, sprite := range game.sprites { for _, sprite := range game.sprites {
sprite.Draw(g) location := graphics.TextureLocation{
Location: graphics.Point{sprite.x, sprite.y},
Source: graphics.Rectangle{
graphics.Point{0, 0},
graphics.Size{texture.Width, texture.Height},
},
} }
locations = append(locations, location)
}
g.DrawTextures(texture.ID, locations,
matrix.IdentityGeometry(), matrix.IdentityColor())
} }
func init() { func init() {

View File

@ -11,18 +11,34 @@ type Device interface {
TextureFactory() TextureFactory TextureFactory() TextureFactory
} }
type Rectangle struct { type Point struct {
X int X int
Y int Y int
}
type Size struct {
Width int Width int
Height int Height int
} }
type Rectangle struct {
Location Point
Size Size
}
type TextureLocation struct {
Location Point
Source Rectangle
}
type GraphicsContext interface { type GraphicsContext interface {
Clear() Clear()
Fill(color color.Color) Fill(color color.Color)
DrawTexture(textureId TextureID, DrawTexture(textureId TextureID,
src Rectangle, source Rectangle,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color)
DrawTextures(textureId TextureID,
locations []TextureLocation,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) geometryMatrix matrix.Geometry, colorMatrix matrix.Color)
SetOffscreen(textureId TextureID) SetOffscreen(textureId TextureID)
} }

View File

@ -56,7 +56,10 @@ func (device *Device) Update() {
}, },
} }
g.DrawTexture(device.offscreenTexture.ID, g.DrawTexture(device.offscreenTexture.ID,
graphics.Rectangle{0, 0, device.screenWidth, device.screenHeight}, graphics.Rectangle{
graphics.Point{0, 0},
graphics.Size{device.screenWidth, device.screenHeight},
},
geometryMatrix, matrix.IdentityColor()) geometryMatrix, matrix.IdentityColor())
g.flush() g.flush()
} }

View File

@ -66,7 +66,20 @@ func (context *GraphicsContext) DrawRect(x, y, width, height int, clr color.Colo
} }
func (context *GraphicsContext) DrawTexture( func (context *GraphicsContext) DrawTexture(
textureID graphics.TextureID, src graphics.Rectangle, textureID graphics.TextureID, source graphics.Rectangle,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
locations := []graphics.TextureLocation{
{
graphics.Point{0, 0},
source,
},
}
context.DrawTextures(textureID, locations,
geometryMatrix, colorMatrix)
}
func (context *GraphicsContext) DrawTextures(
textureID graphics.TextureID, locations []graphics.TextureLocation,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
texture := context.textures[textureID] texture := context.textures[textureID]
@ -74,27 +87,6 @@ func (context *GraphicsContext) DrawTexture(
context.setShaderProgram(geometryMatrix, colorMatrix) context.setShaderProgram(geometryMatrix, colorMatrix)
C.glBindTexture(C.GL_TEXTURE_2D, texture.id) C.glBindTexture(C.GL_TEXTURE_2D, texture.id)
x1 := float32(0)
x2 := float32(src.Width)
y1 := float32(0)
y2 := float32(src.Height)
vertex := [...]float32{
x1, y1,
x2, y1,
x1, y2,
x2, y2,
}
tu1 := float32(src.X) / float32(texture.textureWidth)
tu2 := float32(src.X+src.Width) / float32(texture.textureWidth)
tv1 := float32(src.Y) / float32(texture.textureHeight)
tv2 := float32(src.Y+src.Height) / float32(texture.textureHeight)
texCoord := [...]float32{
tu1, tv1,
tu2, tv1,
tu1, tv2,
tu2, tv2,
}
vertexAttrLocation := getAttributeLocation(context.currentShaderProgram, "vertex") vertexAttrLocation := getAttributeLocation(context.currentShaderProgram, "vertex")
textureAttrLocation := getAttributeLocation(context.currentShaderProgram, "texture") textureAttrLocation := getAttributeLocation(context.currentShaderProgram, "texture")
@ -103,11 +95,35 @@ func (context *GraphicsContext) DrawTexture(
C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY) C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY)
C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation)) C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation))
C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation)) C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation))
for _, location := range locations {
x1 := float32(location.Location.X)
x2 := float32(location.Location.X + location.Source.Size.Width)
y1 := float32(location.Location.Y)
y2 := float32(location.Location.Y + location.Source.Size.Height)
vertex := [...]float32{
x1, y1,
x2, y1,
x1, y2,
x2, y2,
}
src := location.Source
tu1 := float32(src.Location.X) / float32(texture.textureWidth)
tu2 := float32(src.Location.X+src.Size.Width) / float32(texture.textureWidth)
tv1 := float32(src.Location.Y) / float32(texture.textureHeight)
tv2 := float32(src.Location.Y+src.Size.Height) / float32(texture.textureHeight)
texCoord := [...]float32{
tu1, tv1,
tu2, tv1,
tu1, tv2,
tu2, tv2,
}
C.glVertexAttribPointer(C.GLuint(vertexAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE, C.glVertexAttribPointer(C.GLuint(vertexAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE,
0, unsafe.Pointer(&vertex[0])) 0, unsafe.Pointer(&vertex[0]))
C.glVertexAttribPointer(C.GLuint(textureAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE, C.glVertexAttribPointer(C.GLuint(textureAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE,
0, unsafe.Pointer(&texCoord[0])) 0, unsafe.Pointer(&texCoord[0]))
C.glDrawArrays(C.GL_TRIANGLE_STRIP, 0, 4) C.glDrawArrays(C.GL_TRIANGLE_STRIP, 0, 4)
}
C.glDisableVertexAttribArray(C.GLuint(textureAttrLocation)) C.glDisableVertexAttribArray(C.GLuint(textureAttrLocation))
C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation)) C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation))
C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY) C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY)