Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
add tests for fraction pixel rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
elgopher committed Jul 26, 2020
1 parent 8694976 commit b669bf1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
3 changes: 2 additions & 1 deletion glfw/glfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (g *OpenGL) OpenFullScreenWindow(mode VideoMode, options ...WindowOption) (
if err != nil {
return nil, err
}
window.Resize(mode.width, mode.height, window.zoom)
window.Resize(mode.width/window.zoom, mode.height/window.zoom, window.zoom)
g.mainThreadLoop.Execute(func() {
window.fullScreenMode = &mode
// monitor can be set only after window is shown
Expand Down Expand Up @@ -288,6 +288,7 @@ func Resizable(resizable bool) WindowOption {
}
}

// NoAutoIconifyHint is Window hint which does not iconify full screen windows on focus loss
func NoAutoIconifyHint() WindowOption {
return func(window *Window) {
window.setBoolAttrib(glfw.AutoIconify, false)
Expand Down
4 changes: 2 additions & 2 deletions glfw/glfw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func TestOpenGL_OpenWindow(t *testing.T) {
var (
displays, _ = glfw.Displays(mainThreadLoop)
display, _ = displays.Primary()
videoMode = display.VideoModes()[0]
videoMode = display.VideoModes()[0] // TODO avoid using 1:1, 2:1, 1:2 ratios
zoom = videoMode.Height() / 4
expectedWidth = int(math.Ceil(float64(videoMode.Width()) / float64(zoom)))
expectedHeight = 4
Expand All @@ -439,7 +439,7 @@ func TestOpenGL_OpenWindow(t *testing.T) {
var (
displays, _ = glfw.Displays(mainThreadLoop)
display, _ = displays.Primary()
videoMode = display.VideoModes()[0]
videoMode = display.VideoModes()[0] // TODO avoid using 1:1, 2:1, 1:2 ratios
zoom = videoMode.Width() / 4
expectedWidth = 4
expectedHeight = int(math.Ceil(float64(videoMode.Height()) / float64(zoom)))
Expand Down
33 changes: 32 additions & 1 deletion glfw/window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,37 @@ func TestWindow_DrawIntoBackBuffer(t *testing.T) {
})
})

t.Run("should draw perfect pixels when window size is not a zoom multiplication", func(t *testing.T) {
displays, _ := glfw.Displays(mainThreadLoop)
display, _ := displays.Primary()
videoMode := display.VideoModes()[0] // TODO avoid using 1:1, 2:1, 1:2 ratios
zoom := videoMode.Height() / 2
openGL, _ := glfw.NewOpenGL(mainThreadLoop)
defer openGL.Destroy()
window, err := openGL.OpenFullScreenWindow(videoMode, glfw.Zoom(zoom))
require.NoError(t, err)
defer window.Close()
c00 := image.RGBA(10, 20, 30, 40)
c10 := image.RGBA(50, 60, 70, 80)
c01 := image.RGBA(90, 100, 110, 120)
c11 := image.RGBA(130, 140, 150, 160)
window.Screen().SetColor(0, 0, c00)
window.Screen().SetColor(1, 0, c10)
window.Screen().SetColor(0, 1, c01)
window.Screen().SetColor(1, 1, c11)
// when
window.DrawIntoBackBuffer() // TODO This does not work because size was not yet updated
// then
fb := framebufferPixels(window.ContextAPI(), 0, 0, 1, 1)
assert.Equal(t, c00, fb[0])
fb = framebufferPixels(window.ContextAPI(), int32(videoMode.Width()/2), 0, 1, 1)
assert.Equal(t, c10, fb[0])
fb = framebufferPixels(window.ContextAPI(), 0, int32(videoMode.Height()/2), 1, 1)
assert.Equal(t, c01, fb[0])
fb = framebufferPixels(window.ContextAPI(), int32(videoMode.Width()/2), int32(videoMode.Height()/2), 1, 1)
assert.Equal(t, c11, fb[0])
})

}

func TestWindow_Draw(t *testing.T) {
Expand Down Expand Up @@ -323,7 +354,7 @@ func windowOfColor(openGL *glfw.OpenGL, color image.Color) (*glfw.Window, error)
}

func framebufferPixels(context gl2.API, x, y, width, height int32) []image.Color {
size := (height - y) * (width - x)
size := height * width
frameBuffer := make([]image.Color, size)
context.ReadPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(frameBuffer))
return frameBuffer
Expand Down

0 comments on commit b669bf1

Please sign in to comment.