``Intro`` ==================== The purpose of this document is to provide a quick example of how to use the ``Sista`` library. I will use this `sista.cpp `_ file as the example and I will provide a walkthrough. ``Installation`` -------------------- To use the ``Sista`` library, you can either include it as a source library or install it as a shared library. In the following example we illustrate the latter method. .. code-block:: bash git clone https://github.com/FLAK-ZOSO/Sista cd Sista sudo make install This will install the ``Sista`` library in your system library path, allowing you to link against it when compiling your project. Since v2.2.0, Sista is available as a shared library on Windows using MinGW. To install it, you can use the following commands in MinGW or MSYS2: .. code-block:: bash git clone https://github.com/FLAK-ZOSO/Sista cd Sista # Make sure to run this in an elevated shell (as administrator) since 'sudo' is not available in MinGW/MSYS2 make install If you want to use it as a source library, you can simply copy the ``include/sista/`` directory to your project source directory. ``Include`` -------------------- The first thing you need to do is include the ``Sista`` library. The header file you need to include is ``sista.hpp``. If you installed the library as a shared library, you can include it like this: .. code-block:: cpp #include Otherwise, if you are using it as a source library, you can include it like this: .. code-block:: cpp #include "include/sista/sista.hpp" Provided that you have the ``include/sista/`` directory in your project's source directory. ``Namespace`` -------------------- If you want you can use the ``sista`` namespace [#]_. .. code-block:: cpp using namespace sista; ``Input/Output`` -------------------- The next thing the program does is to unsync the C++ standard input/output streams from the C standard streams. .. code-block:: cpp std::ios_base::sync_with_stdio(false); This line will make I/O faster since we only use C++ standard input/output. You can read more about it `on Stack Overflow `_. .. code-block:: cpp sista::resetAnsi(); // Reset the settings This line of code will reset the ANSI settings of the terminal. .. code-block:: cpp std::cout << HIDE_CURSOR; This line of code will hide the cursor to reduce that noisy flickering. ℹ️ - You don't need to do this explicitly since the ``sista::Field`` class includes a private ``sista::Cursor`` object that will hide the cursor when the constructor is called. .. code-block:: cpp sista::clearScreen(); The ``clearScreen()`` [#]_ function will clear the screen and the scrollback buffer, and move the cursor to the top left corner. ℹ️ - You don't need to do this explicitly since the ``sista::Field`` class includes a private ``sista::Cursor`` object that will call ``sista::clearScreen()``. ``Pawn`` -------------------- The next thing to do is to create a ``std::vector>`` object as a list of the Pawns. You can read more about ``std::shared_ptr`` `here `_. .. code-block:: cpp std::vector> pawns; The ``Pawn`` is allocated automatically by using `std::make_shared `_, and the ``std::shared_ptr`` will take care of deallocating the memory when it is no longer needed. .. code-block:: cpp pawns = { std::make_shared( 'X', sista::Coordinates(0, 0), sista::ANSISettings( sista::ForegroundColor::RED, sista::BackgroundColor::BLACK, sista::Attribute::BRIGHT ) ) // You can add more pawns here }; This line of code will add a ``Pawn`` object with the following properties: - ``Character``: ``'X'`` - ``Coordinates``: ``0, 0`` - ``ANSI Settings``: ``Foreground Color``: ``Red``, ``Background Color``: ``Black``, ``Attribute``: ``Bright`` ``Border`` -------------------- The next thing to do is to create a ``Border`` object. .. code-block:: cpp sista::Border border( ' ', sista::ANSISettings( sista::ForegroundColor::BLACK, sista::BackgroundColor::WHITE, sista::Attribute::BRIGHT ) ); The ``Border`` is allocated on the stack, so you don't need to use the ``new`` keyword to create it. I do so because I don't need to use the ``Border`` object outside of the ``main()`` function. This line of code will create a ``Border`` object with the following properties: - ``Character``: ``' '`` (Space) - ``ANSI Settings``: ``Foreground Color``: ``Black``, ``Background Color``: ``White``, ``Attribute``: ``Bright`` ``Field`` -------------------- The next thing to do is to create a ``Field`` object. In this case, I will use the ``sista::SwappableField`` class, which is a subclass of the ``sista::Field`` class that allows you to swap pawns in case of apparent collisions. .. code-block:: cpp sista::SwappableField field(TEST_SIZE, TEST_SIZE); In this case I am creating a ``sista::SwappableField`` object with the following - in order - properties: - ``Width``: ``50`` - ``Height``: ``50`` Now that we have created the ``Field`` object, we can add the ``Pawn*`` to it. .. code-block:: cpp for (auto pawn : pawns) field.addPawn(pawn); This line of code will add the ``pawns`` to the ``Field`` object at the ``pawn->coordinates`` coordinates. .. code-block:: cpp std::vector coords(pawns.size()); This line of code will create a ``std::vector`` object with the same size as the ``pawns`` object, to precalculate the coordinates and then assign them. ``Main Loop`` -------------------- The next thing to do is to create the main loop to test the ``SwappableField`` object and the ``Pawn`` movement. .. code-block:: cpp field.print(border); First of all, we need to print the ``Field`` object with the ``Border`` object. .. code-block:: cpp for (int i=0; i`_. .. code-block:: bash make ``Execution`` -------------------- In BASH or any other UNIX-like shell, you can execute the program like this: .. code-block:: bash ./sista On Windows it may depend on the shell, but it is assumed that whoever reaches this point in the documentation is aware of how to launch an executable from PowerShell. .. code-block:: bash sista.exe ``Notes`` ==================== .. [#] In the example the namespace is always specified for clarity .. [#] The ``clearScreen()`` function was OS-specific and only worked on ``Windows`` until ``v0.5.0`` when it became cross-platform