Skip to content

Commit

Permalink
ebiten: add FilterPixelated
Browse files Browse the repository at this point in the history
Closes #2826
  • Loading branch information
hajimehoshi committed Jan 22, 2025
1 parent b297611 commit 0f93535
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 51 deletions.
22 changes: 18 additions & 4 deletions examples/filter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"image"
_ "image/png"
"log"
"math"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
Expand All @@ -35,27 +36,40 @@ var (
)

type Game struct {
counter int
}

func (g *Game) Update() error {
g.counter++
return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
ebitenutil.DebugPrint(screen, "Nearest Filter (default) VS Linear Filter")
scale := 2*math.Sin(float64(g.counter%360)*math.Pi/180) + 4

ebitenutil.DebugPrintAt(screen, "Nearest Filter (default) and Linear Filter", 16, 16)

op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(4, 4)
op.GeoM.Scale(scale, scale)
op.GeoM.Translate(64, 64)
// By default, nearest filter is used.
screen.DrawImage(ebitenImage, op)

op = &ebiten.DrawImageOptions{}
op.GeoM.Scale(4, 4)
op.GeoM.Translate(64, 64+240)
op.GeoM.Scale(scale, scale)
op.GeoM.Translate(64+240, 64)
// Specify linear filter.
op.Filter = ebiten.FilterLinear
screen.DrawImage(ebitenImage, op)

ebitenutil.DebugPrintAt(screen, "Pixelated Filter", 16, 16+200)

op = &ebiten.DrawImageOptions{}
op.GeoM.Scale(scale, scale)
op.GeoM.Translate(64, 64+200)
// Specify pixelated filter.
op.Filter = ebiten.FilterPixelated
screen.DrawImage(ebitenImage, op)
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
Expand Down
18 changes: 2 additions & 16 deletions gameforui.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
package ebiten

import (
"fmt"
"image"
"math"
"sync"
"sync/atomic"

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

var (
theScreenShader *Shader
theScreenShaderOnce sync.Once
)

// DefaultDrawFinalScreen is the default implementation of [FinalScreenDrawer.DrawFinalScreen],
// used when a [Game] doesn't implement [FinalScreenDrawer].
//
// You can use DefaultDrawFinalScreen when you need the default implementation of [FinalScreenDrawer.DrawFinalScreen]
// in your implementation of [FinalScreenDrawer], for example.
func DefaultDrawFinalScreen(screen FinalScreen, offscreen *Image, geoM GeoM) {
theScreenShaderOnce.Do(func() {
s, err := newShader([]byte(builtinshader.ScreenShaderSource), "screen")
if err != nil {
panic(fmt.Sprintf("ebiten: compiling the screen shader failed: %v", err))
}
theScreenShader = s
})

scale := geoM.Element(0, 0)
switch {
case !screenFilterEnabled.Load(), math.Floor(scale) == scale:
Expand All @@ -166,6 +151,7 @@ func DefaultDrawFinalScreen(screen FinalScreen, offscreen *Image, geoM GeoM) {
op.Images[0] = offscreen
op.GeoM = geoM
w, h := offscreen.Bounds().Dx(), offscreen.Bounds().Dy()
screen.DrawRectShader(w, h, theScreenShader, op)
screenShader := builtinShader(builtinshader.FilterPixelated, builtinshader.AddressUnsafe, false)
screen.DrawRectShader(w, h, screenShader, op)
}
}
4 changes: 4 additions & 0 deletions graphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const (

// FilterLinear represents linear filter
FilterLinear Filter = Filter(builtinshader.FilterLinear)

// FilterPixelated represents a pixelated filter.
// FilterPixelated is similar to FilterNearest, but it preserves the pixelated appearance even when scaled to non-integer sizes.
FilterPixelated Filter = Filter(builtinshader.FilterPixelated)
)

// GraphicsLibrary represents graphics libraries supported by the engine.
Expand Down
30 changes: 24 additions & 6 deletions internal/builtinshader/defs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0f93535

Please sign in to comment.