Compare commits

...

58 Commits

Author SHA1 Message Date
Bertrand Jung
4f3dc766ab
Merge 1dd96726c4 into 17322c9c3b 2024-08-17 16:37:04 +02:00
Hajime Hoshi
17322c9c3b all: reduce files for PS5 2024-08-17 23:06:01 +09:00
Hajime Hoshi
26038c2ac1 internal/ui: bug fix: an unexpected scroll bar was shown
Closes #3066
2024-08-16 23:57:52 +09:00
Hajime Hoshi
949d6abe6f all: update dependencies 2024-08-14 10:43:28 +09:00
Hajime Hoshi
abaad0b6f2 .github/workflows: use the released version of Go 1.23 2024-08-14 09:15:56 +09:00
Hajime Hoshi
435c8b75eb internal/graphicsdriver/opengl: automatically adjust the array buffer layout
Updates #2640
2024-08-12 04:08:02 +09:00
Hajime Hoshi
9e208eee81 all: use QuadVerticesFromDstAndSrc when possible 2024-08-12 02:42:36 +09:00
Hajime Hoshi
b6ab7a10c1 internal/graphics: unify QuadVertices
This is a preparation for adding members to Vertex.

Updates #2640
2024-08-12 00:29:24 +09:00
Hajime Hoshi
6cd00f3b88 internal/graphicsdriver/opengl: exclude playstation5 2024-08-11 22:41:21 +09:00
Hajime Hoshi
9a751d7c26 internal/mipmap: refactoring 2024-08-11 22:33:21 +09:00
Hajime Hoshi
e6807794f2 internal/mipmap: refactoring 2024-08-11 22:29:15 +09:00
Hajime Hoshi
5e820be911 internal/buffered: refactoring
This is a preparation for adding members to Vertex.

Updates #2640
2024-08-11 22:25:45 +09:00
Hajime Hoshi
5f80f4b3de all: refactoring 2024-08-11 21:43:00 +09:00
Hajime Hoshi
b9c24f786a ebiten: add BenchmarkDrawTriangles 2024-08-11 16:41:19 +09:00
Hajime Hoshi
89933bf0ab internal/graphicsdriver/playstation5: bug fix: compile error 2024-08-10 21:52:38 +09:00
Hajime Hoshi
332da38565 internal/graphicsdriver/playstation5: update DrawTriangles
A Go pointer in a C struct could cause some troubles.
2024-08-10 21:21:09 +09:00
Hajime Hoshi
fbf40a4455 vector: bug fix: isPointCloseToSegment didn't work when two p0 and p1 are the same
Closes #3061
2024-08-10 17:13:09 +09:00
Hajime Hoshi
fc37cdedeb vector: reuse previous allocated subpaths
Closes #3060
2024-08-10 15:13:23 +09:00
Hajime Hoshi
309c886c2e vector: use value type for subpath
Updates #3060
2024-08-10 15:04:13 +09:00
Hajime Hoshi
68380e506e vector: reduce memory allocations by reusing the same Path objects 2024-08-10 14:02:49 +09:00
Hajime Hoshi
cdb430b2a5 vector: reduce allocations 2024-08-10 05:35:29 +09:00
Hajime Hoshi
e8e458802d examples/vector: reduce allocations 2024-08-10 05:22:53 +09:00
Hajime Hoshi
071024e89f vector: reduce memory allocations in the utility functions 2024-08-10 05:11:46 +09:00
Hajime Hoshi
38b8ba5677 vector: lazy point calculation
This is a preparation for #2884.

Updates #2884
2024-08-10 04:09:39 +09:00
Hajime Hoshi
361da49887 .github/workflows: remove unnecessary environment variable
Updates #2944
2024-08-10 01:25:07 +09:00
Hajime Hoshi
a5235eea86 internal/graphicsdriver/opengl/gl: always prefer OpenGL ES to OpenGL
Closes #2944
2024-08-10 01:21:35 +09:00
Hajime Hoshi
1a0f50503d .github/workflows: update wasmbrowsertest for the websocket issue
See https://github.com/agnivade/wasmbrowsertest/issues/59.

Closes #2982
2024-08-09 16:02:46 +09:00
Hajime Hoshi
956a95c397 all: update Oto to v3.3.0-alpha.4 2024-08-09 00:08:22 +09:00
Hajime Hoshi
9c80367f2f internal/gamepad: ignore EACCES error for /dev/input
Updates #3057
2024-08-08 11:35:39 +09:00
Hajime Hoshi
3624486f8b all: update PureGo to v0.8.0-alpha.4 2024-08-08 00:07:40 +09:00
Hajime Hoshi
1f03971fa9 internal/debug: reland: rename functions 2024-08-07 23:48:35 +09:00
Hajime Hoshi
fab9482e0e Revert "internal/debug: rename functions"
This reverts commit 74722298a2.

Reason: This included an unexpected change in internal/gamepad
2024-08-07 23:47:51 +09:00
Hajime Hoshi
74722298a2 internal/debug: rename functions 2024-08-07 23:42:36 +09:00
Hajime Hoshi
13353dc465 all: update Oto
Updates ebitengine/oto#242
2024-08-06 13:21:57 +09:00
Hajime Hoshi
72f50c389b Revert "all: update Oto"
This reverts commit 8f36a479a3.

Reason: crash on tests
https://github.com/ebitengine/oto/actions/runs/10253513865/job/28366298117
2024-08-06 03:11:48 +09:00
Hajime Hoshi
8f36a479a3 all: update Oto
Updates ebitengine/oto#242
2024-08-06 02:45:09 +09:00
Hajime Hoshi
2a34d1d47b internal/graphicsdriver/opengl/gl: use PureGo version for Android again
This is basically a revert for 297efea68b.
This updates PureGo for the Android issue.

Updates #3052
Updates ebitengine/purego#262
2024-08-05 23:34:56 +09:00
Hajime Hoshi
1e3ab9e5fc all: update dependencies 2024-08-05 21:23:03 +09:00
Hajime Hoshi
aba652c323 internal/graphicsdriver/playstation5: add DrawTriangles 2024-08-04 19:13:36 +09:00
Hajime Hoshi
6555375b06 all: add .clang-format 2024-08-04 19:06:59 +09:00
Hajime Hoshi
9bd17de2d3 internal/graphicsdriver/opengl: remove unused members 2024-08-04 04:26:53 +09:00
Hajime Hoshi
37a6057230 internal/ui: bug fix: increase maxSkipCount
Closes #3055
2024-08-04 04:16:01 +09:00
Hajime Hoshi
af6072c1b4 internal/shader: add more tests
Updates #3011
2024-08-03 19:08:03 +09:00
Hajime Hoshi
7c4f532b83 internal/shader: bug fix: an index must be a constant for vectors
Updates #3011
2024-08-03 19:03:42 +09:00
Hajime Hoshi
719838b7ab .github/workflows: update wasmbrowsertest to fix timeout issue
The issue agnivade/wasmbrowsertest#60 was fixed.

Updates #1313
2024-08-02 23:03:01 +09:00
Hajime Hoshi
1fc2dedaaa all: update gomobile 2024-08-02 13:33:55 +09:00
Hajime Hoshi
297efea68b internal/graphicsdriver/opengl/gl: use C version for Android
Unfortunately PureGo's dlopen didn't work on some Android devices.
Use the Cgo version instead in the same way as v2.7.

Closes #3052
2024-08-01 11:52:07 +09:00
Hajime Hoshi
856b339298 internal/ui: bug fix: show a black dot at the red button when needed
When SetWindowClosingHandled(true) is called, a black dot should be
shown at the red button to close the window.

Closes #3020
2024-07-31 23:54:18 +09:00
Hajime Hoshi
3106f98b52 internal/graphicsdriver/opengl/gl: add more error messages to load libraries 2024-07-31 21:35:24 +09:00
Hajime Hoshi
1843f6acc1 internal/ui: bug fix: dropping multiple files didn't work on Firefox and Safari
Closes #3045
2024-07-31 11:50:30 +09:00
Hajime Hoshi
95ad1b158c internal/file: refactoring
Updates #3045
2024-07-31 11:21:35 +09:00
Hajime Hoshi
47b8af554c all: update Oto to v3.3.0-alpha.3 2024-07-31 00:15:04 +09:00
Hajime Hoshi
c8aea2df16 examples/blocks: bug fix: do not ignore error 2024-07-29 17:37:30 +09:00
Hajime Hoshi
0281ac7bd2 all: use bufio.NewWriter to improve writing speed 2024-07-29 16:29:57 +09:00
Hajime Hoshi
ef5ac4175d all: use bufio.Reader to improve reading speed 2024-07-29 02:28:37 +09:00
Hajime Hoshi
563f2e0e0b all: update Oto 2024-07-28 20:20:13 +09:00
Hajime Hoshi
09b41846f9 all: update Oto (bug fix) 2024-07-28 17:13:13 +09:00
Hajime Hoshi
b6d5b8a1a6 all: update Oto (use AudioWorklet)
Updates ebitengine/oto#241
2024-07-28 16:34:01 +09:00
58 changed files with 1158 additions and 588 deletions

1
.clang-format Normal file
View File

@ -0,0 +1 @@
CommentPragmas: '^go:build'

View File

@ -7,7 +7,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go: ['1.19.x', '1.20.x', '1.21.x', '1.22.x', '1.23.0-rc.2']
go: ['1.19.x', '1.20.x', '1.21.x', '1.22.x', '1.23.x']
name: Test with Go ${{ matrix.go }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
env:
@ -42,9 +42,12 @@ jobs:
sudo apt-get update
sudo apt-get install libasound2-dev libgl1-mesa-dev libxcursor-dev libxi-dev libxinerama-dev libxrandr-dev libxxf86vm-dev
- name: Install Chrome
uses: browser-actions/setup-chrome@latest
- name: Install wasmbrowsertest
run: |
wasmbrowsertest_version=6e494bb3a5ddfe6cccb449250dbdcaa5777b593d
wasmbrowsertest_version=06679196c7e76f227e71456cdc16fccd6cc33601
go install github.com/agnivade/wasmbrowsertest@${wasmbrowsertest_version}
mv $(go env GOPATH)/bin/wasmbrowsertest${{ runner.os == 'Windows' && '.exe' || '' }} $(go env GOPATH)/bin/go_js_wasm_exec${{ runner.os == 'Windows' && '.exe' || '' }}
go install github.com/agnivade/wasmbrowsertest/cmd/cleanenv@${wasmbrowsertest_version}
@ -150,7 +153,7 @@ jobs:
if: runner.os == 'Linux'
run: |
sudo apt-get install libgles2-mesa-dev
env EBITENGINE_GRAPHICS_LIBRARY=opengl EBITENGINE_OPENGL=es go test -shuffle=on -v -p=1 ./...
env EBITENGINE_GRAPHICS_LIBRARY=opengl go test -shuffle=on -v -p=1 ./...
- name: go test (Windows)
if: runner.os == 'Windows'
@ -165,10 +168,9 @@ jobs:
env GOARCH=386 EBITENGINE_DIRECTX=version=12 go test -shuffle=on -v ./...
- name: go test (Wasm)
if: ${{ runner.os != 'macOS' && runner.os != 'Windows' }}
if: runner.os != 'macOS'
run: |
# Wasm tests don't work on macOS with the headless mode enabled, but the headless mode cannot be disabled in GitHub Actions (#2972).
# Wasm tests don't work on Windows well due to mysterious timeouts (#2982).
env GOOS=js GOARCH=wasm cleanenv -remove-prefix GITHUB_ -remove-prefix JAVA_ -remove-prefix PSModulePath -remove-prefix STATS_ -remove-prefix RUNNER_ -- go test -shuffle=on -v ./...
- name: Install ebitenmobile

View File

@ -17,7 +17,6 @@ package audio_test
import (
"bytes"
"io"
"os"
"runtime"
"testing"
"time"
@ -36,14 +35,6 @@ func teardown() {
context = nil
}
func TestMain(m *testing.M) {
code := m.Run()
// 200[ms] should be enough all the players are consumed.
// TODO: This is a dirty hack. Would it be possible to use virtual time?
time.Sleep(200 * time.Millisecond)
os.Exit(code)
}
// Issue #746
func TestGC(t *testing.T) {
setup()

View File

@ -18,22 +18,12 @@ import (
"bytes"
"io"
"math/rand" // TODO: Use math/rand/v2 when the minimum supported version becomes Go 1.22.
"os"
"testing"
"time"
"unsafe"
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
)
func TestMain(m *testing.M) {
code := m.Run()
// Tests in this package often fails on GitHub Actions due to unfinished goroutines.
// That's mysterious, but to avoid this, sleep for a while before exiting.
time.Sleep(200 * time.Millisecond)
os.Exit(code)
}
func randInt16s(n int) []int16 {
r := make([]int16, n)
for i := range r {

5
doc.go
View File

@ -93,11 +93,6 @@
// The option "featurelevel" is valid only for DirectX 12.
// The possible values are "11_0", "11_1", "12_0", "12_1", and "12_2". The default value is "11_0".
//
// `EBITENGINE_OPENGL` environment variable specifies various parameters for OpenGL.
// You can specify multiple values separated by a comma. The default value is empty (i.e. no parameters).
//
// "es": Use OpenGL ES. Without this, OpenGL and OpenGL ES are automatically chosen.
//
// # Build tags
//
// `ebitenginedebug` outputs a log of graphics commands. This is useful to know what happens in Ebitengine. In general, the

View File

@ -17,6 +17,7 @@
package main
import (
"bufio"
"fmt"
"image"
"image/color"
@ -77,7 +78,11 @@ func run() error {
}
defer f.Close()
if err := png.Encode(f, dst); err != nil {
w := bufio.NewWriter(f)
if err := png.Encode(w, dst); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}

View File

@ -15,6 +15,7 @@
package main
import (
"bufio"
"flag"
"log"
"os"
@ -33,9 +34,15 @@ func main() {
if err != nil {
log.Fatal(err)
}
if err := pprof.StartCPUProfile(f); err != nil {
w := bufio.NewWriter(f)
if err := pprof.StartCPUProfile(w); err != nil {
log.Fatal(err)
}
defer func() {
if err := w.Flush(); err != nil {
log.Fatal(err)
}
}()
defer pprof.StopCPUProfile()
}

View File

@ -17,6 +17,7 @@
package main
import (
"bufio"
"bytes"
"image"
"image/png"
@ -220,7 +221,11 @@ func outputKeyboardImage() (map[ebiten.Key]image.Rectangle, error) {
}
defer out.Close()
if err := png.Encode(out, img); err != nil {
w := bufio.NewWriter(out)
if err := png.Encode(w, img); err != nil {
return nil, err
}
if err := w.Flush(); err != nil {
return nil, err
}
@ -278,10 +283,17 @@ func outputKeyRectsGo(k map[ebiten.Key]image.Rectangle) error {
if err != nil {
return err
}
return tmpl.Execute(f, map[string]any{
w := bufio.NewWriter(f)
if err := tmpl.Execute(w, map[string]any{
"License": license,
"KeyRectsMap": k,
})
}); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
return nil
}
type game struct {

View File

@ -20,6 +20,7 @@
package main
import (
"bufio"
"errors"
"fmt"
"os"
@ -88,7 +89,15 @@ func generateHSLSFiles(source *shaderprecomp.ShaderSource, index int, tmpdir str
}
defer psf.Close()
if err := shaderprecomp.CompileToHLSL(vsf, psf, source); err != nil {
vsfw := bufio.NewWriter(vsf)
psfw := bufio.NewWriter(psf)
if err := shaderprecomp.CompileToHLSL(vsfw, psfw, source); err != nil {
return "", "", err
}
if err := vsfw.Flush(); err != nil {
return "", "", err
}
if err := psfw.Flush(); err != nil {
return "", "", err
}

View File

@ -20,6 +20,7 @@
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
@ -68,10 +69,11 @@ func compile(source *shaderprecomp.ShaderSource, index int, tmpdir string) error
}
defer f.Close()
if err := shaderprecomp.CompileToMSL(f, source); err != nil {
w := bufio.NewWriter(f)
if err := shaderprecomp.CompileToMSL(w, source); err != nil {
return err
}
if err := f.Sync(); err != nil {
if err := w.Flush(); err != nil {
return err
}

View File

@ -44,7 +44,17 @@ const (
screenHeight = 480
)
func drawEbitenText(screen *ebiten.Image, x, y int, aa bool, line bool) {
type Game struct {
counter int
aa bool
line bool
vertices []ebiten.Vertex
indices []uint16
}
func (g *Game) drawEbitenText(screen *ebiten.Image, x, y int, aa bool, line bool) {
var path vector.Path
// E
@ -116,26 +126,24 @@ func drawEbitenText(screen *ebiten.Image, x, y int, aa bool, line bool) {
path.LineTo(290, 20)
path.Close()
var vs []ebiten.Vertex
var is []uint16
if line {
op := &vector.StrokeOptions{}
op.Width = 5
op.LineJoin = vector.LineJoinRound
vs, is = path.AppendVerticesAndIndicesForStroke(nil, nil, op)
g.vertices, g.indices = path.AppendVerticesAndIndicesForStroke(g.vertices[:0], g.indices[:0], op)
} else {
vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil)
g.vertices, g.indices = path.AppendVerticesAndIndicesForFilling(g.vertices[:0], g.indices[:0])
}
for i := range vs {
vs[i].DstX = (vs[i].DstX + float32(x))
vs[i].DstY = (vs[i].DstY + float32(y))
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)
vs[i].ColorA = 1
for i := range g.vertices {
g.vertices[i].DstX = (g.vertices[i].DstX + float32(x))
g.vertices[i].DstY = (g.vertices[i].DstY + float32(y))
g.vertices[i].SrcX = 1
g.vertices[i].SrcY = 1
g.vertices[i].ColorR = 0xdb / float32(0xff)
g.vertices[i].ColorG = 0x56 / float32(0xff)
g.vertices[i].ColorB = 0x20 / float32(0xff)
g.vertices[i].ColorA = 1
}
op := &ebiten.DrawTrianglesOptions{}
@ -150,10 +158,10 @@ func drawEbitenText(screen *ebiten.Image, x, y int, aa bool, line bool) {
// For simplicity, this example always uses FillRuleNonZero, whichever strokes or filling is done.
op.FillRule = ebiten.FillRuleNonZero
screen.DrawTriangles(vs, is, whiteSubImage, op)
screen.DrawTriangles(g.vertices, g.indices, whiteSubImage, op)
}
func drawEbitenLogo(screen *ebiten.Image, x, y int, aa bool, line bool) {
func (g *Game) drawEbitenLogo(screen *ebiten.Image, x, y int, aa bool, line bool) {
const unit = 16
var path vector.Path
@ -179,35 +187,33 @@ func drawEbitenLogo(screen *ebiten.Image, x, y int, aa bool, line bool) {
path.LineTo(unit, 4*unit)
path.Close()
var vs []ebiten.Vertex
var is []uint16
if line {
op := &vector.StrokeOptions{}
op.Width = 5
op.LineJoin = vector.LineJoinRound
vs, is = path.AppendVerticesAndIndicesForStroke(nil, nil, op)
g.vertices, g.indices = path.AppendVerticesAndIndicesForStroke(g.vertices[:0], g.indices[:0], op)
} else {
vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil)
g.vertices, g.indices = path.AppendVerticesAndIndicesForFilling(g.vertices[:0], g.indices[:0])
}
for i := range vs {
vs[i].DstX = (vs[i].DstX + float32(x))
vs[i].DstY = (vs[i].DstY + float32(y))
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)
vs[i].ColorA = 1
for i := range g.vertices {
g.vertices[i].DstX = (g.vertices[i].DstX + float32(x))
g.vertices[i].DstY = (g.vertices[i].DstY + float32(y))
g.vertices[i].SrcX = 1
g.vertices[i].SrcY = 1
g.vertices[i].ColorR = 0xdb / float32(0xff)
g.vertices[i].ColorG = 0x56 / float32(0xff)
g.vertices[i].ColorB = 0x20 / float32(0xff)
g.vertices[i].ColorA = 1
}
op := &ebiten.DrawTrianglesOptions{}
op.AntiAlias = aa
op.FillRule = ebiten.FillRuleNonZero
screen.DrawTriangles(vs, is, whiteSubImage, op)
screen.DrawTriangles(g.vertices, g.indices, whiteSubImage, op)
}
func drawArc(screen *ebiten.Image, count int, aa bool, line bool) {
func (g *Game) drawArc(screen *ebiten.Image, count int, aa bool, line bool) {
var path vector.Path
path.MoveTo(350, 100)
@ -223,37 +229,35 @@ func drawArc(screen *ebiten.Image, count int, aa bool, line bool) {
path.Arc(550, 100, 50, float32(theta1), float32(theta2), vector.Clockwise)
path.Close()
var vs []ebiten.Vertex
var is []uint16
if line {
op := &vector.StrokeOptions{}
op.Width = 5
op.LineJoin = vector.LineJoinRound
vs, is = path.AppendVerticesAndIndicesForStroke(nil, nil, op)
g.vertices, g.indices = path.AppendVerticesAndIndicesForStroke(g.vertices[:0], g.indices[:0], op)
} else {
vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil)
g.vertices, g.indices = path.AppendVerticesAndIndicesForFilling(g.vertices[:0], g.indices[:0])
}
for i := range vs {
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)
vs[i].ColorA = 1
for i := range g.vertices {
g.vertices[i].SrcX = 1
g.vertices[i].SrcY = 1
g.vertices[i].ColorR = 0x33 / float32(0xff)
g.vertices[i].ColorG = 0xcc / float32(0xff)
g.vertices[i].ColorB = 0x66 / float32(0xff)
g.vertices[i].ColorA = 1
}
op := &ebiten.DrawTrianglesOptions{}
op.AntiAlias = aa
op.FillRule = ebiten.FillRuleNonZero
screen.DrawTriangles(vs, is, whiteSubImage, op)
screen.DrawTriangles(g.vertices, g.indices, whiteSubImage, op)
}
func maxCounter(index int) int {
return 128 + (17*index+32)%64
}
func drawWave(screen *ebiten.Image, counter int, aa bool, line bool) {
func (g *Game) drawWave(screen *ebiten.Image, counter int, aa bool, line bool) {
var path vector.Path
const npoints = 8
@ -278,37 +282,28 @@ func drawWave(screen *ebiten.Image, counter int, aa bool, line bool) {
path.LineTo(screenWidth, screenHeight)
path.LineTo(0, screenHeight)
var vs []ebiten.Vertex
var is []uint16
if line {
op := &vector.StrokeOptions{}
op.Width = 5
op.LineJoin = vector.LineJoinRound
vs, is = path.AppendVerticesAndIndicesForStroke(nil, nil, op)
g.vertices, g.indices = path.AppendVerticesAndIndicesForStroke(g.vertices[:0], g.indices[:0], op)
} else {
vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil)
g.vertices, g.indices = path.AppendVerticesAndIndicesForFilling(g.vertices[:0], g.indices[:0])
}
for i := range vs {
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)
vs[i].ColorA = 1
for i := range g.vertices {
g.vertices[i].SrcX = 1
g.vertices[i].SrcY = 1
g.vertices[i].ColorR = 0x33 / float32(0xff)
g.vertices[i].ColorG = 0x66 / float32(0xff)
g.vertices[i].ColorB = 0xff / float32(0xff)
g.vertices[i].ColorA = 1
}
op := &ebiten.DrawTrianglesOptions{}
op.AntiAlias = aa
op.FillRule = ebiten.FillRuleNonZero
screen.DrawTriangles(vs, is, whiteSubImage, op)
}
type Game struct {
counter int
aa bool
line bool
screen.DrawTriangles(g.vertices, g.indices, whiteSubImage, op)
}
func (g *Game) Update() error {
@ -331,10 +326,10 @@ func (g *Game) Draw(screen *ebiten.Image) {
dst := screen
dst.Fill(color.RGBA{0xe0, 0xe0, 0xe0, 0xff})
drawEbitenText(dst, 0, 50, g.aa, g.line)
drawEbitenLogo(dst, 20, 150, g.aa, g.line)
drawArc(dst, g.counter, g.aa, g.line)
drawWave(dst, g.counter, g.aa, g.line)
g.drawEbitenText(dst, 0, 50, g.aa, g.line)
g.drawEbitenLogo(dst, 20, 150, g.aa, g.line)
g.drawArc(dst, g.counter, g.aa, g.line)
g.drawWave(dst, g.counter, g.aa, g.line)
msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS())
msg += "\nPress A to switch anti-alias."

View File

@ -15,6 +15,7 @@
package main
import (
"bufio"
"bytes"
_ "embed"
"fmt"
@ -81,7 +82,7 @@ func main() {
fmt.Println("Play the default video. You can specify a video file as an argument.")
}
player, err := newMPEGPlayer(in)
player, err := newMPEGPlayer(bufio.NewReader(in))
if err != nil {
log.Fatal(err)
}

View File

@ -19,6 +19,7 @@
package main
import (
"bufio"
"log"
"os"
"path/filepath"
@ -809,7 +810,8 @@ func main() {
buildConstraints = "//go:build !android && !ios && !js && !nintendosdk && !playstation5"
}
// NOTE: According to godoc, maps are automatically sorted by key.
if err := tmpl.Execute(f, struct {
w := bufio.NewWriter(f)
if err := tmpl.Execute(w, struct {
License string
DoNotEdit string
BuildConstraints string
@ -840,5 +842,9 @@ func main() {
}); err != nil {
log.Fatal(err)
}
if err := w.Flush(); err != nil {
log.Fatal(err)
}
}
}

18
go.mod
View File

@ -3,10 +3,10 @@ module github.com/hajimehoshi/ebiten/v2
go 1.19
require (
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895
github.com/ebitengine/gomobile v0.0.0-20240802043200-192f051f4fcc
github.com/ebitengine/hideconsole v1.0.0
github.com/ebitengine/oto/v3 v3.3.0-alpha.2
github.com/ebitengine/purego v0.8.0-alpha.3
github.com/ebitengine/oto/v3 v3.3.0-alpha.4
github.com/ebitengine/purego v0.8.0-alpha.4
github.com/gen2brain/mpeg v0.3.2-0.20240412154320-a2ac4fc8a46f
github.com/go-text/typesetting v0.1.1
github.com/hajimehoshi/bitmapfont/v3 v3.2.0-alpha.3
@ -15,15 +15,15 @@ require (
github.com/jezek/xgb v1.1.1
github.com/jfreymuth/oggvorbis v1.0.5
github.com/kisielk/errcheck v1.7.0
golang.org/x/image v0.18.0
golang.org/x/sync v0.7.0
golang.org/x/sys v0.22.0
golang.org/x/text v0.16.0
golang.org/x/tools v0.23.0
golang.org/x/image v0.19.0
golang.org/x/sync v0.8.0
golang.org/x/sys v0.24.0
golang.org/x/text v0.17.0
golang.org/x/tools v0.24.0
)
require (
github.com/jfreymuth/vorbis v1.0.2 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/mod v0.20.0 // indirect
)

33
go.sum
View File

@ -1,11 +1,11 @@
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 h1:48bCqKTuD7Z0UovDfvpCn7wZ0GUZ+yosIteNDthn3FU=
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895/go.mod h1:XZdLv05c5hOZm3fM2NlJ92FyEZjnslcMcNRrhxs8+8M=
github.com/ebitengine/gomobile v0.0.0-20240802043200-192f051f4fcc h1:76TYsaP1F48tiQRlrr71NsbfxBcFM9/8bEHS9/JbsQg=
github.com/ebitengine/gomobile v0.0.0-20240802043200-192f051f4fcc/go.mod h1:RM/c3pvru6dRqgGEW7RCTb6czFXYAa3MxbXu3u8/dcI=
github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE=
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
github.com/ebitengine/oto/v3 v3.3.0-alpha.2 h1:ex2UbULSPuLt76yyaR2JBXICx83Ph6Oz5ugN7h1Jo/I=
github.com/ebitengine/oto/v3 v3.3.0-alpha.2/go.mod h1:yYvXK7mgNwsFawY5RsvGI6yhMHtD+0MfaPkDTl9/uv8=
github.com/ebitengine/purego v0.8.0-alpha.3 h1:qoFlpGuVwJ6J85kuj6Qpyp0DBgxsNYfSY9efidSNFgA=
github.com/ebitengine/purego v0.8.0-alpha.3/go.mod h1:b94LtM1jUWDZPKDyENVhB0WsLdLWFApjbNw5AyxmKyI=
github.com/ebitengine/oto/v3 v3.3.0-alpha.4 h1:w9SD7kK4GgJULkh5pWVTToMA5Ia1bP7VxD4rIjQqb8M=
github.com/ebitengine/oto/v3 v3.3.0-alpha.4/go.mod h1:B+Sz3hzZXcx251YqSPIj+cVMicvlx7Xiq29AEUIbc7E=
github.com/ebitengine/purego v0.8.0-alpha.4 h1:Dg9xRGC3giyQedfISyHH94eQM0md4a84+HHr7KBBH/Q=
github.com/ebitengine/purego v0.8.0-alpha.4/go.mod h1:SQ56/omnSL8DdaBSKswoBvsMjgaWQyxyeMtb48sOskI=
github.com/gen2brain/mpeg v0.3.2-0.20240412154320-a2ac4fc8a46f h1:ysqRe+lvUiL0dH5XzkH0Bz68bFMPJ4f5Si4L/HD9SGk=
github.com/gen2brain/mpeg v0.3.2-0.20240412154320-a2ac4fc8a46f/go.mod h1:i/ebyRRv/IoHixuZ9bElZnXbmfoUVPGQpdsJ4sVuX38=
github.com/go-text/typesetting v0.1.1 h1:bGAesCuo85nXnEN5LmFMVGAGpGkCPtHrZLi//qD7EJo=
@ -37,15 +37,16 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ=
golang.org/x/image v0.19.0/go.mod h1:y0zrRqlQRWQ5PXaYCOMLTW2fpsxZ8Qh9I/ohnInJEys=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
@ -59,8 +60,9 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -73,8 +75,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
@ -91,14 +93,15 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@ -247,7 +247,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) {
colorm, cr, cg, cb, ca := colorMToScale(options.ColorM.affineColorM())
cr, cg, cb, ca = options.ColorScale.apply(cr, cg, cb, ca)
vs := i.ensureTmpVertices(4 * graphics.VertexFloatCount)
graphics.QuadVertices(vs, float32(sx0), float32(sy0), float32(sx1), float32(sy1), a, b, c, d, tx, ty, cr, cg, cb, ca)
graphics.QuadVerticesFromSrcAndMatrix(vs, float32(sx0), float32(sy0), float32(sx1), float32(sy1), a, b, c, d, tx, ty, cr, cg, cb, ca)
is := graphics.QuadIndices()
srcs := [graphics.ShaderSrcImageCount]*ui.Image{img.image}
@ -829,7 +829,7 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR
vs := i.ensureTmpVertices(4 * graphics.VertexFloatCount)
// Do not use srcRegions[0].Dx() and srcRegions[0].Dy() as these might be empty.
graphics.QuadVertices(vs,
graphics.QuadVerticesFromSrcAndMatrix(vs,
float32(srcRegions[0].Min.X), float32(srcRegions[0].Min.Y),
float32(srcRegions[0].Min.X+width), float32(srcRegions[0].Min.Y+height),
a, b, c, d, tx, ty, cr, cg, cb, ca)

View File

@ -662,6 +662,59 @@ func BenchmarkDrawImage(b *testing.B) {
}
}
func BenchmarkDrawTriangles(b *testing.B) {
const w, h = 16, 16
img0 := ebiten.NewImage(w, h)
img1 := ebiten.NewImage(w, h)
op := &ebiten.DrawTrianglesOptions{}
vs := []ebiten.Vertex{
{
DstX: 0,
DstY: 0,
SrcX: 0,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: w,
DstY: 0,
SrcX: w,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: 0,
DstY: h,
SrcX: 0,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: w,
DstY: h,
SrcX: w,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
}
is := []uint16{0, 1, 2, 1, 2, 3}
for i := 0; i < b.N; i++ {
img0.DrawTriangles(vs, is, img1, op)
}
}
func TestImageLinearGraduation(t *testing.T) {
img0 := ebiten.NewImage(2, 2)
img0.WritePixels([]byte{

View File

@ -43,16 +43,6 @@ func min(a, b int) int {
return b
}
// quadVertices returns vertices to render a quad. These values are passed to graphicscommand.Image.
func quadVertices(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, cr, cg, cb, ca float32) []float32 {
return []float32{
dx0, dy0, sx0, sy0, cr, cg, cb, ca,
dx1, dy0, sx1, sy0, cr, cg, cb, ca,
dx0, dy1, sx0, sy1, cr, cg, cb, ca,
dx1, dy1, sx1, sy1, cr, cg, cb, ca,
}
}
func appendDeferred(f func()) {
deferredM.Lock()
defer deferredM.Unlock()
@ -149,7 +139,8 @@ func (b *backend) extendIfNeeded(width, height int) {
srcs := [graphics.ShaderSrcImageCount]*graphicscommand.Image{b.image}
sw, sh := b.image.InternalSize()
vs := quadVertices(0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1)
vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1)
is := graphics.QuadIndices()
dr := image.Rect(0, 0, sw, sh)
newImg.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader.ensureShader(), nil, graphicsdriver.FillRuleFillAll)
@ -174,7 +165,8 @@ func newClearedImage(width, height int, screen bool) *graphicscommand.Image {
}
func clearImage(i *graphicscommand.Image, region image.Rectangle) {
vs := quadVertices(float32(region.Min.X), float32(region.Min.Y), float32(region.Max.X), float32(region.Max.Y), 0, 0, 0, 0, 0, 0, 0, 0)
vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVerticesFromDstAndSrc(vs, float32(region.Min.X), float32(region.Min.Y), float32(region.Max.X), float32(region.Max.Y), 0, 0, 0, 0, 0, 0, 0, 0)
is := graphics.QuadIndices()
i.DrawTriangles([graphics.ShaderSrcImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderSrcImageCount]image.Rectangle{}, clearShader.ensureShader(), nil, graphicsdriver.FillRuleFillAll)
}
@ -353,7 +345,7 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) {
w, h := float32(i.width), float32(i.height)
vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVertices(vs, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, w, h, 0, 0, w, h, 1, 1, 1, 1)
is := graphics.QuadIndices()
dr := image.Rect(0, 0, i.width, i.height)
@ -384,7 +376,7 @@ func (i *Image) putOnSourceBackend() {
w, h := float32(i.width), float32(i.height)
vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVertices(vs, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, w, h, 0, 0, w, h, 1, 1, 1, 1)
is := graphics.QuadIndices()
dr := image.Rect(0, 0, i.width, i.height)
newI.drawTriangles([graphics.ShaderSrcImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll)
@ -866,7 +858,7 @@ func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error {
}()
if debug.IsDebug {
debug.Logf("Internal image sizes:\n")
debug.FrameLogf("Internal image sizes:\n")
imgs := make([]*graphicscommand.Image, 0, len(theBackends))
for _, backend := range theBackends {
imgs = append(imgs, backend.image)

View File

@ -50,12 +50,9 @@ func quadVertices(sw, sh, x, y int, scalex float32) []float32 {
sy0 := float32(0)
sx1 := float32(sw)
sy1 := float32(sh)
return []float32{
dx0, dy0, sx0, sy0, 1, 1, 1, 1,
dx1, dy0, sx1, sy0, 1, 1, 1, 1,
dx0, dy1, sx0, sy1, 1, 1, 1, 1,
dx1, dy1, sx1, sy1, 1, 1, 1, 1,
}
vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVerticesFromDstAndSrc(vs, dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, 1, 1, 1, 1)
return vs
}
const bigSize = 2049

View File

@ -246,45 +246,51 @@ func (i *Image) syncPixelsIfNeeded() {
cbf := float32(c[2]) / 0xff
caf := float32(c[3]) / 0xff
vs[graphics.VertexFloatCount*4*idx] = dx
vs[graphics.VertexFloatCount*4*idx+1] = dy
vs[graphics.VertexFloatCount*4*idx+2] = sx
vs[graphics.VertexFloatCount*4*idx+3] = sy
vs[graphics.VertexFloatCount*4*idx+4] = crf
vs[graphics.VertexFloatCount*4*idx+5] = cgf
vs[graphics.VertexFloatCount*4*idx+6] = cbf
vs[graphics.VertexFloatCount*4*idx+7] = caf
vs[graphics.VertexFloatCount*4*idx+8] = dx + 1
vs[graphics.VertexFloatCount*4*idx+9] = dy
vs[graphics.VertexFloatCount*4*idx+10] = sx + 1
vs[graphics.VertexFloatCount*4*idx+11] = sy
vs[graphics.VertexFloatCount*4*idx+12] = crf
vs[graphics.VertexFloatCount*4*idx+13] = cgf
vs[graphics.VertexFloatCount*4*idx+14] = cbf
vs[graphics.VertexFloatCount*4*idx+15] = caf
vs[graphics.VertexFloatCount*4*idx+16] = dx
vs[graphics.VertexFloatCount*4*idx+17] = dy + 1
vs[graphics.VertexFloatCount*4*idx+18] = sx
vs[graphics.VertexFloatCount*4*idx+19] = sy + 1
vs[graphics.VertexFloatCount*4*idx+20] = crf
vs[graphics.VertexFloatCount*4*idx+21] = cgf
vs[graphics.VertexFloatCount*4*idx+22] = cbf
vs[graphics.VertexFloatCount*4*idx+23] = caf
vs[graphics.VertexFloatCount*4*idx+24] = dx + 1
vs[graphics.VertexFloatCount*4*idx+25] = dy + 1
vs[graphics.VertexFloatCount*4*idx+26] = sx + 1
vs[graphics.VertexFloatCount*4*idx+27] = sy + 1
vs[graphics.VertexFloatCount*4*idx+28] = crf
vs[graphics.VertexFloatCount*4*idx+29] = cgf
vs[graphics.VertexFloatCount*4*idx+30] = cbf
vs[graphics.VertexFloatCount*4*idx+31] = caf
vidx := 4 * idx
iidx := 6 * idx
is[6*idx] = uint32(4 * idx)
is[6*idx+1] = uint32(4*idx + 1)
is[6*idx+2] = uint32(4*idx + 2)
is[6*idx+3] = uint32(4*idx + 1)
is[6*idx+4] = uint32(4*idx + 2)
is[6*idx+5] = uint32(4*idx + 3)
vs[graphics.VertexFloatCount*vidx] = dx
vs[graphics.VertexFloatCount*vidx+1] = dy
vs[graphics.VertexFloatCount*vidx+2] = sx
vs[graphics.VertexFloatCount*vidx+3] = sy
vs[graphics.VertexFloatCount*vidx+4] = crf
vs[graphics.VertexFloatCount*vidx+5] = cgf
vs[graphics.VertexFloatCount*vidx+6] = cbf
vs[graphics.VertexFloatCount*vidx+7] = caf
vs[graphics.VertexFloatCount*(vidx+1)] = dx + 1
vs[graphics.VertexFloatCount*(vidx+1)+1] = dy
vs[graphics.VertexFloatCount*(vidx+1)+2] = sx + 1
vs[graphics.VertexFloatCount*(vidx+1)+3] = sy
vs[graphics.VertexFloatCount*(vidx+1)+4] = crf
vs[graphics.VertexFloatCount*(vidx+1)+5] = cgf
vs[graphics.VertexFloatCount*(vidx+1)+6] = cbf
vs[graphics.VertexFloatCount*(vidx+1)+7] = caf
vs[graphics.VertexFloatCount*(vidx+2)] = dx
vs[graphics.VertexFloatCount*(vidx+2)+1] = dy + 1
vs[graphics.VertexFloatCount*(vidx+2)+2] = sx
vs[graphics.VertexFloatCount*(vidx+2)+3] = sy + 1
vs[graphics.VertexFloatCount*(vidx+2)+4] = crf
vs[graphics.VertexFloatCount*(vidx+2)+5] = cgf
vs[graphics.VertexFloatCount*(vidx+2)+6] = cbf
vs[graphics.VertexFloatCount*(vidx+2)+7] = caf
vs[graphics.VertexFloatCount*(vidx+3)] = dx + 1
vs[graphics.VertexFloatCount*(vidx+3)+1] = dy + 1
vs[graphics.VertexFloatCount*(vidx+3)+2] = sx + 1
vs[graphics.VertexFloatCount*(vidx+3)+3] = sy + 1
vs[graphics.VertexFloatCount*(vidx+3)+4] = crf
vs[graphics.VertexFloatCount*(vidx+3)+5] = cgf
vs[graphics.VertexFloatCount*(vidx+3)+6] = cbf
vs[graphics.VertexFloatCount*(vidx+3)+7] = caf
is[iidx] = uint32(vidx)
is[iidx+1] = uint32(vidx + 1)
is[iidx+2] = uint32(vidx + 2)
is[iidx+3] = uint32(vidx + 1)
is[iidx+4] = uint32(vidx + 2)
is[iidx+5] = uint32(vidx + 3)
idx++
}

View File

@ -52,7 +52,7 @@ func TestUnsyncedPixels(t *testing.T) {
// Flush unsynced pixel cache.
src := buffered.NewImage(16, 16, atlas.ImageTypeRegular)
vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVertices(vs, 0, 0, 16, 16, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, 16, 16, 0, 0, 16, 16, 1, 1, 1, 1)
is := graphics.QuadIndices()
dr := image.Rect(0, 0, 16, 16)
sr := [graphics.ShaderSrcImageCount]image.Rectangle{image.Rect(0, 0, 16, 16)}

View File

@ -14,7 +14,8 @@
package debug
type Logger interface {
Logf(format string, args ...any)
// FrameLogger defines the interface for logging debug information for each frame.
type FrameLogger interface {
FrameLogf(format string, args ...any)
Flush()
}

View File

@ -23,30 +23,32 @@ import (
const IsDebug = true
var theLogger = &logger{}
var theFrameLogger = &frameLogger{}
var flushM sync.Mutex
// Logf calls the current global logger's Logf.
// Logf buffers the arguments and doesn't dump the log immediately.
// FrameLogf calls the current global logger's FrameLogf.
// FrameLogf buffers the arguments and doesn't dump the log immediately.
// You can dump logs by calling SwitchLogger and Flush.
//
// Logf is not concurrent safe.
func Logf(format string, args ...any) {
theLogger.Logf(format, args...)
// FrameLogf is not concurrent safe.
// FrameLogf and SwitchFrameLogger must be called from the same goroutine.
func FrameLogf(format string, args ...any) {
theFrameLogger.FrameLogf(format, args...)
}
// SwitchLogger sets a new logger as the current logger and returns the original global logger.
// SwitchFrameLogger sets a new logger as the current logger and returns the original global logger.
// The new global logger and the returned logger have separate statuses, so you can use them for different goroutines.
//
// SwitchLogger and a returned Logger are not concurrent safe.
func SwitchLogger() Logger {
current := theLogger
theLogger = &logger{}
// SwitchFrameLogger and a returned Logger are not concurrent safe.
// FrameLogf and SwitchFrameLogger must be called from the same goroutine.
func SwitchFrameLogger() FrameLogger {
current := theFrameLogger
theFrameLogger = &frameLogger{}
return current
}
type logger struct {
type frameLogger struct {
items []logItem
}
@ -55,14 +57,14 @@ type logItem struct {
args []any
}
func (l *logger) Logf(format string, args ...any) {
func (l *frameLogger) FrameLogf(format string, args ...any) {
l.items = append(l.items, logItem{
format: format,
args: args,
})
}
func (l *logger) Flush() {
func (l *frameLogger) Flush() {
// Flushing is protected by a mutex not to mix another logger's logs.
flushM.Lock()
defer flushM.Unlock()

View File

@ -18,17 +18,17 @@ package debug
const IsDebug = false
func Logf(format string, args ...any) {
func FrameLogf(format string, args ...any) {
}
func SwitchLogger() Logger {
return dummyLogger{}
func SwitchFrameLogger() FrameLogger {
return dummyFrameLogger{}
}
type dummyLogger struct{}
type dummyFrameLogger struct{}
func (dummyLogger) Logf(format string, args ...any) {
func (dummyFrameLogger) FrameLogf(format string, args ...any) {
}
func (dummyLogger) Flush() {
func (dummyFrameLogger) Flush() {
}

View File

@ -24,13 +24,24 @@ import (
)
type FileEntryFS struct {
rootEntry js.Value
rootEntries []js.Value
}
func NewFileEntryFS(root js.Value) *FileEntryFS {
return &FileEntryFS{
rootEntry: root,
func NewFileEntryFS(rootEntries []js.Value) (*FileEntryFS, error) {
// Check all the full paths are the same.
var fullpath string
for _, ent := range rootEntries {
if fullpath == "" {
fullpath = ent.Get("fullPath").String()
continue
}
if fullpath != ent.Get("fullPath").String() {
return nil, errors.New("file: all the full paths must be the same")
}
}
return &FileEntryFS{
rootEntries: rootEntries,
}, nil
}
func (f *FileEntryFS) Open(name string) (fs.File, error) {
@ -43,33 +54,55 @@ func (f *FileEntryFS) Open(name string) (fs.File, error) {
}
if name == "." {
return &dir{entry: f.rootEntry}, nil
var dirName string
for _, ent := range f.rootEntries {
if dirName == "" {
dirName = ent.Get("name").String()
continue
}
if dirName != ent.Get("name").String() {
return nil, &fs.PathError{
Op: "open",
Path: name,
Err: errors.New("invalid directory"),
}
}
}
return &dir{
name: dirName,
dirEntries: f.rootEntries,
}, nil
}
var chEntry chan js.Value
cbSuccess := js.FuncOf(func(this js.Value, args []js.Value) any {
chEntry <- args[0]
close(chEntry)
return nil
})
defer cbSuccess.Release()
for _, ent := range f.rootEntries {
var chEntry chan js.Value
cbSuccess := js.FuncOf(func(this js.Value, args []js.Value) any {
chEntry <- args[0]
close(chEntry)
return nil
})
defer cbSuccess.Release()
cbFailure := js.FuncOf(func(this js.Value, args []js.Value) any {
close(chEntry)
return nil
})
defer cbFailure.Release()
cbFailure := js.FuncOf(func(this js.Value, args []js.Value) any {
close(chEntry)
return nil
})
defer cbFailure.Release()
chEntry = make(chan js.Value)
f.rootEntry.Call("getFile", name, nil, cbSuccess, cbFailure)
if entry := <-chEntry; entry.Truthy() {
return &file{entry: entry}, nil
}
chEntry = make(chan js.Value)
ent.Call("getFile", name, nil, cbSuccess, cbFailure)
if entry := <-chEntry; entry.Truthy() {
return &file{entry: entry}, nil
}
chEntry = make(chan js.Value)
f.rootEntry.Call("getDirectory", name, nil, cbSuccess, cbFailure)
if entry := <-chEntry; entry.Truthy() {
return &dir{entry: entry}, nil
chEntry = make(chan js.Value)
ent.Call("getDirectory", name, nil, cbSuccess, cbFailure)
if entry := <-chEntry; entry.Truthy() {
return &dir{
name: entry.Get("name").String(),
dirEntries: []js.Value{entry},
}, nil
}
}
return nil, &fs.PathError{
@ -109,8 +142,8 @@ func (f *file) ensureFile() js.Value {
func (f *file) Stat() (fs.FileInfo, error) {
return &fileInfo{
entry: f.entry,
file: f.ensureFile(),
name: f.entry.Get("name").String(),
file: f.ensureFile(),
}, nil
}
@ -163,21 +196,22 @@ func (f *file) Close() error {
}
type dir struct {
entry js.Value
entries []js.Value
offset int
name string
dirEntries []js.Value
fileEntries []js.Value
offset int
}
func (d *dir) Stat() (fs.FileInfo, error) {
return &fileInfo{
entry: d.entry,
name: d.name,
}, nil
}
func (d *dir) Read(buf []byte) (int, error) {
return 0, &fs.PathError{
Op: "read",
Path: d.entry.Get("name").String(),
Path: d.name,
Err: errors.New("is a directory"),
}
}
@ -187,43 +221,52 @@ func (d *dir) Close() error {
}
func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) {
if d.entries == nil {
ch := make(chan struct{})
var rec js.Func
cb := js.FuncOf(func(this js.Value, args []js.Value) any {
entries := args[0]
if entries.Length() == 0 {
close(ch)
if d.fileEntries == nil {
names := map[string]struct{}{}
for _, dirEntry := range d.dirEntries {
ch := make(chan struct{})
var rec js.Func
cb := js.FuncOf(func(this js.Value, args []js.Value) any {
entries := args[0]
if entries.Length() == 0 {
close(ch)
return nil
}
for i := 0; i < entries.Length(); i++ {
ent := entries.Index(i)
name := ent.Get("name").String()
// A name can be empty when this directory is a root directory.
if name == "" {
continue
}
// Avoid entry duplications. Entry duplications happen when multiple files are dropped on Chrome.
if _, ok := names[name]; ok {
continue
}
if !ent.Get("isFile").Bool() && !ent.Get("isDirectory").Bool() {
continue
}
d.fileEntries = append(d.fileEntries, ent)
names[name] = struct{}{}
}
rec.Value.Call("call")
return nil
}
for i := 0; i < entries.Length(); i++ {
ent := entries.Index(i)
// A name can be empty when this directory is a root directory.
if ent.Get("name").String() == "" {
continue
}
if !ent.Get("isFile").Bool() && !ent.Get("isDirectory").Bool() {
continue
}
d.entries = append(d.entries, ent)
}
})
defer cb.Release()
reader := dirEntry.Call("createReader")
rec = js.FuncOf(func(this js.Value, args []js.Value) any {
reader.Call("readEntries", cb)
return nil
})
defer rec.Release()
rec.Value.Call("call")
return nil
})
defer cb.Release()
reader := d.entry.Call("createReader")
rec = js.FuncOf(func(this js.Value, args []js.Value) any {
reader.Call("readEntries", cb)
return nil
})
defer rec.Release()
rec.Value.Call("call")
<-ch
<-ch
}
}
n := len(d.entries) - d.offset
n := len(d.fileEntries) - d.offset
if n == 0 {
if count <= 0 {
@ -238,11 +281,12 @@ func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) {
ents := make([]fs.DirEntry, n)
for i := range ents {
entry := d.fileEntries[d.offset+i]
fi := &fileInfo{
entry: d.entries[d.offset+i],
name: entry.Get("name").String(),
}
if fi.entry.Get("isFile").Bool() {
fi.file = getFile(fi.entry)
if entry.Get("isFile").Bool() {
fi.file = getFile(entry)
}
ents[i] = fs.FileInfoToDirEntry(fi)
}
@ -252,12 +296,12 @@ func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) {
}
type fileInfo struct {
entry js.Value
file js.Value
name string
file js.Value
}
func (f *fileInfo) Name() string {
return f.entry.Get("name").String()
return f.name
}
func (f *fileInfo) Size() int64 {

View File

@ -54,6 +54,10 @@ func (g *nativeGamepadsImpl) init(gamepads *gamepads) error {
if err == unix.ENOENT {
return nil
}
// `/dev/input` might not be accessible in some environments (#3057).
if err == unix.EACCES {
return nil
}
return fmt.Errorf("gamepad: Stat failed: %w", err)
}
if stat.Mode&unix.S_IFDIR == 0 {

View File

@ -155,7 +155,8 @@ func run() error {
}
defer f.Close()
if err := tmpl.Execute(f, struct {
w := bufio.NewWriter(f)
if err := tmpl.Execute(w, struct {
License string
DoNotEdit string
BuildConstraints string
@ -170,6 +171,9 @@ func run() error {
}); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
}
return nil

View File

@ -50,16 +50,16 @@ func QuadIndices() []uint32 {
return quadIndices
}
// QuadVertices sets a float32 slice for a quadrangle.
// QuadVertices sets a slice that never overlaps with other slices returned this function,
// and users can do optimization based on this fact.
func QuadVertices(dst []float32, sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) {
// QuadVerticesFromSrcAndMatrix sets a float32 slice for a quadrangle.
func QuadVerticesFromSrcAndMatrix(dst []float32, sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) {
x := sx1 - sx0
y := sy1 - sy0
ax, by, cx, dy := a*x, b*y, c*x, d*y
u0, v0, u1, v1 := sx0, sy0, sx1, sy1
// This function is very performance-sensitive and implement in a very dumb way.
// Remove the boundary check.
dst = dst[:4*VertexFloatCount]
dst[0] = adjustDestinationPixel(tx)
@ -71,32 +71,79 @@ func QuadVertices(dst []float32, sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty
dst[6] = cb
dst[7] = ca
dst[8] = adjustDestinationPixel(ax + tx)
dst[9] = adjustDestinationPixel(cx + ty)
dst[10] = u1
dst[11] = v0
dst[12] = cr
dst[13] = cg
dst[14] = cb
dst[15] = ca
dst[VertexFloatCount] = adjustDestinationPixel(ax + tx)
dst[VertexFloatCount+1] = adjustDestinationPixel(cx + ty)
dst[VertexFloatCount+2] = u1
dst[VertexFloatCount+3] = v0
dst[VertexFloatCount+4] = cr
dst[VertexFloatCount+5] = cg
dst[VertexFloatCount+6] = cb
dst[VertexFloatCount+7] = ca
dst[16] = adjustDestinationPixel(by + tx)
dst[17] = adjustDestinationPixel(dy + ty)
dst[18] = u0
dst[19] = v1
dst[20] = cr
dst[21] = cg
dst[22] = cb
dst[23] = ca
dst[2*VertexFloatCount] = adjustDestinationPixel(by + tx)
dst[2*VertexFloatCount+1] = adjustDestinationPixel(dy + ty)
dst[2*VertexFloatCount+2] = u0
dst[2*VertexFloatCount+3] = v1
dst[2*VertexFloatCount+4] = cr
dst[2*VertexFloatCount+5] = cg
dst[2*VertexFloatCount+6] = cb
dst[2*VertexFloatCount+7] = ca
dst[24] = adjustDestinationPixel(ax + by + tx)
dst[25] = adjustDestinationPixel(cx + dy + ty)
dst[26] = u1
dst[27] = v1
dst[28] = cr
dst[29] = cg
dst[30] = cb
dst[31] = ca
dst[3*VertexFloatCount] = adjustDestinationPixel(ax + by + tx)
dst[3*VertexFloatCount+1] = adjustDestinationPixel(cx + dy + ty)
dst[3*VertexFloatCount+2] = u1
dst[3*VertexFloatCount+3] = v1
dst[3*VertexFloatCount+4] = cr
dst[3*VertexFloatCount+5] = cg
dst[3*VertexFloatCount+6] = cb
dst[3*VertexFloatCount+7] = ca
}
// QuadVerticesFromDstAndSrc sets a float32 slice for a quadrangle.
func QuadVerticesFromDstAndSrc(dst []float32, dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, cr, cg, cb, ca float32) {
dx0 = adjustDestinationPixel(dx0)
dy0 = adjustDestinationPixel(dy0)
dx1 = adjustDestinationPixel(dx1)
dy1 = adjustDestinationPixel(dy1)
// Remove the boundary check.
dst = dst[:4*VertexFloatCount]
dst[0] = dx0
dst[1] = dy0
dst[2] = sx0
dst[3] = sy0
dst[4] = cr
dst[5] = cg
dst[6] = cb
dst[7] = ca
dst[VertexFloatCount] = dx1
dst[VertexFloatCount+1] = dy0
dst[VertexFloatCount+2] = sx1
dst[VertexFloatCount+3] = sy0
dst[VertexFloatCount+4] = cr
dst[VertexFloatCount+5] = cg
dst[VertexFloatCount+6] = cb
dst[VertexFloatCount+7] = ca
dst[2*VertexFloatCount] = dx0
dst[2*VertexFloatCount+1] = dy1
dst[2*VertexFloatCount+2] = sx0
dst[2*VertexFloatCount+3] = sy1
dst[2*VertexFloatCount+4] = cr
dst[2*VertexFloatCount+5] = cg
dst[2*VertexFloatCount+6] = cb
dst[2*VertexFloatCount+7] = ca
dst[3*VertexFloatCount] = dx1
dst[3*VertexFloatCount+1] = dy1
dst[3*VertexFloatCount+2] = sx1
dst[3*VertexFloatCount+3] = sy1
dst[3*VertexFloatCount+4] = cr
dst[3*VertexFloatCount+5] = cg
dst[3*VertexFloatCount+6] = cb
dst[3*VertexFloatCount+7] = ca
}
func adjustDestinationPixel(x float32) float32 {

View File

@ -183,9 +183,9 @@ func dstRegionFromVertices(vertices []float32) (minX, minY, maxX, maxY float32)
maxX = negInf32
maxY = negInf32
for i := 0; i < len(vertices)/graphics.VertexFloatCount; i++ {
x := vertices[graphics.VertexFloatCount*i]
y := vertices[graphics.VertexFloatCount*i+1]
for i := 0; i < len(vertices); i += graphics.VertexFloatCount {
x := vertices[i]
y := vertices[i+1]
if x < minX {
minX = x
}

View File

@ -197,7 +197,7 @@ func (q *commandQueue) Flush(graphicsDriver graphicsdriver.Graphics, endFrame bo
}
}
logger := debug.SwitchLogger()
logger := debug.SwitchFrameLogger()
var flushErr error
runOnRenderThread(func() {
@ -223,7 +223,7 @@ func (q *commandQueue) Flush(graphicsDriver graphicsdriver.Graphics, endFrame bo
}
// flush must be called the render thread.
func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bool, logger debug.Logger) (err error) {
func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bool, logger debug.FrameLogger) (err error) {
// If endFrame is true, Begin/End should be called to ensure the framebuffer is swapped.
if len(q.commands) == 0 && !endFrame {
return nil
@ -231,7 +231,7 @@ func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bo
es := q.indices
vs := q.vertices
logger.Logf("Graphics commands:\n")
logger.FrameLogf("Graphics commands:\n")
if err := graphicsDriver.Begin(); err != nil {
return err
@ -294,7 +294,7 @@ func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bo
if err := c.Exec(q, graphicsDriver, indexOffset); err != nil {
return err
}
logger.Logf(" %s\n", c)
logger.FrameLogf(" %s\n", c)
// TODO: indexOffset should be reset if the command type is different
// from the previous one. This fix is needed when another drawing command is
// introduced than drawTrianglesCommand.

View File

@ -234,6 +234,6 @@ func LogImagesInfo(images []*Image) {
if i.screen {
screen = " (screen)"
}
debug.Logf(" %d: (%d, %d)%s\n", i.id, w, h, screen)
debug.FrameLogf(" %d: (%d, %d)%s\n", i.id, w, h, screen)
}
}

View File

@ -43,12 +43,9 @@ func TestMain(m *testing.M) {
}
func quadVertices(w, h float32) []float32 {
return []float32{
0, 0, 0, 0, 1, 1, 1, 1,
w, 0, w, 0, 1, 1, 1, 1,
0, w, 0, h, 1, 1, 1, 1,
w, h, w, h, 1, 1, 1, 1,
}
vs := make([]float32, 8*graphics.VertexFloatCount)
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, w, h, 0, 0, w, h, 1, 1, 1, 1)
return vs
}
func TestClear(t *testing.T) {

View File

@ -15,6 +15,7 @@
package mtl_test
import (
"bufio"
"fmt"
"image"
"image/color"
@ -147,7 +148,7 @@ func readPNG(name string) (image.Image, error) {
defer func() {
_ = f.Close()
}()
return png.Decode(f)
return png.Decode(bufio.NewReader(f))
}
// imageEq reports whether images m, n are considered equivalent. Two images are considered

View File

@ -90,7 +90,6 @@ type (
type (
uniformLocation int32
attribLocation int32
)
const (
@ -111,8 +110,6 @@ type context struct {
lastBlend graphicsdriver.Blend
maxTextureSize int
maxTextureSizeOnce sync.Once
highp bool
highpOnce sync.Once
initOnce sync.Once
}

View File

@ -18,8 +18,6 @@ package gl
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/ebitengine/purego"
@ -31,36 +29,10 @@ var (
)
func (c *defaultContext) init() error {
var preferES bool
if runtime.GOOS == "android" {
preferES = true
}
if !preferES {
for _, t := range strings.Split(os.Getenv("EBITENGINE_OPENGL"), ",") {
switch strings.TrimSpace(t) {
case "es":
preferES = true
break
}
}
}
// TODO: Use multiple %w-s as of Go 1.20.
var errors []string
// Try OpenGL first. OpenGL is preferable as this doesn't cause context losses.
if !preferES {
// Usually libGL.so or libGL.so.1 is used. libGL.so.2 might exist only on NetBSD.
// TODO: Should "libOpenGL.so.0" [1] and "libGLX.so.0" [2] be added? These were added as of GLFW 3.3.9.
// [1] https://github.com/glfw/glfw/commit/55aad3c37b67f17279378db52da0a3ab81bbf26d
// [2] https://github.com/glfw/glfw/commit/c18851f52ec9704eb06464058a600845ec1eada1
for _, name := range []string{"libGL.so", "libGL.so.2", "libGL.so.1", "libGL.so.0"} {
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err == nil {
libGL = lib
return nil
}
}
}
// Try OpenGL ES.
// Try OpenGL ES first. Some machines like Android and Raspberry Pi might work only with OpenGL ES.
for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} {
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err == nil {
@ -68,9 +40,24 @@ func (c *defaultContext) init() error {
c.isES = true
return nil
}
errors = append(errors, fmt.Sprintf("%s: %v", name, err))
}
return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so")
// Try OpenGL next.
// Usually libGL.so or libGL.so.1 is used. libGL.so.2 might exist only on NetBSD.
// TODO: Should "libOpenGL.so.0" [1] and "libGLX.so.0" [2] be added? These were added as of GLFW 3.3.9.
// [1] https://github.com/glfw/glfw/commit/55aad3c37b67f17279378db52da0a3ab81bbf26d
// [2] https://github.com/glfw/glfw/commit/c18851f52ec9704eb06464058a600845ec1eada1
for _, name := range []string{"libGL.so", "libGL.so.2", "libGL.so.1", "libGL.so.0"} {
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err == nil {
libGL = lib
return nil
}
errors = append(errors, fmt.Sprintf("%s: %v", name, err))
}
return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so: %s", strings.Join(errors, ", "))
}
func (c *defaultContext) getProcAddress(name string) (uintptr, error) {

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build ebitenginegldebug
//go:build !playstation5 && ebitenginegldebug
package opengl

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !ebitenginegldebug
//go:build !playstation5 && !ebitenginegldebug
package opengl

View File

@ -53,14 +53,14 @@ func (a *arrayBufferLayout) names() []string {
return ns
}
// totalBytes returns the size in bytes for one element of the array buffer.
func (a *arrayBufferLayout) totalBytes() int {
// float32Count returns the total float32 count for one element of the array buffer.
func (a *arrayBufferLayout) float32Count() int {
if a.total != 0 {
return a.total
}
t := 0
for _, p := range a.parts {
t += floatSizeInBytes * p.num
t += p.num
}
a.total = t
return a.total
@ -71,10 +71,10 @@ func (a *arrayBufferLayout) enable(context *context) {
for i := range a.parts {
context.ctx.EnableVertexAttribArray(uint32(i))
}
total := a.totalBytes()
total := a.float32Count()
offset := 0
for i, p := range a.parts {
context.ctx.VertexAttribPointer(uint32(i), int32(p.num), gl.FLOAT, false, int32(total), offset)
context.ctx.VertexAttribPointer(uint32(i), int32(p.num), gl.FLOAT, false, int32(floatSizeInBytes*total), offset)
offset += floatSizeInBytes * p.num
}
}
@ -88,28 +88,39 @@ func (a *arrayBufferLayout) disable(context *context) {
}
// theArrayBufferLayout is the array buffer layout for Ebitengine.
var theArrayBufferLayout = arrayBufferLayout{
// Note that GL_MAX_VERTEX_ATTRIBS is at least 16.
parts: []arrayBufferLayoutPart{
{
name: "A0",
num: 2,
},
{
name: "A1",
num: 2,
},
{
name: "A2",
num: 4,
},
},
}
var theArrayBufferLayout arrayBufferLayout
func init() {
vertexFloatCount := theArrayBufferLayout.totalBytes() / floatSizeInBytes
if graphics.VertexFloatCount != vertexFloatCount {
panic(fmt.Sprintf("vertex float num must be %d but %d", graphics.VertexFloatCount, vertexFloatCount))
theArrayBufferLayout = arrayBufferLayout{
// Note that GL_MAX_VERTEX_ATTRIBS is at least 16.
parts: []arrayBufferLayoutPart{
{
name: "A0",
num: 2,
},
{
name: "A1",
num: 2,
},
{
name: "A2",
num: 4,
},
},
}
n := theArrayBufferLayout.float32Count()
if n > graphics.VertexFloatCount {
panic("opengl: the array buffer layout is too large")
}
if n < graphics.VertexFloatCount {
d := graphics.VertexFloatCount - n
if d > 4 {
panic("opengl: the array buffer layout is too small")
}
theArrayBufferLayout.parts = append(theArrayBufferLayout.parts, arrayBufferLayoutPart{
name: "A3",
num: d,
})
}
}

View File

@ -18,24 +18,31 @@
#include "graphics_playstation5.h"
extern "C" ebitengine_Error ebitengine_InitializeGraphics(void) {
extern "C" ebitengine_Error ebitengine_InitializeGraphics(void) { return {}; }
extern "C" ebitengine_Error ebitengine_NewImage(int *image, int width,
int height) {
return {};
}
extern "C" ebitengine_Error ebitengine_NewImage(int* image, int width, int height) {
extern "C" ebitengine_Error
ebitengine_NewScreenFramebufferImage(int *image, int width, int height) {
return {};
}
extern "C" ebitengine_Error ebitengine_NewScreenFramebufferImage(int* image, int width, int height) {
extern "C" void ebitengine_DisposeImage(int id) {}
ebitengine_Error
ebitengine_DrawTriangles(int dst, int *srcs, int srcCount, int shader,
ebitengine_DstRegion *dstRegions, int dstRegionCount,
int indexOffset, ebitengine_Blend blend,
uint32_t *uniforms, int uniformCount, int fillRule) {
return {};
}
extern "C" void ebitengine_DisposeImage(int id) {
}
extern "C" ebitengine_Error ebitengine_NewShader(int* shader, const char* source) {
extern "C" ebitengine_Error ebitengine_NewShader(int *shader,
const char *source) {
return {};
}
extern "C" void ebitengine_DisposeShader(int id) {
}
extern "C" void ebitengine_DisposeShader(int id) {}

View File

@ -21,6 +21,7 @@ import "C"
import (
"fmt"
"runtime"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
@ -98,7 +99,7 @@ func (g *Graphics) SetVsyncEnabled(enabled bool) {
}
func (g *Graphics) NeedsClearingScreen() bool {
return false
return true
}
func (g *Graphics) MaxImageSize() int {
@ -117,6 +118,42 @@ func (g *Graphics) NewShader(program *shaderir.Program) (graphicsdriver.Shader,
}
func (g *Graphics) DrawTriangles(dst graphicsdriver.ImageID, srcs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shader graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error {
cSrcs := make([]C.int, len(srcs))
for i, src := range srcs {
cSrcs[i] = C.int(src)
}
defer runtime.KeepAlive(cSrcs)
cDstRegions := make([]C.ebitengine_DstRegion, len(dstRegions))
defer runtime.KeepAlive(cDstRegions)
for i, r := range dstRegions {
cDstRegions[i] = C.ebitengine_DstRegion{
MinX: C.int(r.Region.Min.X),
MinY: C.int(r.Region.Min.Y),
MaxX: C.int(r.Region.Max.X),
MaxY: C.int(r.Region.Max.Y),
IndexCount: C.int(r.IndexCount),
}
}
cBlend := C.ebitengine_Blend{
BlendFactorSourceRGB: C.uint8_t(blend.BlendFactorSourceRGB),
BlendFactorSourceAlpha: C.uint8_t(blend.BlendFactorSourceAlpha),
BlendFactorDestinationRGB: C.uint8_t(blend.BlendFactorDestinationRGB),
BlendFactorDestinationAlpha: C.uint8_t(blend.BlendFactorDestinationAlpha),
BlendOperationRGB: C.uint8_t(blend.BlendOperationRGB),
BlendOperationAlpha: C.uint8_t(blend.BlendOperationAlpha),
}
cUniforms := make([]C.uint32_t, len(uniforms))
defer runtime.KeepAlive(cUniforms)
for i, u := range uniforms {
cUniforms[i] = C.uint32_t(u)
}
if err := C.ebitengine_DrawTriangles(C.int(dst), &cSrcs[0], C.int(len(cSrcs)), C.int(shader), &cDstRegions[0], C.int(len(cDstRegions)), C.int(indexOffset), cBlend, &cUniforms[0], C.int(len(cUniforms)), C.int(fillRule)); !C.ebitengine_IsErrorNil(&err) {
return newPlaystation5Error("(*playstation5.Graphics).DrawTriangles", err)
}
return nil
}

View File

@ -19,26 +19,51 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ebitengine_Error {
const char* Message;
int Code;
const char *Message;
int Code;
} ebitengine_Error;
static bool ebitengine_IsErrorNil(ebitengine_Error* err) {
static bool ebitengine_IsErrorNil(ebitengine_Error *err) {
return err->Message == NULL && err->Code == 0;
}
typedef struct ebitengine_DstRegion {
int MinX;
int MinY;
int MaxX;
int MaxY;
int IndexCount;
} ebitengine_DstRegion;
typedef struct ebitengine_Blend {
uint8_t BlendFactorSourceRGB;
uint8_t BlendFactorSourceAlpha;
uint8_t BlendFactorDestinationRGB;
uint8_t BlendFactorDestinationAlpha;
uint8_t BlendOperationRGB;
uint8_t BlendOperationAlpha;
} ebitengine_Blend;
ebitengine_Error ebitengine_InitializeGraphics(void);
ebitengine_Error ebitengine_NewImage(int* image, int width, int height);
ebitengine_Error ebitengine_NewScreenFramebufferImage(int* image, int width, int height);
ebitengine_Error ebitengine_NewImage(int *image, int width, int height);
ebitengine_Error ebitengine_NewScreenFramebufferImage(int *image, int width,
int height);
void ebitengine_DisposeImage(int id);
ebitengine_Error ebitengine_NewShader(int* shader, const char* source);
ebitengine_Error
ebitengine_DrawTriangles(int dst, int *srcs, int srcCount, int shader,
ebitengine_DstRegion *dstRegions, int dstRegionCount,
int indexOffset, ebitengine_Blend blend,
uint32_t *uniforms, int uniformCount, int fillRule);
ebitengine_Error ebitengine_NewShader(int *shader, const char *source);
void ebitengine_DisposeShader(int id);
#ifdef __cplusplus

View File

@ -73,20 +73,22 @@ func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Mipmap, verti
level := 0
if !canSkipMipmap && srcs[0] != nil && canUseMipmap(srcs[0].imageType) {
level = math.MaxInt32
for i := 0; i < len(indices)/3; i++ {
const n = graphics.VertexFloatCount
dx0 := vertices[n*indices[3*i]+0]
dy0 := vertices[n*indices[3*i]+1]
sx0 := vertices[n*indices[3*i]+2]
sy0 := vertices[n*indices[3*i]+3]
dx1 := vertices[n*indices[3*i+1]+0]
dy1 := vertices[n*indices[3*i+1]+1]
sx1 := vertices[n*indices[3*i+1]+2]
sy1 := vertices[n*indices[3*i+1]+3]
dx2 := vertices[n*indices[3*i+2]+0]
dy2 := vertices[n*indices[3*i+2]+1]
sx2 := vertices[n*indices[3*i+2]+2]
sy2 := vertices[n*indices[3*i+2]+3]
for i := 0; i < len(indices); i += 3 {
idx0 := indices[i]
idx1 := indices[i+1]
idx2 := indices[i+2]
dx0 := vertices[graphics.VertexFloatCount*idx0]
dy0 := vertices[graphics.VertexFloatCount*idx0+1]
sx0 := vertices[graphics.VertexFloatCount*idx0+2]
sy0 := vertices[graphics.VertexFloatCount*idx0+3]
dx1 := vertices[graphics.VertexFloatCount*idx1]
dy1 := vertices[graphics.VertexFloatCount*idx1+1]
sx1 := vertices[graphics.VertexFloatCount*idx1+2]
sy1 := vertices[graphics.VertexFloatCount*idx1+3]
dx2 := vertices[graphics.VertexFloatCount*idx2]
dy2 := vertices[graphics.VertexFloatCount*idx2+1]
sx2 := vertices[graphics.VertexFloatCount*idx2+2]
sy2 := vertices[graphics.VertexFloatCount*idx2+3]
if l := mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1); level > l {
level = l
}
@ -109,11 +111,10 @@ func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Mipmap, verti
}
if level != 0 {
if img := src.level(level); img != nil {
const n = graphics.VertexFloatCount
s := float32(pow2(level))
for i := 0; i < len(vertices)/n; i++ {
vertices[i*n+2] /= s
vertices[i*n+3] /= s
for i := 0; i < len(vertices); i += graphics.VertexFloatCount {
vertices[i+2] /= s
vertices[i+3] /= s
}
imgs[i] = img
continue
@ -148,12 +149,10 @@ func (m *Mipmap) level(level int) *buffered.Image {
var src *buffered.Image
vs := make([]float32, 4*graphics.VertexFloatCount)
shader := atlas.NearestFilterShader
switch {
case level == 1:
src = m.orig
graphics.QuadVertices(vs, 0, 0, float32(m.width), float32(m.height), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
shader = atlas.LinearFilterShader
graphics.QuadVerticesFromSrcAndMatrix(vs, 0, 0, float32(m.width), float32(m.height), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
case level > 1:
src = m.level(level - 1)
if src == nil {
@ -162,8 +161,7 @@ func (m *Mipmap) level(level int) *buffered.Image {
}
w := sizeForLevel(m.width, level-1)
h := sizeForLevel(m.height, level-1)
graphics.QuadVertices(vs, 0, 0, float32(w), float32(h), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
shader = atlas.LinearFilterShader
graphics.QuadVerticesFromSrcAndMatrix(vs, 0, 0, float32(w), float32(h), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
default:
panic(fmt.Sprintf("mipmap: invalid level: %d", level))
}
@ -186,7 +184,7 @@ func (m *Mipmap) level(level int) *buffered.Image {
s := buffered.NewImage(w2, h2, m.imageType)
dstRegion := image.Rect(0, 0, w2, h2)
s.DrawTriangles([graphics.ShaderSrcImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendCopy, dstRegion, [graphics.ShaderSrcImageCount]image.Rectangle{}, shader, nil, graphicsdriver.FillRuleFillAll)
s.DrawTriangles([graphics.ShaderSrcImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendCopy, dstRegion, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.LinearFilterShader, nil, graphicsdriver.FillRuleFillAll)
m.setImg(level, s)
return m.imgs[level]

View File

@ -17,6 +17,7 @@
package main
import (
"bufio"
"fmt"
"go/ast"
"go/format"
@ -93,6 +94,8 @@ func run() error {
}
defer out.Close()
w := bufio.NewWriter(out)
// TODO: Remove call of RegisterDecoder
data, err := os.ReadFile(filepath.Join(dir, f))
@ -129,14 +132,14 @@ func run() error {
return true
}, nil)
fmt.Fprintln(out, "// Code generated by gen.go. DO NOT EDIT.")
fmt.Fprintln(out)
format.Node(out, fset, tree)
fmt.Fprintln(w, "// Code generated by gen.go. DO NOT EDIT.")
fmt.Fprintln(w)
format.Node(w, fset, tree)
if f == "reader.go" {
// The min function was removed as of Go 1.22, but this is needed for old Go.
// TODO: Remove this when Go 1.21 is the minimum supported version.
fmt.Fprintln(out, `
fmt.Fprintln(w, `
func min(a, b int) int {
if a < b {
return a
@ -144,6 +147,10 @@ func min(a, b int) int {
return b
}`)
}
if err := w.Flush(); err != nil {
return err
}
}
return nil
}

View File

@ -1150,6 +1150,11 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
x := exprs[0]
t := ts[0]
if (t.IsFloatVector() || t.IsIntVector()) && idx.Const == nil {
cs.addError(e.Pos(), fmt.Sprintf("index must be a constant for the type %s", t.String()))
return nil, nil, nil, false
}
var typ shaderir.Type
switch t.Main {
case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4:

View File

@ -21,7 +21,6 @@ import (
"runtime"
"strings"
"testing"
"time"
"github.com/hajimehoshi/ebiten/v2/internal/shader"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
@ -30,13 +29,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/msl"
)
func TestMain(m *testing.M) {
code := m.Run()
// The Wasm tests on GitHub CI often fail due to some remaining functions. Wait for a while to finish them.
time.Sleep(100 * time.Millisecond)
os.Exit(code)
}
func glslVertexNormalize(str string) string {
p := glsl.VertexPrelude(glsl.GLSLVersionDefault)
if strings.HasPrefix(str, p) {

View File

@ -4363,3 +4363,67 @@ func Foo() int {
t.Error(err)
}
}
func TestSyntaxIndex(t *testing.T) {
// Issue #3011
if _, err := compileToIR([]byte(`package main
func Foo() float {
var a int
var b vec4
return b[a]
}
`)); err == nil {
t.Error("compileToIR must return an error but did not")
}
if _, err := compileToIR([]byte(`package main
func Foo() int {
var a int
var b ivec4
return b[a]
}
`)); err == nil {
t.Error("compileToIR must return an error but did not")
}
if _, err := compileToIR([]byte(`package main
func Foo() float {
var a int
var b mat4
return b[a][0]
}
`)); err != nil {
t.Error(err)
}
if _, err := compileToIR([]byte(`package main
func Foo() float {
const a = 0
var b vec4
return b[a]
}
`)); err != nil {
t.Error(err)
}
if _, err := compileToIR([]byte(`package main
func Foo() int {
const a = 0
var b ivec4
return b[a]
}
`)); err != nil {
t.Error(err)
}
if _, err := compileToIR([]byte(`package main
func Foo() float {
const a = 0
var b mat4
return b[a][0]
}
`)); err != nil {
t.Error(err)
}
}

View File

@ -95,7 +95,7 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
return nil
}
debug.Logf("----\n")
debug.FrameLogf("----\n")
if err := atlas.BeginFrame(graphicsDriver); err != nil {
return err
@ -133,7 +133,7 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
updateCount = 1
c.updateCalled = true
}
debug.Logf("Update count per frame: %d\n", updateCount)
debug.FrameLogf("Update count per frame: %d\n", updateCount)
// Update the game.
for i := 0; i < updateCount; i++ {
@ -192,7 +192,7 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics, ui *UserInter
return err
}
const maxSkipCount = 3
const maxSkipCount = 4
if !forceDraw && !c.isOffscreenModified {
if c.skipCount < maxSkipCount {

View File

@ -158,7 +158,7 @@ func (i *Image) Fill(r, g, b, a float32, region image.Rectangle) {
i.tmpVerticesForFill = make([]float32, 4*graphics.VertexFloatCount)
}
// i.tmpVerticesForFill can be reused as this is sent to DrawTriangles immediately.
graphics.QuadVertices(
graphics.QuadVerticesFromSrcAndMatrix(
i.tmpVerticesForFill,
1, 1, float32(i.ui.whiteImage.width-1), float32(i.ui.whiteImage.height-1),
float32(i.width), 0, 0, float32(i.height), 0, 0,
@ -235,7 +235,7 @@ func (i *bigOffscreenImage) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Im
i.tmpVerticesForCopying = make([]float32, 4*graphics.VertexFloatCount)
}
// i.tmpVerticesForCopying can be reused as this is sent to DrawTriangles immediately.
graphics.QuadVertices(
graphics.QuadVerticesFromSrcAndMatrix(
i.tmpVerticesForCopying,
float32(i.region.Min.X), float32(i.region.Min.Y), float32(i.region.Max.X), float32(i.region.Max.Y),
bigOffscreenScale, 0, 0, bigOffscreenScale, 0, 0,
@ -279,7 +279,7 @@ func (i *bigOffscreenImage) flush() {
i.tmpVerticesForFlushing = make([]float32, 4*graphics.VertexFloatCount)
}
// i.tmpVerticesForFlushing can be reused as this is sent to DrawTriangles in this function.
graphics.QuadVertices(
graphics.QuadVerticesFromSrcAndMatrix(
i.tmpVerticesForFlushing,
0, 0, float32(i.region.Dx()*bigOffscreenScale), float32(i.region.Dy()*bigOffscreenScale),
1.0/bigOffscreenScale, 0, 0, 1.0/bigOffscreenScale, float32(i.region.Min.X), float32(i.region.Min.Y),

View File

@ -238,6 +238,7 @@ var (
sel_origResizable = objc.RegisterName("isOrigResizable")
sel_setCollectionBehavior = objc.RegisterName("setCollectionBehavior:")
sel_setDelegate = objc.RegisterName("setDelegate:")
sel_setDocumentEdited = objc.RegisterName("setDocumentEdited:")
sel_setOrigDelegate = objc.RegisterName("setOrigDelegate:")
sel_setOrigResizable = objc.RegisterName("setOrigResizable:")
sel_toggleFullScreen = objc.RegisterName("toggleFullScreen:")
@ -433,3 +434,13 @@ func initializeWindowAfterCreation(w *glfw.Window) error {
func (u *UserInterface) skipTaskbar() error {
return nil
}
// setDocumentEdited must be called from the main thread.
func (u *UserInterface) setDocumentEdited(edited bool) error {
w, err := u.window.GetCocoaWindow()
if err != nil {
return err
}
objc.ID(w).Send(sel_setDocumentEdited, edited)
return nil
}

View File

@ -568,6 +568,22 @@ func (u *UserInterface) setWindowClosingHandled(handled bool) {
u.m.Lock()
u.windowClosingHandled = handled
u.m.Unlock()
if !u.isRunning() {
return
}
if u.isTerminated() {
return
}
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
if err := u.setDocumentEdited(handled); err != nil {
u.setError(err)
return
}
})
}
// isFullscreen must be called from the main thread.
@ -874,6 +890,13 @@ func (u *UserInterface) createWindow() error {
return err
}
u.m.Lock()
closingHandled := u.windowClosingHandled
u.m.Unlock()
if err := u.setDocumentEdited(closingHandled); err != nil {
return err
}
return nil
}

View File

@ -512,6 +512,7 @@ func (u *UserInterface) init() error {
canvasStyle.Set("height", "100%")
canvasStyle.Set("margin", "0")
canvasStyle.Set("padding", "0")
canvasStyle.Set("display", "block")
// Make the canvas focusable.
canvas.Call("setAttribute", "tabindex", 1)
@ -707,14 +708,21 @@ func (u *UserInterface) appendDroppedFiles(data js.Value) {
defer u.dropFileM.Unlock()
items := data.Get("items")
var entries []js.Value
for i := 0; i < items.Length(); i++ {
kind := items.Index(i).Get("kind").String()
switch kind {
case "file":
fs := items.Index(i).Call("webkitGetAsEntry").Get("filesystem").Get("root")
u.inputState.DroppedFiles = file.NewFileEntryFS(fs)
entries = append(entries, items.Index(i).Call("webkitGetAsEntry").Get("filesystem").Get("root"))
}
}
if len(entries) > 0 {
fs, err := file.NewFileEntryFS(entries)
if err != nil {
u.setError(err)
return
}
u.inputState.DroppedFiles = fs
}
}

View File

@ -200,3 +200,7 @@ func initializeWindowAfterCreation(w *glfw.Window) error {
func (u *UserInterface) skipTaskbar() error {
return nil
}
func (u *UserInterface) setDocumentEdited(edited bool) error {
return nil
}

View File

@ -242,6 +242,10 @@ func (u *UserInterface) skipTaskbar() error {
return nil
}
func (u *UserInterface) setDocumentEdited(edited bool) error {
return nil
}
func init() {
if microsoftgdk.IsXbox() {
// TimeBeginPeriod might not be defined in Xbox.

View File

@ -20,7 +20,6 @@ import (
"io"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/playstation5"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/pssl"
)
@ -41,10 +40,3 @@ func CompileToPSSL(vertexWriter, pixelWriter io.Writer, source *ShaderSource) er
}
return nil
}
// RegisterPlayStationShaders registers a precompiled PlayStation Shader for a shader source.
//
// RegisterPlayStationShaders is concurrent-safe.
func RegisterPlayStationShaders(source *ShaderSource, vertexShader, pixelShader []byte) {
playstation5.RegisterPrecompiledShaders(source.source, vertexShader, pixelShader)
}

View File

@ -12,12 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build playstation5
package vector
// This is a separated pure Go file so that the `shaderprecomp` package can use this without Cgo.
package playstation5
func RegisterPrecompiledShaders(source []byte, vertex, pixel []byte) {
// TODO: Implement this.
type Point struct {
X, Y float32
}
func IsPointCloseToSegment(p, p0, p1 Point, allow float32) bool {
return isPointCloseToSegment(point{
x: p.X,
y: p.Y,
}, point{
x: p0.X,
y: p0.Y,
}, point{
x: p1.X,
y: p1.Y,
}, allow)
}

View File

@ -31,6 +31,23 @@ const (
CounterClockwise
)
type opType int
const (
opTypeMoveTo opType = iota
opTypeLineTo
opTypeQuadTo
opTypeCubicTo
opTypeClose
)
type op struct {
typ opType
p1 point
p2 point
p3 point
}
func abs(x float32) float32 {
if x < 0 {
return -x
@ -48,21 +65,18 @@ type subpath struct {
closed bool
}
func (s *subpath) currentPosition() (point, bool) {
if len(s.points) == 0 {
return point{}, false
}
if s.closed {
return point{}, false
}
return s.points[len(s.points)-1], true
// reset resets the subpath.
// reset doesn't release the allocated memory so that the memory can be reused.
func (s *subpath) reset() {
s.points = s.points[:0]
s.closed = false
}
func (s *subpath) pointCount() int {
func (s subpath) pointCount() int {
return len(s.points)
}
func (s *subpath) lastPoint() point {
func (s subpath) lastPoint() point {
return s.points[len(s.points)-1]
}
@ -71,10 +85,12 @@ func (s *subpath) appendPoint(pt point) {
panic("vector: a closed subpathment cannot append a new point")
}
// Do not add a too close point to the last point.
// This can cause unexpected rendering results.
if lp := s.lastPoint(); abs(lp.x-pt.x) < 1e-2 && abs(lp.y-pt.y) < 1e-2 {
return
if len(s.points) > 0 {
// Do not add a too close point to the last point.
// This can cause unexpected rendering results.
if lp := s.lastPoint(); abs(lp.x-pt.x) < 1e-2 && abs(lp.y-pt.y) < 1e-2 {
return
}
}
s.points = append(s.points, pt)
@ -91,15 +107,66 @@ func (s *subpath) close() {
// Path represents a collection of path subpathments.
type Path struct {
subpaths []*subpath
ops []op
subpaths []subpath
}
// reset resets the path.
// reset doesn't release the allocated memory so that the memory can be reused.
func (p *Path) reset() {
p.ops = p.ops[:0]
p.subpaths = p.subpaths[:0]
}
func (p *Path) appendNewSubpath(pt point) {
if cap(p.subpaths) > len(p.subpaths) {
// Reuse the last subpath since the last subpath might have an already allocated slice.
p.subpaths = p.subpaths[:len(p.subpaths)+1]
p.subpaths[len(p.subpaths)-1].reset()
p.subpaths[len(p.subpaths)-1].appendPoint(pt)
return
}
p.subpaths = append(p.subpaths, subpath{
points: []point{pt},
})
}
func (p *Path) ensureSubpaths() []subpath {
if len(p.subpaths) > 0 || len(p.ops) == 0 {
return p.subpaths
}
var cur point
for _, op := range p.ops {
switch op.typ {
case opTypeMoveTo:
p.appendNewSubpath(op.p1)
cur = op.p1
case opTypeLineTo:
p.lineTo(op.p1)
cur = op.p1
case opTypeQuadTo:
p.quadTo(cur, op.p1, op.p2, 0)
cur = op.p2
case opTypeCubicTo:
p.cubicTo(cur, op.p1, op.p2, op.p3, 0)
cur = op.p3
case opTypeClose:
p.close()
cur = point{}
}
}
return p.subpaths
}
// MoveTo starts a new subpath with the given position (x, y) without adding a subpath,
func (p *Path) MoveTo(x, y float32) {
p.subpaths = append(p.subpaths, &subpath{
points: []point{
{x: x, y: y},
},
p.subpaths = p.subpaths[:0]
p.ops = append(p.ops, op{
typ: opTypeMoveTo,
p1: point{x: x, y: y},
})
}
@ -107,22 +174,52 @@ func (p *Path) MoveTo(x, y float32) {
// and ends to the given position (x, y).
// If p doesn't have any subpaths or the last subpath is closed, LineTo sets (x, y) as the start position of a new subpath.
func (p *Path) LineTo(x, y float32) {
if len(p.subpaths) == 0 || p.subpaths[len(p.subpaths)-1].closed {
p.subpaths = append(p.subpaths, &subpath{
points: []point{
{x: x, y: y},
},
})
return
}
p.subpaths[len(p.subpaths)-1].appendPoint(point{x: x, y: y})
p.subpaths = p.subpaths[:0]
p.ops = append(p.ops, op{
typ: opTypeLineTo,
p1: point{x: x, y: y},
})
}
// QuadTo adds a quadratic Bézier curve to the path.
// (x1, y1) is the control point, and (x2, y2) is the destination.
func (p *Path) QuadTo(x1, y1, x2, y2 float32) {
p.quadTo(point{x: x1, y: y1}, point{x: x2, y: y2}, 0)
p.subpaths = p.subpaths[:0]
p.ops = append(p.ops, op{
typ: opTypeQuadTo,
p1: point{x: x1, y: y1},
p2: point{x: x2, y: y2},
})
}
// CubicTo adds a cubic Bézier curve to the path.
// (x1, y1) and (x2, y2) are the control points, and (x3, y3) is the destination.
func (p *Path) CubicTo(x1, y1, x2, y2, x3, y3 float32) {
p.subpaths = p.subpaths[:0]
p.ops = append(p.ops, op{
typ: opTypeCubicTo,
p1: point{x: x1, y: y1},
p2: point{x: x2, y: y2},
p3: point{x: x3, y: y3},
})
}
// Close adds a new line from the last position of the current subpath to the first position of the current subpath,
// and marks the current subpath closed.
// Following operations for this path will start with a new subpath.
func (p *Path) Close() {
p.subpaths = p.subpaths[:0]
p.ops = append(p.ops, op{
typ: opTypeClose,
})
}
func (p *Path) lineTo(pt point) {
if len(p.subpaths) == 0 || p.subpaths[len(p.subpaths)-1].closed {
p.appendNewSubpath(pt)
return
}
p.subpaths[len(p.subpaths)-1].appendPoint(pt)
}
// lineForTwoPoints returns parameters for a line passing through p0 and p1.
@ -135,12 +232,17 @@ func lineForTwoPoints(p0, p1 point) (a, b, c float32) {
}
// isPointCloseToSegment detects the distance between a segment (x0, y0)-(x1, y1) and a point (x, y) is less than allow.
// If p0 and p1 are the same, isPointCloseToSegment returns true when the distance between p0 and p is less than allow.
func isPointCloseToSegment(p, p0, p1 point, allow float32) bool {
if p0 == p1 {
return allow*allow >= (p0.x-p.x)*(p0.x-p.x)+(p0.y-p.y)*(p0.y-p.y)
}
a, b, c := lineForTwoPoints(p0, p1)
// The distance between a line ax+by+c=0 and (x0, y0) is
// |ax0 + by0 + c| / √(a² + b²)
return allow*allow*(a*a+b*b) > (a*p.x+b*p.y+c)*(a*p.x+b*p.y+c)
return allow*allow*(a*a+b*b) >= (a*p.x+b*p.y+c)*(a*p.x+b*p.y+c)
}
// crossingPointForTwoLines returns a crossing point for two lines.
@ -154,24 +256,13 @@ func crossingPointForTwoLines(p00, p01, p10, p11 point) point {
}
}
func (p *Path) currentPosition() (point, bool) {
if len(p.subpaths) == 0 {
return point{}, false
}
return p.subpaths[len(p.subpaths)-1].currentPosition()
}
func (p *Path) quadTo(p1, p2 point, level int) {
func (p *Path) quadTo(p0, p1, p2 point, level int) {
if level > 10 {
return
}
p0, ok := p.currentPosition()
if !ok {
p0 = p1
}
if isPointCloseToSegment(p1, p0, p2, 0.5) {
p.LineTo(p2.x, p2.y)
p.lineTo(p2)
return
}
@ -187,27 +278,17 @@ func (p *Path) quadTo(p1, p2 point, level int) {
x: (p01.x + p12.x) / 2,
y: (p01.y + p12.y) / 2,
}
p.quadTo(p01, p012, level+1)
p.quadTo(p12, p2, level+1)
p.quadTo(p0, p01, p012, level+1)
p.quadTo(p012, p12, p2, level+1)
}
// CubicTo adds a cubic Bézier curve to the path.
// (x1, y1) and (x2, y2) are the control points, and (x3, y3) is the destination.
func (p *Path) CubicTo(x1, y1, x2, y2, x3, y3 float32) {
p.cubicTo(point{x: x1, y: y1}, point{x: x2, y: y2}, point{x: x3, y: y3}, 0)
}
func (p *Path) cubicTo(p1, p2, p3 point, level int) {
func (p *Path) cubicTo(p0, p1, p2, p3 point, level int) {
if level > 10 {
return
}
p0, ok := p.currentPosition()
if !ok {
p0 = p1
}
if isPointCloseToSegment(p1, p0, p3, 0.5) && isPointCloseToSegment(p2, p0, p3, 0.5) {
p.LineTo(p3.x, p3.y)
p.lineTo(p3)
return
}
@ -235,8 +316,8 @@ func (p *Path) cubicTo(p1, p2, p3 point, level int) {
x: (p012.x + p123.x) / 2,
y: (p012.y + p123.y) / 2,
}
p.cubicTo(p01, p012, p0123, level+1)
p.cubicTo(p123, p23, p3, level+1)
p.cubicTo(p0, p01, p012, p0123, level+1)
p.cubicTo(p0123, p123, p23, p3, level+1)
}
func normalize(p point) point {
@ -248,6 +329,26 @@ func cross(p0, p1 point) float32 {
return p0.x*p1.y - p1.x*p0.y
}
func (p *Path) currentPosition() (point, bool) {
if len(p.ops) == 0 {
return point{}, false
}
op := p.ops[len(p.ops)-1]
switch op.typ {
case opTypeMoveTo:
return op.p1, true
case opTypeLineTo:
return op.p1, true
case opTypeQuadTo:
return op.p2, true
case opTypeCubicTo:
return op.p3, true
case opTypeClose:
return point{}, false
}
return point{}, false
}
// ArcTo adds an arc curve to the path.
// (x1, y1) is the first control point, and (x2, y2) is the second control point.
func (p *Path) ArcTo(x1, y1, x2, y2, radius float32) {
@ -362,7 +463,7 @@ func (p *Path) Arc(x, y, radius, startAngle, endAngle float32, dir Direction) {
p.LineTo(x0, y0)
// Calculate the control points for an approximated Bézier curve.
// See https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/curves/beziers.
// See https://learn.microsoft.com/en-us/previous-versions/xamarin/xamarin-forms/user-interface/graphics/skiasharp/curves/beziers.
l := radius * float32(math.Tan(da/4)*4/3)
var cx0, cy0, cx1, cy1 float32
if dir == Clockwise {
@ -379,15 +480,11 @@ func (p *Path) Arc(x, y, radius, startAngle, endAngle float32, dir Direction) {
p.CubicTo(cx0, cy0, cx1, cy1, x1, y1)
}
// Close adds a new line from the last position of the current subpath to the first position of the current subpath,
// and marks the current subpath closed.
// Following operations for this path will start with a new subpath.
func (p *Path) Close() {
func (p *Path) close() {
if len(p.subpaths) == 0 {
return
}
subpath := p.subpaths[len(p.subpaths)-1]
subpath.close()
p.subpaths[len(p.subpaths)-1].close()
}
// AppendVerticesAndIndicesForFilling appends vertices and indices to fill this path and returns them.
@ -405,7 +502,7 @@ func (p *Path) AppendVerticesAndIndicesForFilling(vertices []ebiten.Vertex, indi
// TODO: Add tests.
base := uint16(len(vertices))
for _, subpath := range p.subpaths {
for _, subpath := range p.ensureSubpaths() {
if subpath.pointCount() < 3 {
continue
}
@ -486,12 +583,14 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
return vertices, indices
}
for _, subpath := range p.subpaths {
var rects [][4]point
var tmpPath Path
for _, subpath := range p.ensureSubpaths() {
if subpath.pointCount() < 2 {
continue
}
var rects [][4]point
rects = rects[:0]
for i := 0; i < subpath.pointCount()-1; i++ {
pt := subpath.points[i]
@ -571,46 +670,49 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
delta := math.Pi - da
exceed := float32(math.Abs(1/math.Sin(float64(delta/2)))) > op.MiterLimit
var quad Path
quad.MoveTo(c.x, c.y)
// Quadrilateral
tmpPath.reset()
tmpPath.MoveTo(c.x, c.y)
if da < math.Pi {
quad.LineTo(rect[1].x, rect[1].y)
tmpPath.LineTo(rect[1].x, rect[1].y)
if !exceed {
pt := crossingPointForTwoLines(rect[0], rect[1], nextRect[0], nextRect[1])
quad.LineTo(pt.x, pt.y)
tmpPath.LineTo(pt.x, pt.y)
}
quad.LineTo(nextRect[0].x, nextRect[0].y)
tmpPath.LineTo(nextRect[0].x, nextRect[0].y)
} else {
quad.LineTo(rect[3].x, rect[3].y)
tmpPath.LineTo(rect[3].x, rect[3].y)
if !exceed {
pt := crossingPointForTwoLines(rect[2], rect[3], nextRect[2], nextRect[3])
quad.LineTo(pt.x, pt.y)
tmpPath.LineTo(pt.x, pt.y)
}
quad.LineTo(nextRect[2].x, nextRect[2].y)
tmpPath.LineTo(nextRect[2].x, nextRect[2].y)
}
vertices, indices = quad.AppendVerticesAndIndicesForFilling(vertices, indices)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
case LineJoinBevel:
var tri Path
tri.MoveTo(c.x, c.y)
// Triangle
tmpPath.reset()
tmpPath.MoveTo(c.x, c.y)
if da < math.Pi {
tri.LineTo(rect[1].x, rect[1].y)
tri.LineTo(nextRect[0].x, nextRect[0].y)
tmpPath.LineTo(rect[1].x, rect[1].y)
tmpPath.LineTo(nextRect[0].x, nextRect[0].y)
} else {
tri.LineTo(rect[3].x, rect[3].y)
tri.LineTo(nextRect[2].x, nextRect[2].y)
tmpPath.LineTo(rect[3].x, rect[3].y)
tmpPath.LineTo(nextRect[2].x, nextRect[2].y)
}
vertices, indices = tri.AppendVerticesAndIndicesForFilling(vertices, indices)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
case LineJoinRound:
var arc Path
arc.MoveTo(c.x, c.y)
// Arc
tmpPath.reset()
tmpPath.MoveTo(c.x, c.y)
if da < math.Pi {
arc.Arc(c.x, c.y, op.Width/2, a0, a1, Clockwise)
tmpPath.Arc(c.x, c.y, op.Width/2, a0, a1, Clockwise)
} else {
arc.Arc(c.x, c.y, op.Width/2, a0+math.Pi, a1+math.Pi, CounterClockwise)
tmpPath.Arc(c.x, c.y, op.Width/2, a0+math.Pi, a1+math.Pi, CounterClockwise)
}
vertices, indices = arc.AppendVerticesAndIndicesForFilling(vertices, indices)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
}
}
@ -635,10 +737,11 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
y: (startR[0].y + startR[2].y) / 2,
}
a := float32(math.Atan2(float64(startR[0].y-startR[2].y), float64(startR[0].x-startR[2].x)))
var arc Path
arc.MoveTo(startR[0].x, startR[0].y)
arc.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, CounterClockwise)
vertices, indices = arc.AppendVerticesAndIndicesForFilling(vertices, indices)
// Arc
tmpPath.reset()
tmpPath.MoveTo(startR[0].x, startR[0].y)
tmpPath.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, CounterClockwise)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
}
{
c := point{
@ -646,10 +749,11 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
y: (endR[1].y + endR[3].y) / 2,
}
a := float32(math.Atan2(float64(endR[1].y-endR[3].y), float64(endR[1].x-endR[3].x)))
var arc Path
arc.MoveTo(endR[1].x, endR[1].y)
arc.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, Clockwise)
vertices, indices = arc.AppendVerticesAndIndicesForFilling(vertices, indices)
// Arc
tmpPath.reset()
tmpPath.MoveTo(endR[1].x, endR[1].y)
tmpPath.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, Clockwise)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
}
case LineCapSquare:
@ -659,24 +763,26 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
s, c := math.Sincos(a)
dx, dy := float32(c)*op.Width/2, float32(s)*op.Width/2
var quad Path
quad.MoveTo(startR[0].x, startR[0].y)
quad.LineTo(startR[0].x+dx, startR[0].y+dy)
quad.LineTo(startR[2].x+dx, startR[2].y+dy)
quad.LineTo(startR[2].x, startR[2].y)
vertices, indices = quad.AppendVerticesAndIndicesForFilling(vertices, indices)
// Quadrilateral
tmpPath.reset()
tmpPath.MoveTo(startR[0].x, startR[0].y)
tmpPath.LineTo(startR[0].x+dx, startR[0].y+dy)
tmpPath.LineTo(startR[2].x+dx, startR[2].y+dy)
tmpPath.LineTo(startR[2].x, startR[2].y)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
}
{
a := math.Atan2(float64(endR[1].y-endR[0].y), float64(endR[1].x-endR[0].x))
s, c := math.Sincos(a)
dx, dy := float32(c)*op.Width/2, float32(s)*op.Width/2
var quad Path
quad.MoveTo(endR[1].x, endR[1].y)
quad.LineTo(endR[1].x+dx, endR[1].y+dy)
quad.LineTo(endR[3].x+dx, endR[3].y+dy)
quad.LineTo(endR[3].x, endR[3].y)
vertices, indices = quad.AppendVerticesAndIndicesForFilling(vertices, indices)
// Quadrilateral
tmpPath.reset()
tmpPath.MoveTo(endR[1].x, endR[1].y)
tmpPath.LineTo(endR[1].x+dx, endR[1].y+dy)
tmpPath.LineTo(endR[3].x+dx, endR[3].y+dy)
tmpPath.LineTo(endR[3].x, endR[3].y)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
}
}
}

88
vector/path_test.go Normal file
View File

@ -0,0 +1,88 @@
// Copyright 2024 The Ebitengine 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.
package vector_test
import (
"testing"
"github.com/hajimehoshi/ebiten/v2/vector"
)
func TestIsPointCloseToSegment(t *testing.T) {
testCases := []struct {
p vector.Point
p0 vector.Point
p1 vector.Point
allow float32
want bool
}{
{
p: vector.Point{0.5, 0.5},
p0: vector.Point{0, 0},
p1: vector.Point{1, 0},
allow: 1,
want: true,
},
{
p: vector.Point{0.5, 1.5},
p0: vector.Point{0, 0},
p1: vector.Point{1, 0},
allow: 1,
want: false,
},
{
p: vector.Point{0.5, 0.5},
p0: vector.Point{0, 0},
p1: vector.Point{1, 1},
allow: 0,
want: true,
},
{
p: vector.Point{0, 1},
p0: vector.Point{0, 0},
p1: vector.Point{1, 1},
allow: 0.7,
want: false,
},
{
p: vector.Point{0, 1},
p0: vector.Point{0, 0},
p1: vector.Point{1, 1},
allow: 0.8,
want: true,
},
{
// p0 and p1 are the same.
p: vector.Point{0, 1},
p0: vector.Point{0.5, 0.5},
p1: vector.Point{0.5, 0.5},
allow: 0.7,
want: false,
},
{
// p0 and p1 are the same.
p: vector.Point{0, 1},
p0: vector.Point{0.5, 0.5},
p1: vector.Point{0.5, 0.5},
allow: 0.8,
want: true,
},
}
for _, tc := range testCases {
if got := vector.IsPointCloseToSegment(tc.p, tc.p0, tc.p1, tc.allow); got != tc.want {
t.Errorf("got: %v, want: %v", got, tc.want)
}
}
}

View File

@ -18,6 +18,7 @@ import (
"image"
"image/color"
"math"
"sync"
"github.com/hajimehoshi/ebiten/v2"
)
@ -27,6 +28,18 @@ var (
whiteSubImage = whiteImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
)
var (
cachedVertices []ebiten.Vertex
cachedIndices []uint16
cacheM sync.Mutex
)
func useCachedVerticesAndIndices(fn func([]ebiten.Vertex, []uint16) (vs []ebiten.Vertex, is []uint16)) {
cacheM.Lock()
defer cacheM.Unlock()
cachedVertices, cachedIndices = fn(cachedVertices[:0], cachedIndices[:0])
}
func init() {
b := whiteImage.Bounds()
pix := make([]byte, 4*b.Dx()*b.Dy())
@ -63,9 +76,12 @@ func StrokeLine(dst *ebiten.Image, x0, y0, x1, y1 float32, strokeWidth float32,
path.LineTo(x1, y1)
strokeOp := &StrokeOptions{}
strokeOp.Width = strokeWidth
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias)
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForStroke(vs, is, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
}
// DrawFilledRect fills a rectangle with the specified width and color.
@ -75,9 +91,12 @@ func DrawFilledRect(dst *ebiten.Image, x, y, width, height float32, clr color.Co
path.LineTo(x, y+height)
path.LineTo(x+width, y+height)
path.LineTo(x+width, y)
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
drawVerticesForUtil(dst, vs, is, clr, antialias)
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForFilling(vs, is)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
}
// StrokeRect strokes a rectangle with the specified width and color.
@ -94,18 +113,24 @@ func StrokeRect(dst *ebiten.Image, x, y, width, height float32, strokeWidth floa
strokeOp := &StrokeOptions{}
strokeOp.Width = strokeWidth
strokeOp.MiterLimit = 10
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias)
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForStroke(vs, is, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
}
// DrawFilledCircle fills a circle with the specified center position (cx, cy), the radius (r), width and color.
func DrawFilledCircle(dst *ebiten.Image, cx, cy, r float32, clr color.Color, antialias bool) {
var path Path
path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise)
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
drawVerticesForUtil(dst, vs, is, clr, antialias)
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForFilling(vs, is)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
}
// StrokeCircle strokes a circle with the specified center position (cx, cy), the radius (r), width and color.
@ -118,7 +143,10 @@ func StrokeCircle(dst *ebiten.Image, cx, cy, r float32, strokeWidth float32, clr
strokeOp := &StrokeOptions{}
strokeOp.Width = strokeWidth
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias)
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForStroke(vs, is, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
}