ebiten/examples/triangles/main.go

147 lines
3.0 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.
// +build example jsgo
package main
import (
"fmt"
"image/color"
"log"
"math"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
2018-08-13 18:49:45 +02:00
"github.com/hajimehoshi/ebiten/inpututil"
2018-06-12 03:33:09 +02:00
)
const (
screenWidth = 640
screenHeight = 480
)
var (
emptyImage, _ = ebiten.NewImage(16, 16, ebiten.FilterDefault)
)
func init() {
emptyImage.Fill(color.White)
}
var (
vertices []ebiten.Vertex
2018-08-13 18:49:45 +02:00
2018-08-13 18:55:22 +02:00
ngon = 10
prevNgon = 0
2018-06-12 03:33:09 +02:00
)
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 18:49:45 +02:00
theta := float64(i) / float64(num) * 2 * math.Pi
2018-06-12 03:33:09 +02:00
cr := float32(0)
cg := float32(0)
cb := float32(0)
if 0 <= i && i < 2*num/3 {
cr = 2 * float32(i) / float32(num/3)
}
if num/3 <= i && i < 2*num/3 {
cr = 2 - 2*float32(i-num/3)/float32(num/3)
}
if num/3 <= i && i < 2*num/3 {
cg = 2 * float32(i-num/3) / float32(num/3)
}
if 2*num/3 <= i && i < num {
cg = 2 - 2*float32(i-2*num/3)/float32(num/3)
}
if 2*num/3 <= i && i < num {
cb = 2 * float32(i-2*num/3) / float32(num/3)
}
if 0 <= i && i < num/3 {
cb = 2 - 2*float32(i)/float32(num/3)
}
2018-08-13 18:49:45 +02:00
vs = append(vs, ebiten.Vertex{
2018-06-12 03:33:09 +02:00
DstX: float32(r*math.Cos(theta)) + centerX,
DstY: float32(r*math.Sin(theta)) + centerY,
SrcX: 0,
SrcY: 0,
ColorR: cr,
ColorG: cg,
ColorB: cb,
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
}
func update(screen *ebiten.Image) error {
2018-08-13 18:49:45 +02:00
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
2018-08-13 18:55:22 +02:00
ngon--
if ngon < 1 {
ngon = 1
2018-08-13 18:49:45 +02:00
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
2018-08-13 18:55:22 +02:00
ngon++
if ngon > 120 {
ngon = 120
2018-08-13 18:49:45 +02:00
}
}
2018-08-13 18:55:22 +02:00
if prevNgon != ngon || len(vertices) == 0 {
vertices = genVertices(ngon)
prevNgon = ngon
2018-08-13 18:49:45 +02:00
}
2018-06-12 03:33:09 +02:00
if ebiten.IsDrawingSkipped() {
return nil
}
op := &ebiten.DrawTrianglesOptions{}
2018-08-13 18:49:45 +02:00
indices := []uint16{}
2018-08-13 18:55:22 +02:00
for i := 0; i < ngon; i++ {
indices = append(indices, uint16(i), uint16(i+1)%uint16(ngon), uint16(ngon))
2018-08-13 18:49:45 +02:00
}
2018-06-12 03:33:09 +02:00
screen.DrawTriangles(vertices, indices, emptyImage, op)
2018-08-13 18:49:45 +02:00
2018-08-13 18:55:22 +02:00
msg := fmt.Sprintf("TPS: %0.2f\n%d-gon\nPress <- or -> to change the number of the vertices", ebiten.CurrentTPS(), ngon)
2018-08-13 18:49:45 +02:00
ebitenutil.DebugPrint(screen, msg)
2018-06-12 03:33:09 +02:00
return nil
}
func main() {
2018-08-13 18:30:42 +02:00
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Triangles (Ebiten Demo)"); err != nil {
2018-06-12 03:33:09 +02:00
log.Fatal(err)
}
}