Skip to content

Commit

Permalink
Tutorials update for HARFANG v3.2.2.
Browse files Browse the repository at this point in the history
Core update for for HARFANG v3.2.2.
The tutorials can now be stopped using the 'close' button of the render window.
New tutorial showing OGG streaming.
Fixed a missing 'integer divide' in the Lua multiple viewports tutorial.
  • Loading branch information
astrofra committed Jun 3, 2022
1 parent c347fa0 commit a59cffd
Show file tree
Hide file tree
Showing 72 changed files with 726 additions and 422 deletions.
18 changes: 18 additions & 0 deletions audio_stream_ogg_stereo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Stream a mono OGG file with stereo panning

hg = require("harfang")

hg.InputInit()
hg.AudioInit()

src_ref = hg.StreamOGGFileStereo("resources_compiled/sounds/metro_announce.ogg", hg.StereoSourceState(1, hg.SR_Loop)) -- OGG 44.1kHz 16bit mono

angle = 0

while not hg.ReadKeyboard('raw'):Key(hg.K_Escape) do
angle = angle + hg.time_to_sec_f(hg.TickClock()) * 0.5
hg.SetSourcePanning(src_ref, math.sin(angle)) -- panning left = -1, panning right = 1
end

hg.AudioShutdown()
hg.InputShutdown()
18 changes: 18 additions & 0 deletions audio_stream_ogg_stereo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Stream a mono OGG file with stereo panning

import harfang as hg
import math

hg.InputInit()
hg.AudioInit()

src_ref = hg.StreamOGGFileStereo("resources_compiled/sounds/metro_announce.ogg", hg.StereoSourceState(1, hg.SR_Loop)) # OGG 44.1kHz 16bit mono

angle = 0

while not hg.ReadKeyboard('raw').Key(hg.K_Escape):
angle += hg.time_to_sec_f(hg.TickClock()) * 0.5
hg.SetSourcePanning(src_ref, math.sin(angle)) # panning left = -1, panning right = 1

hg.AudioShutdown()
hg.InputShutdown()
2 changes: 1 addition & 1 deletion basic_loop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ hg.WindowSystemInit()
width, height = 1280, 720
window = hg.RenderInit('Harfang - Basic Loop', width, height, hg.RF_VSync)

while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(window) do
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.Color.Green, 1, 0)
hg.SetViewRect(0, 0, 0, width, height)

Expand Down
2 changes: 1 addition & 1 deletion basic_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
width, height = 1280, 720
window = hg.RenderInit('Harfang - Basic Loop', width, height, hg.RF_VSync)

while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(window):
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.Color.Green, 1, 0)
hg.SetViewRect(0, 0, 0, width, height)

Expand Down
2 changes: 1 addition & 1 deletion draw_lines.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ vtx = hg.Vertices(vtx_layout, line_count * 2)
-- main loop
angle = 0

while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.ColorI(64, 64, 64), 1, 0)
hg.SetViewRect(0, 0, 0, res_x, res_y)

Expand Down
2 changes: 1 addition & 1 deletion draw_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# main loop
angle = 0

while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.ColorI(64, 64, 64), 1, 0)
hg.SetViewRect(0, 0, 0, res_x, res_y)

Expand Down
2 changes: 1 addition & 1 deletion draw_lines_starfield.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ for i = 1, max_stars do
end

-- main loop
while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(window) do
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.Color.Black, 1, 0)
hg.SetViewRect(0, 0, 0, width, height)

Expand Down
2 changes: 1 addition & 1 deletion draw_lines_starfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
stars.append(hg.RandomVec3(-starfield_size, starfield_size))

# main loop
while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(window):
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.Color.Black, 1, 0)
hg.SetViewRect(0, 0, 0, width, height)

Expand Down
2 changes: 1 addition & 1 deletion draw_model_no_pipeline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ shader = hg.LoadProgramFromFile('resources_compiled/shaders/mdl')
-- main loop
angle = 0

while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
dt = hg.TickClock()
angle = angle + hg.time_to_sec_f(dt)

Expand Down
2 changes: 1 addition & 1 deletion draw_model_no_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# main loop
angle = 0

while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
dt = hg.TickClock()
angle = angle + hg.time_to_sec_f(dt)

Expand Down
2 changes: 1 addition & 1 deletion draw_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ text_uniform_values = {hg.MakeUniformSetValue('u_color', hg.Vec4(1, 1, 0))}
text_render_state = hg.ComputeRenderState(hg.BM_Alpha, hg.DT_Always, hg.FC_Disabled)

-- main loop
while not hg.ReadKeyboard('default'):Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
hg.SetView2D(0, 0, 0, res_x, res_y, -1, 1, hg.CF_Color | hg.CF_Depth, hg.ColorI(32, 32, 32), 0, 1)

hg.DrawText(0, font, 'Hello world!', font_prg, 'u_tex', 0, hg.Mat4.Identity, hg.Vec3(res_x / 2, res_y / 2, 0), hg.DTHA_Center, hg.DTVA_Center, text_uniform_values, {}, text_render_state)
Expand Down
2 changes: 1 addition & 1 deletion draw_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
text_render_state = hg.ComputeRenderState(hg.BM_Alpha, hg.DT_Always, hg.FC_Disabled)

# main loop
while not hg.ReadKeyboard('default').Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
hg.SetView2D(0, 0, 0, res_x, res_y, -1, 1, hg.CF_Color | hg.CF_Depth, hg.ColorI(32, 32, 32), 0, 1)

hg.DrawText(0, font, 'Hello world!', font_prg, 'u_tex', 0, hg.Mat4.Identity, hg.Vec3(res_x / 2, res_y / 2, 0), hg.DTHA_Center, hg.DTVA_Center, text_uniform_values, [], text_render_state)
Expand Down
2 changes: 1 addition & 1 deletion draw_text_over_models.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ text_render_state = hg.ComputeRenderState(hg.BM_Alpha, hg.DT_Always, hg.FC_Disab
-- main loop
angle = 0

while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
dt = hg.TickClock()
angle = angle + hg.time_to_sec_f(dt)

Expand Down
2 changes: 1 addition & 1 deletion draw_text_over_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# main loop
angle = 0

while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
dt = hg.TickClock()
angle = angle + hg.time_to_sec_f(dt)

Expand Down
2 changes: 1 addition & 1 deletion game_mouse_flight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function update_chase_camera(target_pos)
end

-- game loop
while not keyboard:Down(hg.K_Escape) do
while not keyboard:Down(hg.K_Escape) and hg.IsWindowOpen(win) do
dt = hg.TickClock() -- tick clock, retrieve elapsed clock since last call

-- update mouse/keyboard devices
Expand Down
2 changes: 1 addition & 1 deletion game_mouse_flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def update_chase_camera(target_pos):


# game loop
while not keyboard.Down(hg.K_Escape):
while not keyboard.Down(hg.K_Escape) and hg.IsWindowOpen(win):
dt = hg.TickClock() # tick clock, retrieve elapsed clock since last call

# update mouse/keyboard devices
Expand Down
2 changes: 1 addition & 1 deletion imgui_basic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ imgui_img_prg = hg.LoadProgramFromAssets('core/shader/imgui_image')
hg.ImGuiInit(10, imgui_prg, imgui_img_prg)

-- main loop
while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
hg.ImGuiBeginFrame(res_x, res_y, hg.TickClock(), hg.ReadMouse(), hg.ReadKeyboard())

if hg.ImGuiBegin('Window') then
Expand Down
2 changes: 1 addition & 1 deletion imgui_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
hg.ImGuiInit(10, imgui_prg, imgui_img_prg)

# main loop
while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
hg.ImGuiBeginFrame(res_x, res_y, hg.TickClock(), hg.ReadMouse(), hg.ReadKeyboard())

if hg.ImGuiBegin('Window'):
Expand Down
2 changes: 1 addition & 1 deletion imgui_edit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ imgui_view_clear_color = hg.Color(0, 0, 0)
imgui_clear_color_preset = 0

-- main loop
while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
dt = hg.TickClock()

-- ImGui frame
Expand Down
2 changes: 1 addition & 1 deletion imgui_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
imgui_clear_color_preset = 0

# main loop
while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
dt = hg.TickClock()

# ImGui frame
Expand Down
2 changes: 1 addition & 1 deletion imgui_mouse_capture.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ text_value = 'Clicking into this field will not clear the screen in red.'
mouse = hg.Mouse()
keyboard = hg.Keyboard()

while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
mouse:Update()
keyboard:Update()

Expand Down
2 changes: 1 addition & 1 deletion imgui_mouse_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
mouse = hg.Mouse()
keyboard = hg.Keyboard()

while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
mouse.Update()
keyboard.Update()

Expand Down
2 changes: 1 addition & 1 deletion input_read_gamepad.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

gamepad = hg.Gamepad()

while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
gamepad.Update()

if gamepad.Connected():
Expand Down
2 changes: 1 addition & 1 deletion material_update_value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mat_update_delay = 0
texture_ref = hg.LoadTextureFromAssets('textures/squares.png', 0, res)

-- main loop
while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
dt = hg.TickClock()

mat_update_delay = mat_update_delay - dt
Expand Down
2 changes: 1 addition & 1 deletion material_update_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
texture_ref = hg.LoadTextureFromAssets('textures/squares.png', 0, res)

# main loop
while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
dt = hg.TickClock()

mat_update_delay = mat_update_delay - dt
Expand Down
2 changes: 1 addition & 1 deletion physics_impulse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ keyboard = hg.Keyboard()

use_force = true

while not keyboard:Down(hg.K_Escape) do
while not keyboard:Down(hg.K_Escape) and hg.IsWindowOpen(win) do
keyboard:Update()

dt = hg.TickClock()
Expand Down
2 changes: 1 addition & 1 deletion physics_impulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

use_force = True

while not keyboard.Down(hg.K_Escape):
while not keyboard.Down(hg.K_Escape) and hg.IsWindowOpen(win):
keyboard.Update()

dt = hg.TickClock()
Expand Down
2 changes: 1 addition & 1 deletion physics_kapla.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ physics:SceneCreatePhysicsFromAssets(scene)
physics_step = hg.time_from_sec_f(1 / 60)

-- main loop
while not keyboard:Down(hg.K_Escape) do
while not keyboard:Down(hg.K_Escape) and hg.IsWindowOpen(win) do
keyboard:Update()
mouse:Update()

Expand Down
2 changes: 1 addition & 1 deletion physics_kapla.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def fill_ring(r, ring_y, size, r_adjust, y_off):
physics_step = hg.time_from_sec_f(1 / 60)

# main loop
while not keyboard.Down(hg.K_Escape):
while not keyboard.Down(hg.K_Escape) and hg.IsWindowOpen(win):
keyboard.Update()
mouse.Update()

Expand Down
2 changes: 1 addition & 1 deletion physics_manual_setup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ physics = hg.SceneBullet3Physics()
physics:SceneCreatePhysicsFromAssets(scene)

-- main loop
while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
dt = hg.TickClock()

hg.SceneUpdateSystems(scene, clocks, dt, physics, hg.time_from_sec_f(1 / 60), 1)
Expand Down
2 changes: 1 addition & 1 deletion physics_manual_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
physics.SceneCreatePhysicsFromAssets(scene)

# main loop
while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
dt = hg.TickClock()

hg.SceneUpdateSystems(scene, clocks, dt, physics, hg.time_from_sec_f(1 / 60), 1)
Expand Down
2 changes: 1 addition & 1 deletion physics_overrides_matrix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ physics:SceneCreatePhysicsFromAssets(scene)
-- main loop
mouse, keyboard = hg.Mouse(), hg.Keyboard()

while not keyboard:Pressed(hg.K_Escape) do
while not keyboard:Pressed(hg.K_Escape) and hg.IsWindowOpen(win) do
keyboard:Update()
mouse:Update()

Expand Down
2 changes: 1 addition & 1 deletion physics_overrides_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
# main loop
mouse, keyboard = hg.Mouse(), hg.Keyboard()

while not keyboard.Pressed(hg.K_Escape):
while not keyboard.Pressed(hg.K_Escape) and hg.IsWindowOpen(win):
keyboard.Update()
mouse.Update()

Expand Down
2 changes: 1 addition & 1 deletion physics_pool_of_objects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ text_uniform_values = {hg.MakeUniformSetValue('u_color', hg.Vec4(1, 1, 0.5))}
text_render_state = hg.ComputeRenderState(hg.BM_Alpha, hg.DT_Always, hg.FC_Disabled)

-- main loop
while true do
while hg.IsWindowOpen(win) do
state = hg.ReadKeyboard()

if state:Key(hg.K_S) then
Expand Down
2 changes: 1 addition & 1 deletion physics_pool_of_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def create_material(diffuse, specular, self):
text_render_state = hg.ComputeRenderState(hg.BM_Alpha, hg.DT_Always, hg.FC_Disabled)

# main loop
while True:
while hg.IsWindowOpen(win):
state = hg.ReadKeyboard()

if state.Key(hg.K_S):
Expand Down
2 changes: 1 addition & 1 deletion render_resize_to_window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cube_mdl = hg.CreateCubeModel(vtx_layout, 1, 1, 1)
cube_prg = hg.LoadProgramFromFile('resources_compiled/shaders/mdl')

-- main loop
while not hg.ReadKeyboard():Key(hg.K_Escape) do
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
render_was_reset, res_x, res_y = hg.RenderResetToWindow(win, res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X | hg.RF_MaxAnisotropy)
if render_was_reset then
print(string.format('Render reset to %dx%d', res_x, res_y))
Expand Down
2 changes: 1 addition & 1 deletion render_resize_to_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
cube_prg = hg.LoadProgramFromFile('resources_compiled/shaders/mdl')

# main loop
while not hg.ReadKeyboard().Key(hg.K_Escape):
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
render_was_reset, res_x, res_y = hg.RenderResetToWindow(win, res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X | hg.RF_MaxAnisotropy)
if render_was_reset:
print('Render reset to %dx%d' % (res_x, res_y))
Expand Down
Loading

0 comments on commit a59cffd

Please sign in to comment.