Skip to content

Commit 0f93535

Browse files
committed
ebiten: add FilterPixelated
Closes #2826
1 parent b297611 commit 0f93535

File tree

6 files changed

+71
-51
lines changed

6 files changed

+71
-51
lines changed

examples/filter/main.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"image"
2020
_ "image/png"
2121
"log"
22+
"math"
2223

2324
"github.com/hajimehoshi/ebiten/v2"
2425
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@@ -35,27 +36,40 @@ var (
3536
)
3637

3738
type Game struct {
39+
counter int
3840
}
3941

4042
func (g *Game) Update() error {
43+
g.counter++
4144
return nil
4245
}
4346

4447
func (g *Game) Draw(screen *ebiten.Image) {
45-
ebitenutil.DebugPrint(screen, "Nearest Filter (default) VS Linear Filter")
48+
scale := 2*math.Sin(float64(g.counter%360)*math.Pi/180) + 4
49+
50+
ebitenutil.DebugPrintAt(screen, "Nearest Filter (default) and Linear Filter", 16, 16)
4651

4752
op := &ebiten.DrawImageOptions{}
48-
op.GeoM.Scale(4, 4)
53+
op.GeoM.Scale(scale, scale)
4954
op.GeoM.Translate(64, 64)
5055
// By default, nearest filter is used.
5156
screen.DrawImage(ebitenImage, op)
5257

5358
op = &ebiten.DrawImageOptions{}
54-
op.GeoM.Scale(4, 4)
55-
op.GeoM.Translate(64, 64+240)
59+
op.GeoM.Scale(scale, scale)
60+
op.GeoM.Translate(64+240, 64)
5661
// Specify linear filter.
5762
op.Filter = ebiten.FilterLinear
5863
screen.DrawImage(ebitenImage, op)
64+
65+
ebitenutil.DebugPrintAt(screen, "Pixelated Filter", 16, 16+200)
66+
67+
op = &ebiten.DrawImageOptions{}
68+
op.GeoM.Scale(scale, scale)
69+
op.GeoM.Translate(64, 64+200)
70+
// Specify pixelated filter.
71+
op.Filter = ebiten.FilterPixelated
72+
screen.DrawImage(ebitenImage, op)
5973
}
6074

6175
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {

gameforui.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
package ebiten
1616

1717
import (
18-
"fmt"
1918
"image"
2019
"math"
21-
"sync"
2220
"sync/atomic"
2321

2422
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
@@ -131,25 +129,12 @@ func (g *gameForUI) DrawFinalScreen(scale, offsetX, offsetY float64) {
131129
DefaultDrawFinalScreen(g.screen, g.offscreen, geoM)
132130
}
133131

134-
var (
135-
theScreenShader *Shader
136-
theScreenShaderOnce sync.Once
137-
)
138-
139132
// DefaultDrawFinalScreen is the default implementation of [FinalScreenDrawer.DrawFinalScreen],
140133
// used when a [Game] doesn't implement [FinalScreenDrawer].
141134
//
142135
// You can use DefaultDrawFinalScreen when you need the default implementation of [FinalScreenDrawer.DrawFinalScreen]
143136
// in your implementation of [FinalScreenDrawer], for example.
144137
func DefaultDrawFinalScreen(screen FinalScreen, offscreen *Image, geoM GeoM) {
145-
theScreenShaderOnce.Do(func() {
146-
s, err := newShader([]byte(builtinshader.ScreenShaderSource), "screen")
147-
if err != nil {
148-
panic(fmt.Sprintf("ebiten: compiling the screen shader failed: %v", err))
149-
}
150-
theScreenShader = s
151-
})
152-
153138
scale := geoM.Element(0, 0)
154139
switch {
155140
case !screenFilterEnabled.Load(), math.Floor(scale) == scale:
@@ -166,6 +151,7 @@ func DefaultDrawFinalScreen(screen FinalScreen, offscreen *Image, geoM GeoM) {
166151
op.Images[0] = offscreen
167152
op.GeoM = geoM
168153
w, h := offscreen.Bounds().Dx(), offscreen.Bounds().Dy()
169-
screen.DrawRectShader(w, h, theScreenShader, op)
154+
screenShader := builtinShader(builtinshader.FilterPixelated, builtinshader.AddressUnsafe, false)
155+
screen.DrawRectShader(w, h, screenShader, op)
170156
}
171157
}

graphics.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const (
2828

2929
// FilterLinear represents linear filter
3030
FilterLinear Filter = Filter(builtinshader.FilterLinear)
31+
32+
// FilterPixelated represents a pixelated filter.
33+
// FilterPixelated is similar to FilterNearest, but it preserves the pixelated appearance even when scaled to non-integer sizes.
34+
FilterPixelated Filter = Filter(builtinshader.FilterPixelated)
3135
)
3236

3337
// GraphicsLibrary represents graphics libraries supported by the engine.

internal/builtinshader/defs.go

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)