added ag::drawImage()

git-svn-id: svn://anubis/anaglym/trunk@185 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-11-17 04:38:39 +00:00
parent f410c305c2
commit 5de4ed348b
5 changed files with 115 additions and 28 deletions

View File

@ -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();
}

View File

@ -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);

51
ag.cc
View File

@ -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;

4
ag.h
View File

@ -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);

View File

@ -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