/
draw.go
57 lines (48 loc) · 1.48 KB
/
draw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2020, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"math"
"github.com/go-pogo/errors"
sdlgfx "github.com/veandco/go-sdl2/gfx"
"github.com/veandco/go-sdl2/sdl"
"github.com/roeldev/go-sdl2-experiments/pkg/sdlkit"
)
func drawCircle(rad, x, y int32, color sdl.Color) sdlkit.Renderable {
col := color
return sdlkit.RenderableFunc(func(r *sdl.Renderer) error {
sdlgfx.FilledCircleColor(r, x, y, rad, col)
return nil
})
}
func drawRect(w, h, x, y int32, color sdl.Color) sdlkit.Renderable {
rect := sdl.Rect{X: x, Y: y, W: w, H: h}
return sdlkit.RenderableFunc(func(r *sdl.Renderer) error {
var err error
errors.Append(&err,
r.SetDrawColor(color.R, color.G, color.B, 0xFF),
r.FillRect(&rect),
)
return err
})
}
func drawSquare(size, x, y int32, color sdl.Color) sdlkit.Renderable {
return drawRect(size, size, x, y, color)
}
// this is basically a sprite
func drawImg(tx *sdl.Texture, scale float32, x, y int32, flip sdl.RendererFlip) sdlkit.Renderable {
_, _, w, h, _ := tx.Query()
if scale != 1 {
w = int32(math.RoundToEven(float64(float32(w) * scale)))
h = int32(math.RoundToEven(float64(float32(h) * scale)))
}
rect := &sdl.Rect{X: x, Y: y, W: w, H: h}
return sdlkit.RenderableFunc(func(r *sdl.Renderer) error {
if flip == sdl.FLIP_NONE {
return r.Copy(tx, nil, rect)
} else {
return r.CopyEx(tx, nil, rect, 0, nil, flip)
}
})
}