mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Compare commits
16 Commits
d79ac50bc2
...
a0d9122cae
Author | SHA1 | Date | |
---|---|---|---|
|
a0d9122cae | ||
|
361da49887 | ||
|
a5235eea86 | ||
|
1a0f50503d | ||
|
956a95c397 | ||
|
9c80367f2f | ||
|
3624486f8b | ||
|
1f03971fa9 | ||
|
fab9482e0e | ||
|
74722298a2 | ||
|
30157b5dea | ||
|
b20692f523 | ||
|
2eebe55b90 | ||
|
ec06c68fa3 | ||
|
4601cffaba | ||
|
5e8d969034 |
10
.github/workflows/test.yml
vendored
10
.github/workflows/test.yml
vendored
@ -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=6e5bbb88049c42eb62e19d10e5be9940b9271aab
|
||||
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
|
||||
|
5
doc.go
5
doc.go
@ -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
|
||||
|
4
go.mod
4
go.mod
@ -5,8 +5,8 @@ go 1.19
|
||||
require (
|
||||
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.3.0.20240806041909-d412d64fb19f
|
||||
github.com/ebitengine/purego v0.8.0-alpha.3.0.20240805123034-6cc30db8f187
|
||||
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
|
||||
|
8
go.sum
8
go.sum
@ -2,10 +2,10 @@ github.com/ebitengine/gomobile v0.0.0-20240802043200-192f051f4fcc h1:76TYsaP1F48
|
||||
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.3.0.20240806041909-d412d64fb19f h1:1a7SoSH0DOZEIRXcWNRCAYV2dj9POTlyqi7zKrmhcTM=
|
||||
github.com/ebitengine/oto/v3 v3.3.0-alpha.3.0.20240806041909-d412d64fb19f/go.mod h1:yYvXK7mgNwsFawY5RsvGI6yhMHtD+0MfaPkDTl9/uv8=
|
||||
github.com/ebitengine/purego v0.8.0-alpha.3.0.20240805123034-6cc30db8f187 h1:vXEgFw8Ni26tlWLmeI8nFXa7pMLKUTR9hfXcQPCYpQg=
|
||||
github.com/ebitengine/purego v0.8.0-alpha.3.0.20240805123034-6cc30db8f187/go.mod h1:SQ56/omnSL8DdaBSKswoBvsMjgaWQyxyeMtb48sOskI=
|
||||
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=
|
||||
|
@ -866,7 +866,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)
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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() {
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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.
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,6 @@ package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/ebitengine/purego"
|
||||
@ -31,40 +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
|
||||
}
|
||||
errors = append(errors, fmt.Sprintf("%s: %v", name, err))
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
@ -75,6 +43,20 @@ func (c *defaultContext) init() error {
|
||||
errors = append(errors, fmt.Sprintf("%s: %v", name, err))
|
||||
}
|
||||
|
||||
// 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, ", "))
|
||||
}
|
||||
|
||||
|
@ -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++ {
|
||||
|
295
text/v2/atlas.go
Normal file
295
text/v2/atlas.go
Normal file
@ -0,0 +1,295 @@
|
||||
// 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 text
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/packing"
|
||||
)
|
||||
|
||||
type glyphAtlas struct {
|
||||
page *packing.Page
|
||||
image *ebiten.Image
|
||||
}
|
||||
|
||||
type glyphImage struct {
|
||||
atlas *glyphAtlas
|
||||
node *packing.Node
|
||||
}
|
||||
|
||||
func (i *glyphImage) Image() *ebiten.Image {
|
||||
return i.atlas.image.SubImage(i.node.Region()).(*ebiten.Image)
|
||||
}
|
||||
|
||||
func newGlyphAtlas() *glyphAtlas {
|
||||
return &glyphAtlas{
|
||||
// Note: 128x128 is arbitrary, maybe a better value can be inferred
|
||||
// from the font size or something
|
||||
page: packing.NewPage(128, 128, 1024), // TODO: not 1024
|
||||
image: ebiten.NewImage(128, 128),
|
||||
}
|
||||
}
|
||||
|
||||
func (g *glyphAtlas) NewImage(w, h int) *glyphImage {
|
||||
n := g.page.Alloc(w, h)
|
||||
pw, ph := g.page.Size()
|
||||
if pw > g.image.Bounds().Dx() || ph > g.image.Bounds().Dy() {
|
||||
newImage := ebiten.NewImage(pw, ph)
|
||||
newImage.DrawImage(g.image, nil)
|
||||
g.image = newImage
|
||||
}
|
||||
|
||||
return &glyphImage{
|
||||
atlas: g,
|
||||
node: n,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *glyphAtlas) Free(img *glyphImage) {
|
||||
g.page.Free(img.node)
|
||||
}
|
||||
|
||||
type drawRange struct {
|
||||
atlas *glyphAtlas
|
||||
end int
|
||||
}
|
||||
|
||||
// drawList stores triangle versions of DrawImage calls when
|
||||
// all images are sub-images of an atlas.
|
||||
// Temporary vertices and indices can be re-used after calling
|
||||
// Flush, so it is more efficient to keep a reference to a drawList
|
||||
// instead of creating a new one every frame.
|
||||
type drawList struct {
|
||||
ranges []drawRange
|
||||
vx []ebiten.Vertex
|
||||
ix []uint16
|
||||
}
|
||||
|
||||
// drawCommand is the equivalent of the regular DrawImageOptions
|
||||
// but only including options that will not break batching.
|
||||
// Filter, Address, Blend and AntiAlias are determined at Flush()
|
||||
type drawCommand struct {
|
||||
Image *glyphImage
|
||||
|
||||
ColorScale ebiten.ColorScale
|
||||
GeoM ebiten.GeoM
|
||||
}
|
||||
|
||||
var rectIndices = [6]uint16{0, 1, 2, 1, 2, 3}
|
||||
|
||||
type point struct {
|
||||
X, Y float32
|
||||
}
|
||||
|
||||
func pt(x, y float64) point {
|
||||
return point{
|
||||
X: float32(x),
|
||||
Y: float32(y),
|
||||
}
|
||||
}
|
||||
|
||||
type rectOpts struct {
|
||||
Dsts [4]point
|
||||
SrcX0, SrcY0 float32
|
||||
SrcX1, SrcY1 float32
|
||||
R, G, B, A float32
|
||||
}
|
||||
|
||||
// adjustDestinationPixel is the original ebitengine implementation found here:
|
||||
// https://github.com/hajimehoshi/ebiten/blob/v2.8.0-alpha.1/internal/graphics/vertex.go#L102-L126
|
||||
func adjustDestinationPixel(x float32) float32 {
|
||||
// Avoid the center of the pixel, which is problematic (#929, #1171).
|
||||
// Instead, align the vertices with about 1/3 pixels.
|
||||
//
|
||||
// The intention here is roughly this code:
|
||||
//
|
||||
// float32(math.Floor((float64(x)+1.0/6.0)*3) / 3)
|
||||
//
|
||||
// The actual implementation is more optimized than the above implementation.
|
||||
ix := float32(int(x))
|
||||
if x < 0 && x != ix {
|
||||
ix -= 1
|
||||
}
|
||||
frac := x - ix
|
||||
switch {
|
||||
case frac < 3.0/16.0:
|
||||
return ix
|
||||
case frac < 8.0/16.0:
|
||||
return ix + 5.0/16.0
|
||||
case frac < 13.0/16.0:
|
||||
return ix + 11.0/16.0
|
||||
default:
|
||||
return ix + 16.0/16.0
|
||||
}
|
||||
}
|
||||
|
||||
func appendRectVerticesIndices(vertices []ebiten.Vertex, indices []uint16, index int, opts *rectOpts) ([]ebiten.Vertex, []uint16) {
|
||||
sx0, sy0, sx1, sy1 := opts.SrcX0, opts.SrcY0, opts.SrcX1, opts.SrcY1
|
||||
r, g, b, a := opts.R, opts.G, opts.B, opts.A
|
||||
vertices = append(vertices,
|
||||
ebiten.Vertex{
|
||||
DstX: adjustDestinationPixel(opts.Dsts[0].X),
|
||||
DstY: adjustDestinationPixel(opts.Dsts[0].Y),
|
||||
SrcX: sx0,
|
||||
SrcY: sy0,
|
||||
ColorR: r,
|
||||
ColorG: g,
|
||||
ColorB: b,
|
||||
ColorA: a,
|
||||
},
|
||||
ebiten.Vertex{
|
||||
DstX: adjustDestinationPixel(opts.Dsts[1].X),
|
||||
DstY: adjustDestinationPixel(opts.Dsts[1].Y),
|
||||
SrcX: sx1,
|
||||
SrcY: sy0,
|
||||
ColorR: r,
|
||||
ColorG: g,
|
||||
ColorB: b,
|
||||
ColorA: a,
|
||||
},
|
||||
ebiten.Vertex{
|
||||
DstX: adjustDestinationPixel(opts.Dsts[2].X),
|
||||
DstY: adjustDestinationPixel(opts.Dsts[2].Y),
|
||||
SrcX: sx0,
|
||||
SrcY: sy1,
|
||||
ColorR: r,
|
||||
ColorG: g,
|
||||
ColorB: b,
|
||||
ColorA: a,
|
||||
},
|
||||
ebiten.Vertex{
|
||||
DstX: adjustDestinationPixel(opts.Dsts[3].X),
|
||||
DstY: adjustDestinationPixel(opts.Dsts[3].Y),
|
||||
SrcX: sx1,
|
||||
SrcY: sy1,
|
||||
ColorR: r,
|
||||
ColorG: g,
|
||||
ColorB: b,
|
||||
ColorA: a,
|
||||
},
|
||||
)
|
||||
|
||||
indiceCursor := uint16(index * 4)
|
||||
indices = append(indices,
|
||||
rectIndices[0]+indiceCursor,
|
||||
rectIndices[1]+indiceCursor,
|
||||
rectIndices[2]+indiceCursor,
|
||||
rectIndices[3]+indiceCursor,
|
||||
rectIndices[4]+indiceCursor,
|
||||
rectIndices[5]+indiceCursor,
|
||||
)
|
||||
|
||||
return vertices, indices
|
||||
}
|
||||
|
||||
// Add adds DrawImage commands to the DrawList, images from multiple
|
||||
// atlases can be added but they will break the previous batch bound to
|
||||
// a different atlas, requiring an additional draw call internally.
|
||||
// So, it is better to have the maximum of consecutive DrawCommand images
|
||||
// sharing the same atlas.
|
||||
func (dl *drawList) Add(commands ...*drawCommand) {
|
||||
if len(commands) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var batch *drawRange
|
||||
|
||||
if len(dl.ranges) > 0 {
|
||||
batch = &dl.ranges[len(dl.ranges)-1]
|
||||
} else {
|
||||
dl.ranges = append(dl.ranges, drawRange{
|
||||
atlas: commands[0].Image.atlas,
|
||||
})
|
||||
batch = &dl.ranges[0]
|
||||
}
|
||||
// Add vertices and indices
|
||||
opts := &rectOpts{}
|
||||
for _, cmd := range commands {
|
||||
if cmd.Image.atlas != batch.atlas {
|
||||
dl.ranges = append(dl.ranges, drawRange{
|
||||
atlas: cmd.Image.atlas,
|
||||
})
|
||||
batch = &dl.ranges[len(dl.ranges)-1]
|
||||
}
|
||||
|
||||
// Dst attributes
|
||||
bounds := cmd.Image.node.Region()
|
||||
opts.Dsts[0] = pt(cmd.GeoM.Apply(0, 0))
|
||||
opts.Dsts[1] = pt(cmd.GeoM.Apply(
|
||||
float64(bounds.Dx()), 0,
|
||||
))
|
||||
opts.Dsts[2] = pt(cmd.GeoM.Apply(
|
||||
0, float64(bounds.Dy()),
|
||||
))
|
||||
opts.Dsts[3] = pt(cmd.GeoM.Apply(
|
||||
float64(bounds.Dx()), float64(bounds.Dy()),
|
||||
))
|
||||
|
||||
// Color and source attributes
|
||||
opts.R = cmd.ColorScale.R()
|
||||
opts.G = cmd.ColorScale.G()
|
||||
opts.B = cmd.ColorScale.B()
|
||||
opts.A = cmd.ColorScale.A()
|
||||
opts.SrcX0 = float32(bounds.Min.X)
|
||||
opts.SrcY0 = float32(bounds.Min.Y)
|
||||
opts.SrcX1 = float32(bounds.Max.X)
|
||||
opts.SrcY1 = float32(bounds.Max.Y)
|
||||
|
||||
dl.vx, dl.ix = appendRectVerticesIndices(
|
||||
dl.vx, dl.ix, batch.end, opts,
|
||||
)
|
||||
|
||||
batch.end++
|
||||
}
|
||||
}
|
||||
|
||||
// DrawOptions are additional options that will be applied to
|
||||
// all draw commands from the draw list when calling Flush().
|
||||
type drawOptions struct {
|
||||
ColorScaleMode ebiten.ColorScaleMode
|
||||
Blend ebiten.Blend
|
||||
Filter ebiten.Filter
|
||||
Address ebiten.Address
|
||||
AntiAlias bool
|
||||
}
|
||||
|
||||
// Flush executes all the draw commands as the smallest possible
|
||||
// amount of draw calls, and then clears the list for next uses.
|
||||
func (dl *drawList) Flush(dst *ebiten.Image, opts *drawOptions) {
|
||||
var topts *ebiten.DrawTrianglesOptions
|
||||
if opts != nil {
|
||||
topts = &ebiten.DrawTrianglesOptions{
|
||||
ColorScaleMode: opts.ColorScaleMode,
|
||||
Blend: opts.Blend,
|
||||
Filter: opts.Filter,
|
||||
Address: opts.Address,
|
||||
AntiAlias: opts.AntiAlias,
|
||||
}
|
||||
}
|
||||
index := 0
|
||||
for _, r := range dl.ranges {
|
||||
dst.DrawTriangles(
|
||||
dl.vx[index*4:(index+r.end)*4],
|
||||
dl.ix[index*6:(index+r.end)*6],
|
||||
r.atlas.image,
|
||||
topts,
|
||||
)
|
||||
index += r.end
|
||||
}
|
||||
// Clear buffers
|
||||
dl.ranges = dl.ranges[:0]
|
||||
dl.vx = dl.vx[:0]
|
||||
dl.ix = dl.ix[:0]
|
||||
}
|
@ -18,7 +18,6 @@ import (
|
||||
"math"
|
||||
"sync"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/hook"
|
||||
)
|
||||
|
||||
@ -38,17 +37,18 @@ func init() {
|
||||
}
|
||||
|
||||
type glyphImageCacheEntry struct {
|
||||
image *ebiten.Image
|
||||
image *glyphImage
|
||||
atime int64
|
||||
}
|
||||
|
||||
type glyphImageCache[Key comparable] struct {
|
||||
atlas *glyphAtlas
|
||||
cache map[Key]*glyphImageCacheEntry
|
||||
atime int64
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *ebiten.Image) *ebiten.Image {
|
||||
func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func(a *glyphAtlas) *glyphImage) *glyphImage {
|
||||
g.m.Lock()
|
||||
defer g.m.Unlock()
|
||||
|
||||
@ -61,10 +61,11 @@ func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *eb
|
||||
}
|
||||
|
||||
if g.cache == nil {
|
||||
g.atlas = newGlyphAtlas()
|
||||
g.cache = map[Key]*glyphImageCacheEntry{}
|
||||
}
|
||||
|
||||
img := create()
|
||||
img := create(g.atlas)
|
||||
e = &glyphImageCacheEntry{
|
||||
image: img,
|
||||
}
|
||||
@ -91,6 +92,7 @@ func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *eb
|
||||
continue
|
||||
}
|
||||
delete(g.cache, key)
|
||||
g.atlas.Free(e.image)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -310,11 +310,16 @@ func (g *GoTextFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffse
|
||||
}))
|
||||
// Append a glyph even if img is nil.
|
||||
// This is necessary to return index information for control characters.
|
||||
var ebitenImage *ebiten.Image
|
||||
if img != nil {
|
||||
ebitenImage = img.Image()
|
||||
}
|
||||
glyphs = append(glyphs, Glyph{
|
||||
img: img,
|
||||
StartIndexInBytes: indexOffset + glyph.startIndex,
|
||||
EndIndexInBytes: indexOffset + glyph.endIndex,
|
||||
GID: uint32(glyph.shapingGlyph.GlyphID),
|
||||
Image: img,
|
||||
Image: ebitenImage,
|
||||
X: float64(imgX),
|
||||
Y: float64(imgY),
|
||||
})
|
||||
@ -327,7 +332,7 @@ func (g *GoTextFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffse
|
||||
return glyphs
|
||||
}
|
||||
|
||||
func (g *GoTextFace) glyphImage(glyph glyph, origin fixed.Point26_6) (*ebiten.Image, int, int) {
|
||||
func (g *GoTextFace) glyphImage(glyph glyph, origin fixed.Point26_6) (*glyphImage, int, int) {
|
||||
if g.direction().isHorizontal() {
|
||||
origin.X = adjustGranularity(origin.X, g)
|
||||
origin.Y &^= ((1 << 6) - 1)
|
||||
@ -347,8 +352,8 @@ func (g *GoTextFace) glyphImage(glyph glyph, origin fixed.Point26_6) (*ebiten.Im
|
||||
yoffset: subpixelOffset.Y,
|
||||
variations: g.ensureVariationsString(),
|
||||
}
|
||||
img := g.Source.getOrCreateGlyphImage(g, key, func() *ebiten.Image {
|
||||
return segmentsToImage(glyph.scaledSegments, subpixelOffset, b)
|
||||
img := g.Source.getOrCreateGlyphImage(g, key, func(a *glyphAtlas) *glyphImage {
|
||||
return segmentsToImage(a, glyph.scaledSegments, subpixelOffset, b)
|
||||
})
|
||||
|
||||
imgX := (origin.X + b.Min.X).Floor()
|
||||
|
@ -26,8 +26,6 @@ import (
|
||||
"github.com/go-text/typesetting/opentype/loader"
|
||||
"github.com/go-text/typesetting/shaping"
|
||||
"golang.org/x/image/math/fixed"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type goTextOutputCacheKey struct {
|
||||
@ -282,7 +280,7 @@ func (g *GoTextFaceSource) scale(size float64) float64 {
|
||||
return size / float64(g.f.Upem())
|
||||
}
|
||||
|
||||
func (g *GoTextFaceSource) getOrCreateGlyphImage(goTextFace *GoTextFace, key goTextGlyphImageCacheKey, create func() *ebiten.Image) *ebiten.Image {
|
||||
func (g *GoTextFaceSource) getOrCreateGlyphImage(goTextFace *GoTextFace, key goTextGlyphImageCacheKey, create func(a *glyphAtlas) *glyphImage) *glyphImage {
|
||||
if g.glyphImageCache == nil {
|
||||
g.glyphImageCache = map[float64]*glyphImageCache[goTextGlyphImageCacheKey]{}
|
||||
}
|
||||
|
@ -19,11 +19,11 @@ import (
|
||||
"image/draw"
|
||||
"math"
|
||||
|
||||
"github.com/go-text/typesetting/opentype/api"
|
||||
"golang.org/x/image/math/fixed"
|
||||
gvector "golang.org/x/image/vector"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/go-text/typesetting/opentype/api"
|
||||
"golang.org/x/image/math/fixed"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
)
|
||||
|
||||
@ -75,7 +75,7 @@ func segmentsToBounds(segs []api.Segment) fixed.Rectangle26_6 {
|
||||
}
|
||||
}
|
||||
|
||||
func segmentsToImage(segs []api.Segment, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *ebiten.Image {
|
||||
func segmentsToImage(a *glyphAtlas, segs []api.Segment, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *glyphImage {
|
||||
if len(segs) == 0 {
|
||||
return nil
|
||||
}
|
||||
@ -122,7 +122,10 @@ func segmentsToImage(segs []api.Segment, subpixelOffset fixed.Point26_6, glyphBo
|
||||
|
||||
dst := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||
rast.Draw(dst, dst.Bounds(), image.Opaque, image.Point{})
|
||||
return ebiten.NewImageFromImage(dst)
|
||||
img := a.NewImage(w, h)
|
||||
img.Image().WritePixels(dst.Pix)
|
||||
|
||||
return img
|
||||
}
|
||||
|
||||
func appendVectorPathFromSegments(path *vector.Path, segs []api.Segment, x, y float32) {
|
||||
|
@ -21,7 +21,6 @@ import (
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/math/fixed"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
)
|
||||
|
||||
@ -119,9 +118,10 @@ func (s *GoXFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset i
|
||||
// Append a glyph even if img is nil.
|
||||
// This is necessary to return index information for control characters.
|
||||
glyphs = append(glyphs, Glyph{
|
||||
img: img,
|
||||
StartIndexInBytes: indexOffset + i,
|
||||
EndIndexInBytes: indexOffset + i + size,
|
||||
Image: img,
|
||||
Image: img.Image(),
|
||||
X: float64(imgX),
|
||||
Y: float64(imgY),
|
||||
})
|
||||
@ -132,7 +132,7 @@ func (s *GoXFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset i
|
||||
return glyphs
|
||||
}
|
||||
|
||||
func (s *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, int, int, fixed.Int26_6) {
|
||||
func (s *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*glyphImage, int, int, fixed.Int26_6) {
|
||||
// Assume that GoXFace's direction is always horizontal.
|
||||
origin.X = adjustGranularity(origin.X, s)
|
||||
origin.Y &^= ((1 << 6) - 1)
|
||||
@ -146,15 +146,15 @@ func (s *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, int
|
||||
rune: r,
|
||||
xoffset: subpixelOffset.X,
|
||||
}
|
||||
img := s.glyphImageCache.getOrCreate(s, key, func() *ebiten.Image {
|
||||
return s.glyphImageImpl(r, subpixelOffset, b)
|
||||
img := s.glyphImageCache.getOrCreate(s, key, func(a *glyphAtlas) *glyphImage {
|
||||
return s.glyphImageImpl(a, r, subpixelOffset, b)
|
||||
})
|
||||
imgX := (origin.X + b.Min.X).Floor()
|
||||
imgY := (origin.Y + b.Min.Y).Floor()
|
||||
return img, imgX, imgY, a
|
||||
}
|
||||
|
||||
func (s *GoXFace) glyphImageImpl(r rune, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *ebiten.Image {
|
||||
func (s *GoXFace) glyphImageImpl(a *glyphAtlas, r rune, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *glyphImage {
|
||||
w, h := (glyphBounds.Max.X - glyphBounds.Min.X).Ceil(), (glyphBounds.Max.Y - glyphBounds.Min.Y).Ceil()
|
||||
if w == 0 || h == 0 {
|
||||
return nil
|
||||
@ -178,7 +178,10 @@ func (s *GoXFace) glyphImageImpl(r rune, subpixelOffset fixed.Point26_6, glyphBo
|
||||
}
|
||||
d.DrawString(string(r))
|
||||
|
||||
return ebiten.NewImageFromImage(rgba)
|
||||
img := a.NewImage(w, h)
|
||||
img.Image().WritePixels(rgba.Pix)
|
||||
|
||||
return img
|
||||
}
|
||||
|
||||
// direction implements Face.
|
||||
|
@ -111,15 +111,24 @@ func Draw(dst *ebiten.Image, text string, face Face, options *DrawOptions) {
|
||||
|
||||
geoM := drawOp.GeoM
|
||||
|
||||
dl := &drawList{}
|
||||
dc := &drawCommand{}
|
||||
for _, g := range AppendGlyphs(nil, text, face, &layoutOp) {
|
||||
if g.Image == nil {
|
||||
continue
|
||||
}
|
||||
drawOp.GeoM.Reset()
|
||||
drawOp.GeoM.Translate(g.X, g.Y)
|
||||
drawOp.GeoM.Concat(geoM)
|
||||
dst.DrawImage(g.Image, &drawOp)
|
||||
dc.GeoM.Reset()
|
||||
dc.GeoM.Translate(g.X, g.Y)
|
||||
dc.GeoM.Concat(geoM)
|
||||
dc.ColorScale = drawOp.ColorScale
|
||||
dc.Image = g.img
|
||||
dl.Add(dc)
|
||||
}
|
||||
dl.Flush(dst, &drawOptions{
|
||||
Blend: drawOp.Blend,
|
||||
Filter: drawOp.Filter,
|
||||
ColorScaleMode: ebiten.ColorScaleModePremultipliedAlpha,
|
||||
})
|
||||
}
|
||||
|
||||
// AppendGlyphs appends glyphs to the given slice and returns a slice.
|
||||
|
@ -115,6 +115,11 @@ func adjustGranularity(x fixed.Int26_6, face Face) fixed.Int26_6 {
|
||||
|
||||
// Glyph represents one glyph to render.
|
||||
type Glyph struct {
|
||||
// Image is a rasterized glyph image.
|
||||
// Image is a grayscale image i.e. RGBA values are the same.
|
||||
// Image should be used as a render source and should not be modified.
|
||||
img *glyphImage
|
||||
|
||||
// StartIndexInBytes is the start index in bytes for the given string at AppendGlyphs.
|
||||
StartIndexInBytes int
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user