From 12a75c8545d80e1667825bac477cae2b3cfa9e3d Mon Sep 17 00:00:00 2001 From: "e.mattsan" Date: Thu, 7 Jan 2010 21:13:47 +0900 Subject: [PATCH] first commit --- Makefile | 4 ++ glut.cpp | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++ glut.h | 6 ++ lua_glut.cpp | 49 ++++++++++++++++ sample.lua | 36 ++++++++++++ 5 files changed, 250 insertions(+) create mode 100644 Makefile create mode 100644 glut.cpp create mode 100644 glut.h create mode 100644 lua_glut.cpp create mode 100644 sample.lua diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fe2f493 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +# for Mac OS X + +lua_glut: lua_glut.cpp glut.cpp glut.h + g++ -framework OpenGL -framework GLUT -framework Foundation -ansi -Wall -o lua_glut lua_glut.cpp glut.cpp -llua \ No newline at end of file diff --git a/glut.cpp b/glut.cpp new file mode 100644 index 0000000..8827fcd --- /dev/null +++ b/glut.cpp @@ -0,0 +1,155 @@ +extern "C" +{ +#include +#include +} + +#include +#include + +static lua_State *G_L; + +static int interval = 200; + +static int width = 320; +static int height = 240; + +static int vertex2i(lua_State *L) +{ + int x = luaL_checkinteger(L, 1); + int y = luaL_checkinteger(L, 2); + glVertex2i(x, y); + return 0; +} + +static void display() +{ + glClear(GL_COLOR_BUFFER_BIT); + + glColor3d(0.0, 0.0, 0.0); + glBegin(GL_LINES); + + lua_getfield(G_L, 1, "display"); + if(lua_isfunction(G_L, -1)) + { + lua_pushvalue(G_L, 1); + lua_call(G_L, 1, 0); + } + lua_settop(G_L, 1); + + glEnd(); + + glFlush(); +} + +static void resize(int w, int h) +{ + glViewport(0, 0, w, h); + width = w; + height = h; + glLoadIdentity(); + glOrtho(-0.5, w - 0.5, h - 0.5, -0.5, -1.0, 1.0); +} + +static void keyboard(unsigned char key, int x, int y) +{ + lua_getfield(G_L, 1, "keyboard"); + if(lua_isfunction(G_L, -1)) + { + lua_pushvalue(G_L, 1); + lua_pushinteger(G_L, key); + lua_pushinteger(G_L, x); + lua_pushinteger(G_L, y); + lua_call(G_L, 4, 0); + } + lua_settop(G_L, 1); +} + +static void timer(int n) +{ + lua_getfield(G_L, 1, "timer"); + if(lua_isfunction(G_L, -1)) + { + lua_pushvalue(G_L, 1); + lua_call(G_L, 1, 0); + } + lua_settop(G_L, 1); + + glColor3d(0.0, 0.0, 0.0); + glBegin(GL_LINES); + + lua_getfield(G_L, 1, "display"); + if(lua_isfunction(G_L, -1)) + { + lua_pushvalue(G_L, 1); + lua_call(G_L, 1, 0); + } + lua_settop(G_L, 1); + + glEnd(); + + glFlush(); + + glutTimerFunc(interval, timer, 0); +} + +static int main_loop(lua_State *L) +{ + int argc = 0; + char* argv[] = { 0 }; + + G_L = L; + + lua_getfield(G_L, 1, "width"); + if(lua_isnumber(G_L, -1)) + { + width = lua_tointeger(G_L, -1); + } + lua_getfield(G_L, 1, "height"); + if(lua_isnumber(G_L, -1)) + { + height = lua_tointeger(G_L, -1); + } + lua_getfield(G_L, 1, "interval"); + if(lua_isnumber(G_L, -1)) + { + interval = lua_tointeger(G_L, -1); + } + lua_settop(G_L, 1); + + glutInitWindowPosition(100, 100); + glutInitWindowSize(width, height); + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGBA); + + glutCreateWindow(argv[0]); + + glutDisplayFunc(display); + glutReshapeFunc(resize); + glutKeyboardFunc(keyboard); + glutTimerFunc(interval, timer, 0); + + glClearColor(1.0, 1.0, 1.0, 1.0); + + glutMainLoop(); + + return 0; +} + +static const luaL_Reg glutlib[] = +{ + { "vertex2i", vertex2i }, + { "main_loop", main_loop }, + { NULL, NULL } +}; + +extern "C" +{ + +LUALIB_API int luaopen_glut(lua_State *L) +{ + luaL_register(L, "glut", glutlib); + return 1; +} + +} // extern "C" diff --git a/glut.h b/glut.h new file mode 100644 index 0000000..72b39f1 --- /dev/null +++ b/glut.h @@ -0,0 +1,6 @@ +#ifndef GLUT_H +#define GLUT_H + +LUALIB_API int luaopen_glut(lua_State *L); + +#endif//GLUT_H diff --git a/lua_glut.cpp b/lua_glut.cpp new file mode 100644 index 0000000..29a8cc4 --- /dev/null +++ b/lua_glut.cpp @@ -0,0 +1,49 @@ +#include + +extern "C" +{ +#include +#include +#include + +#include "glut.h" +} + +void lua(int argc, char* argv[]) +{ + lua_State* L = lua_open(); + + luaL_openlibs(L); + + lua_pushcfunction(L, luaopen_glut); + lua_pushstring(L, "glut"); + lua_call(L, 1, 0); + + lua_newtable(L); + for(int i = 0; i < argc; ++i) + { + lua_pushstring(L, argv[i]); + lua_rawseti(L, -2, i - 1); + } + lua_setglobal(L, "arg"); + + if(luaL_dofile(L, argv[1]) != 0) + { + std::cerr << "error : " << lua_tostring(L, -1) << std::endl; + } + + lua_close(L); +} + +int main(int argc, char* argv[]) +{ + if(argc == 1) + { + std::cout << "usage: " << argv[0] << " [args...]" << std::endl; + return 0; + } + + lua(argc, argv); + + return 0; +} \ No newline at end of file diff --git a/sample.lua b/sample.lua new file mode 100644 index 0000000..a8ab8a2 --- /dev/null +++ b/sample.lua @@ -0,0 +1,36 @@ +sample = +{ + ctrl_c = 3, + + interval = 100, + width = 160, + height = 120, + + pos1 = { x = 10, y = 10 }, + vel1 = { x = 8, y = -2 }, + pos2 = { x = 75, y = 55 }, + vel2 = { x = -6, y = 4 } +} + + +function sample:keyboard(key, x, y) + if key == self.ctrl_c then os.exit() end +end + +function sample:display() + glut.vertex2i(self.pos1.x, self.pos1.y) + glut.vertex2i(self.pos2.x, self.pos2.y) +end + +function sample:timer() + self.pos1.x = self.pos1.x + self.vel1.x + self.pos1.y = self.pos1.y + self.vel1.y + self.pos2.x = self.pos2.x + self.vel2.x + self.pos2.y = self.pos2.y + self.vel2.y + if (self.pos1.x < 0) or (self.width < self.pos1.x) then self.vel1.x = -self.vel1.x end + if (self.pos1.y < 0) or (self.height < self.pos1.y) then self.vel1.y = -self.vel1.y end + if (self.pos2.x < 0) or (self.width < self.pos2.x) then self.vel2.x = -self.vel2.x end + if (self.pos2.y < 0) or (self.height < self.pos2.y) then self.vel2.y = -self.vel2.y end +end + +glut.main_loop(sample)