Taruga is a work in progress single-header turtle graphics library written in C++11.
With the simple Turtle directives, we can build complex fractals!
Full source code for this example can be found here.
We can draw this spiral by simply moving forward and then turning to the right by slightly over 90°.
int size = 1;
while (size <= 500)
{
turtle.forward(size++);
turtle.turn_right(91);
}
Full source code for this example can be found here.
We can change around the color of the lines drawn by using Turtle::set_line_color
, to which you can supply either an RGB triplet (by individual values or in an array) or a sf::Color.
std::vector<sf::Color> colors = {sf::Color::Red, sf::Color::Blue, sf::Color::Green, sf::Color::Yellow};
for(auto color : colors)
{
turtle.set_line_color(color);
turtle.forward(70);
turtle.turn_left(90);
}
Full source code for this example can be found here.
We can set the turtle's pen up or down. This allows us to move the turtle without having to draw the line of its path.
Full source code for this example can be found here.
Full source code for this example can be found here.
The only dependencies are a C++11 conformant compiler and SMFL, a small multimedia API with an installed size of around 10MB.
Taruga has been built on SFML 2.5.1 and has also been tested on SFML 2.4.1. It may work on older versions of the library, but there are no guarantees.
On Ubuntu-based distributions, you can install SFML with:
sudo apt install libsfml-dev
On Fedora, with:
dnf install SFML
You can then build it g++ -O3 -std=c++11 your_program.cpp -lsfml-graphics -lsfml-window -lsfml-system
.
Taruga exposes a single namespace, contained within a single header file (taruga.hpp
).
The public methods that taruga::Turtle
exposes are:
void set_window_title(const char* new_title);
Turtle(); //! Default constructor. Makes a window size of 800x600 and sets the turtle to (400, 300).
Turtle(uint16_t width, uint16_t height); //! Makes a window size of width * height and sets the turtle (width/2, height/2).
explicit Turtle(const sf::Vector2f& p); //! Makes a window size of p.x * p.y and sets the turtle (p.x/2, p.y/2).
void save_to_image(const char *); //! Saves the current window view to an image with the given filename
void set_line_color(sf::Color); //! Sets the new color of the line the turtle will draw.
void set_line_color(u8, u8, u8); //! Sets the new color of the line the turtle will draw.
void set_line_color(u8[3]); //! Sets the new color of the line the turtle will draw.
void pen_up(); //! Sets the pen up so lines don't get drawn
void pen_down(); //! Sets the pen down so that the turtle draws a line wherever it walks
void set_icon(Icon); //! Allows to switch around between the two built-in icons: turtle or straight arrow.
void set_icon(sf::Texture); //! Allows for any image to be used as an icon. Do notice that Taruga won't scale the texture. If needed, use the Turtle::scale method.
void scale(float, float); //! Scales the turtle sprite
void forward(float units); //! Walk forward the given amount of units.
void backwards(float units); //! Walk backwards the given amount of units. The same as using forward() with a negative parameter.
void turn_right(float ang); //! Turns right by the specified amount of degrees.
void set_walking_speed(float); //! Sets a new walking speed
void set_rotation_speed(float); //! Sets a new rotation speed
void push_state(); //! Saves the current state in a stack
void pop_state(); //! Returns the Turtle's state to the top of the state stack
void turn_left(float ang); //! Turns left by the specified amount of degrees.
void act(); //! Start moving the turtle. Will deplete the actions queue.
The code contained in this repo is MIT-licensed.
Turtle icon made by Freepik, available under the Flaticon license.
SFML is zlib/libpng-licensed.