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)
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,
matrix.IdentityColor())
}

View File

@ -57,16 +57,6 @@ func (sprite *Sprite) Update() {
<-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 {
ebitenTexture graphics.Texture
sprites []*Sprite
@ -97,7 +87,7 @@ func (game *Sprites) Init(tf graphics.TextureFactory) {
}
game.ebitenTexture = tf.NewTextureFromImage(img)
game.sprites = []*Sprite{}
for i := 0; i < 200; i++ {
for i := 0; i < 1000; i++ {
sprite := NewSprite(
game.ScreenWidth(),
game.ScreenHeight(),
@ -114,9 +104,22 @@ func (game *Sprites) Update() {
func (game *Sprites) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) {
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 {
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() {

View File

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

View File

@ -56,7 +56,10 @@ func (device *Device) Update() {
},
}
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())
g.flush()
}

View File

@ -66,7 +66,20 @@ func (context *GraphicsContext) DrawRect(x, y, width, height int, clr color.Colo
}
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) {
texture := context.textures[textureID]
@ -74,27 +87,6 @@ func (context *GraphicsContext) DrawTexture(
context.setShaderProgram(geometryMatrix, colorMatrix)
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")
textureAttrLocation := getAttributeLocation(context.currentShaderProgram, "texture")
@ -103,11 +95,35 @@ func (context *GraphicsContext) DrawTexture(
C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY)
C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation))
C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation))
C.glVertexAttribPointer(C.GLuint(vertexAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE,
0, unsafe.Pointer(&vertex[0]))
C.glVertexAttribPointer(C.GLuint(textureAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE,
0, unsafe.Pointer(&texCoord[0]))
C.glDrawArrays(C.GL_TRIANGLE_STRIP, 0, 4)
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,
0, unsafe.Pointer(&vertex[0]))
C.glVertexAttribPointer(C.GLuint(textureAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE,
0, unsafe.Pointer(&texCoord[0]))
C.glDrawArrays(C.GL_TRIANGLE_STRIP, 0, 4)
}
C.glDisableVertexAttribArray(C.GLuint(textureAttrLocation))
C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation))
C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY)