diff --git a/main.cpp b/main.cpp index 955db9a..e309c7e 100755 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,14 @@ #include #include #include "src/out_api/init.hpp" +#include "src/out_api/menu_draw.hpp" +#include "src/out_api/game_draw.hpp" int main() { init::begin(); - for (; !init::stop();) { + while (!init::stop()) { + game_draw::drawAll(); init::step(); } init::end(); -} \ No newline at end of file +} diff --git a/src/game/game_events.cpp b/src/game/game_events.cpp index 40c681a..14663d9 100644 --- a/src/game/game_events.cpp +++ b/src/game/game_events.cpp @@ -1,5 +1,5 @@ #include "game_events.hpp" namespace events { - -} \ No newline at end of file + size_t EventHandler::lastId = 0; +} diff --git a/src/game/game_events.hpp b/src/game/game_events.hpp index 94f8c59..62c972e 100644 --- a/src/game/game_events.hpp +++ b/src/game/game_events.hpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -56,8 +56,8 @@ namespace events { Event(const EventId& id, const EventData& data) : id(id), data(data) {} - bool isId(const EventId& eId) const { - return id == eId; + const EventId& getEventId() const { + return id; } EventData& getData() { @@ -72,37 +72,57 @@ namespace events { class EventHandler { private: std::function handler; + + static size_t lastId; + + size_t id; EventId eventId; public: EventHandler(std::function handler, - const EventId& eventId) : handler(handler), eventId(eventId) {} + const EventId& eventId) : handler(handler), eventId(eventId) { + id = lastId; + ++lastId; + } - bool handleEvent(const Event& event) { - if (event.isId(eventId)) { - handler(event.getData()); - return true; - } - return false; + void handleEvent(const Event& event) { + handler(event.getData()); + } + + size_t getId() const { + return id; + } + + const EventId& getEventId() const { + return eventId; } }; class EventCenter { private: - std::set handlers; + std::map> handlers; public: void addEventHandler(const EventHandler& handler) { - handlers.insert(handler); + handlers[handler.getEventId()].push_back(handler); } void removeEventHandler(const EventHandler& handler) { - handlers.erase(handler); + std::vector& v = handlers[handler.getEventId()]; + size_t id = handler.getId(); + for (size_t i = 0; i < v.size(); ++i) { + if (v[i].getId() == id) { + if (i + 1 != v.size()) { + std::swap(v[i], v.back()); + } + v.pop_back(); + } + } } void invokeEvent(const Event& event) { - for (auto handler : handlers) { + for (auto& handler : handlers[event.getEventId()]) { // is it really work ?? handler.handleEvent(event); } } }; -} \ No newline at end of file +} diff --git a/src/out_api/draw.cpp b/src/out_api/draw.cpp index fe78c50..522d13c 100644 --- a/src/out_api/draw.cpp +++ b/src/out_api/draw.cpp @@ -4,7 +4,7 @@ namespace draw { void initColorPairs() { - init_pair(ColorScheme::blank, COLOR_BLACK, COLOR_WHITE); + init_pair(ColorScheme::simple, COLOR_BLACK, COLOR_WHITE); init_pair(ColorScheme::map_simple, COLOR_BLACK, COLOR_WHITE); init_pair(ColorScheme::map_active, COLOR_WHITE, COLOR_BLACK); init_pair(ColorScheme::menu_simple, COLOR_BLACK, COLOR_WHITE); @@ -33,14 +33,14 @@ namespace draw { clear(); } - void drawCh(uint32_t x, uint32_t y, + void drawCh(size_t x, size_t y, Cell cell, ColorScheme colorScheme) { attron(COLOR_PAIR(colorScheme)); mvaddch(y, x, cell); attroff(COLOR_PAIR(colorScheme)); } - void getSize(uint32_t& x, uint32_t& y) { + void getSize(size_t& x, size_t& y) { // is it work ?? getmaxyx(stdscr, y, x); } -} \ No newline at end of file +} diff --git a/src/out_api/draw.hpp b/src/out_api/draw.hpp index ef33014..4c6f799 100644 --- a/src/out_api/draw.hpp +++ b/src/out_api/draw.hpp @@ -3,6 +3,7 @@ #pragma once namespace draw { + using std::size_t; enum ColorScheme { simple, @@ -37,10 +38,10 @@ namespace draw { void clearAll(); - void drawCh(uint32_t x, uint32_t y, + void drawCh(size_t x, size_t y, Cell cell, ColorScheme colorScheme = ColorScheme::simple); // void setxy(uint32_t x, uint32_t y); - void getSize(uint32_t& x, uint32_t& y); + void getSize(size_t& x, size_t& y); } diff --git a/src/out_api/game_draw.cpp b/src/out_api/game_draw.cpp index f8887d8..7e0c547 100644 --- a/src/out_api/game_draw.cpp +++ b/src/out_api/game_draw.cpp @@ -2,9 +2,30 @@ #include "game_draw.hpp" namespace game_draw { - void draw() { - std::size_t width; - std::size_t height; + const size_t MENU_WIDTH = 16; + + void drawAll() { + size_t width; + size_t height; + draw::getSize(width, height); + // check width && height + for(size_t i = 0; i < height; ++i) { + draw::drawCh(i, 0, draw::Cell::menu_hb); + draw::drawCh(i, width - 1, draw::Cell::menu_hb); + draw::drawCh(i, width - MENU_WIDTH, draw::Cell::menu_hb); + } + for(size_t i = 0; i < width; ++i) { + draw::drawCh(0, i, draw::Cell::menu_vb); + draw::drawCh(height - 1, i, draw::Cell::menu_vb); + } + // draw corners + } + + void drawMenu(std::pair start, std::pair end) { - } -} \ No newline at end of file + } + + void drawMap(std::pair start, std::pair end) { + + } +} diff --git a/src/out_api/game_draw.hpp b/src/out_api/game_draw.hpp index 6b7b9eb..40ce220 100644 --- a/src/out_api/game_draw.hpp +++ b/src/out_api/game_draw.hpp @@ -1,5 +1,13 @@ +#include +#include #pragma once namespace game_draw { - void draw(); -} \ No newline at end of file + using std::size_t; + + void drawAll(); + + void drawMenu(size_t width, size_t height); + + void drawMap(size_t width, size_t height); +}