X-Git-Url: http://git.localhorst.tv/?p=space.git;a=blobdiff_plain;f=src%2Fgraphics%2Fprimitive.cpp;h=f8b5863c68db7ffe12941cdfb9ea254ee2dd8f8a;hp=1a68d45a9a8a16acfcd5843af60fce1242f35119;hb=a1fd5c4181db1da990b6280892eb1b9f31b73871;hpb=2d0a41dc0a53915153ceccda914d59affd388864 diff --git a/src/graphics/primitive.cpp b/src/graphics/primitive.cpp index 1a68d45..f8b5863 100644 --- a/src/graphics/primitive.cpp +++ b/src/graphics/primitive.cpp @@ -164,4 +164,86 @@ void Grid( } } +void Grid2( + SDL_Surface *dst, + Vector first, + Vector second, + Vector size, + Vector n, + Color color1, + Color color2) { + Uint32 c1 = color1.MapRGBA(dst); + Uint32 c2 = color2.MapRGBA(dst); + + Vector from = min(first, second); + Vector to = max(first, second); + + if (size.x <= 1 || size.y <= 1) { + FillRect(dst, from, to - from, c1); + Grid(dst, from, to, size * n, color2); + return; + } + + if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) { + return; + } + + Vector i(0, 0); + while (from.x < -size.x) { from.x += size.x; ++i.x; } + while (from.y < -size.y) { from.y += size.y; ++i.y; } + while (to.x > dst->w + size.x) to.x -= size.x; + while (to.y > dst->h + size.y) to.y -= size.y; + + int width = to.x - from.x; + int height = to.y - from.y; + + for (Vector pos(from); pos.x <= to.x; pos.x += size.x) { + VLine(dst, pos, height, (i.x++ % n.x) ? c1 : c2); + } + for (Vector pos(from); pos.y <= to.y; pos.y += size.y) { + HLine(dst, pos, width, (i.y++ % n.y) ? c1 : c2); + } +} + +void Grid2( + SDL_Surface *dst, + Vector first, + Vector second, + Vector size, + Vector n, + Color color1, + Color color2) { + Uint32 c1 = color1.MapRGBA(dst); + Uint32 c2 = color2.MapRGBA(dst); + + Vector from = min(first, second); + Vector to = max(first, second); + + if (size.x <= 1 || size.y <= 1) { + FillRect(dst, from, to - from, c1); + Grid(dst, from, to, size * Vector(n), color2); + return; + } + + if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) { + return; + } + + Vector i(0, 0); + while (from.x < -size.x) { from.x += size.x; ++i.x; } + while (from.y < -size.y) { from.y += size.y; ++i.y; } + while (to.x > dst->w + size.x) to.x -= size.x; + while (to.y > dst->h + size.y) to.y -= size.y; + + float width = to.x - from.x; + float height = to.y - from.y; + + for (Vector pos(from); pos.x <= to.x; pos.x += size.x) { + VLine(dst, pos, height, (i.x++ % n.x) ? c1 : c2); + } + for (Vector pos(from); pos.y <= to.y; pos.y += size.y) { + HLine(dst, pos, width, (i.y++ % n.y) ? c1 : c2); + } +} + }