Skip to content

Commit

Permalink
Add jngl::drawSquare and simplify drawRect implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Nov 27, 2024
1 parent e5cee5c commit e5b174e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 46 deletions.
12 changes: 11 additions & 1 deletion src/jngl/shapes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,17 @@ void drawRect(const Mat3& modelview, Vec2 size, Rgb);
/// Draws a rectangle spawning from (0, 0) to (size.x, size.y) with the specified color
///
/// Use setAlpha to set the opacity.
void drawRect(const Mat3& modelview, Vec2 size, Rgba color);
void drawRect(Mat3 modelview, Vec2 size, Rgba color);

/// Draws a 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:
/// \code
/// // draws a rectangle at (12 - 56 / 2, 34 - 78 / 2) with a size of 56x78 in red:
/// drawSquare(jngl::modelview().translate(12, 34).scale(56, 78), 0xff0000ff_rgba);
/// \endcode
void drawSquare(const Mat3& modelview, Rgba color);

template <class Vect> void drawRect(Vect pos, Vect size) {
drawRect(pos.x, pos.y, size.x, size.y);
Expand Down
14 changes: 9 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,19 +563,23 @@ void popMatrix() {

void drawRect(const double xposition, const double yposition, const double width,
const double height) {
pWindow->drawRect(Vec2{ xposition, yposition }, { width, height });
drawRect(Vec2{ xposition, yposition }, { width, height });
}

void drawRect(const Vec2 position, const Vec2 size) {
pWindow->drawRect(position, size);
pWindow->drawSquare(modelview().translate(position + size / 2).scale(size), gShapeColor);
}

void drawRect(const Mat3& modelview, const Vec2 size, const Rgb color) {
pWindow->drawRect(modelview, size, Rgba(color, Alpha(gShapeColor.getAlpha())));
drawRect(modelview, size, Rgba(color, Alpha(gShapeColor.getAlpha())));
}

void drawRect(const Mat3& modelview, const Vec2 size, const Rgba color) {
pWindow->drawRect(modelview, size, color);
void drawRect(Mat3 modelview, const Vec2 size, const Rgba color) {
pWindow->drawSquare(modelview.translate(size / 2).scale(size), color);
}

void drawSquare(const Mat3& modelview, Rgba color) {
pWindow->drawSquare(modelview, color);
}

void drawTriangle(const Vec2 a, const Vec2 b, const Vec2 c) {
Expand Down
17 changes: 16 additions & 1 deletion src/unittest/shapesTest.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2022-2023 Jan Niklas Hasse <[email protected]>
// Copyright 2022-2024 Jan Niklas Hasse <[email protected]>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
#include "Fixture.hpp"

#include <boost/ut.hpp>
#include <jngl/matrix.hpp>
#include <jngl/shapes.hpp>
#include <jngl/Rgba.hpp>

namespace {
boost::ut::suite _ = [] {
Expand All @@ -28,6 +29,20 @@ boost::ut::suite _ = [] {
▒░░░░░░░░░░▓▓▓▓▓▓▓▓▓▓███████████
▒ ░░░░░░░░░░███████████
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒███████████
)")));
}
for (double scaleFactor : { 1, 2 }) {
Fixture f(scaleFactor);
auto mv = jngl::modelview();
jngl::drawSquare(mv.translate({-100, 0}).scale(42), 0x111111ff_rgba);
expect(eq(f.getAsciiArt(), std::string(R"(
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▒ ▒▒▒▒ ▒
▒ ████ ▒
▒ ████ ▒
▒ ████ ▒
▒ ▒▒▒▒ ▒
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
)")));
}
};
Expand Down
42 changes: 7 additions & 35 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,11 @@ void Window::initGlObjects() {
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);

glGenVertexArrays(1, &vaoRect);
glBindVertexArray(vaoRect);
glGenVertexArrays(1, &vaoSquare);
glBindVertexArray(vaoSquare);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
const static float rect[] = { 0, 0, 1, 0, 1, 1, 0, 1 };
const static float rect[] = { -.5, -.5, .5, -.5, .5, .5, -.5, .5 };
glBufferData(GL_ARRAY_BUFFER, sizeof(rect), rect, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
Expand All @@ -564,41 +564,13 @@ void Window::drawTriangle(const Vec2 a, const Vec2 b, const Vec2 c) {

void Window::drawLine(Mat3 modelview, const Vec2 b) const {
glBindVertexArray(vaoLine);
auto tmp =
useSimpleShaderProgram(modelview.scale(static_cast<float>(b.x * jngl::getScaleFactor()),
static_cast<float>(b.y * jngl::getScaleFactor())),
gShapeColor);
auto tmp = useSimpleShaderProgram(modelview.scale(b * getScaleFactor()), gShapeColor);
glDrawArrays(GL_LINES, 0, 2);
}

void Window::drawRect(const Vec2 pos, const Vec2 size) const {
glBindVertexArray(vaoRect);
pushMatrix();
translate(pos);
opengl::scale(static_cast<float>(size.x * getScaleFactor()),
static_cast<float>(size.y * getScaleFactor()));
auto tmp = useSimpleShaderProgram();
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
popMatrix();
}

void Window::drawRect(Mat3 modelview, const Vec2 size, Rgba color) const {
glBindVertexArray(vaoRect);
auto context = jngl::simpleShaderProgram->use();
glUniform4f(simpleColorUniform, color.getRed(), color.getGreen(), color.getBlue(),
color.getAlpha());
glUniformMatrix3fv(
simpleModelviewUniform, 1, GL_FALSE,
modelview.scale(size.x * jngl::getScaleFactor(), size.y * jngl::getScaleFactor()).data);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}

void Window::drawRect(Mat3 modelview, const Vec2 size) const {
glBindVertexArray(vaoRect);
auto tmp = useSimpleShaderProgram(
modelview.scale(size.x * jngl::getScaleFactor(), size.y * jngl::getScaleFactor()),
gShapeColor);
void Window::drawSquare(Mat3 modelview, Rgba color) const {
glBindVertexArray(vaoSquare);
auto context = useSimpleShaderProgram(modelview.scale(getScaleFactor()), color);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}

Expand Down
6 changes: 2 additions & 4 deletions src/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ class Window {
void initGlObjects();
static void drawTriangle(Vec2 a, Vec2 b, Vec2 c);
void drawLine(Mat3 modelview, Vec2 b) const;
void drawRect(Vec2 pos, Vec2 size) const;
void drawRect(Mat3 modelview, Vec2 size, Rgba color) const;
void drawRect(Mat3 modelview, Vec2 size) const;
void drawSquare(Mat3 modelview, Rgba color) const;
void onControllerChanged(std::function<void()>);

friend class WindowImpl;
Expand All @@ -135,7 +133,7 @@ class Window {
double timePerStep = 1.0 / 60.0;
double mouseWheel = 0;
GLuint vaoLine = 0;
GLuint vaoRect = 0;
GLuint vaoSquare = 0;
unsigned int maxStepsPerFrame = 3;
bool running = true;
bool fullscreen_;
Expand Down

0 comments on commit e5b174e

Please sign in to comment.