Skip to content

Instantly share code, notes, and snippets.

@radgeRayden
Last active October 1, 2020 02:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radgeRayden/e30d729ae7a0214126bdd210db0481df to your computer and use it in GitHub Desktop.
Save radgeRayden/e30d729ae7a0214126bdd210db0481df to your computer and use it in GitHub Desktop.
sdl gl
# 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;
import .gl
import .sdl
sdl.Init
|
sdl.INIT_VIDEO
sdl.INIT_GAMECONTROLLER
sdl.INIT_EVENTS
sdl.INIT_JOYSTICK
sdl.GL_SetAttribute sdl.GL_DOUBLEBUFFER true
sdl.GL_SetAttribute sdl.GL_CONTEXT_MAJOR_VERSION 4
sdl.GL_SetAttribute sdl.GL_CONTEXT_MINOR_VERSION 4
sdl.GL_SetAttribute sdl.GL_CONTEXT_FLAGS
|
sdl.GL_CONTEXT_DEBUG_FLAG
sdl.GL_CONTEXT_FORWARD_COMPATIBLE_FLAG
sdl.GL_SetAttribute sdl.GL_CONTEXT_PROFILE_MASK
sdl.GL_CONTEXT_PROFILE_CORE as u32
global app-window =
sdl.CreateWindow
"Untitled"
sdl.WINDOWPOS_CENTERED
sdl.WINDOWPOS_CENTERED
720
480
sdl.WINDOW_OPENGL
let context = (sdl.GL_CreateContext app-window)
if (context == null)
error "Couldn't create OpenGL context."
let context-change-result = (sdl.GL_MakeCurrent app-window context)
if (context-change-result < 0)
error "Couldn't make OpenGL context current."
gl.init!;
label run
loop ()
local ev : sdl.Event
while (sdl.PollEvent &ev)
switch ev.type
case (sdl.SDL_QUIT as u32) # cast probably unnecessary now but I didn't update yet
merge run
default
;
gl.ClearColor 1.0 0.14 0.14 0.14
gl.Clear gl.GL_COLOR_BUFFER_BIT
sdl.GL_SwapWindow app-window
#
bindings module for the SDL2 library
if (operating-system == 'windows)
load-library (module-dir .. "/runtime/SDL2.dll")
elseif (operating-system == 'linux)
load-library "libSDL2.so"
vvv bind sdl
do
let module =
include
"SDL2/SDL.h"
options
"-D_REENTRANT"
"-I/usr/include/SDL2"
using module.extern
using module.const
using module.define
using module.typedef
locals;
# sanitize scope
let sdl =
fold (scope = sdl) for k v in sdl
let name = ((k as Symbol) as string)
if ('match? "^SDL_" name)
let new-name = (rslice name (countof "SDL_"))
'bind scope (Symbol new-name) v
else
scope
run-stage;
let sdl-macros =
do
inline WINDOWPOS_UNDEFINED_DISPLAY (X)
sdl.WINDOWPOS_UNDEFINED_MASK | X
inline WINDOWPOS_ISUNDEFINED (X)
(X & 0xFFFF0000) == sdl.WINDOWPOS_UNDEFINED_MASK
inline WINDOWPOS_CENTERED_DISPLAY (X)
sdl.WINDOWPOS_CENTERED_MASK | X
inline WINDOWPOS_ISCENTERED (X)
(X & 0xFFFF0000) == sdl.WINDOWPOS_CENTERED_MASK
let WINDOWPOS_CENTERED = (WINDOWPOS_CENTERED_DISPLAY 0)
let WINDOWPOS_UNDEFINED = (WINDOWPOS_UNDEFINED_DISPLAY 0)
inline VERSION (version-struct)
imply version-struct sdl.version
version-struct.major = sdl.MAJOR_VERSION
version-struct.minor = sdl.MINOR_VERSION
version-struct.patch = sdl.PATCHLEVEL
;
inline VERSIONNUM (major minor patch)
(major * 1000) + (minor * 100) + patch
inline COMPILEDVERSION ()
VERSIONNUM sdl.MAJOR_VERSION sdl.MINOR_VERSION sdl.PATCHLEVEL
inline VERSION_ATLEAST (major minor patch)
(COMPILEDVERSION) >= (VERSIONNUM major minor patch)
locals;
let sdl = (sdl .. sdl-macros)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment