diff --git a/Engine.cc b/Engine.cc index 13a0663..a1d0054 100644 --- a/Engine.cc +++ b/Engine.cc @@ -540,6 +540,40 @@ void Engine::drawLine(float r, float g, float b, glPopAttrib(); } +void Engine::drawRect(float r, float g, float b, + float width, float height, float x, float y) +{ + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glEnable(GL_LINE_SMOOTH); + glBegin(GL_LINE_LOOP); + glColor3f(r, g, b); + glVertex2f(x - width / 2, y - height / 2); + glVertex2f(x + width / 2, y - height / 2); + glVertex2f(x + width / 2, y + height / 2); + glVertex2f(x - width / 2, y + height / 2); + glEnd(); + glPopAttrib(); +} + +void Engine::fillRect(float r, float g, float b, + float width, float height, float x, float y) +{ + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glEnable(GL_POLYGON_SMOOTH); + glBegin(GL_QUADS); + glColor3f(r, g, b); + glVertex2f(x - width / 2, y - height / 2); + glVertex2f(x + width / 2, y - height / 2); + glVertex2f(x + width / 2, y + height / 2); + glVertex2f(x - width / 2, y + height / 2); + 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 b8a7eb3..e5cfe49 100644 --- a/Engine.h +++ b/Engine.h @@ -145,6 +145,10 @@ class Engine float * width, float * height); void drawLine(float r, float g, float b, float x1, float y1, float x2, float y2, float width = 1.0f); + void drawRect(float r, float g, float b, + float width, float height, float x, float y); + void fillRect(float r, float g, float b, + float width, float height, float x, float y); /* lua services */ int setCamera(lua_State * L); diff --git a/ag.cc b/ag.cc index d37ed39..63ad148 100644 --- a/ag.cc +++ b/ag.cc @@ -47,6 +47,8 @@ namespace ag { "drawText", drawText}, { "getTextSize", getTextSize}, { "drawLine", drawLine}, + { "drawRect", drawRect}, + { "fillRect", fillRect}, { "createBox", createBox}, { "createStaticBox", createStaticBox}, @@ -420,6 +422,54 @@ namespace ag return 0; } + int drawRect(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 7) + { + bool valid = true; + for (int i = 1; i <= argc; i++) + if (!lua_isnumber(L, i)) + valid = false; + if (valid) + { + g_engine->drawRect( + 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)); + } + } + return 0; + } + + int fillRect(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 7) + { + bool valid = true; + for (int i = 1; i <= argc; i++) + if (!lua_isnumber(L, i)) + valid = false; + if (valid) + { + g_engine->fillRect( + 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)); + } + } + 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 e6e6e65..9fcd3d2 100644 --- a/ag.h +++ b/ag.h @@ -33,6 +33,8 @@ namespace ag int drawText(lua_State * L); int getTextSize(lua_State * L); int drawLine(lua_State * L); + int drawRect(lua_State * L); + int fillRect(lua_State * L); int createBox(lua_State * L); int createStaticBox(lua_State * L); diff --git a/tests/bowling.lua b/tests/bowling.lua index c87f8b1..562545f 100644 --- a/tests/bowling.lua +++ b/tests/bowling.lua @@ -57,4 +57,6 @@ function update_overlay_event(width, height) 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) + ag.drawRect(0, 1, 0, 40, 40, width / 2, height / 2) + ag.fillRect(0, 0, 1, 20, 30, width / 2, height / 2) end