2024-01-16 04:48:47 +01:00
|
|
|
// Copyright 2024 The Ebitengine Authors
|
2019-10-11 20:09:43 +02:00
|
|
|
//
|
|
|
|
// 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 buffered_test
|
|
|
|
|
|
|
|
import (
|
2024-01-16 04:48:47 +01:00
|
|
|
"image"
|
2019-10-11 20:09:43 +02:00
|
|
|
"testing"
|
|
|
|
|
2023-10-09 17:31:42 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/buffered"
|
2024-01-16 04:48:47 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2024-09-08 04:42:02 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
|
2024-01-16 04:48:47 +01:00
|
|
|
t "github.com/hajimehoshi/ebiten/v2/internal/testing"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
2019-10-11 20:09:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2024-01-16 04:48:47 +01:00
|
|
|
t.MainWithRunLoop(m)
|
2020-01-10 18:38:03 +01:00
|
|
|
}
|
|
|
|
|
2024-01-16 04:48:47 +01:00
|
|
|
func TestUnsyncedPixels(t *testing.T) {
|
|
|
|
dst := buffered.NewImage(16, 16, atlas.ImageTypeRegular)
|
2020-01-10 18:38:03 +01:00
|
|
|
|
2024-01-16 04:48:47 +01:00
|
|
|
// Add an entry for dotsBuffer at (0, 0).
|
|
|
|
dst.WritePixels([]byte{0xff, 0xff, 0xff, 0xff}, image.Rect(0, 0, 1, 1))
|
2020-01-10 18:38:03 +01:00
|
|
|
|
2024-01-16 04:48:47 +01:00
|
|
|
// Merge the entry into the cached pixels.
|
|
|
|
// The entry for dotsBuffer is now gone in the current implementation.
|
2024-03-26 04:59:31 +01:00
|
|
|
ok, err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), make([]byte, 4*16*16), image.Rect(0, 0, 16, 16))
|
|
|
|
if err != nil {
|
2024-01-16 05:26:31 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-03-26 04:59:31 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("ReadPixels failed")
|
|
|
|
}
|
2020-01-10 18:38:03 +01:00
|
|
|
|
2024-01-16 04:48:47 +01:00
|
|
|
// Call WritePixels with the outside region of (0, 0).
|
|
|
|
dst.WritePixels(make([]byte, 4*2*2), image.Rect(1, 1, 3, 3))
|
2020-01-10 18:38:03 +01:00
|
|
|
|
2024-01-16 04:48:47 +01:00
|
|
|
// Flush unsynced pixel cache.
|
|
|
|
src := buffered.NewImage(16, 16, atlas.ImageTypeRegular)
|
|
|
|
vs := make([]float32, 4*graphics.VertexFloatCount)
|
2024-08-11 19:42:36 +02:00
|
|
|
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, 16, 16, 0, 0, 16, 16, 1, 1, 1, 1)
|
2024-01-16 04:48:47 +01:00
|
|
|
is := graphics.QuadIndices()
|
|
|
|
dr := image.Rect(0, 0, 16, 16)
|
2024-06-09 19:02:47 +02:00
|
|
|
sr := [graphics.ShaderSrcImageCount]image.Rectangle{image.Rect(0, 0, 16, 16)}
|
2024-09-08 04:42:02 +02:00
|
|
|
dst.DrawTriangles([graphics.ShaderSrcImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, sr, atlas.NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintNone)
|
2019-10-11 20:09:43 +02:00
|
|
|
|
2024-01-16 04:48:47 +01:00
|
|
|
// Check the result is correct.
|
|
|
|
var got [4]byte
|
2024-03-26 04:59:31 +01:00
|
|
|
ok, err = dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), got[:], image.Rect(0, 0, 1, 1))
|
|
|
|
if err != nil {
|
2024-01-16 05:26:31 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-03-26 04:59:31 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("ReadPixels failed")
|
|
|
|
}
|
2024-01-16 04:48:47 +01:00
|
|
|
want := [4]byte{0xff, 0xff, 0xff, 0xff}
|
2019-10-11 20:09:43 +02:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|