-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsdl_gl_app.cr
78 lines (57 loc) · 1.8 KB
/
sdl_gl_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
76
77
require "sdl_ext"
require "gl"
require "glew"
require "utils"
class SdlGlApp
def initialize(@width = 1024, @height = 768)
SDL.init
info = SDL.get_video_info
pixel_format = info.value.vfmt
raise "Cannot get video info" if info.nil?
puts "Flags: " + info.value.flags.to_s(2)
puts "Video memory: " + info.value.video_mem.to_s
puts "Palette: " + (pixel_format.value.palette.nil?.to_s ? "NO" : "YES")
puts "BPP: " + pixel_format.value.bits_per_pixel.to_s
puts "Screen dimensions: " + info.value.current_w.to_s + "x" + info.value.current_h.to_s
bpp = pixel_format.value.bits_per_pixel
SDL.gl_set_attribute(LibSDL::GLAttribute::GL_RED_SIZE, 5)
SDL.gl_set_attribute(LibSDL::GLAttribute::GL_GREEN_SIZE, 5)
SDL.gl_set_attribute(LibSDL::GLAttribute::GL_BLUE_SIZE, 5)
SDL.gl_set_attribute(LibSDL::GLAttribute::GL_DEPTH_SIZE, 16)
SDL.gl_set_attribute(LibSDL::GLAttribute::GL_DOUBLEBUFFER, 1)
flags = LibSDL::OPENGL
@window = SDL.set_video_mode(@width, @height, bpp.to_i32, flags)
raise "Cannot set video mode" if @window.nil?
GLEW.experimental = LibGL::TRUE
unless GLEW.init == GLEW::OK
raise "Failed to initialize GLEW"
end
check_error "after GLEW initialization"
puts "OpenGL version: " + GL.version
end
def run
frames = 0
start = SDL.ticks
running = true
while running
SDL.poll_events do |event|
if event.type == LibSDL::QUIT || (event.type == LibSDL::KEYDOWN)
running = false
end
end
render_frame
SDL.gl_swap_buffers
frames += 1
end
ms = SDL.ticks - start
puts "#{frames} in #{ms} ms"
puts "FPS: #{frames / (ms * 0.001)}"
terminate
end
def terminate
cleanup
SDL.quit
end
abstract def render_frame
abstract def cleanup
end