Created
October 1, 2020 01:32
-
-
Save radgeRayden/cd9f99dfc88da27f188196f31c00a6c9 to your computer and use it in GitHub Desktop.
Scopes GLFW/OpenGL example
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
# import C functions and sanitize the scope | |
vvv bind glad | |
do | |
let glad = | |
include | |
options | |
.. "-I" module-dir "/glad/include" | |
"glad/src/glad.c" | |
do | |
using glad.extern | |
using glad.typedef | |
using glad.define | |
locals; | |
let gl = | |
fold (scope = (Scope)) for k v in glad | |
let name = ((k as Symbol) as string) | |
if ('match? "^(gl[A-Z])" name ) | |
let new-name = (rslice name (countof "gl")) | |
'bind scope (Symbol new-name) v | |
elseif ('match? "^GL" name) | |
'bind scope k v | |
else scope | |
run-stage; | |
fn init! () | |
let status = (glad.gladLoadGL) | |
if (status == 0) | |
error "failed to initialize openGL" | |
.. gl | |
do | |
let init! | |
locals; |
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
if (operating-system == 'linux) | |
load-library "libglfw.so" | |
# matches glfw or GLFW if there's no `_` afterwards. This keeps CONSTANTS intact. | |
let filter-pattern = "^(glfw|GLFW)(?=[^_])" | |
run-stage; | |
vvv bind glfw | |
do | |
let header = | |
include | |
options "-I./include" | |
"GLFW/glfw3.h" | |
using header.extern filter filter-pattern | |
using header.typedef filter filter-pattern | |
using header.define filter "^GLFW_" | |
locals; | |
# sanitize scope | |
let glfw = | |
fold (scope = glfw) for k v in glfw | |
let name = ((k as Symbol) as string) | |
if ('match? "^glfw" name) | |
let new-name = (rslice name (countof "glfw")) | |
'bind scope (Symbol new-name) v | |
else | |
scope |
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
import .glfw | |
import .gl | |
glfw.SetErrorCallback | |
fn "glfw-raise-error" (error-code error-text) | |
print (string error-text) | |
# handle possible errors gracefully if possible or quit | |
assert false | |
glfw.Init; | |
glfw.WindowHint glfw.GLFW_CLIENT_API glfw.GLFW_OPENGL_API | |
glfw.WindowHint glfw.GLFW_DOUBLEBUFFER true | |
glfw.WindowHint glfw.GLFW_OPENGL_FORWARD_COMPAT true | |
glfw.WindowHint glfw.GLFW_CONTEXT_VERSION_MAJOR 4 | |
glfw.WindowHint glfw.GLFW_CONTEXT_VERSION_MINOR 4 | |
glfw.WindowHint glfw.GLFW_OPENGL_PROFILE glfw.GLFW_OPENGL_CORE_PROFILE | |
let window-handle = | |
glfw.CreateWindow 1280 720 "GLFW/OpenGL Scopes" null null | |
if (window-handle == null) | |
error "Failed to create a window with specified context settings." | |
glfw.MakeContextCurrent window-handle | |
# must be done after creating the context and making it current | |
gl.init!; | |
while (not (glfw.WindowShouldClose window-handle)) | |
glfw.PollEvents; | |
gl.ClearColor 0.14 0.14 0.14 1.0 | |
gl.Clear (gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) | |
glfw.SwapBuffers window-handle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment