Skip to content

Commit

Permalink
Add drawSquareOutline
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Feb 12, 2025
1 parent 987f522 commit 6e17a89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/jngl/shapes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void drawRect(const Mat3& modelview, Vec2 size, Rgb);
/// Use setAlpha to set the opacity.
void drawRect(Mat3 modelview, Vec2 size, Rgba color);

/// Draws a square of size 1x1 centered at (0, 0) with the specified color
/// Draws a (filled) square of size 1x1 centered at (0, 0) with the specified color
///
/// By squaling the modelview matrix you can change the size of the square, effectively turning it
/// into a rectangle:
Expand All @@ -151,6 +151,17 @@ void drawRect(Mat3 modelview, Vec2 size, Rgba color);
/// \endcode
void drawSquare(const Mat3& modelview, Rgba color);

/// Draws the outline of a square (so □ instead of ■) of size 1x1 centered at (0, 0) with the
/// specified color
///
/// By squaling the modelview matrix you can change the size of the square, effectively turning it
/// into a rectangle:
/// \code
/// // draws a rectangle at (12 - 56 / 2, 34 - 78 / 2) with a size of 56x78 in red:
/// drawSquareOutline(jngl::modelview().translate(12, 34).scale(56, 78), 0xff0000ff_rgba, 2.f);
/// \endcode
void drawSquareOutline(Mat3 modelview, float lineWidth, Rgba color);

template <class Vect> void drawRect(Vect pos, Vect size) {
drawRect(pos.x, pos.y, size.x, size.y);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,14 @@ void drawSquare(const Mat3& modelview, Rgba color) {
pWindow->drawSquare(modelview, color);
}

void drawSquareOutline(Mat3 modelview, float lineWidth, Rgba color) {
glLineWidth(lineWidth);
pWindow->drawLine(modelview.translate({ -.5, -.5 }), { 1, 0 }, color);
pWindow->drawLine(modelview, { 0, 1 }, color);
pWindow->drawLine(modelview.translate({ 1, 1 }), { 0, -1 }, color);
pWindow->drawLine(modelview, { -1, 0 }, color);
}

void drawTriangle(const Vec2 a, const Vec2 b, const Vec2 c) {
ShaderCache::handle().drawTriangle(a, b, c);
}
Expand Down

0 comments on commit 6e17a89

Please sign in to comment.