Initial revision
[luabind.git] / examples / glut / glut_bind.cpp
blob05ffa7b6b5118db9594c5fbbbb60d30748089b37
1 extern "C"
3 #include "lua.h"
4 #include "lauxlib.h"
5 #include "lualib.h"
9 #include <luabind/luabind.hpp>
10 #include <luabind/class.hpp>
11 #include <luabind/function.hpp>
12 #include <luabind/functor.hpp>
15 #include <GL/glut.h>
16 #include <GL/gl.h>
17 #include <GL/glu.h>
19 struct glut_constants {};
20 struct gl_constants {};
22 namespace glut_bindings
24 luabind::functor<void> displayfunc;
26 void displayfunc_callback()
28 displayfunc();
31 void set_displayfunc(const luabind::functor<void>& fun)
33 glutDisplayFunc(&displayfunc_callback);
34 displayfunc = fun;
37 luabind::functor<void> idlefunc;
39 void idlefunc_callback()
41 idlefunc();
44 void set_idlefunc(const luabind::functor<void>& fun)
46 glutIdleFunc(&idlefunc_callback);
47 idlefunc = fun;
51 luabind::functor<void> reshapefunc;
53 void reshapefunc_callback(int w, int h)
55 reshapefunc(w,h);
58 void set_reshapefunc(const luabind::functor<void>& fun)
60 reshapefunc = fun;
63 luabind::functor<void> keyboardfunc;
65 void keyboardfunc_callback(unsigned char key, int x, int y)
67 keyboardfunc(key,x,y);
70 void set_keyboardfunc(const luabind::functor<void>& fun)
72 glutKeyboardFunc(&keyboardfunc_callback);
73 keyboardfunc = fun;
76 luabind::functor<void> mousefunc;
78 void mousefunc_callback(int button, int state, int x, int y)
80 mousefunc(button, state, x, y);
83 void set_mousefunc(const luabind::functor<void>& fun)
85 mousefunc = fun;
89 void bind_glut(lua_State* L)
91 using namespace luabind;
92 using namespace glut_bindings;
94 open(L);
96 function(L, "glutInitWindowSize", &glutInitWindowSize);
97 function(L, "glutInitWindowPosition", &glutInitWindowPosition);
98 function(L, "glutInitDisplayMode", &glutInitDisplayMode);
100 class_<glut_constants>(L, "glut")
101 .enum_("constants")
103 value("RGB", GLUT_RGB),
104 value("RGBA", GLUT_RGBA),
105 value("INDEX", GLUT_INDEX),
106 value("SINGLE", GLUT_SINGLE),
107 value("DOUBLE", GLUT_DOUBLE),
108 value("DEPTH", GLUT_DEPTH),
109 value("STENCIL", GLUT_STENCIL),
110 value("LEFT_BUTTON", GLUT_LEFT_BUTTON),
111 value("MIDDLE_BUTTON", GLUT_MIDDLE_BUTTON),
112 value("RIGHT_BUTTON", GLUT_RIGHT_BUTTON),
113 value("UP", GLUT_UP),
114 value("DOWN", GLUT_DOWN),
115 value("ELAPSED_TIME", GLUT_ELAPSED_TIME)
118 function(L, "glutCreateWindow", &glutCreateWindow);
119 function(L, "glutDestroyWindow", &glutDestroyWindow);
120 function(L, "glutFullScreen", &glutFullScreen);
121 function(L, "glutDisplayFunc", &set_displayfunc);
122 function(L, "glutKeyboardFunc", &set_keyboardfunc);
123 function(L, "glutReshapeFunc", &set_reshapefunc);
124 function(L, "glutIdleFunc", &set_idlefunc);
125 function(L, "glutMainLoop", &glutMainLoop);
126 function(L, "glutSwapBuffers", &glutSwapBuffers);
127 function(L, "glutGet", &glutGet);
128 function(L, "glutSolidSphere", &glutSolidSphere);
129 function(L, "glutWireSphere", &glutWireSphere);
130 function(L, "glutWireTeapot", &glutWireTeapot);
131 function(L, "glutSolidTeapot", &glutSolidTeapot);
133 // -- opengl
135 class_<gl_constants>(L, "gl")
136 .enum_("constants")
138 value("COLOR_BUFFER_BIT", GL_COLOR_BUFFER_BIT),
139 value("DEPTH_BUFFER_BIT", GL_DEPTH_BUFFER_BIT),
140 value("TRIANGLES", GL_TRIANGLES),
141 value("MODELVIEW", GL_MODELVIEW),
142 value("PROJECTION", GL_PROJECTION)
145 function(L, "glBegin", &glBegin);
146 function(L, "glVertex3", &glVertex3f);
147 function(L, "glEnd", &glEnd);
148 function(L, "glClear", &glClear);
149 function(L, "glPushMatrix", &glPushMatrix);
150 function(L, "glPopMatrix", &glPopMatrix);
151 function(L, "glRotate", &glRotatef);
152 function(L, "glColor3", &glColor3f);
153 function(L, "glColor4", &glColor4f);
154 function(L, "glMatrixMode", &glMatrixMode);
155 function(L, "glLoadIdentity", &glLoadIdentity);
156 function(L, "glViewport", &glViewport);
157 function(L, "glTranslate", &glTranslatef);
159 // -- glu
161 function(L, "gluPerspective", &gluPerspective);
164 int main()
166 lua_State* L = lua_open();
167 lua_baselibopen(L);
168 lua_mathlibopen(L);
169 bind_glut(L);
171 int argc = 1;
172 char* argv[1];
173 argv[0] = "blabla";
175 glutInit (&argc, argv);
177 lua_dofile(L, "glut_bindings.lua");
179 lua_close(L);
180 return 0;