Permalink
Browse files

lua binding jetzt per macro

  • Loading branch information...
1 parent e3b4fb9 commit d8a047626e658864c2719aaecc0566e64d93a421 @dividuum committed Feb 18, 2012
Showing with 107 additions and 176 deletions.
  1. +1 −0 .gitignore
  2. +13 −54 font.c
  3. +24 −66 image.c
  4. +52 −0 misc.h
  5. +17 −56 video.c
View
@@ -4,3 +4,4 @@ news/
kernel.h
userlib.h
bin2c
+*.compiled
View
@@ -10,31 +10,15 @@
#include <lauxlib.h>
#include <lualib.h>
-#define FONT "font"
+#include "misc.h"
typedef struct {
FTGLfont *font;
} font_t;
-static font_t *to_font(lua_State *L, int index) {
- font_t *font = (font_t *)lua_touserdata(L, index);
- if (!font) luaL_typerror(L, index, FONT);
- return font;
-}
-
-static font_t *checked_font(lua_State *L, int index) {
- luaL_checktype(L, index, LUA_TUSERDATA);
- font_t *font = (font_t *)luaL_checkudata(L, index, FONT);
- if (!font) luaL_typerror(L, index, FONT);
- return font;
-}
+LUA_TYPE_DECL(font);
-static font_t *push_font(lua_State *L) {
- font_t *font = (font_t *)lua_newuserdata(L, sizeof(font_t));
- luaL_getmetatable(L, FONT);
- lua_setmetatable(L, -2);
- return font;
-}
+/* Instance methods */
static int font_write(lua_State *L) {
font_t *font = checked_font(L, 1);
@@ -68,41 +52,7 @@ static const luaL_reg font_methods[] = {
{0,0}
};
-static int font_gc(lua_State *L) {
- font_t *font = to_font(L, 1);
- ftglDestroyFont(font->font);
- fprintf(stderr, "gc'ing font\n");
- return 0;
-}
-
-static int font_tostring(lua_State *L) {
- lua_pushfstring(L, "font: %p", lua_touserdata(L, 1));
- return 1;
-}
-
-static const luaL_reg font_meta[] = {
- {"__gc", font_gc},
- {"__tostring", font_tostring},
- {0, 0}
-};
-
-
-int font_register (lua_State *L) {
- luaL_openlib(L, FONT, font_methods, 0); /* create methods table,
- add it to the globals */
- luaL_newmetatable(L, FONT); /* create metatable for Image,
- add it to the Lua registry */
- luaL_openlib(L, 0, font_meta, 0); /* fill metatable */
- lua_pushliteral(L, "__index");
- lua_pushvalue(L, -3); /* dup methods table*/
- lua_rawset(L, -3); /* metatable.__index = methods */
- lua_pushliteral(L, "__metatable");
- lua_pushvalue(L, -3); /* dup methods table*/
- lua_rawset(L, -3); /* hide metatable:
- metatable.__metatable = methods */
- lua_pop(L, 1); /* drop metatable */
- return 1; /* return methods on the stack */
-}
+/* Lifecycle */
int font_new(lua_State *L, const char *path, const char *name) {
FTGLfont *ftgl_font = ftglCreatePolygonFont(path);
@@ -120,3 +70,12 @@ int font_new(lua_State *L, const char *path, const char *name) {
font->font = ftgl_font;
return 1;
}
+
+static int font_gc(lua_State *L) {
+ font_t *font = to_font(L, 1);
+ ftglDestroyFont(font->font);
+ fprintf(stderr, "gc'ing font\n");
+ return 0;
+}
+
+LUA_TYPE_IMPL(font);
View
@@ -29,15 +29,17 @@
#include "framebuffer.h"
#include "misc.h"
-#define IMAGE "image"
-
typedef struct {
unsigned int tex;
unsigned int fbo;
int width;
int height;
} image_t;
+LUA_TYPE_DECL(image);
+
+/* Helper functions */
+
static int load_jpeg(const char *filename, int *width, int *height) {
/* JPEG error manager */
struct my_error_mgr {
@@ -268,26 +270,7 @@ static int load_png(const char *filename, int *width, int *height) {
return 0;
}
-
-static image_t *to_image(lua_State *L, int index) {
- image_t *image = (image_t *)lua_touserdata(L, index);
- if (!image) luaL_typerror(L, index, IMAGE);
- return image;
-}
-
-static image_t *checked_image(lua_State *L, int index) {
- luaL_checktype(L, index, LUA_TUSERDATA);
- image_t *image = (image_t *)luaL_checkudata(L, index, IMAGE);
- if (!image) luaL_typerror(L, index, IMAGE);
- return image;
-}
-
-static image_t *push_image(lua_State *L) {
- image_t *image = (image_t *)lua_newuserdata(L, sizeof(image_t));
- luaL_getmetatable(L, IMAGE);
- lua_setmetatable(L, -2);
- return image;
-}
+/* Instance methods */
static int image_size(lua_State *L) {
image_t *image = checked_image(L, 1);
@@ -324,50 +307,7 @@ static const luaL_reg image_methods[] = {
{0,0}
};
-static int image_gc(lua_State *L) {
- image_t *image = to_image(L, 1);
- if (image->fbo) {
- // If images has attached Framebuffer, put the
- // texture and framebuffer into the recycler.
- // Allocations for new framebuffers can then
- // reuse these => Better performance.
- recycle_framebuffer(image->width, image->height,
- image->tex, image->fbo);
- } else {
- // No Framebuffer? Just remove the texture.
- glDeleteTextures(1, &image->tex);
- }
- return 0;
-}
-
-static int image_tostring(lua_State *L) {
- lua_pushfstring(L, "image: %p", lua_touserdata(L, 1));
- return 1;
-}
-
-static const luaL_reg image_meta[] = {
- {"__gc", image_gc},
- {"__tostring", image_tostring},
- {0, 0}
-};
-
-
-int image_register(lua_State *L) {
- luaL_openlib(L, IMAGE, image_methods, 0); /* create methods table,
- add it to the globals */
- luaL_newmetatable(L, IMAGE); /* create metatable for Image,
- add it to the Lua registry */
- luaL_openlib(L, 0, image_meta, 0); /* fill metatable */
- lua_pushliteral(L, "__index");
- lua_pushvalue(L, -3); /* dup methods table*/
- lua_rawset(L, -3); /* metatable.__index = methods */
- lua_pushliteral(L, "__metatable");
- lua_pushvalue(L, -3); /* dup methods table*/
- lua_rawset(L, -3); /* hide metatable:
- metatable.__metatable = methods */
- lua_pop(L, 1); /* drop metatable */
- return 1; /* return methods on the stack */
-}
+/* Lifecycle */
int image_create(lua_State *L, int tex, int fbo, int width, int height) {
image_t *image = push_image(L);
@@ -405,3 +345,21 @@ int image_load(lua_State *L, const char *path, const char *name) {
image->height = height;
return 1;
}
+
+static int image_gc(lua_State *L) {
+ image_t *image = to_image(L, 1);
+ if (image->fbo) {
+ // If images has attached Framebuffer, put the
+ // texture and framebuffer into the recycler.
+ // Allocations for new framebuffers can then
+ // reuse these => Better performance.
+ recycle_framebuffer(image->width, image->height,
+ image->tex, image->fbo);
+ } else {
+ // No Framebuffer? Just remove the texture.
+ glDeleteTextures(1, &image->tex);
+ }
+ return 0;
+}
+
+LUA_TYPE_IMPL(image);
View
@@ -6,4 +6,56 @@
void die(const char *fmt, ...);
void *xmalloc(size_t size);
+// Simple Lua binder
+#define LUA_TYPE_DECL(type) \
+static type##_t *to_##type(lua_State *L, int index); \
+static type##_t *checked_##type(lua_State *L, int index); \
+static type##_t *push_##type(lua_State *L);
+
+#define LUA_TYPE_IMPL(type) \
+static type##_t *to_##type(lua_State *L, int index) { \
+ type##_t *obj = (type##_t *)lua_touserdata(L, index); \
+ if (!obj) luaL_typerror(L, index, #type); \
+ return obj; \
+} \
+ \
+static type##_t *checked_##type(lua_State *L, int index) { \
+ luaL_checktype(L, index, LUA_TUSERDATA); \
+ type##_t *obj = (type##_t *)luaL_checkudata(L, index, #type); \
+ if (!obj) luaL_typerror(L, index, #type); \
+ return obj; \
+} \
+ \
+static type##_t *push_##type(lua_State *L) { \
+ type##_t *obj = (type##_t *)lua_newuserdata(L, sizeof(type##_t)); \
+ luaL_getmetatable(L, #type); \
+ lua_setmetatable(L, -2); \
+ return obj; \
+} \
+ \
+static int type##_tostring(lua_State *L) { \
+ lua_pushfstring(L, "<" #type " %p>", lua_touserdata(L, 1)); \
+ return 1; \
+} \
+ \
+static const luaL_reg type##_meta[] = { \
+ {"__gc", type##_gc}, \
+ {"__tostring", type##_tostring}, \
+ {0, 0} \
+}; \
+ \
+int type##_register(lua_State *L) { \
+ luaL_openlib(L, #type, type##_methods, 0); \
+ luaL_newmetatable(L, #type); \
+ luaL_openlib(L, 0, type##_meta, 0); \
+ lua_pushliteral(L, "__index"); \
+ lua_pushvalue(L, -3); \
+ lua_rawset(L, -3); \
+ lua_pushliteral(L, "__metatable"); \
+ lua_pushvalue(L, -3); \
+ lua_rawset(L, -3); \
+ lua_pop(L, 1); \
+ return 1; \
+}
+
#endif
View
@@ -36,7 +36,7 @@
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
-#define VIDEO "video"
+#include "misc.h"
typedef struct {
AVFormatContext *format_context;
@@ -51,6 +51,10 @@ typedef struct {
double fps;
} video_t;
+LUA_TYPE_DECL(video);
+
+/* Helper functions */
+
static void video_free(video_t *video) {
if (video->scaler)
sws_freeContext(video->scaler);
@@ -208,25 +212,7 @@ static int video_next_frame(video_t *video) {
return 1;
}
-static video_t *to_video(lua_State *L, int index) {
- video_t *video = (video_t *)lua_touserdata(L, index);
- if (!video) luaL_typerror(L, index, VIDEO);
- return video;
-}
-
-static video_t *checked_video(lua_State *L, int index) {
- luaL_checktype(L, index, LUA_TUSERDATA);
- video_t *video = (video_t *)luaL_checkudata(L, index, VIDEO);
- if (!video) luaL_typerror(L, index, VIDEO);
- return video;
-}
-
-static video_t *push_video(lua_State *L) {
- video_t *video = (video_t *)lua_newuserdata(L, sizeof(video_t));
- luaL_getmetatable(L, VIDEO);
- lua_setmetatable(L, -2);
- return video;
-}
+/* Instance methods */
static int video_size(lua_State *L) {
video_t *video = checked_video(L, 1);
@@ -307,42 +293,7 @@ static const luaL_reg video_methods[] = {
{0,0}
};
-static int video_gc(lua_State *L) {
- video_t *video = to_video(L, 1);
- fprintf(stderr, "gc'ing video: tex id: %d\n", video->tex);
- glDeleteTextures(1, &video->tex);
- video_free(video);
- return 0;
-}
-
-static int video_tostring(lua_State *L) {
- lua_pushfstring(L, "video: %p", lua_touserdata(L, 1));
- return 1;
-}
-
-static const luaL_reg video_meta[] = {
- {"__gc", video_gc},
- {"__tostring", video_tostring},
- {0, 0}
-};
-
-
-int video_register(lua_State *L) {
- luaL_openlib(L, VIDEO, video_methods, 0); /* create methods table,
- add it to the globals */
- luaL_newmetatable(L, VIDEO); /* create metatable for Image,
- add it to the Lua registry */
- luaL_openlib(L, 0, video_meta, 0); /* fill metatable */
- lua_pushliteral(L, "__index");
- lua_pushvalue(L, -3); /* dup methods table*/
- lua_rawset(L, -3); /* metatable.__index = methods */
- lua_pushliteral(L, "__metatable");
- lua_pushvalue(L, -3); /* dup methods table*/
- lua_rawset(L, -3); /* hide metatable:
- metatable.__metatable = methods */
- lua_pop(L, 1); /* drop metatable */
- return 1; /* return methods on the stack */
-}
+/* Lifecycle */
int video_load(lua_State *L, const char *path, const char *name) {
video_t video;
@@ -374,3 +325,13 @@ int video_load(lua_State *L, const char *path, const char *name) {
*push_video(L) = video;
return 1;
}
+
+static int video_gc(lua_State *L) {
+ video_t *video = to_video(L, 1);
+ fprintf(stderr, "gc'ing video: tex id: %d\n", video->tex);
+ glDeleteTextures(1, &video->tex);
+ video_free(video);
+ return 0;
+}
+
+LUA_TYPE_IMPL(video);

0 comments on commit d8a0476

Please sign in to comment.