Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Imported from 5.0/lv3.tar.gz.

  • Loading branch information...
commit cffcfbd00a1a1312cc857dcc65416ef4deb2e60b 0 parents
Luiz Henrique de Figueiredo authored davidm committed
Showing with 188 additions and 0 deletions.
  1. +57 −0 Makefile
  2. +20 −0 README
  3. +90 −0 lv3.c
  4. +11 −0 test.lua
  5. +10 −0 v3.lua
57 Makefile
@@ -0,0 +1,57 @@
+# makefile for random library for Lua
+
+# change these to reflect your Lua installation
+LUA= /tmp/lhf/lua-5.0
+LUAINC= $(LUA)/include
+LUALIB= $(LUA)/lib
+LUABIN= $(LUA)/bin
+
+# no need to change anything below here
+CFLAGS= $(INCS) $(WARN) -O2 $G
+WARN= -ansi -pedantic -Wall
+INCS= -I$(LUAINC)
+
+MYNAME= v3
+MYLIB= l$(MYNAME)
+T= $(MYLIB).so
+OBJS= $(MYLIB).o
+TEST= test.lua
+
+all: test
+
+test: $T
+ $(LUABIN)/lua -l$(MYNAME) $(TEST)
+
+o: $(MYLIB).o
+
+so: $T
+
+$T: $(OBJS)
+ $(CC) -o $@ -shared $(OBJS)
+
+clean:
+ rm -f $(OBJS) $T core core.* a.out
+
+doc:
+ @echo "$(MYNAME) library:"
+ @fgrep '/**' $(MYLIB).c | cut -f2 -d/ | tr -d '*' | sort | column
+
+# distribution
+
+FTP= $(HOME)/public/ftp/lua
+D= $(MYNAME)
+A= $(MYLIB).tar.gz
+TOTAR= Makefile,README,$(MYLIB).c,$(MYNAME).lua,test.lua
+
+tar: clean
+ tar zcvf $A -C .. $D/{$(TOTAR)}
+
+distr: tar
+ touch -r $A .stamp
+ mv $A $(FTP)
+
+diff: clean
+ tar zxf $(FTP)/$A
+ diff $D .
+
+# eof
20 README
@@ -0,0 +1,20 @@
+This is a 3d-vector library for Lua 5.0. It is meant for learning.
+
+To try the library, just edit Makefile to reflect your installation of Lua and
+then run make. This will build the library and run a simple test. For detailed
+installation instructions, see
+ http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html
+
+There is no manual but the library is simple and intuitive; see the summary
+below. Read also test.lua, which shows the library in action.
+
+This code is hereby placed in the public domain.
+
+Please send comments, suggestions, and bug reports to lhf@tecgraf.puc-rio.br .
+
+-------------------------------------------------------------------------------
+
+v3 library:
+ vector3([x,y,z])
+
+-------------------------------------------------------------------------------
90 lv3.c
@@ -0,0 +1,90 @@
+/*
+* lv3.c
+* 3d vectors for Lua 5.0
+* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
+* 03 Dec 2004 11:29:50
+* This code is hereby placed in the public domain.
+*/
+
+#include <stdio.h>
+
+#include "lua.h"
+#include "lauxlib.h"
+
+#define MYTYPE "vector3"
+
+static double *Pget(lua_State *L, int i)
+{
+ if (luaL_checkudata(L,i,MYTYPE)==NULL) luaL_typerror(L,i,MYTYPE);
+ return lua_touserdata(L,i);
+}
+
+static double *Pnew(lua_State *L)
+{
+ double *v=lua_newuserdata(L,3*sizeof(double));
+ luaL_getmetatable(L,MYTYPE);
+ lua_setmetatable(L,-2);
+ return v;
+}
+
+static int Lnew(lua_State *L) /** vector3([x,y,z]) */
+{
+ double *v;
+ lua_settop(L,3);
+ v=Pnew(L);
+ v[0]=luaL_optnumber(L,1,0);
+ v[1]=luaL_optnumber(L,2,0);
+ v[2]=luaL_optnumber(L,3,0);
+ return 1;
+}
+
+static int Lget(lua_State *L)
+{
+ double *v=Pget(L,1);
+ const char* i=luaL_checkstring(L,2);
+ switch (*i) { /* lazy! */
+ case '1': case 'x': case 'r': lua_pushnumber(L,v[0]); break;
+ case '2': case 'y': case 'g': lua_pushnumber(L,v[1]); break;
+ case '3': case 'z': case 'b': lua_pushnumber(L,v[2]); break;
+ default: lua_pushnil(L); break;
+ }
+ return 1;
+}
+
+static int Lset(lua_State *L) {
+ double *v=Pget(L,1);
+ const char* i=luaL_checkstring(L,2);
+ double t=luaL_checknumber(L,3);
+ switch (*i) { /* lazy! */
+ case '1': case 'x': case 'r': v[0]=t; break;
+ case '2': case 'y': case 'g': v[1]=t; break;
+ case '3': case 'z': case 'b': v[2]=t; break;
+ default: break;
+ }
+ return 1;
+}
+
+static int Ltostring(lua_State *L)
+{
+ double *v=Pget(L,1);
+ char s[64];
+ sprintf(s,"%s %p",MYTYPE,(void*)v);
+ lua_pushstring(L,s);
+ return 1;
+}
+
+static const luaL_reg R[] =
+{
+ { "__index", Lget },
+ { "__newindex", Lset },
+ { "__tostring", Ltostring },
+ { NULL, NULL }
+};
+
+LUALIB_API int luaopen_v3(lua_State *L)
+{
+ luaL_newmetatable(L,MYTYPE);
+ luaL_openlib(L,NULL,R,0);
+ lua_register(L,"vector3",Lnew);
+ return 1;
+}
11 test.lua
@@ -0,0 +1,11 @@
+a=vector3(10,20,30)
+a=vector3(10,20)
+--a=vector3(10)
+--a=vector3()
+print(a)
+
+print(a.x,a.y,a.z)
+print(a.t,a[1],a[4])
+
+a.x,a.y,a.z=100,200,300
+print(a.x,a.y,a.z)
10 v3.lua
@@ -0,0 +1,10 @@
+-- random.lua
+-- support code for random library
+-- usage lua -lrandom ...
+
+local function so(x)
+ local SOPATH= os.getenv"LUA_SOPATH" or "./"
+ assert(loadlib(SOPATH.."l"..x..".so","luaopen_"..x))()
+end
+
+so"v3"
Please sign in to comment.
Something went wrong with that request. Please try again.