Render sprites from sprite sheets (no animation yet)

This commit is contained in:
stevenhowes
2022-01-17 22:36:05 +00:00
parent fa13e4c97b
commit 2bf460b999
5 changed files with 80 additions and 5 deletions
+15 -5
View File
@@ -10,6 +10,8 @@ var TexList map[string]*sdl.Texture
func drawTexture(
tex *sdl.Texture,
size VectorInt32,
sheetposition VectorInt32,
position Vector,
rotation float64,
renderer *sdl.Renderer) error {
@@ -19,16 +21,24 @@ func drawTexture(
return fmt.Errorf("querying texture: %v", err)
}
// If we're given -1,-1 for the size then we're using the entire texture
if size.X < 0 {
size.X = width
}
if size.Y < 0 {
size.Y = height
}
// Convert coordinates to the top left of the sprite
position.X -= float64(width) / 2.0
position.Y -= float64(height) / 2.0
position.X -= float64(size.X) / 2.0
position.Y -= float64(size.Y) / 2.0
return renderer.CopyEx(
tex,
&sdl.Rect{X: 0, Y: 0, W: width, H: height},
&sdl.Rect{X: int32(position.X), Y: int32(position.Y), W: width, H: height},
&sdl.Rect{X: sheetposition.X, Y: sheetposition.Y, W: size.X, H: size.Y},
&sdl.Rect{X: int32(position.X), Y: int32(position.Y), W: size.X, H: size.Y},
rotation,
&sdl.Point{X: width / 2, Y: height / 2},
&sdl.Point{X: size.X / 2, Y: size.Y / 2},
sdl.FLIP_NONE)
}