LCOV - code coverage report
Current view: top level - screeneffect - filter.go Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 38 38
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package screeneffect
       2              : 
       3              : import "github.com/hajimehoshi/ebiten/v2"
       4              : 
       5              : // Filter は画面エフェクトのインターフェース
       6              : type Filter interface {
       7              :         // Apply はソース画像にエフェクトを適用して描画先に出力する
       8              :         Apply(dst, src *ebiten.Image)
       9              : }
      10              : 
      11              : // Pipeline は Filter を適用順に並べたポスト処理チェーン。src へ各 Filter を順にかけて dst へ出す。
      12              : // 中間結果は内部の ping-pong バッファに置く。エフェクトを増やすときは NewPipeline に足すだけでよい。
      13              : type Pipeline struct {
      14              :         filters []Filter
      15              :         scratch [2]*ebiten.Image // 多段適用の中間バッファ。dst と同じサイズで使い回す
      16              :         lastW   int
      17              :         lastH   int
      18              : }
      19              : 
      20              : // NewPipeline は Filter を適用順に受け取ってチェーンを作る。Filter が無ければ素通しになる。
      21              : func NewPipeline(filters ...Filter) *Pipeline {
      22            1 :         return &Pipeline{filters: filters}
      23            1 : }
      24              : 
      25              : // Apply は src へ Filter を順にかけて dst へ出力する。有効な Filter が無ければ src をそのまま dst へ
      26              : // 写す。nilレシーバや nil の src では何もしない。
      27              : func (p *Pipeline) Apply(dst, src *ebiten.Image) {
      28            1 :         if p == nil || dst == nil || src == nil {
      29            1 :                 return
      30            1 :         }
      31              : 
      32            1 :         active := p.activeFilters()
      33            1 :         if len(active) == 0 {
      34            1 :                 dst.DrawImage(src, nil)
      35            1 :                 return
      36            1 :         }
      37            1 :         if len(active) == 1 {
      38              :                 // 1枚だけなら中間バッファは要らない。dst へ直接かける。
      39            1 :                 active[0].Apply(dst, src)
      40            1 :                 return
      41            1 :         }
      42              : 
      43            1 :         b := dst.Bounds()
      44            1 :         p.ensureScratch(b.Dx(), b.Dy())
      45            1 :         cur := src
      46            1 :         for i, f := range active {
      47            1 :                 if i == len(active)-1 {
      48            1 :                         f.Apply(dst, cur)
      49            1 :                         break
      50              :                 }
      51            1 :                 out := p.scratch[i%2]
      52            1 :                 out.Clear()
      53            1 :                 f.Apply(out, cur)
      54            1 :                 cur = out
      55              :         }
      56              : }
      57              : 
      58              : // activeFilters は nil を除いた有効な Filter だけを返す。
      59              : func (p *Pipeline) activeFilters() []Filter {
      60            1 :         fs := make([]Filter, 0, len(p.filters))
      61            1 :         for _, f := range p.filters {
      62            1 :                 if f != nil {
      63            1 :                         fs = append(fs, f)
      64            1 :                 }
      65              :         }
      66              : 
      67            1 :         return fs
      68              : }
      69              : 
      70              : // ensureScratch は中間バッファを dst と同じサイズで用意する。サイズが変わったら作り直す。
      71              : func (p *Pipeline) ensureScratch(width, height int) {
      72            1 :         if p.scratch[0] != nil && p.lastW == width && p.lastH == height {
      73            1 :                 return
      74            1 :         }
      75            1 :         p.scratch[0] = ebiten.NewImage(width, height)
      76            1 :         p.scratch[1] = ebiten.NewImage(width, height)
      77            1 :         p.lastW = width
      78            1 :         p.lastH = height
      79              : }
        

Generated by: LCOV version 2.0-1