2019-04-13 17:16:18 +02:00
|
|
|
// Copyright 2019 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
|
2019-04-13 17:16:18 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-28 16:05:03 +01:00
|
|
|
"fmt"
|
2021-07-02 12:26:09 +02:00
|
|
|
"image"
|
2019-04-13 17:16:18 +02:00
|
|
|
"image/color"
|
|
|
|
"log"
|
2019-12-28 16:05:03 +01:00
|
|
|
"math"
|
2019-04-13 17:16:18 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
2022-10-09 14:26:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
2019-04-13 17:16:18 +02:00
|
|
|
)
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
var (
|
2022-04-26 16:23:31 +02:00
|
|
|
emptyImage = ebiten.NewImage(3, 3)
|
|
|
|
|
|
|
|
// emptySubImage is an internal sub image of emptyImage.
|
|
|
|
// Use emptySubImage at DrawTriangles instead of emptyImage in order to avoid bleeding edges.
|
2021-07-02 12:26:09 +02:00
|
|
|
emptySubImage = emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
emptyImage.Fill(color.White)
|
|
|
|
}
|
|
|
|
|
2019-04-13 17:16:18 +02:00
|
|
|
const (
|
|
|
|
screenWidth = 640
|
|
|
|
screenHeight = 480
|
|
|
|
)
|
|
|
|
|
2022-10-09 14:26:13 +02:00
|
|
|
func drawEbitenText(screen *ebiten.Image, scale float32) {
|
2019-04-13 17:16:18 +02:00
|
|
|
var path vector.Path
|
2019-04-13 18:58:20 +02:00
|
|
|
|
|
|
|
// E
|
2019-12-27 03:26:16 +01:00
|
|
|
path.MoveTo(20, 20)
|
|
|
|
path.LineTo(20, 70)
|
|
|
|
path.LineTo(70, 70)
|
|
|
|
path.LineTo(70, 60)
|
|
|
|
path.LineTo(30, 60)
|
|
|
|
path.LineTo(30, 50)
|
|
|
|
path.LineTo(70, 50)
|
|
|
|
path.LineTo(70, 40)
|
|
|
|
path.LineTo(30, 40)
|
|
|
|
path.LineTo(30, 30)
|
|
|
|
path.LineTo(70, 30)
|
|
|
|
path.LineTo(70, 20)
|
|
|
|
|
2019-12-28 06:57:50 +01:00
|
|
|
// B
|
|
|
|
path.MoveTo(80, 20)
|
|
|
|
path.LineTo(80, 70)
|
|
|
|
path.LineTo(100, 70)
|
2019-12-30 08:14:47 +01:00
|
|
|
path.QuadTo(150, 57.5, 100, 45)
|
|
|
|
path.QuadTo(150, 32.5, 100, 20)
|
2019-12-28 06:57:50 +01:00
|
|
|
|
2019-12-28 07:20:05 +01:00
|
|
|
// I
|
|
|
|
path.MoveTo(140, 20)
|
|
|
|
path.LineTo(140, 70)
|
|
|
|
path.LineTo(150, 70)
|
|
|
|
path.LineTo(150, 20)
|
|
|
|
|
|
|
|
// T
|
|
|
|
path.MoveTo(160, 20)
|
|
|
|
path.LineTo(160, 30)
|
|
|
|
path.LineTo(180, 30)
|
|
|
|
path.LineTo(180, 70)
|
|
|
|
path.LineTo(190, 70)
|
|
|
|
path.LineTo(190, 30)
|
|
|
|
path.LineTo(210, 30)
|
|
|
|
path.LineTo(210, 20)
|
|
|
|
|
|
|
|
// E
|
|
|
|
path.MoveTo(220, 20)
|
|
|
|
path.LineTo(220, 70)
|
|
|
|
path.LineTo(270, 70)
|
|
|
|
path.LineTo(270, 60)
|
|
|
|
path.LineTo(230, 60)
|
|
|
|
path.LineTo(230, 50)
|
|
|
|
path.LineTo(270, 50)
|
|
|
|
path.LineTo(270, 40)
|
|
|
|
path.LineTo(230, 40)
|
|
|
|
path.LineTo(230, 30)
|
|
|
|
path.LineTo(270, 30)
|
|
|
|
path.LineTo(270, 20)
|
|
|
|
|
|
|
|
// N
|
|
|
|
path.MoveTo(280, 20)
|
|
|
|
path.LineTo(280, 70)
|
|
|
|
path.LineTo(290, 70)
|
|
|
|
path.LineTo(290, 35)
|
|
|
|
path.LineTo(320, 70)
|
|
|
|
path.LineTo(330, 70)
|
|
|
|
path.LineTo(330, 20)
|
|
|
|
path.LineTo(320, 20)
|
|
|
|
path.LineTo(320, 55)
|
|
|
|
path.LineTo(290, 20)
|
2019-12-27 03:26:16 +01:00
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
op := &ebiten.DrawTrianglesOptions{
|
2021-09-19 09:19:40 +02:00
|
|
|
FillRule: ebiten.EvenOdd,
|
2020-03-16 15:49:55 +01:00
|
|
|
}
|
2021-07-16 10:20:00 +02:00
|
|
|
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
|
2021-07-02 12:26:09 +02:00
|
|
|
for i := range vs {
|
2022-10-09 14:26:13 +02:00
|
|
|
vs[i].DstX *= scale
|
|
|
|
vs[i].DstY *= scale
|
2021-07-02 12:26:09 +02:00
|
|
|
vs[i].SrcX = 1
|
|
|
|
vs[i].SrcY = 1
|
|
|
|
vs[i].ColorR = 0xdb / float32(0xff)
|
|
|
|
vs[i].ColorG = 0x56 / float32(0xff)
|
|
|
|
vs[i].ColorB = 0x20 / float32(0xff)
|
|
|
|
}
|
|
|
|
screen.DrawTriangles(vs, is, emptySubImage, op)
|
2019-04-14 16:15:03 +02:00
|
|
|
}
|
|
|
|
|
2022-10-09 14:26:13 +02:00
|
|
|
func drawEbitenLogo(screen *ebiten.Image, x, y int, scale float32) {
|
2019-12-28 08:22:18 +01:00
|
|
|
const unit = 16
|
|
|
|
|
|
|
|
var path vector.Path
|
|
|
|
xf, yf := float32(x), float32(y)
|
|
|
|
|
2019-12-28 16:05:03 +01:00
|
|
|
// TODO: Add curves
|
2019-12-28 08:22:18 +01:00
|
|
|
path.MoveTo(xf, yf+4*unit)
|
|
|
|
path.LineTo(xf, yf+6*unit)
|
|
|
|
path.LineTo(xf+2*unit, yf+6*unit)
|
|
|
|
path.LineTo(xf+2*unit, yf+5*unit)
|
|
|
|
path.LineTo(xf+3*unit, yf+5*unit)
|
|
|
|
path.LineTo(xf+3*unit, yf+4*unit)
|
|
|
|
path.LineTo(xf+4*unit, yf+4*unit)
|
|
|
|
path.LineTo(xf+4*unit, yf+2*unit)
|
|
|
|
path.LineTo(xf+6*unit, yf+2*unit)
|
|
|
|
path.LineTo(xf+6*unit, yf+1*unit)
|
|
|
|
path.LineTo(xf+5*unit, yf+1*unit)
|
|
|
|
path.LineTo(xf+5*unit, yf)
|
|
|
|
path.LineTo(xf+4*unit, yf)
|
|
|
|
path.LineTo(xf+4*unit, yf+2*unit)
|
|
|
|
path.LineTo(xf+2*unit, yf+2*unit)
|
|
|
|
path.LineTo(xf+2*unit, yf+3*unit)
|
|
|
|
path.LineTo(xf+unit, yf+3*unit)
|
|
|
|
path.LineTo(xf+unit, yf+4*unit)
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
op := &ebiten.DrawTrianglesOptions{
|
2021-09-19 09:19:40 +02:00
|
|
|
FillRule: ebiten.EvenOdd,
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
2021-07-16 10:20:00 +02:00
|
|
|
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
|
2021-07-02 12:26:09 +02:00
|
|
|
for i := range vs {
|
2022-10-09 14:26:13 +02:00
|
|
|
vs[i].DstX *= scale
|
|
|
|
vs[i].DstY *= scale
|
2021-07-02 12:26:09 +02:00
|
|
|
vs[i].SrcX = 1
|
|
|
|
vs[i].SrcY = 1
|
|
|
|
vs[i].ColorR = 0xdb / float32(0xff)
|
|
|
|
vs[i].ColorG = 0x56 / float32(0xff)
|
|
|
|
vs[i].ColorB = 0x20 / float32(0xff)
|
2020-03-16 15:49:55 +01:00
|
|
|
}
|
2021-07-02 12:26:09 +02:00
|
|
|
screen.DrawTriangles(vs, is, emptySubImage, op)
|
2019-12-28 08:22:18 +01:00
|
|
|
}
|
|
|
|
|
2022-10-09 14:26:13 +02:00
|
|
|
func drawArc(screen *ebiten.Image, count int, scale float32) {
|
2021-07-16 09:58:20 +02:00
|
|
|
var path vector.Path
|
|
|
|
|
|
|
|
path.MoveTo(350, 100)
|
|
|
|
const cx, cy, r = 450, 100, 70
|
2021-07-16 18:29:41 +02:00
|
|
|
theta1 := math.Pi * float64(count) / 180
|
|
|
|
x := cx + r*math.Cos(theta1)
|
|
|
|
y := cy + r*math.Sin(theta1)
|
2021-07-16 09:58:20 +02:00
|
|
|
path.ArcTo(450, 100, float32(x), float32(y), 30)
|
|
|
|
|
2021-07-16 18:29:41 +02:00
|
|
|
theta2 := math.Pi * float64(count) / 180 / 3
|
|
|
|
path.MoveTo(550, 100)
|
2021-07-17 09:41:12 +02:00
|
|
|
path.Arc(550, 100, 50, float32(theta1), float32(theta2), vector.Clockwise)
|
2021-07-16 18:29:41 +02:00
|
|
|
|
2021-07-16 09:58:20 +02:00
|
|
|
op := &ebiten.DrawTrianglesOptions{
|
2021-09-19 09:19:40 +02:00
|
|
|
FillRule: ebiten.EvenOdd,
|
2021-07-16 09:58:20 +02:00
|
|
|
}
|
|
|
|
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
|
|
|
|
for i := range vs {
|
2022-10-09 14:26:13 +02:00
|
|
|
vs[i].DstX *= scale
|
|
|
|
vs[i].DstY *= scale
|
2021-07-16 09:58:20 +02:00
|
|
|
vs[i].SrcX = 1
|
|
|
|
vs[i].SrcY = 1
|
|
|
|
vs[i].ColorR = 0x33 / float32(0xff)
|
|
|
|
vs[i].ColorG = 0xcc / float32(0xff)
|
|
|
|
vs[i].ColorB = 0x66 / float32(0xff)
|
|
|
|
}
|
|
|
|
screen.DrawTriangles(vs, is, emptySubImage, op)
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:05:03 +01:00
|
|
|
func maxCounter(index int) int {
|
|
|
|
return 128 + (17*index+32)%64
|
|
|
|
}
|
|
|
|
|
2022-10-09 14:26:13 +02:00
|
|
|
func drawWave(screen *ebiten.Image, counter int, scale float32) {
|
2019-12-28 16:05:03 +01:00
|
|
|
var path vector.Path
|
|
|
|
|
|
|
|
const npoints = 8
|
|
|
|
indexToPoint := func(i int, counter int) (float32, float32) {
|
|
|
|
x, y := float32(i*screenWidth/(npoints-1)), float32(screenHeight/2)
|
|
|
|
y += float32(30 * math.Sin(float64(counter)*2*math.Pi/float64(maxCounter(i))))
|
|
|
|
return x, y
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i <= npoints; i++ {
|
|
|
|
if i == 0 {
|
|
|
|
path.MoveTo(indexToPoint(i, counter))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cpx0, cpy0 := indexToPoint(i-1, counter)
|
|
|
|
x, y := indexToPoint(i, counter)
|
|
|
|
cpx1, cpy1 := x, y
|
|
|
|
cpx0 += 30
|
|
|
|
cpx1 -= 30
|
2019-12-30 08:14:47 +01:00
|
|
|
path.CubicTo(cpx0, cpy0, cpx1, cpy1, x, y)
|
2019-12-28 16:05:03 +01:00
|
|
|
}
|
|
|
|
path.LineTo(screenWidth, screenHeight)
|
|
|
|
path.LineTo(0, screenHeight)
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
op := &ebiten.DrawTrianglesOptions{
|
2021-09-19 09:19:40 +02:00
|
|
|
FillRule: ebiten.EvenOdd,
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
2021-07-16 10:20:00 +02:00
|
|
|
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
|
2021-07-02 12:26:09 +02:00
|
|
|
for i := range vs {
|
2022-10-09 14:26:13 +02:00
|
|
|
vs[i].DstX *= scale
|
|
|
|
vs[i].DstY *= scale
|
2021-07-02 12:26:09 +02:00
|
|
|
vs[i].SrcX = 1
|
|
|
|
vs[i].SrcY = 1
|
|
|
|
vs[i].ColorR = 0x33 / float32(0xff)
|
|
|
|
vs[i].ColorG = 0x66 / float32(0xff)
|
|
|
|
vs[i].ColorB = 0xff / float32(0xff)
|
2020-03-16 15:49:55 +01:00
|
|
|
}
|
2021-07-02 12:26:09 +02:00
|
|
|
screen.DrawTriangles(vs, is, emptySubImage, op)
|
2019-12-28 16:05:03 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 14:36:46 +02:00
|
|
|
type Game struct {
|
|
|
|
counter int
|
2022-10-09 14:26:13 +02:00
|
|
|
|
|
|
|
aa bool
|
|
|
|
offscreen *ebiten.Image
|
2020-06-07 14:36:46 +02:00
|
|
|
}
|
2019-04-14 16:15:03 +02:00
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2020-06-07 14:36:46 +02:00
|
|
|
g.counter++
|
2022-10-09 14:26:13 +02:00
|
|
|
|
|
|
|
// Switch anti-alias.
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyA) {
|
|
|
|
g.aa = !g.aa
|
|
|
|
}
|
|
|
|
|
2020-06-07 14:36:46 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-14 16:15:03 +02:00
|
|
|
|
2020-06-07 14:36:46 +02:00
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
2022-10-09 14:26:13 +02:00
|
|
|
if g.offscreen != nil {
|
|
|
|
w, h := screen.Size()
|
|
|
|
if ow, oh := g.offscreen.Size(); ow != w || oh != h {
|
|
|
|
g.offscreen.Dispose()
|
|
|
|
g.offscreen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if g.aa && g.offscreen == nil {
|
|
|
|
w, h := screen.Size()
|
|
|
|
g.offscreen = ebiten.NewImage(w*2, h*2)
|
|
|
|
}
|
|
|
|
|
|
|
|
scale := float32(1)
|
|
|
|
dst := screen
|
|
|
|
if g.aa {
|
|
|
|
scale = 2
|
|
|
|
dst = g.offscreen
|
|
|
|
}
|
|
|
|
|
|
|
|
dst.Fill(color.White)
|
|
|
|
drawEbitenText(dst, scale)
|
|
|
|
drawEbitenLogo(dst, 20, 90, scale)
|
|
|
|
drawArc(dst, g.counter, scale)
|
|
|
|
drawWave(dst, g.counter, scale)
|
|
|
|
|
|
|
|
if g.aa {
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(0.5, 0.5)
|
|
|
|
op.Filter = ebiten.FilterLinear
|
|
|
|
screen.DrawImage(g.offscreen, op)
|
|
|
|
}
|
2019-12-28 16:05:03 +01:00
|
|
|
|
2022-10-09 14:26:13 +02:00
|
|
|
msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f\nPress A to switch anti-alias", ebiten.ActualTPS(), ebiten.ActualFPS())
|
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
2020-06-07 14:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
2019-04-13 17:16:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-06-07 14:36:46 +02:00
|
|
|
g := &Game{counter: 0}
|
|
|
|
|
|
|
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
2022-08-29 04:16:39 +02:00
|
|
|
ebiten.SetWindowTitle("Vector (Ebitengine Demo)")
|
2020-06-07 14:36:46 +02:00
|
|
|
if err := ebiten.RunGame(g); err != nil {
|
2019-04-13 17:16:18 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|