diff --git a/Engine.cc b/Engine.cc index 3da9110..13a0663 100644 --- a/Engine.cc +++ b/Engine.cc @@ -524,6 +524,22 @@ void Engine::getTextSize(const char * text, int ptsize, *height = box.Upper().Yf() - box.Lower().Yf(); } +void Engine::drawLine(float r, float g, float b, + float x1, float y1, float x2, float y2, float width) +{ + glPushAttrib(GL_LINE_BIT | GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glEnable(GL_LINE_SMOOTH); + glLineWidth(width); + glBegin(GL_LINES); + glColor3f(r, g, b); + glVertex2f(x1, y1); + glVertex2f(x2, y2); + glEnd(); + glPopAttrib(); +} + /* called by SDL when the update timer expires */ Uint32 Engine::updateCallback(Uint32 interval, void * param) { diff --git a/Engine.h b/Engine.h index 5e2722a..b8a7eb3 100644 --- a/Engine.h +++ b/Engine.h @@ -143,6 +143,8 @@ class Engine int ptsize, float x, float y); void getTextSize(const char * text, int ptsize, float * width, float * height); + void drawLine(float r, float g, float b, + float x1, float y1, float x2, float y2, float width = 1.0f); /* lua services */ int setCamera(lua_State * L); diff --git a/ag.cc b/ag.cc index 8b79c5d..d37ed39 100644 --- a/ag.cc +++ b/ag.cc @@ -46,6 +46,7 @@ namespace ag { "getScreenSize", getScreenSize }, { "drawText", drawText}, { "getTextSize", getTextSize}, + { "drawLine", drawLine}, { "createBox", createBox}, { "createStaticBox", createStaticBox}, @@ -391,6 +392,34 @@ namespace ag return 2; } + int drawLine(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 7 || argc == 8) + { + bool valid = true; + for (int i = 1; i <= argc; i++) + if (!lua_isnumber(L, i)) + valid = false; + if (valid) + { + float width = 1.0f; + if (argc == 8) + width = lua_tonumber(L, 8); + g_engine->drawLine( + lua_tonumber(L, 1), + lua_tonumber(L, 2), + lua_tonumber(L, 3), + lua_tonumber(L, 4), + lua_tonumber(L, 5), + lua_tonumber(L, 6), + lua_tonumber(L, 7), + width); + } + } + return 0; + } + static void addManagedObject(lua_State * L, bool is_static, OdeWorld::GeomType geom_type, refptr< vector > args) { diff --git a/ag.h b/ag.h index 5ad7f51..e6e6e65 100644 --- a/ag.h +++ b/ag.h @@ -32,6 +32,7 @@ namespace ag int getScreenSize(lua_State * L); int drawText(lua_State * L); int getTextSize(lua_State * L); + int drawLine(lua_State * L); int createBox(lua_State * L); int createStaticBox(lua_State * L); diff --git a/tests/bowling.lua b/tests/bowling.lua index e134bc2..c87f8b1 100644 --- a/tests/bowling.lua +++ b/tests/bowling.lua @@ -52,7 +52,9 @@ function key_down_event(key) end end ---function update_overlay_event(width, height) --- local tw, th = ag.getTextSize("Hi there", 18) --- ag.drawText("Hi there", 1, 1, 1, 18, width - tw - 4, height - th - 4) ---end +function update_overlay_event(width, height) + local tw, th = ag.getTextSize("Hi there", 18) + ag.drawText("Hi there", 1, 1, 1, 18, width - tw - 4, height - th - 4) + ag.drawLine(1, 0, 0.3, 10, 10, 40, 60) + ag.drawLine(1, 0.7, 1, 50, 10, 100, 60, 5) +end