some fixes

This commit is contained in:
ProgramSnail 2021-04-06 19:31:54 +03:00
parent eea91417c9
commit 4f2bfac443
7 changed files with 85 additions and 32 deletions

View file

@ -1,10 +1,13 @@
#include <curses.h> #include <curses.h>
#include <cstdlib> #include <cstdlib>
#include "src/out_api/init.hpp" #include "src/out_api/init.hpp"
#include "src/out_api/menu_draw.hpp"
#include "src/out_api/game_draw.hpp"
int main() { int main() {
init::begin(); init::begin();
for (; !init::stop();) { while (!init::stop()) {
game_draw::drawAll();
init::step(); init::step();
} }
init::end(); init::end();

View file

@ -1,5 +1,5 @@
#include "game_events.hpp" #include "game_events.hpp"
namespace events { namespace events {
size_t EventHandler::lastId = 0;
} }

View file

@ -1,4 +1,4 @@
#include <set> #include <vector>
#include <functional> #include <functional>
#include <string> #include <string>
#include <map> #include <map>
@ -56,8 +56,8 @@ namespace events {
Event(const EventId& id, const EventData& data) : Event(const EventId& id, const EventData& data) :
id(id), data(data) {} id(id), data(data) {}
bool isId(const EventId& eId) const { const EventId& getEventId() const {
return id == eId; return id;
} }
EventData& getData() { EventData& getData() {
@ -73,34 +73,54 @@ namespace events {
private: private:
std::function<void(const EventData&)> handler; std::function<void(const EventData&)> handler;
static size_t lastId;
size_t id;
EventId eventId; EventId eventId;
public: public:
EventHandler(std::function<void(const EventData&)> handler, EventHandler(std::function<void(const EventData&)> handler,
const EventId& eventId) : handler(handler), eventId(eventId) {} const EventId& eventId) : handler(handler), eventId(eventId) {
id = lastId;
bool handleEvent(const Event& event) { ++lastId;
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 { class EventCenter {
private: private:
std::set<EventHandler> handlers; std::map<EventId, std::vector<EventHandler>> handlers;
public: public:
void addEventHandler(const EventHandler& handler) { void addEventHandler(const EventHandler& handler) {
handlers.insert(handler); handlers[handler.getEventId()].push_back(handler);
} }
void removeEventHandler(const EventHandler& handler) { void removeEventHandler(const EventHandler& handler) {
handlers.erase(handler); std::vector<EventHandler>& 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) { void invokeEvent(const Event& event) {
for (auto handler : handlers) { for (auto& handler : handlers[event.getEventId()]) { // is it really work ??
handler.handleEvent(event); handler.handleEvent(event);
} }
} }

View file

@ -4,7 +4,7 @@
namespace draw { namespace draw {
void initColorPairs() { 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_simple, COLOR_BLACK, COLOR_WHITE);
init_pair(ColorScheme::map_active, COLOR_WHITE, COLOR_BLACK); init_pair(ColorScheme::map_active, COLOR_WHITE, COLOR_BLACK);
init_pair(ColorScheme::menu_simple, COLOR_BLACK, COLOR_WHITE); init_pair(ColorScheme::menu_simple, COLOR_BLACK, COLOR_WHITE);
@ -33,14 +33,14 @@ namespace draw {
clear(); clear();
} }
void drawCh(uint32_t x, uint32_t y, void drawCh(size_t x, size_t y,
Cell cell, ColorScheme colorScheme) { Cell cell, ColorScheme colorScheme) {
attron(COLOR_PAIR(colorScheme)); attron(COLOR_PAIR(colorScheme));
mvaddch(y, x, cell); mvaddch(y, x, cell);
attroff(COLOR_PAIR(colorScheme)); 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); getmaxyx(stdscr, y, x);
} }
} }

View file

@ -3,6 +3,7 @@
#pragma once #pragma once
namespace draw { namespace draw {
using std::size_t;
enum ColorScheme { enum ColorScheme {
simple, simple,
@ -37,10 +38,10 @@ namespace draw {
void clearAll(); void clearAll();
void drawCh(uint32_t x, uint32_t y, void drawCh(size_t x, size_t y,
Cell cell, ColorScheme colorScheme = ColorScheme::simple); Cell cell, ColorScheme colorScheme = ColorScheme::simple);
// void setxy(uint32_t x, uint32_t y); // void setxy(uint32_t x, uint32_t y);
void getSize(uint32_t& x, uint32_t& y); void getSize(size_t& x, size_t& y);
} }

View file

@ -2,9 +2,30 @@
#include "game_draw.hpp" #include "game_draw.hpp"
namespace game_draw { namespace game_draw {
void draw() { const size_t MENU_WIDTH = 16;
std::size_t width;
std::size_t height; 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<size_t, size_t> start, std::pair<size_t, size_t> end) {
}
void drawMap(std::pair<size_t, size_t> start, std::pair<size_t, size_t> end) {
} }
} }

View file

@ -1,5 +1,13 @@
#include <cstdlib>
#include <utility>
#pragma once #pragma once
namespace game_draw { namespace game_draw {
void draw(); using std::size_t;
void drawAll();
void drawMenu(size_t width, size_t height);
void drawMap(size_t width, size_t height);
} }