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

57 make ncurse implementation #91

Merged
merged 12 commits into from
Apr 6, 2024
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SNAKE_SRC = src/snake/Entities/Void.cpp \
classes/Timer.cpp \
classes/Color.cpp \

NCURSES_SRC = src/ncurses/ncurses.cpp \
NCURSES_SRC = src/ncurses/Ncurses.cpp \

SFML_SRC = src/sfml/Sfml.cpp \

Expand Down
18 changes: 9 additions & 9 deletions abstract/AGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
namespace Arcade {
class AGame : public IGame {
public:
virtual ~AGame() = default;
~AGame() override = default;

//Game
virtual int startGame() = 0;
virtual int stopGame() = 0;
int getScore();
virtual int simulate() = 0;
int startGame() override = 0;
int stopGame() override = 0;
int getScore() override;
int simulate() override = 0;

//Event
virtual void catchKeyEvent(int key) = 0;
void catchKeyEvent(int key) override = 0;

//Display
std::vector<std::shared_ptr<IEntity>> getEntities();
std::vector<std::shared_ptr<IText>> getTexts();
std::vector<std::shared_ptr<ISound>> getSounds();
std::vector<std::shared_ptr<IEntity>> getEntities() override;
std::vector<std::shared_ptr<IText>> getTexts() override;
std::vector<std::shared_ptr<ISound>> getSounds() override;

protected:
std::vector<std::shared_ptr<IEntity>> _entities;
Expand Down
12 changes: 6 additions & 6 deletions classes/Color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace Arcade {
class Color : public IColor {
public:
Color(short r, short g, short b, short a);
~Color() = default;
~Color() override = default;

void setColor(short r, short g, short b, short a);
void setColor(short r, short g, short b, short a) override;

short getR();
short getG();
short getB();
short getA();
short getR() override;
short getG() override;
short getB() override;
short getA() override;

private:
short _r;
Expand Down
26 changes: 13 additions & 13 deletions classes/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ namespace Arcade {
class Text : public IText {
public:
Text(std::string text, std::vector<std::size_t> pos, std::vector<std::size_t> size, char c);
~Text() = default;
~Text() override = default;

void setFontPath(const std::string &fontPath);
void setText(const std::string &text);
void setColor(std::unique_ptr<IColor> color);
void setPos(std::size_t x, std::size_t y);
void setSize(std::size_t x);
void setRotation(float rotation);
void setFontPath(const std::string &fontPath) override;
void setText(const std::string &text) override;
void setColor(std::unique_ptr<IColor> color) override;
void setPos(std::size_t x, std::size_t y) override;
void setSize(std::size_t x) override;
void setRotation(float rotation) override;

//getters
std::string getFontPath();
std::string getText();
std::shared_ptr<IColor> getColor() const;
std::vector<std::size_t> getPos() const;
std::size_t getSize() const;
float getRotation() const;
std::string getFontPath() override;
std::string getText() override;
std::shared_ptr<IColor> getColor() const override;
std::vector<std::size_t> getPos() const override;
std::size_t getSize() const override;
float getRotation() const override;
private:
std::string _text;
std::vector<std::size_t> _pos;
Expand Down
192 changes: 192 additions & 0 deletions src/ncurses/Ncurses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
** EPITECH PROJECT, 2024
** Arcade
** File description:
** ncurses.cpp
*/

#include "Ncurses.hpp"
#include "includes/Keys.hpp"
#include <cmath>
#include <map>
#include <memory>
#include <iostream>


Arcade::Ncurses::Ncurses()
{
initscr();
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
curs_set(0);
start_color();
init_color(COLOR_BLACK, 0, 0, 0);
init_pair(1, COLOR_WHITE, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
init_pair(3, COLOR_GREEN, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_YELLOW, COLOR_BLACK);
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
init_pair(7, COLOR_CYAN, COLOR_BLACK);
init_pair(8, COLOR_WHITE, COLOR_BLACK);
wbkgd(stdscr, COLOR_PAIR(1));

}

bool Arcade::Ncurses::isWindowOpen() const
{
return true;
}

void Arcade::Ncurses::closeWindow()
{
endwin();
}

void Arcade::Ncurses::clearWindow()
{
for (int i = 0; i < LINES; i++) {
for (int j = 0; j < COLS; j++) {
mvprintw(i, j, " ");
}
}
}

int Arcade::Ncurses::getKeyEvent()
{
int key = getch();
static std::map<int, int> keyMap = {
{'A', Arcade::Keys::A},
{'B', Arcade::Keys::B},
{'C', Arcade::Keys::C},
{'D', Arcade::Keys::D},
{'E', Arcade::Keys::E},
{'F', Arcade::Keys::F},
{'G', Arcade::Keys::G},
{'H', Arcade::Keys::H},
{'I', Arcade::Keys::I},
{'J', Arcade::Keys::J},
{'K', Arcade::Keys::K},
{'L', Arcade::Keys::L},
{'M', Arcade::Keys::M},
{'N', Arcade::Keys::N},
{'O', Arcade::Keys::O},
{'P', Arcade::Keys::P},
{'Q', Arcade::Keys::Q},
{'R', Arcade::Keys::R},
{'S', Arcade::Keys::S},
{'T', Arcade::Keys::T},
{'U', Arcade::Keys::U},
{'V', Arcade::Keys::V},
{'W', Arcade::Keys::W},
{'X', Arcade::Keys::X},
{'Y', Arcade::Keys::Y},
{'Z', Arcade::Keys::Z},
{'1', Arcade::Keys::ONE},
{'2', Arcade::Keys::TWO},
{'3', Arcade::Keys::THREE},
{'4', Arcade::Keys::FOUR},
{'5', Arcade::Keys::FIVE},
{'6', Arcade::Keys::SIX},
{'7', Arcade::Keys::SEVEN},
{'8', Arcade::Keys::EIGHT},
{'9', Arcade::Keys::NINE},
{'0', Arcade::Keys::ZERO},
{27, Arcade::Keys::ESCAPE},
{9, Arcade::Keys::TAB},
{KEY_BACKSPACE, Arcade::Keys::BACKSPACE},
{KEY_UP, Arcade::Keys::UP},
{KEY_DOWN, Arcade::Keys::DOWN},
{KEY_LEFT, Arcade::Keys::LEFT},
{KEY_RIGHT, Arcade::Keys::RIGHT},
{10, Arcade::Keys::ENTER},
{32, Arcade::Keys::SPACE},
{KEY_MOUSE, Arcade::Keys::MOUSE_LEFT}
};
return keyMap[key];
}

std::pair<int, int> Arcade::Ncurses::getMousePosition()
{
MEVENT event;
if (getmouse(&event) == OK) {
return {event.x * 29 , event.y * 29};
}
return {-1, -1};
}

void Arcade::Ncurses::displayWindow()
{
refresh();
}

int Arcade::Ncurses::colorToNcurses(const std::shared_ptr<IColor>& color)
{
int bestColor = 1;
double minDistance = 1000000;

std::map<int, std::tuple<int, int, int, int>> colors = {
{1, {255, 255, 255, 255}},
{2, {255, 0, 0, 255}},
{3, {0, 255, 0, 255}},
{4, {0, 0, 255, 255}},
{5, {255, 255, 0, 255}},
{6, {255, 0, 255, 255}},
{7, {0, 255, 255, 255}},
{8, {255, 255, 255, 255}}

};

for (auto &colorPair : colors) {
double distance = sqrt(pow(std::get<0>(colorPair.second) - color->getR(), 2) + pow(std::get<1>(colorPair.second) - color->getG(), 2) + pow(std::get<2>(colorPair.second) - color->getB(), 2));
if (distance < minDistance) {
minDistance = distance;
bestColor = colorPair.first;
}
}
return bestColor;
}

void Arcade::Ncurses::displayEntities(std::vector<std::shared_ptr<IEntity>> entities)
{
for (auto &entity : entities) {
int x = colorToNcurses(entity->getColor());
wattron(stdscr, COLOR_PAIR(x));
mvwaddch(stdscr, entity->getPos()[1], entity->getPos()[0], (char)entity->getChar());
wattroff(stdscr, COLOR_PAIR(x));
}
}

void Arcade::Ncurses::displayText(std::vector<std::shared_ptr<IText>> texts)
{
for (auto &text : texts) {
int x = colorToNcurses(text->getColor());
if (text->getFontPath().empty())
continue;
wattron(stdscr, COLOR_PAIR(x));
mvprintw(text->getPos()[1], text->getPos()[0], "%s", text->getText().c_str());
wattroff(stdscr, COLOR_PAIR(x));
}
}

void Arcade::Ncurses::playSound(std::vector<std::shared_ptr<ISound>> sounds)
{
(void)sounds;
}

extern "C"
{
__attribute__((constructor))
void constructor()
{
}
__attribute__((destructor))
void destructor()
{
}
Arcade::Ncurses *loadGraphicInstance()
{
return new Arcade::Ncurses();
}
}
12 changes: 7 additions & 5 deletions src/ncurses/ncurses.hpp → src/ncurses/Ncurses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,28 @@

#include "Interfaces/IGraphic.hpp"
#include "includes/Keys.hpp"
#include <ncurses.h>

namespace Arcade {
class Ncurses : public IGraphic {
public:
Ncurses();
~Ncurses() = default;
~Ncurses() override = default;
bool isWindowOpen() const override;
void closeWindow();
void clearWindow();
void closeWindow() override;
void clearWindow() override;

//Event
int getKeyEvent() override;
std::pair<int, int> getMousePosition();
std::pair<int, int> getMousePosition() override;

//Display
void displayWindow();
void displayWindow() override;
void displayEntities(std::vector<std::shared_ptr<IEntity>> entities) override;
void displayText(std::vector<std::shared_ptr<IText>> texts) override;
void playSound(std::vector<std::shared_ptr<ISound>> sounds) override;
private:
static int colorToNcurses(const std::shared_ptr<IColor>& color);
std::vector<IEntity> _entities;
std::vector<IText> _texts;
std::vector<ISound> _sounds;
Expand Down
Loading