Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header segregated #10

Merged
merged 7 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions Benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ The following benchmarks were run on...
- Windows 11 Pro `22H2`
- 64-bit OS, x64-based processor

From version `1.0.0` the benchmarks were run on...

- Acer Aspire E5-573G
- Intel core `i7-5500U` CPU `3.00GHz`
- `8GB` RAM
- Ubuntu 22.04.3 LTS x86_64

## v`0.1.0`

### `sista.exe` I - 7/12/2022
Expand Down Expand Up @@ -114,7 +121,7 @@ The following test was run with...

- `50 x 50` Field size (`2500` cells)
- `1000` = `1000` iterations
- `50` `Pawn` instances (allocated on heap with `new`)
- `6` `Pawn` instances (allocated on heap with `new`)
- `10 milliseconds` per frame (`100 FPS`)

...and the following numeric results were obtained...
Expand All @@ -129,4 +136,40 @@ The following test was run with...

- The refresh is smooth (actually there's no refresh)
- No flickering at all (there's no more refresh)
- Not a single `Pawn` movement is snappy (due to the fact that they're at the same x coordinate)
- Not a single `Pawn` movement is snappy (due to the fact that they're at the same x coordinate)

## v`1.0.0`

### [`header-test`](https://github.com/FLAK-ZOSO/Sista/blob/main/demo/header-test.cpp) - 21/12/2023

The following test was run with...

- `50 x 50` Field size (`2500` cells)
- `50` `Pawn` instances (allocated on heap with `new`)
- `10 milliseconds` per frame (`100 FPS`)
- `1000` = `1000` iterations

...and the following numeric results were obtained...

- `11"` (11 seconds) elapsed time
- `1000 x 0.010` = `10"` slept time
- `1000 x 2500 / 11` = `227272` cells per second
- `header-test` max memory usage: `131KBytes`
- `header-test` max CPU usage: `2.17%` (according to System Monitor)

### `sista` VI - 21/12/2023

The following test was run with...

- `50 x 50` Field size (`2500` cells)
- `6` `Pawn` instances (allocated on heap with `new`)
- `10 milliseconds` per frame (`100 FPS`)
- `1000` = `1000` iterations

...and the following numeric results were obtained...

- `11"` (11 seconds) elapsed time
- `1000 x 0.010` = `10"` slept time
- `1000 x 2500 / 11` = `227272` cells per second
- `sista` max memory usage: `262KBytes`
- `sista` max CPU usage: `0.26%` (according to System Monitor)
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Makefile for sista.cpp
IMPLEMENTATIONS = include/sista/ANSI-Settings.cpp include/sista/border.cpp include/sista/coordinates.cpp include/sista/cursor.cpp include/sista/field.cpp include/sista/pawn.cpp

all:
g++ -std=c++17 -Wall -g -c $(IMPLEMENTATIONS)
g++ -std=c++17 -Wall -g -c sista.cpp
g++ -std=c++17 -Wall -g -o sista sista.o ANSI-Settings.o border.o coordinates.o cursor.o pawn.o field.o
rm -f *.o
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# `Sista`

`Sista` is a *lightweight*, *fast*, and *easy-to-use* *OOP* library for making terminal games in `C++`.
`Sista` is a *header-only* library that provides a set of classes and functions to make it easier to create terminal games in `C++`.
`Sista` provides a set of classes and functions to make terminal games in `C++` easier.

Inspired by their [`Forsiktig`](https://github.com/Lioydiano/Forsiktig)'s [`variables.hpp`](https://github.com/Lioydiano/Forsiktig/blob/main/variables.hpp) header, @FLAK-ZOSO decided to make a library to prevent others from having to reinvent the wheel every time they wanted to make a terminal game.

Expand Down Expand Up @@ -30,3 +30,5 @@ The `"sista.hpp"` header includes all of the other headers in the `"include/sist
Some of the documentation for Sista can be found in the [`Release Notes`](ReleaseNotes.md) and [`Benchmarks`](Benchmarks.md) files.

The english documentation for Sista can be found at [`Sista - ReadTheDocs`](https://sista.readthedocs.io/).

ℹ️ - ReadTheDocs is now asking for a `.readthedocs.yaml` file to be added to the repository, so the documentation is not updated at the moment
19 changes: 19 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,22 @@ void addPrintPawn(Pawn* pawn) { // Add a pawn to the matrix and print it
pawn->print(); // Print the pawn
}
```

## v`1.0.0`

- Divided Sista in `.hpp` headers and `.cpp` sources

This should make it possible to use Sista in projects with multiple source files.

## v`1.1.1`

- Added `sista::Field::erasePawn()` to remove and erase Pawn from the screen

```c++
void Field::erasePawn(Pawn* pawn) { // Erase a pawn from the matrix
removePawn(pawn); // Remove the pawn from the matrix
cursor.set(pawn->getCoordinates()); // Set the cursor to the pawn's coordinates
ANSI::reset(); // Reset the settings for that cell
std::cout << ' '; // Print a space to clear the cell
}
```
7 changes: 7 additions & 0 deletions demo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
IMPLEMENTATIONS = ../include/sista/ANSI-Settings.cpp ../include/sista/border.cpp ../include/sista/coordinates.cpp ../include/sista/cursor.cpp ../include/sista/field.cpp ../include/sista/pawn.cpp

all:
g++ -std=c++17 -Wall -g -c $(IMPLEMENTATIONS)
g++ -std=c++17 -Wall -g -c header-test.cpp
g++ -std=c++17 -Wall -g -o header-test header-test.o ANSI-Settings.o border.o coordinates.o cursor.o pawn.o field.o
rm -f *.o
Binary file added demo/colors256
Binary file not shown.
Binary file added demo/header-test
Binary file not shown.
56 changes: 56 additions & 0 deletions demo/header-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// make
#include "../include/sista/sista.hpp"
#include <chrono>
#include <thread>


ANSI::Settings default_settings(
ANSI::ForegroundColor::F_WHITE,
ANSI::BackgroundColor::B_BLACK,
ANSI::Attribute::RESET
);
sista::Border border(
'#', ANSI::Settings(
ANSI::ForegroundColor::F_WHITE,
ANSI::BackgroundColor::B_BLACK,
ANSI::Attribute::BLINK
)
);


int main() {
sista::SwappableField field(50, 50);
std::vector<sista::Pawn*> pawns;
for (int i = 0; i < 50; i++) {
pawns.push_back(
new sista::Pawn(
'X', {
rand() % 50,
rand() % 50
}, default_settings
)
);
field.addPawn(pawns[i]);
}
field.print(border);

std::vector<sista::Coordinates> coords(pawns.size());
for (int i = 0; i < 1000; i++) {
for (int k = 0; k < pawns.size(); k++) {
coords[k] = field.movingByCoordinates(
pawns[k], rand() % 3 - 1, rand() % 3 - 1, PACMAN_EFFECT
);
}
try {
for (int k = 0; k < pawns.size(); k++) {
field.movePawn(pawns[k], coords[k]);
}
} catch (std::invalid_argument& e) {
for (int k = 0; k < pawns.size(); k++) {
field.addPawnToSwap(pawns[k], coords[k]);
}
field.applySwaps();
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

``Sista`` is a *lightweight*, *fast*, and *easy-to-use* *OOP* library for making terminal games in ``C++``.

``Sista`` is a *header-only* library that provides a set of classes and functions to make it easier to create terminal games in ``C++``.
``Sista`` provides a set of classes and functions to make terminal games in ``C++`` easier.

Inspired by their `Forsiktig <https://github.com/Lioydiano/Forsiktig>`_ 's `variables.hpp <https://github.com/Lioydiano/Forsiktig/blob/main/variables.hpp>`_ header, `FLAK-ZOSO <https://flak-zoso.github.io>`_ decided to make a library to prevent others from having to reinvent the wheel every time they wanted to make a terminal game.

Expand Down
15 changes: 15 additions & 0 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ Since now we'll never going to re-print the ``Field`` object, we'll edit only th

After applying all the movements, we'll swap the characters in the ``stdout`` stream, and then we'll flush the ``stdout`` stream.

``Compilation``
--------------------

To compile the ``sista.cpp`` file, you need to use the following command...

.. code-block:: bash

g++ -std=c++17 -Wall -g -c $(IMPLEMENTATIONS)
g++ -std=c++17 -Wall -g -c sista.cpp
g++ -std=c++17 -Wall -g -o sista sista.o ANSI-Settings.o border.o coordinates.o cursor.o pawn.o field.o
rm -f *.o

...where ``$(IMPLEMENTATIONS)`` is the list of the ``Sista`` library implementation files.


``Notes``
====================

Expand Down
15 changes: 15 additions & 0 deletions docs/sista.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
- ``pawn.hpp``: Pawn
- ``sista.hpp``: main header file, includes all the others

Since `v1.1.0` the library is divided in headers and implementation files.
This fact isn't relevant in terms of importation, but it is in terms of compilation.

.. code-block:: bash

# Makefile for sista.cpp
IMPLEMENTATIONS = include/sista/ANSI-Settings.cpp include/sista/border.cpp include/sista/coordinates.cpp include/sista/cursor.cpp include/sista/field.cpp include/sista/pawn.cpp

all:
g++ -std=c++17 -Wall -g -c $(IMPLEMENTATIONS)
g++ -std=c++17 -Wall -g -c sista.cpp
g++ -std=c++17 -Wall -g -o sista sista.o ANSI-Settings.o border.o coordinates.o cursor.o pawn.o field.o
rm -f *.o


``ANSI-Settings.hpp``
================

Expand Down
62 changes: 62 additions & 0 deletions include/sista/ANSI-Settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "ANSI-Settings.hpp" // ANSI::ForegroundColor, ANSI::BackgroundColor, ANSI::Attribute, ANSI::Settings


namespace ANSI {
void setForegroundColor(ForegroundColor color) {
std::cout << CSI << color << "m";
}
void setBackgroundColor(BackgroundColor color) {
std::cout << CSI << color << "m";
}
void setAttribute(Attribute attribute) {
std::cout << CSI << attribute << "m";
}
void resetAttribute(Attribute attribute) {
if (attribute == Attribute::BRIGHT) {
std::cout << CSI << attribute + 21 << "m";
return;
}
std::cout << CSI << attribute + 20 << "m";
}

void reset() {
setAttribute(Attribute::RESET);
setForegroundColor(ForegroundColor::F_WHITE);
setBackgroundColor(BackgroundColor::B_BLACK);
}

void setForegroundColor(unsigned short int red, unsigned short int green, unsigned short int blue) {
std::cout << CSI << "38;2;" << red << ";" << green << ";" << blue << "m";
}
void setBackgroundColor(unsigned short int red, unsigned short int green, unsigned short int blue) {
std::cout << CSI << "48;2;" << red << ";" << green << ";" << blue << "m";
}
void setForegroundColor(unsigned short int color) {
std::cout << CSI << "38;5;" << color << "m";
}
void setBackgroundColor(unsigned short int color) {
std::cout << CSI << "48;5;" << color << "m";
}

void setScreenMode(ScreenMode mode) {
std::cout << CSI << '=' << mode << 'h';
}
void unsetScreenMode(ScreenMode mode) {
std::cout << CSI << '=' << mode << 'l';
}

Settings::Settings() {
foregroundColor = ForegroundColor::F_WHITE;
backgroundColor = BackgroundColor::B_BLACK;
attribute = Attribute::RESET;
}
Settings::Settings(ForegroundColor foregroundColor_, BackgroundColor backgroundColor_, Attribute attribute_): foregroundColor(foregroundColor_), backgroundColor(backgroundColor_), attribute(attribute_) {}
Settings::Settings(ForegroundColor& foregroundColor_, BackgroundColor& backgroundColor_, Attribute& attribute_, bool _by_reference): foregroundColor(foregroundColor_), backgroundColor(backgroundColor_), attribute(attribute_) {}

void Settings::apply() {
setAttribute(Attribute::RESET);
setAttribute(attribute);
setForegroundColor(foregroundColor);
setBackgroundColor(backgroundColor);
}
};
70 changes: 17 additions & 53 deletions include/sista/ANSI-Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,23 @@ namespace ANSI {
ITALIC = 3,
UNDERSCORE = 4,
BLINK = 5,
RAPID_BLINK = 6,
REVERSE = 7,
HIDDEN = 8,
STRIKETHROUGH = 9
};

void setForegroundColor(ForegroundColor color) {
std::cout << CSI << color << "m";
}
void setBackgroundColor(BackgroundColor color) {
std::cout << CSI << color << "m";
}
void setAttribute(Attribute attribute) {
std::cout << CSI << attribute << "m";
}
void resetAttribute(Attribute attribute) {
if (attribute == Attribute::BRIGHT) {
std::cout << CSI << attribute + 21 << "m";
return;
}
std::cout << CSI << attribute + 20 << "m";
}
void setForegroundColor(ForegroundColor);
void setBackgroundColor(BackgroundColor);
void setAttribute(Attribute);
void resetAttribute(Attribute);

void reset() {
setAttribute(Attribute::RESET);
setForegroundColor(ForegroundColor::F_WHITE);
setBackgroundColor(BackgroundColor::B_BLACK);
}
void reset();

void setForegroundColor(unsigned short int red, unsigned short int green, unsigned short int blue) {
std::cout << CSI << "38;2;" << red << ";" << green << ";" << blue << "m";
}
void setBackgroundColor(unsigned short int red, unsigned short int green, unsigned short int blue) {
std::cout << CSI << "48;2;" << red << ";" << green << ";" << blue << "m";
}
void setForegroundColor(unsigned short int color) {
std::cout << CSI << "38;5;" << color << "m";
}
void setBackgroundColor(unsigned short int color) {
std::cout << CSI << "48;5;" << color << "m";
}
void setForegroundColor(unsigned short int, unsigned short int, unsigned short int);
void setBackgroundColor(unsigned short int, unsigned short int, unsigned short int);
void setForegroundColor(unsigned short int);
void setBackgroundColor(unsigned short int);

enum ScreenMode {
MONOCROME_TEXT_40_25 = 0,
Expand All @@ -99,31 +76,18 @@ namespace ANSI {
COLOR_256_COLORS_GRAPHICS_320_200 = 19
};

void setScreenMode(ScreenMode mode) {
std::cout << CSI << '=' << mode << 'h';
}
void unsetScreenMode(ScreenMode mode) {
std::cout << CSI << '=' << mode << 'l';
}
void setScreenMode(ScreenMode);
void unsetScreenMode(ScreenMode);

struct Settings {
ForegroundColor foregroundColor;
BackgroundColor backgroundColor;
Attribute attribute;

Settings() {
foregroundColor = ForegroundColor::F_WHITE;
backgroundColor = BackgroundColor::B_BLACK;
attribute = Attribute::RESET;
}
Settings(ForegroundColor foregroundColor_, BackgroundColor backgroundColor_, Attribute attribute_): foregroundColor(foregroundColor_), backgroundColor(backgroundColor_), attribute(attribute_) {}
Settings(ForegroundColor& foregroundColor_, BackgroundColor& backgroundColor_, Attribute& attribute_, bool _by_reference): foregroundColor(foregroundColor_), backgroundColor(backgroundColor_), attribute(attribute_) {}
Settings();
Settings(ForegroundColor, BackgroundColor, Attribute);
Settings(ForegroundColor&, BackgroundColor&, Attribute&, bool);

void apply() {
setAttribute(Attribute::RESET);
setAttribute(attribute);
setForegroundColor(foregroundColor);
setBackgroundColor(backgroundColor);
}
void apply();
};
};
};
Loading