summaryrefslogtreecommitdiff
path: root/examples/custom_main_loop/main.lua
diff options
context:
space:
mode:
authorjussi2024-11-22 14:57:31 +0200
committerjussi2024-11-22 14:57:31 +0200
commitcb2b0e4dff5e1d98054f9e5f809fd3a14cd220a1 (patch)
treee64efb0dedce13c03011186de34c72a06cad1757 /examples/custom_main_loop/main.lua
parentc9ebe23d6282e96b410dc7687e0be1c4f3ba1b4d (diff)
downloadreilua-enhanced-cb2b0e4dff5e1d98054f9e5f809fd3a14cd220a1.tar.gz
reilua-enhanced-cb2b0e4dff5e1d98054f9e5f809fd3a14cd220a1.tar.bz2
reilua-enhanced-cb2b0e4dff5e1d98054f9e5f809fd3a14cd220a1.zip
WindowShouldClose and custom main loop example.
Diffstat (limited to 'examples/custom_main_loop/main.lua')
-rw-r--r--examples/custom_main_loop/main.lua52
1 files changed, 52 insertions, 0 deletions
diff --git a/examples/custom_main_loop/main.lua b/examples/custom_main_loop/main.lua
new file mode 100644
index 0000000..952496c
--- /dev/null
+++ b/examples/custom_main_loop/main.lua
@@ -0,0 +1,52 @@
+package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
+
+Vector2 = require( "vector2" )
+
+local function dysfunctionalPrint( message )
+ printt( message )
+end
+
+local function main()
+ RL.SetConfigFlags( RL.FLAG_VSYNC_HINT )
+ RL.InitWindow( { 800, 600 }, "Custom Main Loop" )
+
+ local catTex = RL.LoadTexture( RL.GetBasePath().."../resources/images/cat.png" )
+ local catPos = Vector2:new( 20, 20 )
+ local catSpeed = 200
+
+ while not RL.WindowShouldClose() do
+ local delta = RL.GetFrameTime()
+ local vel = Vector2:new()
+
+ if RL.IsKeyDown( RL.KEY_RIGHT ) then
+ vel.x = catSpeed * delta
+ elseif RL.IsKeyDown( RL.KEY_LEFT ) then
+ vel.x = -catSpeed * delta
+ end
+ if RL.IsKeyDown( RL.KEY_DOWN ) then
+ vel.y = catSpeed * delta
+ elseif RL.IsKeyDown( RL.KEY_UP ) then
+ vel.y = -catSpeed * delta
+ end
+
+ if RL.IsKeyPressed( RL.KEY_ENTER ) then
+ dysfunctionalPrint( "This will fail and give usefull traceback info" )
+ end
+
+ catPos:addEq( vel )
+
+ RL.BeginDrawing()
+ RL.ClearBackground( RL.LIGHTGRAY )
+ RL.DrawTexture( catTex, catPos, RL.WHITE )
+ RL.DrawFPS( { 10, 10 } )
+ RL.EndDrawing()
+ end
+end
+
+-- Call main to get traceback info.
+local success, result = xpcall( main, debug.traceback )
+
+if not success then
+ RL.TraceLog( RL.LOG_WARNING, "Error: "..result )
+ RL.CloseWindow()
+end