descriptionC++11 library for turtle graphics using SFML
homepage URLhttps://github.com/vrmiguel/taruga
repository URLhttps://github.com/vrmiguel/taruga.git
ownervrmiguel99@gmail.com
last changeTue, 3 Nov 2020 18:32:37 +0000 (3 15:32 -0300)
last refreshSat, 27 Apr 2024 08:53:46 +0000 (27 10:53 +0200)
content tags
add:
README.md

taruga Codacy Badge CodeFactor

Taruga is a work in progress single-header turtle graphics library written in C++11.

Spiral

Examples

Drawing a Sierpinski fractal

Sierpinski

With the simple Turtle directives, we can build complex fractals!

Full source code for this example can be found here.

Drawing a spiral

Spiral

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.

Using colors

Colors

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.

Lifting the pen up, drawing regular polygons

Regular polygons

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.

Drawing a Koch fractal

Koch

Full source code for this example can be found here.

Dependencies and how to build

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.

Usage

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.

Licensing

The code contained in this repo is MIT-licensed.

Turtle icon made by Freepik, available under the Flaticon license.

SFML is zlib/libpng-licensed.

shortlog
2020-11-03 Vinícius R... Better docs, remove WIP Taruga interpreter codemain
2020-10-13 Vinícius R... Use std::fabs instead of std::abs
2020-10-13 Vinícius R... Merge remote-tracking branch 'origin/main' into main
2020-10-13 Vinícius R... Very initial interpreter work
2020-10-12 Vinícius MiguelMerge pull request #2 from lucasssvaz/patch-2
2020-10-12 Vinícius MiguelFix typo
2020-10-12 Lucas Saavedra VazUpdate taruga.hpp
2020-10-12 Vinícius R... Add manual setting of rotation and walking speeds,...
2020-10-12 Vinícius R... Add pop_state and push_state
2020-10-12 Vinícius R... Three more examples, now using floats to store current...
2020-10-12 Vinícius R... New example
2020-10-12 Vinícius R... Update README
2020-10-11 Vinícius R... Screenshot support, another example added
2020-10-11 Vinícius R... Add line color change, pen movement and a new example
2020-10-11 Vinícius R... Some examples, add GIF to README
2020-10-11 Vinícius R... Initial commit
...
heads
3 years ago main