Skip to content

Commit

Permalink
Merge pull request #30 from ut112002/sizefix
Browse files Browse the repository at this point in the history
Preserve aspect ratio and make size changeable
  • Loading branch information
arhik authored Mar 17, 2024
2 parents e751a2e + a83eac6 commit 7f35261
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
26 changes: 18 additions & 8 deletions examples/uiCircleCaptureExample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ using Debugger

WGPUCore.SetLogLevel(WGPUCore.WGPULogLevel_Off)

scene = Scene();
scene = Scene((750, 750));
renderer = getRenderer(scene);

circle = defaultUICircle(;nSectors=20, radius=0.2, color = [0.4, 0.3, 0.5, 0.6]);
circle2 = defaultUICircle(;nSectors=100, radius=rand(0.1:0.01:0.8), color = rand(4));

addObject!(renderer, circle, scene.camera);
addObject!(renderer, circle2, scene.camera);

# quad.uniformData = Matrix{Float32}(I, (4, 4))

mouseState = defaultMouseState()
Expand All @@ -29,18 +28,29 @@ attachEventSystem(renderer, mouseState, keyboardState)

function runApp(renderer)
init(renderer)
#render(renderer)
object = mouseState.leftClick ? renderer.scene.objects[2] : renderer.scene.objects[1]
WGPUgfx.render(renderer.renderPass, renderer.renderPassOptions, object, renderer.scene.cameraId)
render(renderer)
deinit(renderer)
end

main = () -> begin
try
flag = false
pos = [0.0,0.0]
speed = 0.05
while !WindowShouldClose(scene.canvas.windowRef[])
runApp(renderer)
WaitEvents()
if keyboardState.action in (GLFW.PRESS, GLFW.REPEAT)
if keyboardState.key == GLFW.KEY_RIGHT
pos[1]+=speed
elseif keyboardState.key == GLFW.KEY_LEFT
pos[1]-=speed
elseif keyboardState.key == GLFW.KEY_UP
pos[2]+=speed
elseif keyboardState.key == GLFW.KEY_DOWN
pos[2]-=speed
end
renderer.scene.objects[1].uniformData = translate([pos[1], pos[2] ,0.0]).linear
end
PollEvents()
end
finally
WGPUCore.destroyWindow(scene.canvas)
Expand Down
11 changes: 11 additions & 0 deletions src/events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ function attachKeyCallback(scene, camera::Camera, mouseState::MouseState, keyboa
)
end

function attachWindowSizeCallback(scene, camera)
callback = (_, w, h) -> begin
camera.aspectRatio *= w/scene.canvas.size[1]
camera.aspectRatio *= scene.canvas.size[2]/h
scene.canvas.size = (w,h)
WGPUCore.determineSize(scene.canvas.context)
end
GLFW.SetWindowSizeCallback(scene.canvas.windowRef[], callback)
end

function detachMouseButtonCallback(scene, camera)
WGPUCanvas.setMouseButtonCallback(scene.canvas, nothing)
end
Expand All @@ -150,6 +160,7 @@ function attachEventSystem(renderer, mouseState = defaultMouseState(), keyboardS
attachScrollCallback(scene, camera, mouseState)
attachCursorPosCallback(scene, camera, mouseState)
attachKeyCallback(scene, camera, mouseState, keyboardState)
attachWindowSizeCallback(scene, camera)
end

function detachEventSystem(renderer)
Expand Down
4 changes: 2 additions & 2 deletions src/scene.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ mutable struct Scene
cameraId::Int # TODO cameras
light # TODO lights
objects # ::Union{WorldObject, ObjectGroup}
function Scene()
canvas = WGPUCore.getCanvas(:GLFW)
function Scene(size::Tuple{Int, Int} = (500,500))
canvas = WGPUCore.getCanvas(:GLFW, size)
gpuDevice = WGPUCore.getDefaultDevice(canvas);
camera = defaultCamera()
light = defaultLighting()
Expand Down

0 comments on commit 7f35261

Please sign in to comment.