Skip to content

Instantly share code, notes, and snippets.

@yorung
yorung / wgl_grabber.cpp
Last active August 29, 2015 14:14
parsing OpenGL extension header files
struct GLFunc
{
GLFunc(){}
GLFunc(const GLFunc& r) {
*this = r;
}
const GLFunc& operator=(const GLFunc& r) {
name = r.name;
decl = r.decl;
caster = r.caster;
@yorung
yorung / mes_box.cpp
Last active August 29, 2015 14:14
Bind MessageBox to Lua
int MesBox(lua_State *L)
{
const char* str = lua_tostring(L, 1);
MessageBoxA(nullptr, str, "LuaTest", MB_OK);
return 0;
}
void Bind()
{
lua_register(L, "MesBox", MesBox);
@yorung
yorung / my_class.cpp
Created February 7, 2015 12:50
class bind to Lua
class MyClass
{
public:
int value;
};
static const char* myClassName = "MyClass";
static int MyClassSetValue(lua_State *L)
{
@yorung
yorung / wrong_lua_stack_dump.cpp
Created February 9, 2015 00:05
Lua stack dump (wrong! numbers on the Lua stack will be replaced with string)
void _dumpStack(lua_State* L, const char* func, int line)
{
int top = lua_gettop(L);
printf("(%s,%d) top=%d\n", func, line, top);
for (int i = 0; i < top; i++) {
int positive = top - i;
int negative = -(i + 1);
int type = lua_type(L, positive);
const char* typeName = lua_typename(L, type);
const char* value = lua_tostring(L, positive); // danger!!! numbers on the Lua stack will be replaced with string
@yorung
yorung / vector_swizzling.lua
Created February 10, 2015 00:05
vector swizzling from lua
local v = Vec4()
v = Vec4(1, 2, 3, 4)
v.wzyx = Vec4(1, 2, 3, 4)
v = Vec4(1.111, 2.2222, 3.333, 4.444).wzyx
v.w = 0
@yorung
yorung / vector_swizzling.cpp
Last active August 29, 2015 14:15
vector swizzling
#include <algorithm>
#include <string.h>
#include "af_lua_helpers.h"
#include "af_math.h"
extern lua_State *L;
static const char* myClassName = "Vec4";
@yorung
yorung / my_class_lambda.cpp
Created February 17, 2015 15:25
binding MyClass by using lambda
#include <new>
class MyClass
{
public:
MyClass()
{
value = 0;
puts("MyClass::MyClass called");
}
@yorung
yorung / my_class_bind_final.cpp
Created February 18, 2015 06:19
bind MyClass to Lua
#include <new>
extern lua_State *L;
class MyClass
{
public:
MyClass()
{
value = 0;
@yorung
yorung / af_lua_helper.cpp
Last active August 29, 2015 14:15
a very simple class binder for Lua
static int CreateCppClassInstance(lua_State* L)
{
const char* className = lua_tostring(L, lua_upvalueindex(1));
lua_CFunction actualInstanceCreator = lua_tocfunction(L, lua_upvalueindex(2));
actualInstanceCreator(L);
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
return 1;
}
@yorung
yorung / correct_lua_stack_dump.cpp
Created February 18, 2015 07:27
correct Lua stack dump
void _dumpStack(lua_State* L, const char* func, int line)
{
int top = lua_gettop(L);
printf("(%s,%d) top=%d\n", func, line, top);
for (int i = 0; i < top; i++) {
int positive = top - i;
int negative = -(i + 1);
int type = lua_type(L, positive);
int typeN = lua_type(L, negative);
assert(type == typeN);