mirror of
https://github.com/stevenhowes/GoRetro.git
synced 2026-05-26 15:53:31 +01:00
Render sprites from sprite sheets (no animation yet)
This commit is contained in:
@@ -58,6 +58,8 @@ func (an *animator) onDraw() error {
|
||||
|
||||
return drawTexture(
|
||||
tex,
|
||||
VectorInt32{-1, -1},
|
||||
VectorInt32{0, 0},
|
||||
an.container.Position,
|
||||
an.container.Rotation,
|
||||
an.container.Renderer)
|
||||
|
||||
@@ -48,6 +48,8 @@ func (sr *spriteRenderer) onUpdate() error {
|
||||
func (sr *spriteRenderer) onDraw() error {
|
||||
return drawTexture(
|
||||
sr.tex,
|
||||
VectorInt32{-1, -1},
|
||||
VectorInt32{0, 0},
|
||||
sr.container.Position,
|
||||
sr.container.Rotation,
|
||||
sr.container.Renderer)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package GoRetro
|
||||
|
||||
/*
|
||||
* --------------------
|
||||
* spriteSheetRenderer
|
||||
* --------------------
|
||||
*/
|
||||
|
||||
import (
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
type spriteSheetRenderer struct {
|
||||
container *Element
|
||||
tex *sdl.Texture
|
||||
size VectorInt32
|
||||
sheetposition VectorInt32
|
||||
}
|
||||
|
||||
func NewSpriteSheetRenderer(container *Element, renderer *sdl.Renderer, filename string, x int32, y int32, width int32, height int32) *spriteSheetRenderer {
|
||||
sr := &spriteSheetRenderer{}
|
||||
var err error
|
||||
|
||||
filename = Config.DataDir + filename
|
||||
sr.tex, err = loadTextureFromBMP(filename, renderer)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
sr.size.X = width
|
||||
sr.size.Y = height
|
||||
sr.sheetposition.X = x
|
||||
sr.sheetposition.Y = y
|
||||
|
||||
sr.container = container
|
||||
|
||||
return sr
|
||||
}
|
||||
|
||||
func (sr *spriteSheetRenderer) onUpdate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sr *spriteSheetRenderer) onDraw() error {
|
||||
return drawTexture(
|
||||
sr.tex,
|
||||
sr.size,
|
||||
sr.sheetposition,
|
||||
sr.container.Position,
|
||||
sr.container.Rotation,
|
||||
sr.container.Renderer)
|
||||
}
|
||||
|
||||
func (sr *spriteSheetRenderer) onCollision(other *Element) error {
|
||||
return nil
|
||||
}
|
||||
+15
-5
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,11 @@ type Vector struct {
|
||||
Y float64
|
||||
}
|
||||
|
||||
type VectorInt32 struct {
|
||||
X int32
|
||||
Y int32
|
||||
}
|
||||
|
||||
func vectorAdd(v1, v2 Vector) Vector {
|
||||
return Vector{
|
||||
X: v1.X + v2.X,
|
||||
|
||||
Reference in New Issue
Block a user