summaryrefslogtreecommitdiff
path: root/demo/first_person/common.lua
blob: 7ddd487272094310184a0bc09f04194570010cd2 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
honey.run = function()
	local gl = honey.gl
	local window = honey.window

	-- initialize opengl
	gl.Init()
	window.setHint(window.hintType.contextVersionMajor, 3)
	window.setHint(window.hintType.contextVersionMinor, 3)
	
	local win = window.create(640, 480, 'first person demo')
	honey.window.win = win
	window.makeContextCurrent(win)
	gl.InitGlad()
	gl.Enable(gl.DEPTH_TEST)

	if honey.init then
		honey.init()
	end

	window.setFramebufferSizeCallback(win, function(_, width, height)
		if honey.windowSizeCallback then
			honey.windowSizeCallback(width, height)
		end
	end)

	local time = 0
	drawTime = 1/60
	while not window.shouldClose(win) do
		local t = window.getTime()
		local dt = t-time
		time = t

		honey.update(dt)
		window.pollEvents()

		if time > drawTime then
			if honey.clearColor then
				gl.ClearColor(
					honey.clearColor.r,
					honey.clearColor.g,
					honey.clearColor.b,
					1.0
				)
			else
				gl.ClearColor(0.2, 0.3, 0.3, 1.0)
			end
			gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT)
			honey.draw()
			window.swapBuffers(win)
			drawTime = drawTime + 1/60
		end
	end

	window.destroy(win)
	gl.Terminate()
end


local set = honey.glm.vec3_set
local get = honey.glm.vec3_get
function honey.glm.setVector(vec, x, y, z)
	set(vec, 0, x)
	set(vec, 1, y)
	set(vec, 2, z)
end


function honey.glm.makeVector(x, y, z)
	local v = honey.glm.vec3()
	honey.glm.setVector(v, x, y, z)
	return v
end


function honey.glm.vec3_tostring(v)
	return string.format(
		'[%0.2f, %0.2f, %0.2f]',
		get(v, 0), get(v, 1), get(v, 2)
	)
end