-
Notifications
You must be signed in to change notification settings - Fork 8
/
glfw_app.cr
75 lines (57 loc) · 1.81 KB
/
glfw_app.cr
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
require "glfw"
require "glew"
require "gl"
abstract class GlfwApp
def initialize(@width = 1024, @height = 768)
unless GLFW.init
raise "Failed to initialize GLFW"
end
GLFW.window_hint GLFW::SAMPLES, 4
GLFW.window_hint GLFW::CONTEXT_VERSION_MAJOR, 3
GLFW.window_hint GLFW::CONTEXT_VERSION_MINOR, 3
GLFW.window_hint GLFW::OPENGL_FORWARD_COMPAT, 1
GLFW.window_hint GLFW::OPENGL_PROFILE, GLFW::OPENGL_CORE_PROFILE
@window = GLFW.create_window @width, @height, "Crystal OpenGL", nil, nil
raise "Failed to open GLFW window" if @window.null?
GLFW.set_current_context @window
GLEW.experimental = LibGL::TRUE
unless GLEW.init == GLEW::OK
raise "Failed to initialize GLEW"
end
check_error "after GLEW initialization (ignore on OSX)"
GLFW.set_input_mode @window, GLFW::STICKY_KEYS, 1
GLFW.set_input_mode @window, GLFW::CURSOR, GLFW::CURSOR_DISABLED
puts "OpenGL version: " + GL.version
puts "OpenGL extensions: " + GL.extensions.join(", ")
end
def run
frames = 0
start = last_time = GLFW.get_time
while true
GLFW.poll_events
if GLFW.get_key(@window, GLFW::KEY_ESCAPE) == GLFW::PRESS &&
GLFW.window_should_close(@window)
break
end
current_time = GLFW.get_time
delta_time = current_time - last_time
process_inputs delta_time
render_frame delta_time
frames += 1
last_time = current_time
# Swap buffers and do the GLFW events bookkeeping
GLFW.swap_buffers @window
end
delta_t = GLFW.get_time - start
puts "#{frames} in #{delta_t} secs"
puts "FPS: #{frames / delta_t}"
terminate
end
def terminate
cleanup
GLFW.terminate
end
abstract def process_inputs(delta_time)
abstract def render_frame(delta_time)
abstract def cleanup
end