
local elapsed_time = gh_utils.get_elapsed_time()
local dt = gh_utils.get_time_step()

frames = frames+1
fps_time = fps_time + dt
if (fps_time >= 1.0) then
  fps_time = 0
  fps = frames
  frames = 0
end  



local mx, my = gh_input.mouse_get_position()
local mouse_x_2d = mx - winW/2
local mouse_y_2d = winH/2 - my



gh_camera.bind(camera_ortho)


gh_renderer.set_depth_test_state(0)

gh_renderer.clear_color_depth_buffers(bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a, 1.0)




if (wave_type == 1) then
  update_curve_v1(curve, elapsed_time)
elseif (wave_type == 2) then
  update_curve_v2(curve, elapsed_time)
end

gh_gpu_program.bind(color_prog)
draw_curve(curve, color_prog)













---[[

  imgui_frame_begin()

  gh_imgui.set_color(IMGUI_WINDOW_BG_COLOR, 0.1, 0.1, 0.1, 0.0)

  local is_open = imgui_window_begin_pos_size_always("Wave - Control panel", winW, winH, 0, 0)
  if (is_open == 1) then
  
    win_hovered = gh_imgui.is_window_hovered()
  
    local window_w = gh_imgui.get_content_region_available_width()

    local widget_width = window_w * 1.0

    gh_imgui.push_item_width(200)

    
    gh_imgui.text("Press [ESC] to quit the demo")
    
    imgui_vertical_space_v2(4)

    gh_imgui.text(string.format("Mouse screen space: <%d ; %d>", mx, my))
    gh_imgui.text(string.format("Mouse 2D space: <%.0f ; %.0f>", mouse_x_2d, mouse_y_2d))
    gh_imgui.text(string.format("Window size: <%d ; %d>", winW, winH))

    imgui_vertical_space_v2(4)


    gh_imgui.text("Background color")
    bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a = gh_imgui.color_edit_rgba("##bgcolor", bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a)

    imgui_vertical_space_v2(2)

    gh_imgui.text("Curve color")
    curve.color.r, curve.color.g, curve.color.b, curve.color.a = gh_imgui.color_edit_rgba("##curve-color", curve.color.r, curve.color.g, curve.color.b, curve.color.a)

    imgui_vertical_space_v2(4)

    
    local power = 1.0
    
    gh_imgui.text("Curve amplitude")
    min_value = 1.0
    max_value = 100.0
    curve.amplitude = gh_imgui.slider_1f("##curve-amplitude", curve.amplitude,   min_value, max_value,   power)

    gh_imgui.text("Curve frequency")
    min_value = 1.0
    max_value = 100.0
    curve.frequency = gh_imgui.slider_1f("##curve-frequency", curve.frequency,   min_value, max_value,   power)


    gh_imgui.text("Curve x size")
    min_value = 10.0
    max_value = winW
    curve.x_size = gh_imgui.slider_1f("##curve-x_size", curve.x_size,   min_value, max_value,   power)



    imgui_vertical_space_v2(6)



    if (gh_imgui.button("wave 1", 100, 20) == 1) then
      wave_type = 1
    end

    gh_imgui.same_line(0.0, 10.0)

    if (gh_imgui.button("wave 2", 100, 20) == 1) then
      wave_type = 2
    end
    

    imgui_vertical_space_v2(4)

    if (gh_imgui.button("reset params##resetparams", 100, 20) == 1) then
      reset_curve_params(curve)
    end


    if (wave_type == 2) then

      imgui_vertical_space_v2(2)

      gh_imgui.group_begin()
      gh_imgui.text("H1 params:")
      min_value = 0.0
      max_value = 10.0
      curve.f1 = gh_imgui.slider_1f("freq##curve-f1", curve.f1,   min_value, max_value,   power)
      curve.a1 = gh_imgui.slider_1f("amp##curve-a1", curve.a1,   min_value, max_value,   power)
      min_value = 0.1
      max_value = 10.0
      curve.t1 = gh_imgui.slider_1f("time##curve-t1", curve.t1,   min_value, max_value,   power)
      gh_imgui.group_end()

      gh_imgui.same_line(0.0, 30.0)

      gh_imgui.group_begin()
      gh_imgui.text("H2 params:")
      min_value = 0.0
      max_value = 10.0
      curve.f2 = gh_imgui.slider_1f("freq##curve-f2", curve.f2,   min_value, max_value,   power)
      curve.a2 = gh_imgui.slider_1f("amp##curve-a2", curve.a2,   min_value, max_value,   power)
      min_value = 0.1
      max_value = 10.0
      curve.t2 = gh_imgui.slider_1f("time##curve-t2", curve.t2,   min_value, max_value,   power)
      gh_imgui.group_end()

      gh_imgui.group_begin()
      gh_imgui.text("H3 params:")
      min_value = 0.0
      max_value = 10.0
      curve.f3 = gh_imgui.slider_1f("freq##curve-f3", curve.f3,   min_value, max_value,   power)
      curve.a3 = gh_imgui.slider_1f("amp##curve-a3", curve.a3,   min_value, max_value,   power)
      min_value = 0.1
      max_value = 10.0
      curve.t3 = gh_imgui.slider_1f("time##curve-t3", curve.t3,   min_value, max_value,   power)
      gh_imgui.group_end()

      gh_imgui.same_line(0.0, 30.0)

      gh_imgui.group_begin()
      gh_imgui.text("H4 params:")
      min_value = 0.0
      max_value = 10.0
      curve.f4 = gh_imgui.slider_1f("freq##curve-f4", curve.f4,   min_value, max_value,   power)
      curve.a4 = gh_imgui.slider_1f("amp##curve-a4", curve.a4,   min_value, max_value,   power)
      min_value = 0.1
      max_value = 10.0
      curve.t4 = gh_imgui.slider_1f("time##curve-t4", curve.t4,   min_value, max_value,   power)
      gh_imgui.group_end()

    end

    gh_imgui.pop_item_width()

  end 

  imgui_window_end()

  imgui_frame_end()
  --]]