Created
October 11, 2012 15:19
-
-
Save fowlmouth/3873166 to your computer and use it in GitHub Desktop.
opengl wrapper wrapper, maybe
This file contains hidden or 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 opengl | |
when defined(Linux): | |
import x, xlib, xutil | |
type | |
PWindow = ref TWindow | |
TWindow = object | |
when defined(Linux): | |
context: GLXcontext | |
# elif defined(Windows): | |
# context: | |
proc DrawAQuad() | |
proc newWindow*(width, height: int32): PWindow = | |
##when defined(Linux): | |
var | |
dpy: PDisplay | |
root: x.TWindow | |
attributes = [GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, GLX_NONE] | |
vinfo: PXVisualInfo | |
swa: TXsetWindowAttributes | |
window: x.TWindow | |
cmap: TColorMap | |
glc: GLXcontext | |
dpy = XOpenDisplay(nil) | |
if dpy.isNil: | |
echo "Cannot connect to X server." | |
return | |
root = XDefaultRootWindow(dpy) | |
loadExtensions() | |
vinfo = glxChooseVisual(dpy, 0, addr attributes[0]) | |
cmap = XCreateColormap(dpy, root, vinfo.visual, AllocNone) | |
swa.colormap = cmap | |
swa.event_mask = ExposureMask or KeyPressMask | |
window = XCreateWindow(dpy, root, 0, 0, width.cuint, height.cuint, 0, | |
vinfo.depth, x.CopyFromParent, vinfo.visual, CWColormap or CWeventMask, | |
addr swa) | |
echo XMapWindow(dpy, window) | |
echo XStoreName(dpy, window, "Sup dude.") | |
glc = glxcreateContext(dpy, vinfo, nil, true) | |
echo glxmakeCurrent(dpy, window, glc) | |
glEnable GL_DEPTH_TEST | |
var | |
xev: TXevent | |
xwa: TXWindowAttributes | |
while true: | |
echo xnextevent(dpy, addr xev) | |
if xev.theType == Expose: | |
discard xgetWindowAttributes(dpy, window, addr xwa) | |
glViewport(0, 0, xwa.width, xwa.height) | |
drawAQuad() | |
glxSwapBuffers(dpy, window) | |
elif xev.theType == keyPress: | |
discard glxmakeCurrent(dpy, GL_NONE, nil) | |
glxDestroyContext(dpy, glc) | |
discard XDestroyWindow(dpy, window) | |
discard XCloseDisplay(dpy) | |
break | |
proc DrawAQuad() = | |
glClearColor(1.0, 1.0, 1.0, 1.0) | |
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, 20.0) | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) | |
glBegin(GL_QUADS) | |
glColor3f(1.0, 0.0, 0.0) | |
glVertex3f(-0.75, -0.75, 0.0) | |
glColor3f(0.0, 1.0, 0.0) | |
glVertex3f(0.75, -0.75, 0.0) | |
glColor3f(0.0, 0.0, 1.0) | |
glVertex3f(0.75, 0.75, 0.0) | |
glColor3f(1.0, 1.0, 0.0) | |
glVertex3f(-0.75, 0.75, 0.0) | |
glEnd() | |
when isMainModule: | |
var w = newWindow(500, 250) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment