ebiten/examples/polygons/main.go

148 lines
3.1 KiB
Go
Raw Normal View History

2018-06-12 03:33:09 +02:00
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-06-24 14:49:37 +02:00
//go:build example
2020-10-06 17:45:54 +02:00
// +build example
2018-06-12 03:33:09 +02:00
package main
import (
"fmt"
"image"
2018-06-12 03:33:09 +02:00
"image/color"
"log"
"math"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
2018-06-12 03:33:09 +02:00
)
const (
screenWidth = 640
screenHeight = 480
)
var (
emptyImage = ebiten.NewImage(3, 3)
2018-06-12 03:33:09 +02:00
)
func init() {
emptyImage.Fill(color.White)
}
2018-08-13 18:49:45 +02:00
func genVertices(num int) []ebiten.Vertex {
2018-06-12 03:33:09 +02:00
const (
centerX = screenWidth / 2
centerY = screenHeight / 2
r = 160
)
2018-08-13 18:49:45 +02:00
vs := []ebiten.Vertex{}
2018-06-12 03:33:09 +02:00
for i := 0; i < num; i++ {
2018-08-13 19:14:19 +02:00
rate := float64(i) / float64(num)
cr := 0.0
cg := 0.0
cb := 0.0
if rate < 1.0/3.0 {
cb = 2 - 2*(rate*3)
cr = 2 * (rate * 3)
2018-06-12 03:33:09 +02:00
}
2018-08-13 19:14:19 +02:00
if 1.0/3.0 <= rate && rate < 2.0/3.0 {
cr = 2 - 2*(rate-1.0/3.0)*3
cg = 2 * (rate - 1.0/3.0) * 3
2018-06-12 03:33:09 +02:00
}
2018-08-13 19:14:19 +02:00
if 2.0/3.0 <= rate {
cg = 2 - 2*(rate-2.0/3.0)*3
cb = 2 * (rate - 2.0/3.0) * 3
2018-06-12 03:33:09 +02:00
}
2018-08-13 18:49:45 +02:00
vs = append(vs, ebiten.Vertex{
2018-08-13 19:14:19 +02:00
DstX: float32(r*math.Cos(2*math.Pi*rate)) + centerX,
DstY: float32(r*math.Sin(2*math.Pi*rate)) + centerY,
2018-06-12 03:33:09 +02:00
SrcX: 0,
SrcY: 0,
2018-08-13 19:14:19 +02:00
ColorR: float32(cr),
ColorG: float32(cg),
ColorB: float32(cb),
2018-06-12 03:33:09 +02:00
ColorA: 1,
})
}
2018-08-13 18:49:45 +02:00
vs = append(vs, ebiten.Vertex{
2018-06-12 03:33:09 +02:00
DstX: centerX,
DstY: centerY,
SrcX: 0,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
})
2018-08-13 18:49:45 +02:00
return vs
2018-06-12 03:33:09 +02:00
}
2020-05-23 16:18:21 +02:00
type Game struct {
vertices []ebiten.Vertex
ngon int
prevNgon int
}
func (g *Game) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyArrowLeft) {
2020-05-23 16:18:21 +02:00
g.ngon--
if g.ngon < 1 {
g.ngon = 1
2018-08-13 18:49:45 +02:00
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowRight) {
2020-05-23 16:18:21 +02:00
g.ngon++
if g.ngon > 120 {
g.ngon = 120
2018-08-13 18:49:45 +02:00
}
}
2020-05-23 16:18:21 +02:00
if g.prevNgon != g.ngon || len(g.vertices) == 0 {
g.vertices = genVertices(g.ngon)
g.prevNgon = g.ngon
2018-06-12 03:33:09 +02:00
}
2020-05-23 16:18:21 +02:00
return nil
}
2018-06-12 03:33:09 +02:00
2020-05-23 16:18:21 +02:00
func (g *Game) Draw(screen *ebiten.Image) {
2018-06-12 03:33:09 +02:00
op := &ebiten.DrawTrianglesOptions{}
2020-07-02 17:33:04 +02:00
op.Address = ebiten.AddressUnsafe
2018-08-13 18:49:45 +02:00
indices := []uint16{}
2020-05-23 16:18:21 +02:00
for i := 0; i < g.ngon; i++ {
indices = append(indices, uint16(i), uint16(i+1)%uint16(g.ngon), uint16(g.ngon))
2018-08-13 18:49:45 +02:00
}
screen.DrawTriangles(g.vertices, indices, emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op)
2018-08-13 18:49:45 +02:00
msg := fmt.Sprintf("TPS: %0.2f\n%d-gon\nPress <- or -> to change the number of the vertices", ebiten.ActualTPS(), g.ngon)
2018-08-13 18:49:45 +02:00
ebitenutil.DebugPrint(screen, msg)
2020-05-23 16:18:21 +02:00
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
2018-06-12 03:33:09 +02:00
}
func main() {
2020-05-23 16:18:21 +02:00
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Polygons (Ebitengine Demo)")
2020-05-23 16:18:21 +02:00
if err := ebiten.RunGame(&Game{ngon: 10}); err != nil {
2018-06-12 03:33:09 +02:00
log.Fatal(err)
}
}