diff --git a/Engine.cc b/Engine.cc index a1d0054..23772c5 100644 --- a/Engine.cc +++ b/Engine.cc @@ -541,36 +541,70 @@ void Engine::drawLine(float r, float g, float b, } void Engine::drawRect(float r, float g, float b, - float width, float height, float x, float y) + float width, float height, float x, float y, float rot) { 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(); + glPushMatrix(); + glTranslatef(x, y, 0); + glRotatef(rot, 0, 0, 1); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glEnable(GL_LINE_SMOOTH); + glBegin(GL_LINE_LOOP); + glColor3f(r, g, b); + glVertex2f(-width / 2, -height / 2); + glVertex2f(width / 2, -height / 2); + glVertex2f(width / 2, height / 2); + glVertex2f(-width / 2, height / 2); + glEnd(); + glPopMatrix(); glPopAttrib(); } void Engine::fillRect(float r, float g, float b, - float width, float height, float x, float y) + float width, float height, float x, float y, float rot) { 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(); + glPushMatrix(); + glTranslatef(x, y, 0); + glRotatef(rot, 0, 0, 1); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glEnable(GL_POLYGON_SMOOTH); + glBegin(GL_QUADS); + glColor3f(r, g, b); + glVertex2f(-width / 2, -height / 2); + glVertex2f(width / 2, -height / 2); + glVertex2f(width / 2, height / 2); + glVertex2f(-width / 2, height / 2); + glEnd(); + glPopMatrix(); + glPopAttrib(); +} + +void Engine::drawImage(float width, float height, float x, float y, + int tex, float rot) +{ + glPushAttrib(GL_ENABLE_BIT); + glPushMatrix(); + glTranslatef(x, y, 0); + glRotatef(rot, 0, 0, 1); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glEnable(GL_POLYGON_SMOOTH); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, tex); + glBegin(GL_QUADS); + glTexCoord2f(0, 0); + glVertex2f(-width / 2, -height / 2); + glTexCoord2f(1, 0); + glVertex2f(width / 2, -height / 2); + glTexCoord2f(1, 1); + glVertex2f(width / 2, height / 2); + glTexCoord2f(0, 1); + glVertex2f(-width / 2, height / 2); + glEnd(); + glPopMatrix(); glPopAttrib(); } diff --git a/Engine.h b/Engine.h index e5cfe49..7203461 100644 --- a/Engine.h +++ b/Engine.h @@ -146,9 +146,11 @@ class Engine 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); + float width, float height, float x, float y, float rot = 0.0f); void fillRect(float r, float g, float b, - float width, float height, float x, float y); + float width, float height, float x, float y, float rot = 0.0f); + void drawImage(float width, float height, float x, float y, + int tex, float rot = 0.0f); /* lua services */ int setCamera(lua_State * L); diff --git a/ag.cc b/ag.cc index 63ad148..f8c493d 100644 --- a/ag.cc +++ b/ag.cc @@ -49,6 +49,7 @@ namespace ag { "drawLine", drawLine}, { "drawRect", drawRect}, { "fillRect", fillRect}, + { "drawImage", drawImage}, { "createBox", createBox}, { "createStaticBox", createStaticBox}, @@ -425,7 +426,7 @@ namespace ag int drawRect(lua_State * L) { int argc = lua_gettop(L); - if (argc == 7) + if (argc == 7 || argc == 8) { bool valid = true; for (int i = 1; i <= argc; i++) @@ -433,6 +434,9 @@ namespace ag valid = false; if (valid) { + float rot = 0.0f; + if (argc == 8) + rot = lua_tonumber(L, 8); g_engine->drawRect( lua_tonumber(L, 1), lua_tonumber(L, 2), @@ -440,7 +444,8 @@ namespace ag lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), - lua_tonumber(L, 7)); + lua_tonumber(L, 7), + rot); } } return 0; @@ -449,7 +454,7 @@ namespace ag int fillRect(lua_State * L) { int argc = lua_gettop(L); - if (argc == 7) + if (argc == 7 || argc == 8) { bool valid = true; for (int i = 1; i <= argc; i++) @@ -457,6 +462,9 @@ namespace ag valid = false; if (valid) { + float rot = 0.0f; + if (argc == 8) + rot = lua_tonumber(L, 8); g_engine->fillRect( lua_tonumber(L, 1), lua_tonumber(L, 2), @@ -464,7 +472,42 @@ namespace ag lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), - lua_tonumber(L, 7)); + lua_tonumber(L, 7), + rot); + } + } + return 0; + } + + int drawImage(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 5 || argc == 6) + { + bool valid = true; + for (int i = 1; i <= 6; i++) + if (i != 5 && !lua_isnumber(L, i)) + valid = false; + if (!lua_istable(L, 5)) + valid = false; + if (valid) + { + lua_getfield(L, 5, "id"); + if (lua_isnumber(L, -1)) + { + float rot = 0.0f; + if (argc == 6) + rot = lua_tonumber(L, 6); + GLuint texture_id = (GLuint) lua_tointeger(L, -1); + g_engine->drawImage( + lua_tonumber(L, 1), + lua_tonumber(L, 2), + lua_tonumber(L, 3), + lua_tonumber(L, 4), + texture_id, + rot); + } + lua_pop(L, 1); } } return 0; diff --git a/ag.h b/ag.h index 9fcd3d2..e3dee28 100644 --- a/ag.h +++ b/ag.h @@ -30,12 +30,16 @@ namespace ag int import(lua_State * L); int loadTexture(lua_State * L); int getScreenSize(lua_State * L); + + /* 2D overlay functions */ 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 drawImage(lua_State * L); + /* managed object creation functions */ int createBox(lua_State * L); int createStaticBox(lua_State * L); int createSphere(lua_State * L); diff --git a/tests/bowling.lua b/tests/bowling.lua index 562545f..d782bde 100644 --- a/tests/bowling.lua +++ b/tests/bowling.lua @@ -25,6 +25,8 @@ function init() ball:setMass(5) --ball = ag.createSphere(0.4) --ball:setColor(0.2, 0.2, 0.9) + + crate_texture = ag.loadTexture("crate.png") end function mousebutton_down_event(button) @@ -59,4 +61,6 @@ function update_overlay_event(width, height) 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) + local rot = ag.elapsedTime() / 10 + ag.drawImage(50, 50, 80, 160, crate_texture, rot) end