mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
examples/triangles: Draw polygons
This commit is contained in:
parent
5032546238
commit
1b8cad4e1d
@ -24,6 +24,7 @@ import (
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"github.com/hajimehoshi/ebiten/inpututil"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -41,19 +42,21 @@ func init() {
|
||||
|
||||
var (
|
||||
vertices []ebiten.Vertex
|
||||
indices []uint16
|
||||
|
||||
numVerticesToDraw = 10
|
||||
prevNumVerticesToDraw = 0
|
||||
)
|
||||
|
||||
func init() {
|
||||
func genVertices(num int) []ebiten.Vertex {
|
||||
const (
|
||||
num = 120
|
||||
centerX = screenWidth / 2
|
||||
centerY = screenHeight / 2
|
||||
r = 160
|
||||
)
|
||||
|
||||
vs := []ebiten.Vertex{}
|
||||
for i := 0; i < num; i++ {
|
||||
theta := float64(i) / num * 2 * math.Pi
|
||||
theta := float64(i) / float64(num) * 2 * math.Pi
|
||||
cr := float32(0)
|
||||
cg := float32(0)
|
||||
cb := float32(0)
|
||||
@ -75,7 +78,7 @@ func init() {
|
||||
if 0 <= i && i < num/3 {
|
||||
cb = 2 - 2*float32(i)/float32(num/3)
|
||||
}
|
||||
vertices = append(vertices, ebiten.Vertex{
|
||||
vs = append(vs, ebiten.Vertex{
|
||||
DstX: float32(r*math.Cos(theta)) + centerX,
|
||||
DstY: float32(r*math.Sin(theta)) + centerY,
|
||||
SrcX: 0,
|
||||
@ -87,7 +90,7 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
vertices = append(vertices, ebiten.Vertex{
|
||||
vs = append(vs, ebiten.Vertex{
|
||||
DstX: centerX,
|
||||
DstY: centerY,
|
||||
SrcX: 0,
|
||||
@ -97,19 +100,42 @@ func init() {
|
||||
ColorB: 1,
|
||||
ColorA: 1,
|
||||
})
|
||||
for i := 0; i < num; i++ {
|
||||
indices = append(indices, uint16(i), uint16(i+1)%num, num)
|
||||
}
|
||||
|
||||
return vs
|
||||
}
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
|
||||
numVerticesToDraw--
|
||||
if numVerticesToDraw < 1 {
|
||||
numVerticesToDraw = 1
|
||||
}
|
||||
}
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
|
||||
numVerticesToDraw++
|
||||
if numVerticesToDraw > 120 {
|
||||
numVerticesToDraw = 120
|
||||
}
|
||||
}
|
||||
|
||||
if prevNumVerticesToDraw != numVerticesToDraw || len(vertices) == 0 {
|
||||
vertices = genVertices(numVerticesToDraw)
|
||||
prevNumVerticesToDraw = numVerticesToDraw
|
||||
}
|
||||
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
}
|
||||
|
||||
op := &ebiten.DrawTrianglesOptions{}
|
||||
indices := []uint16{}
|
||||
for i := 0; i < numVerticesToDraw; i++ {
|
||||
indices = append(indices, uint16(i), uint16(i+1)%uint16(numVerticesToDraw), uint16(numVerticesToDraw))
|
||||
}
|
||||
screen.DrawTriangles(vertices, indices, emptyImage, op)
|
||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
|
||||
|
||||
msg := fmt.Sprintf("TPS: %0.2f\nVertices: %d\nPress <- or -> to change the number of the vertices", ebiten.CurrentTPS(), numVerticesToDraw)
|
||||
ebitenutil.DebugPrint(screen, msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user