Created
September 26, 2012 10:22
-
-
Save Quit/3787214 to your computer and use it in GitHub Desktop.
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
#include "LuaObject.hpp" | |
#include <gl/GL.h> | |
/** | |
* lDraw - draw. Library | |
**/ | |
namespace gle { | |
namespace lDraw { | |
// drawRect(x, y, w, h, z, color1, c2 = nil, c3 = nil, c4 = nil) | |
LUA_FUNCTION(rect) { | |
int x = luaL_checkint(L, 1), y = luaL_checkint(L, 2), w = luaL_checkint(L, 3), h = luaL_checkint(L, 4); | |
double z = luaL_checknumber(L, 5); | |
COLOR_CLASS& c1 = lgColor::checkColor(L, 6); | |
gle::gGraphics->drawQuad(x, y, c1, x+w, y, lgColor::optColor(L, 7, c1), x+w, y+h, lgColor::optColor(L, 8, c1), x, y+h, lgColor::optColor(L, 9, c1), z); | |
return 0; | |
} | |
// drawLine(x1, y1, x2, y2, z, color) | |
LUA_FUNCTION(line) { | |
int x1 = luaL_checkint(L,1), y1 = luaL_checkint(L, 2), x2 = luaL_checkint(L, 3), y2 = luaL_checkint(L, 4); | |
double z = luaL_checknumber(L, 5); | |
COLOR_CLASS& c = lgColor::checkColor(L, 6); | |
gle::gGraphics->drawLine(x1, y1, c, x2, y2, c, z); | |
return 0; | |
} | |
// drawQuad(x1, y1, x2, y2, x3, y3, x4, y4, z, color1, color2, color3, color4) | |
LUA_FUNCTION(quad) { | |
COLOR_CLASS& c1 = lgColor::checkColor(L, 10); | |
Gosu::Color& c2 = lgColor::optColor(L, 11, c1); | |
Gosu::Color& c3 = lgColor::optColor(L, 12, c1); | |
Gosu::Color& c4 = lgColor::optColor(L, 13, c1); | |
gle::gGraphics->drawQuad(luaL_checknumber(L, 1), luaL_checknumber(L, 2), c1, luaL_checknumber(L, 3), luaL_checknumber(L, 4), c2, luaL_checknumber(L, 5), luaL_checknumber(L, 6), c3, luaL_checknumber(L, 7), luaL_checknumber(L, 8), c4, luaL_checknumber(L, 9)); | |
return 0; | |
} | |
// debugText(text, x, y, color = WHITE) | |
LUA_FUNCTION(debugText) { | |
gle::gWindow->debugText(gle::checkWstring(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), lgColor::optColor(L, 4, Gosu::Color::WHITE)); | |
return 0; | |
} | |
// multiline({ x, y, x, y, ...}, z, color) | |
LUA_FUNCTION(multiline) { | |
std::vector<double> coords; | |
// Check the first thing's a table. | |
if (!lua_istable(L, 1)) | |
luaL_argerror(L, 1, "table expected"); | |
double z = luaL_checknumber(L, 2); | |
Gosu::Color& c = lgColor::optColor(L, 3, Gosu::Color::WHITE); | |
// Iterate through the table... | |
lua_pushnil(L); | |
while (lua_next(L, 1)) { | |
if (!lua_isnumber(L, -1)) | |
luaL_argerror(L, 1, "table contained non-number"); | |
coords.push_back(lua_tonumber(L, -1)); | |
lua_pop(L, 1); | |
} | |
// empty. Pff. | |
if (!coords.size()) | |
return 0; | |
if (coords.size() % 2) | |
luaL_argerror(L, 1, "odd count detected"); | |
std::vector<double> *nc = new std::vector<double>(coords); | |
gle::gGraphics->scheduleGL([c, nc]() { | |
glColor4ub(c.red(), c.green(), c.blue(), c.alpha()); | |
glBegin(GL_LINE_STRIP); | |
double x, y; | |
for (auto it = nc->begin(); it != nc->end(); ++it) { | |
x = *it; | |
y = *(++it); | |
glVertex2d(x, y); | |
} | |
glEnd(); | |
delete nc; | |
}, z); | |
return 0; | |
} | |
const luaL_Reg reg[] = { | |
{ "rect", rect }, | |
{ "line", line }, | |
{ "quad", quad }, | |
{ "debugText", debugText }, | |
{ "multiline", multiline }, | |
{ NULL, NULL } | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment