Skip to content

Commit

Permalink
add callback based input handling (#8)
Browse files Browse the repository at this point in the history
* replace process_inputs with key_callback and mouse_button_callback

* minor cleanup

* add cursor position
  • Loading branch information
Sid-Bhatia-0 authored Mar 28, 2022
1 parent 92eb0cd commit 460cc92
Showing 1 changed file with 86 additions and 5 deletions.
91 changes: 86 additions & 5 deletions example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ import SimpleDraw as SD

include("opengl_utils.jl")

function process_input(window)
if GLFW.GetKey(window, GLFW.KEY_Q)
GLFW.SetWindowShouldClose(window, true)
mutable struct Button
ended_down::Bool
half_transition_count::Int
end

mutable struct Cursor
i::Int
j::Int
end

function update_button!(button, action)
if action == GLFW.PRESS
button.ended_down = true
button.half_transition_count += 1
elseif action == GLFW.RELEASE
button.ended_down = false
button.half_transition_count += 1
end

return nothing
Expand Down Expand Up @@ -61,6 +75,58 @@ function start()

clear_display()

key_up = Button(false, 0)
key_down = Button(false, 0)
key_left = Button(false, 0)
key_right = Button(false, 0)

function key_callback(window, key, scancode, action, mods)::Cvoid
if key == GLFW.KEY_Q && action == GLFW.PRESS
GLFW.SetWindowShouldClose(window, true)
elseif key == GLFW.KEY_UP
update_button!(key_up, action)
elseif key == GLFW.KEY_DOWN
update_button!(key_down, action)
elseif key == GLFW.KEY_LEFT
update_button!(key_left, action)
elseif key == GLFW.KEY_RIGHT
update_button!(key_right, action)
end

return nothing
end

GLFW.SetKeyCallback(window, key_callback)

mouse_left = Button(false, 0)
mouse_right = Button(false, 0)
mouse_middle = Button(false, 0)

function mouse_button_callback(window, button, action, mods)::Cvoid
if button == GLFW.MOUSE_BUTTON_LEFT
update_button!(mouse_left, action)
elseif button == GLFW.MOUSE_BUTTON_RIGHT
update_button!(mouse_right, action)
elseif button == GLFW.MOUSE_BUTTON_MIDDLE
update_button!(mouse_middle, action)
end

return nothing
end

GLFW.SetMouseButtonCallback(window, mouse_button_callback)

cursor = Cursor(0.0, 0.0)

function cursor_position_callback(window, x, y)::Cvoid
cursor.i = round(Int, y)
cursor.j = round(Int, x)

return nothing
end

GLFW.SetCursorPosCallback(window, cursor_position_callback)

i = 0

time_stamp_buffer = DS.CircularBuffer{typeof(time_ns())}(sliding_window_size)
Expand All @@ -70,12 +136,18 @@ function start()
push!(drawing_time_buffer, zero(UInt))

while !GLFW.WindowShouldClose(window)
process_input(window)

empty!(lines)
push!(lines, "previous frame number: $(i)")
push!(lines, "average time spent per frame (averaged over previous $(length(time_stamp_buffer)) frames): $(round((last(time_stamp_buffer) - first(time_stamp_buffer)) / (1e6 * length(time_stamp_buffer)), digits = 2)) ms")
push!(lines, "average drawing time spent per frame (averaged over previous $(length(drawing_time_buffer)) frames): $(round(sum(drawing_time_buffer) / (1e6 * length(drawing_time_buffer)), digits = 2)) ms")
push!(lines, "key_up: $(key_up)")
push!(lines, "key_down: $(key_down)")
push!(lines, "key_left: $(key_left)")
push!(lines, "key_right: $(key_right)")
push!(lines, "mouse_left: $(mouse_left)")
push!(lines, "mouse_right: $(mouse_right)")
push!(lines, "mouse_middle: $(mouse_middle)")
push!(lines, "cursor: $(cursor)")

drawing_time_start = time_ns()
SD.draw!(image, SD.Background(), background_color)
Expand All @@ -86,6 +158,15 @@ function start()
update_back_buffer(image)

GLFW.SwapBuffers(window)

key_up.half_transition_count = 0
key_down.half_transition_count = 0
key_left.half_transition_count = 0
key_right.half_transition_count = 0
mouse_left.half_transition_count = 0
mouse_right.half_transition_count = 0
mouse_middle.half_transition_count = 0

GLFW.PollEvents()

i = i + 1
Expand Down

0 comments on commit 460cc92

Please sign in to comment.