Skip to content

Commit

Permalink
Merge pull request #273 from jstine35/testscale
Browse files Browse the repository at this point in the history
update transform test to use static images (no animation)
  • Loading branch information
RobLoach authored Jan 28, 2025
2 parents 76d013e + 772012f commit e077129
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 60 deletions.
59 changes: 59 additions & 0 deletions test/graphics/base_transform.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

local tiles = {}
local quads = {}

function load_transform_tiles()
local tile_sizes = { 32, 64, 128, 256 }
for _, tsz in ipairs(tile_sizes) do
if tiles[tsz] == nil then
tiles[tsz] = lutro.graphics.newImage(("graphics/grid-transform-%dpx.png"):format(tsz))
quads[tsz] = lutro.graphics.newQuad(0, 0, tsz, tsz, tsz, tsz)
end
end
end

function draw_transform_tiles(params)
lutro.graphics.setColor(255, 255, 255)

local scalex = params.scalex or 1
local scaley = params.scaley or 1

-- draw multiple fixed scales of the quad onscreen at once.
-- avoid use of animation since it complicates automated verification
-- display multiple screens of tiled tests instead.
-- Include scales that converge toward zero to ensure safety of maths (DivZero, etc)
-- use the first tile of the 2nd column to display scale=0 to ensure it's handled correctly (DivZero)

local xpos = 10
local ypos = 40

local xscales = {
1.00, 1.00, 1.00, 1.00, 1.00,
0.00, 0.70, 0.50, 0.30, 0.001
}

local yscales = {
1.00, 0.70, 0.50, 0.30, 0.001,
0.00, 1.00, 1.00, 1.00, 1.00,
}

local tsz = 128
local adv_y = tsz + 4

for i = 1, 10 do
if i == 6 then
xpos = 10
ypos = ypos + adv_y + 15
end
local orig = tsz / 2
lutro.graphics.draw(
tiles[tsz], quads[tsz],
xpos + orig, ypos + orig,
rotation,
xscales[i] * scalex, yscales[i] * scaley,
orig, orig
)
lutro.graphics.print(("%.1f x %.1f"):format(xscales[i] * scalex, yscales[i] * scaley), xpos + 32, ypos - 15)
xpos = xpos + tsz + 5
end
end
Binary file added test/graphics/grid-transform-128px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/graphics/grid-transform-256px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/graphics/grid-transform-32px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/graphics/grid-transform-64px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 17 additions & 60 deletions test/graphics/scale.lua
Original file line number Diff line number Diff line change
@@ -1,74 +1,31 @@
local t, rotation, scale_x, scale_y, org_x, org_y, src_x, src_w = 0, 0, 0, 0, 0, 0, 0, 0
local draw_line = false
local line_segments = {}
local img = nil

require("graphics/base_transform")

local rotation = 0
local xflip = 1
local yflip = 1
local time = 0

return {
intervalTime = 5,
intervalTime = 12,

load = function()
img = lutro.graphics.newImage("graphics/font.png")
line_segments[1] = {x=104, y=108} -- 100 + centred on quad
load_transform_tiles()
end,

draw = function()
lutro.graphics.setColor(255, 255, 255)

-- track scaling-offset interaction using a line
if draw_line then
lutro.graphics.print("Image should follow the line:", 40, 80)
line_segments[#line_segments + 1] = {x=104 - scale_x * org_x, y=108 - scale_y * org_y}
for idx, coord in ipairs(line_segments) do
if idx > 1 then
local prevcoord = line_segments[idx - 1]
lutro.graphics.line(prevcoord.x, prevcoord.y, coord.x, coord.y)
end
end
end

lutro.graphics.draw(img,
lutro.graphics.newQuad( src_x, 0, src_w, 16, img:getWidth(), img:getHeight()),
100, 100,
rotation,
scale_x,
scale_y,
org_x,
org_y
)
draw_transform_tiles{ scalex = xflip, scaley = yflip }
end,

update = function(dt)
-------------------------------------------
-- Set parameters based on current time. --
-------------------------------------------
t = t + dt

-- rotation not supported, so we leave it as zero.
rotation = 0

scale_x = 1 + math.sin(t * 3) * 4
scale_y = math.cos(t * 2) * 2
time = time + dt

-- test zero scaling (should be blank)
if t < 0.2 then
scale_x = 0
scale_y = 0
end

-- image offset while scaled
if t > 2.5 then
-- set xscale, yscale to something arbitrary
-- then ensure origin is offset correctly by scale
scale_x = 1.1 + math.pow(t - 2.5, 2)
scale_y = 1.1 + math.pow(t - 2.5, 2)
org_x = -(t - 2.5) * 20
org_y = -10 - (math.sin((t - 2.5) * 3) * 10)
-- (origin should follow the line being drawn)
draw_line = true
end
local lutx = { 1, 1, -1, -1 }
local luty = { 1, -1, 1, -1 }

-- src position
src_x = 9 * math.floor(t + 1)
src_w = 8
local ix = math.min(math.floor(time / 3) + 1, 4)
local iy = math.min(math.floor(time / 3) + 1, 4)
xflip = lutx[ix]
yflip = luty[iy]
end
}

0 comments on commit e077129

Please sign in to comment.