Single header, cross-platform openGL framework for drawing 2D squares, circles, triangles, and sprites.
This is a smaller, more lightweight version of my AGL API, which supports a variety of graphics demos. This project supports drawing circles, squares, and triangles using an orthographic projection based on the initial window size. User mouse and keyboard input is also supported.
Here is a code example with corresponding output
#include "tinygl-cpp.h"
using namespace tinygl;
class MyWindow : public Window {
public:
MyWindow(int w, int h) : Window(w, h) {}
void setup() override {
std::cout << "Window size: " << width() << ", " << height() << std::endl;
}
void draw() override {
background(0.2f, 0.2f, 0.2f); // parameters: r, g, b
color(0.5, 1.0, 0.25); // parameters: r, g, b
circle(50, 30, 100); // parameters: x, y, radius
square(300, 150, 200, 50); // parameters: x, y, width, height
color(1, 0, 0, 0.5);
triangle(350, 150, 50, 100);
color(1, 0, 1, 0.5);
ellipsoid(150, 350+25*sin(elapsedTime()), 100, 100);
}
};
int main() {
MyWindow window(500, 500);
window.run();
}
This example shows sprites with user input.
#include "tinygl-cpp.h"
using namespace tinygl;
class MyWindow : public Window {
private:
struct Point { float x; float y; };
public:
MyWindow(int w, int h) : Window(w, h) {}
void setup() override {
loadSprite("particle", "../sprites/particle.png");
loadSprite("star", "../sprites/star5.png");
float dx = width() / (float)numParticles;
for (int i = 0; i < numParticles; i++) {
float x = i * dx;
float y = height() * abs(sin(x));
particles.push_back(Point{x, y});
}
}
void draw() override {
background(0.2f, 0.2f, 0.2f); // parameters: r, g, b
color(0.5, 1.0, 1.0); // parameters: r, g, b
for (int i = 0; i < numParticles; i++) {
sprite("particle", particles[i].x, particles[i].y, 0.25);
particles[i].y = fmod(particles[i].y + 200 * dt(), (float)height());
}
color(1.0, 1.0, 0.0); // parameters: r, g, b
float speed = 200;
if (keyIsDown(GLFW_KEY_LEFT)) {
p.x = std::max(0.0f, p.x - speed*dt());
}
else if (keyIsDown(GLFW_KEY_RIGHT)) {
p.x = std::min((float)width(), p.x + speed*dt());
}
sprite("star", p.x, p.y, 0.5); // parameters: x, y, scale
}
protected:
std::vector<Point> particles;
Point p = Point{250, 400};
int numParticles = 50;
};
int main() {
MyWindow window(500, 500);
window.run();
}
All functionality is implemented in the class Window. Just include the header file, #include "tinygl-cpp.h"
to use it.
This project relies on OpenGL 4.0, GLEW, and GLFW builds using CMake.
Windows
Using Visual Studio Community 2019, you can generate the build files from the Git bash
terminal.
$ git clone <this repository>
$ cd tinygl-cpp
$ mkdir build
$ cd build
$ cmake ..
$ start tinygl-cpp.sln
The final command opens visual studio, where you can build and run the same program. The dependencies (GLEW and GLFW) are included in this repository.
MacOS
On macOS, you should have git and a C++ compiler from running command xcode-select --install
. To install cmake, glew, and glfw, you can use brew.
brew install glew
brew install glfw3
To build, open terminal and execute
$ git clone <this repository>
$ cd tinygl-cpp
$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./tinygl-example
Ubuntu
On Ubuntu, you will need to install openGL, glew and glfw.
sudo apt-get install libglew-dev
sudo apt-get install mesa-utils
sudo apt-get install libglfw3-dev
To build, open terminal and execute
$ git clone <this repository>
$ cd tinygl-cpp
$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./tinygl-example