diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 485f16704..15f2defb6 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -1876,6 +1876,8 @@ void LuaScriptInterface::initGraphicsAPI() {"drawLine", graphics_drawLine}, {"drawRect", graphics_drawRect}, {"fillRect", graphics_fillRect}, + {"drawCircle", graphics_drawCircle}, + {"fillCircle", graphics_fillCircle}, {NULL, NULL} }; luaL_register(l, "graphics", graphicsAPIMethods); @@ -1953,11 +1955,11 @@ int LuaScriptInterface::graphics_drawLine(lua_State * l) int LuaScriptInterface::graphics_drawRect(lua_State * l) { - int x, y, w, h, r, g, b, a; + int x, y, rx, ry, r, g, b, a; x = lua_tointeger(l, 1); y = lua_tointeger(l, 2); - w = lua_tointeger(l, 3); - h = lua_tointeger(l, 4); + rx = lua_tointeger(l, 3); + ry = lua_tointeger(l, 4); r = luaL_optint(l, 5, 255); g = luaL_optint(l, 6, 255); b = luaL_optint(l, 7, 255); @@ -1971,11 +1973,35 @@ int LuaScriptInterface::graphics_drawRect(lua_State * l) if (b>255) b = 255; if (a<0) a = 0; if (a>255) a = 255; - luacon_g->drawrect(x, y, w, h, r, g, b, a); + luacon_g->drawrect(x, y, rx, ry, r, g, b, a); return 0; } int LuaScriptInterface::graphics_fillRect(lua_State * l) +{ + int x, y, rx, ry, r, g, b, a; + x = lua_tointeger(l, 1); + y = lua_tointeger(l, 2); + rx = lua_tointeger(l, 3); + ry = lua_tointeger(l, 4); + r = luaL_optint(l, 5, 255); + g = luaL_optint(l, 6, 255); + b = luaL_optint(l, 7, 255); + a = luaL_optint(l, 8, 255); + + if (r<0) r = 0; + if (r>255) r = 255; + if (g<0) g = 0; + if (g>255) g = 255; + if (b<0) b = 0; + if (b>255) b = 255; + if (a<0) a = 0; + if (a>255) a = 255; + luacon_g->fillrect(x, y, rx, ry, r, g, b, a); + return 0; +} + +int LuaScriptInterface::graphics_drawCircle(lua_State * l) { int x, y, w, h, r, g, b, a; x = lua_tointeger(l, 1); @@ -1995,7 +2021,31 @@ int LuaScriptInterface::graphics_fillRect(lua_State * l) if (b>255) b = 255; if (a<0) a = 0; if (a>255) a = 255; - luacon_g->fillrect(x, y, w, h, r, g, b, a); + luacon_g->drawcircle(x, y, w, h, r, g, b, a); + return 0; +} + +int LuaScriptInterface::graphics_fillCircle(lua_State * l) +{ + int x, y, w, h, r, g, b, a; + x = lua_tointeger(l, 1); + y = lua_tointeger(l, 2); + w = lua_tointeger(l, 3); + h = lua_tointeger(l, 4); + r = luaL_optint(l, 5, 255); + g = luaL_optint(l, 6, 255); + b = luaL_optint(l, 7, 255); + a = luaL_optint(l, 8, 255); + + if (r<0) r = 0; + if (r>255) r = 255; + if (g<0) g = 0; + if (g>255) g = 255; + if (b<0) b = 0; + if (b>255) b = 255; + if (a<0) a = 0; + if (a>255) a = 255; + luacon_g->fillcircle(x, y, w, h, r, g, b, a); return 0; } diff --git a/src/cat/LuaScriptInterface.h b/src/cat/LuaScriptInterface.h index fc3f41a4e..9b5468852 100644 --- a/src/cat/LuaScriptInterface.h +++ b/src/cat/LuaScriptInterface.h @@ -109,6 +109,8 @@ class LuaScriptInterface: public CommandInterface static int graphics_drawLine(lua_State * l); static int graphics_drawRect(lua_State * l); static int graphics_fillRect(lua_State * l); + static int graphics_drawCircle(lua_State * l); + static int graphics_fillCircle(lua_State * l); void initFileSystemAPI(); static int fileSystem_list(lua_State * l); diff --git a/src/graphics/Graphics.h b/src/graphics/Graphics.h index 3a87bb665..2cbdd8728 100644 --- a/src/graphics/Graphics.h +++ b/src/graphics/Graphics.h @@ -239,6 +239,8 @@ public: void draw_line(int x, int y, int x2, int y2, int r, int g, int b, int a); void drawrect(int x, int y, int width, int height, int r, int g, int b, int a); void fillrect(int x, int y, int width, int height, int r, int g, int b, int a); + void drawcircle(int x, int y, int rx, int ry, int r, int g, int b, int a); + void fillcircle(int x, int y, int rx, int ry, int r, int g, int b, int a); void clearrect(int x, int y, int width, int height); void gradientrect(int x, int y, int width, int height, int r, int g, int b, int a, int r2, int g2, int b2, int a2); diff --git a/src/graphics/OpenGLDrawMethods.inl b/src/graphics/OpenGLDrawMethods.inl index bfc338a77..2bba731f4 100644 --- a/src/graphics/OpenGLDrawMethods.inl +++ b/src/graphics/OpenGLDrawMethods.inl @@ -314,6 +314,47 @@ void PIXELMETHODS_CLASS::fillrect(int x, int y, int width, int height, int r, in glEnd(); } +void PIXELMETHODS_CLASS::drawcircle(int x, int y, int rx, int ry, int r, int g, int b, int a) +{ + int yTop = ry, yBottom, i, j; + for (i = 0; i <= rx; i++) { + yBottom = yTop; + while (pow(i-rx,2.0)*pow(ry,2.0) + pow(yTop-ry,2.0)*pow(rx,2.0) <= pow(rx,2.0)*pow(ry,2.0)) + yTop++; + if (yBottom != yTop) + yTop--; + for (int j = yBottom; j <= yTop; j++) + { + blendpixel(x+i, y+j, r, g, b, a); + if (i != rx) + blendpixel(x+2*rx-i, y+j, r, g, b, a); + if (j != ry) + { + blendpixel(x+i, y+2*ry-j, r, g, b, a); + if (i != rx) + blendpixel(x+2*rx-i, y+2*ry-j, r, g, b, a); + } + } + } +} + +void PIXELMETHODS_CLASS::fillcircle(int x, int y, int rx, int ry, int r, int g, int b, int a) +{ + int yTop = ry+1, yBottom, i, j; + for (i = 0; i <= rx; i++) + { + while (pow(i-rx,2.0)*pow(ry,2.0) + pow(yTop-ry,2.0)*pow(rx,2.0) <= pow(rx,2.0)*pow(ry,2.0)) + yTop++; + yBottom = 2*ry - yTop; + for (int j = yBottom+1; j < yTop; j++) + { + blendpixel(x+i, y+j, r, g, b, a); + if (i != rx) + blendpixel(x+2*rx-i, y+j, r, g, b, a); + } + } +} + void PIXELMETHODS_CLASS::gradientrect(int x, int y, int width, int height, int r, int g, int b, int a, int r2, int g2, int b2, int a2) { glBegin(GL_QUADS); diff --git a/src/graphics/RasterDrawMethods.inl b/src/graphics/RasterDrawMethods.inl index b4de87554..c53c0a88f 100644 --- a/src/graphics/RasterDrawMethods.inl +++ b/src/graphics/RasterDrawMethods.inl @@ -356,6 +356,47 @@ void PIXELMETHODS_CLASS::fillrect(int x, int y, int w, int h, int r, int g, int blendpixel(x+i, y+j, r, g, b, a); } +void PIXELMETHODS_CLASS::drawcircle(int x, int y, int rx, int ry, int r, int g, int b, int a) +{ + int yTop = ry, yBottom, i, j; + for (i = 0; i <= rx; i++) { + yBottom = yTop; + while (pow(i-rx,2.0)*pow(ry,2.0) + pow(yTop-ry,2.0)*pow(rx,2.0) <= pow(rx,2.0)*pow(ry,2.0)) + yTop++; + if (yBottom != yTop) + yTop--; + for (int j = yBottom; j <= yTop; j++) + { + blendpixel(x+i, y+j, r, g, b, a); + if (i != rx) + blendpixel(x+2*rx-i, y+j, r, g, b, a); + if (j != ry) + { + blendpixel(x+i, y+2*ry-j, r, g, b, a); + if (i != rx) + blendpixel(x+2*rx-i, y+2*ry-j, r, g, b, a); + } + } + } +} + +void PIXELMETHODS_CLASS::fillcircle(int x, int y, int rx, int ry, int r, int g, int b, int a) +{ + int yTop = ry+1, yBottom, i, j; + for (i = 0; i <= rx; i++) + { + while (pow(i-rx,2.0)*pow(ry,2.0) + pow(yTop-ry,2.0)*pow(rx,2.0) <= pow(rx,2.0)*pow(ry,2.0)) + yTop++; + yBottom = 2*ry - yTop; + for (int j = yBottom+1; j < yTop; j++) + { + blendpixel(x+i, y+j, r, g, b, a); + if (i != rx) + blendpixel(x+2*rx-i, y+j, r, g, b, a); + } + } +} + void PIXELMETHODS_CLASS::gradientrect(int x, int y, int width, int height, int r, int g, int b, int a, int r2, int g2, int b2, int a2) { diff --git a/src/graphics/Renderer.h b/src/graphics/Renderer.h index 92510069e..8a7120fa6 100644 --- a/src/graphics/Renderer.h +++ b/src/graphics/Renderer.h @@ -126,6 +126,8 @@ public: void draw_line(int x, int y, int x2, int y2, int r, int g, int b, int a); void drawrect(int x, int y, int width, int height, int r, int g, int b, int a); void fillrect(int x, int y, int width, int height, int r, int g, int b, int a); + void drawcircle(int x, int y, int rx, int ry, int r, int g, int b, int a); + void fillcircle(int x, int y, int rx, int ry, int r, int g, int b, int a); void clearrect(int x, int y, int width, int height); void gradientrect(int x, int y, int width, int height, int r, int g, int b, int a, int r2, int g2, int b2, int a2);