add draw and draw_rect message
This commit is contained in:
@@ -26,6 +26,9 @@ class render_interface {
|
||||
virtual ~render_interface() = default;
|
||||
virtual void update_pixel(commons::protocol::vec2 position,
|
||||
commons::protocol::pixel pixel) = 0;
|
||||
virtual void update_rect(commons::protocol::vec2 position,
|
||||
commons::protocol::vec2 width,
|
||||
commons::protocol::pixel pixel) = 0;
|
||||
virtual void draw() = 0;
|
||||
};
|
||||
|
||||
@@ -34,12 +37,13 @@ namespace protocol {
|
||||
enum class Type {
|
||||
DRAW_RECTANGLE = 0,
|
||||
DRAW_PIXEL,
|
||||
DRAW,
|
||||
};
|
||||
|
||||
class generic_message_base
|
||||
: std::enable_shared_from_this<generic_message_base> {
|
||||
public:
|
||||
virtual ~generic_message_base() { spdlog::debug("base dtor"); }
|
||||
virtual ~generic_message_base() = default;
|
||||
virtual constexpr Type get_type() const = 0;
|
||||
virtual uint32_t get_size() const = 0;
|
||||
|
||||
@@ -67,6 +71,8 @@ class generic_message_base
|
||||
|
||||
virtual void handle_sent(const boost::system::error_code& er) {
|
||||
if (!er) {
|
||||
spdlog::debug("size {}", get_size());
|
||||
std::cout << "MSG WAS SNT \n";
|
||||
spdlog::debug("Message was sent");
|
||||
} else {
|
||||
spdlog::error("Error occured on client::send");
|
||||
@@ -129,14 +135,14 @@ enum class foreground_color {
|
||||
};
|
||||
|
||||
struct pixel {
|
||||
char value = '#';
|
||||
char value = ' ';
|
||||
foreground_color color_fg = foreground_color::GREEN;
|
||||
background_color color_bg = background_color::BLACK;
|
||||
};
|
||||
|
||||
class draw_rectangle : public generic_message<Type::DRAW_RECTANGLE> {
|
||||
public:
|
||||
virtual ~draw_rectangle() { spdlog::debug("draw_rect dtor"); }
|
||||
virtual ~draw_rectangle() = default;
|
||||
|
||||
virtual uint32_t get_size() const override { return sizeof(*this); }
|
||||
|
||||
@@ -147,14 +153,18 @@ class draw_rectangle : public generic_message<Type::DRAW_RECTANGLE> {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
virtual void handle_message(render_interface* renderer) override {
|
||||
renderer->update_rect(position, size, pixel_);
|
||||
}
|
||||
|
||||
vec2 position;
|
||||
vec2 size;
|
||||
vec3 color;
|
||||
pixel pixel_;
|
||||
};
|
||||
|
||||
class draw_pixel : public generic_message<Type::DRAW_PIXEL> {
|
||||
public:
|
||||
virtual ~draw_pixel() { spdlog::debug("draw_pixel dtor"); }
|
||||
virtual ~draw_pixel() = default;
|
||||
|
||||
virtual uint32_t get_size() const override { return sizeof(*this); }
|
||||
|
||||
@@ -174,6 +184,19 @@ class draw_pixel : public generic_message<Type::DRAW_PIXEL> {
|
||||
vec3 color;
|
||||
};
|
||||
|
||||
class draw : public generic_message<Type::DRAW> {
|
||||
public:
|
||||
virtual ~draw() = default;
|
||||
|
||||
virtual uint32_t get_size() const override { return sizeof(*this); }
|
||||
|
||||
virtual void handle_message() override {}
|
||||
|
||||
virtual void handle_message(render_interface* renderer) override {
|
||||
renderer->draw();
|
||||
}
|
||||
};
|
||||
|
||||
class Serializer {};
|
||||
|
||||
} // namespace protocol
|
||||
@@ -181,19 +204,27 @@ class Serializer {};
|
||||
class cli_renderer : public render_interface {
|
||||
public:
|
||||
cli_renderer(size_t screen_size)
|
||||
: screen{screen_size, std::vector<protocol::pixel>{screen_size}} {}
|
||||
: size_(screen_size),
|
||||
screen_{screen_size, std::vector<protocol::pixel>{screen_size}} {}
|
||||
|
||||
virtual ~cli_renderer() = default;
|
||||
|
||||
virtual void update_pixel(commons::protocol::vec2 position,
|
||||
commons::protocol::pixel pixel) override {
|
||||
screen[position.y][position.x] = pixel;
|
||||
draw();
|
||||
virtual void update_pixel(protocol::vec2 position,
|
||||
protocol::pixel pixel) override {
|
||||
screen_[position.y][position.x] = pixel;
|
||||
}
|
||||
|
||||
virtual void update_rect(protocol::vec2 position, protocol::vec2 width,
|
||||
protocol::pixel pixel) override {
|
||||
for (int y = position.y; y < size_ && y < width.y + position.y; ++y) {
|
||||
for (int x = position.x; x < size_ && x < width.x + position.x; ++x)
|
||||
update_pixel(protocol::vec2{x, y}, pixel);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void draw() override {
|
||||
std::stringstream str;
|
||||
for (const auto& row : screen) {
|
||||
for (const auto& row : screen_) {
|
||||
for (const auto& pixel : row) {
|
||||
str << "\033[" << static_cast<size_t>(pixel.color_fg) << ";"
|
||||
<< static_cast<size_t>(pixel.color_bg) << "m" << pixel.value;
|
||||
@@ -206,7 +237,8 @@ class cli_renderer : public render_interface {
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::vector<protocol::pixel>> screen;
|
||||
size_t size_;
|
||||
std::vector<std::vector<protocol::pixel>> screen_;
|
||||
};
|
||||
|
||||
} // namespace commons
|
||||
|
||||
@@ -32,7 +32,6 @@ class server {
|
||||
}
|
||||
|
||||
void do_receive() {
|
||||
spdlog::debug("do receive");
|
||||
socket_.async_receive_from(
|
||||
boost::asio::buffer(data_.data(), max_length), sender_endpoint_,
|
||||
[this](boost::system::error_code ec, std::size_t bytes_recvd) {
|
||||
@@ -45,7 +44,6 @@ class server {
|
||||
});
|
||||
|
||||
// do_send(bytes_recvd);
|
||||
spdlog::debug("leaving do receive");
|
||||
} else {
|
||||
spdlog::error("error during do receive");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user