summaryrefslogtreecommitdiff
path: root/examples/custom_main_loop/main.lua
blob: 952496c3e5025dfd936e049b5c31d88650ea6a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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