-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 12a75c8
Showing
5 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
extern "C" | ||
{ | ||
#include <lua.h> | ||
#include <lauxlib.h> | ||
} | ||
|
||
#include <GLUT/glut.h> | ||
#include <stdlib.h> | ||
|
||
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" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef GLUT_H | ||
#define GLUT_H | ||
|
||
LUALIB_API int luaopen_glut(lua_State *L); | ||
|
||
#endif//GLUT_H |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
|
||
extern "C" | ||
{ | ||
#include <lua.h> | ||
#include <lualib.h> | ||
#include <lauxlib.h> | ||
|
||
#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] << " <lua script file> [args...]" << std::endl; | ||
return 0; | ||
} | ||
|
||
lua(argc, argv); | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |