OpenGL: Difference between revisions

→‎{{header|Wren}}: Improved way of dealing with callbacks.
(Added Wren)
(→‎{{header|Wren}}: Improved way of dealing with callbacks.)
Line 2,794:
We wrap as many OpenGL/FreeGLUT calls as possible from the Wren side though glutInit (requires command line parameters) and glutMainLoop (requires a re-entrant VM) must be called by the host.
 
Notice that we can't pass Wren methods directly to the glutDisplayFunc and glutReshapeFunc functions for callback registration purposes because of re-entrancy problems so, instead, we simplypass instigatesufficient theminformation from Wren andto fix upenable the callbacks to be constructed from the C side.
<lang ecmascript>/* opengl.wren */
 
Line 2,842:
foreign static createWindow(name)
 
foreign static displayFunc(clazz, signature)
 
foreign static reshapeFunc(clazz, signature)
 
foreign static setOption(eWhat, value)
Line 2,878:
Glut.initWindowSize(640, 480)
Glut.createWindow("Triangle")
Glut.displayFunc("GLCallbacks", "paint()")
Glut.reshapeFunc("GLCallbacks", "reshape(_,_)")
Glut.setOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS)</lang>
<br>
Line 2,893:
 
WrenVM *vm;
 
const char *displayClass, *displayMethod, *reshapeClass, *reshapeMethod;
 
void display() {
wrenEnsureSlots(vm, 1);
wrenGetVariable(vm, "main", "GLCallbacks"displayClass, 0);
WrenHandle *method = wrenMakeCallHandle(vm, "paint()"displayMethod);
wrenCall(vm, method);
wrenReleaseHandle(vm, method);
Line 2,904 ⟶ 2,906:
void reshape(int width, int height) {
wrenEnsureSlots(vm, 3);
wrenGetVariable(vm, "main", "GLCallbacks"reshapeClass, 0);
WrenHandle *method = wrenMakeCallHandle(vm, "reshape(_,_)"reshapeMethod);
wrenSetSlotDouble(vm, 1, (double)width);
wrenSetSlotDouble(vm, 2, (double)height);
Line 3,002 ⟶ 3,004:
 
void C_displayFunc(WrenVM* vm) {
displayClass = wrenGetSlotString(vm, 1);
displayMethod = wrenGetSlotString(vm, 2);
glutDisplayFunc(&display);
}
 
void C_reshapeFunc(WrenVM* vm) {
reshapeClass = wrenGetSlotString(vm, 1);
reshapeMethod = wrenGetSlotString(vm, 2);
glutReshapeFunc(&reshape);
}
Line 3,039 ⟶ 3,045:
if (isStatic && strcmp(signature, "initWindowSize(_,_)") == 0) return C_initWindowSize;
if (isStatic && strcmp(signature, "createWindow(_)") == 0) return C_createWindow;
if (isStatic && strcmp(signature, "displayFunc(_,_)") == 0) return C_displayFunc;
if (isStatic && strcmp(signature, "reshapeFunc(_,_)") == 0) return C_reshapeFunc;
if (isStatic && strcmp(signature, "setOption(_,_)") == 0) return C_setOption;
}
9,487

edits