Created
July 7, 2017 06:29
-
-
Save doughsay/6f025fb508b1a7c544e50572a9727636 to your computer and use it in GitHub Desktop.
Crystal OpenGL type safety test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gotta have an opengl context to do the below... | |
require "lib_glfw" | |
LibGLFW.init | |
LibGLFW.window_hint LibGLFW::CONTEXT_VERSION_MAJOR, 3 | |
LibGLFW.window_hint LibGLFW::CONTEXT_VERSION_MINOR, 3 | |
LibGLFW.window_hint LibGLFW::OPENGL_FORWARD_COMPAT, 1 | |
LibGLFW.window_hint LibGLFW::OPENGL_PROFILE, LibGLFW::OPENGL_CORE_PROFILE | |
window = LibGLFW.create_window 800, 600, "foo", nil, nil | |
LibGLFW.set_current_context(window) | |
# "raw" opengl bindings to C | |
@[Link(framework: "OpenGL")] | |
lib LibGLRaw | |
ARRAY_BUFFER = 0x8892_u32 | |
fun gen_buffers = glGenBuffers(n : UInt32, buffers : UInt32*) | |
fun bind_buffer = glBindBuffer(target : UInt32, buffer : UInt32) | |
end | |
# a "better?" opengl lib | |
module LibGL | |
alias SizeI = UInt32 | |
enum BufferBindingTarget : UInt32 | |
ArrayBuffer = LibGLRaw::ARRAY_BUFFER | |
end | |
abstract class Wrapper | |
getter :value | |
def to_unsafe | |
@value | |
end | |
end | |
class UInt32Wrapper < Wrapper | |
def initialize(@value : UInt32); end | |
end | |
class Buffer < UInt32Wrapper; end | |
def self.gen_buffers(n : SizeI) : Array(Buffer) | |
Array(UInt32).build(n) do |buff| | |
LibGLRaw.gen_buffers(n, buff) | |
n | |
end.map { |id| Buffer.new(id) } | |
end | |
def self.bind_buffer(target : BufferBindingTarget, buffer : Buffer) | |
LibGLRaw.bind_buffer(target, buffer) | |
end | |
end | |
# test | |
buffer1, buffer2 = buffers = LibGL.gen_buffers(2_u32) | |
puts buffer1 # => #<LibGL::Buffer:0x106b09fe0> | |
puts buffer2 # => #<LibGL::Buffer:0x106b09fd0> | |
puts typeof(buffers) # => Array(LibGL::Buffer) | |
puts buffers.class # => Array(LibGL::Buffer) | |
puts typeof(buffer1) # => LibGL::Buffer | |
puts buffer1.class # => LibGL::Buffer | |
# better type checking for opengl functions! | |
LibGL.bind_buffer(LibGL::BufferBindingTarget::ArrayBuffer, buffer1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment