From 8994a0687466e5c2cc43088c14081b8b3a221750 Mon Sep 17 00:00:00 2001 From: kalipso Date: Wed, 27 Apr 2022 00:11:12 +0200 Subject: [PATCH] add draw and draw_rect message --- include/protocol.hpp | 56 +++++++++++++++----- include/server.hpp | 2 - src/main.cpp | 118 ++++++++++++++++++++++++++++++------------- 3 files changed, 128 insertions(+), 48 deletions(-) diff --git a/include/protocol.hpp b/include/protocol.hpp index 4f4b116..901b375 100644 --- a/include/protocol.hpp +++ b/include/protocol.hpp @@ -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 { 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 { 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 { 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 { 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 { vec3 color; }; +class draw : public generic_message { + 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{screen_size}} {} + : size_(screen_size), + screen_{screen_size, std::vector{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(pixel.color_fg) << ";" << static_cast(pixel.color_bg) << "m" << pixel.value; @@ -206,7 +237,8 @@ class cli_renderer : public render_interface { } private: - std::vector> screen; + size_t size_; + std::vector> screen_; }; } // namespace commons diff --git a/include/server.hpp b/include/server.hpp index c68829d..a595e30 100644 --- a/include/server.hpp +++ b/include/server.hpp @@ -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"); } diff --git a/src/main.cpp b/src/main.cpp index e876f86..583f003 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,6 +24,81 @@ auto run_server(boost::asio::io_context& context) { return std::make_unique(context, 9000); } +void draw_random_rect(std::shared_ptr client, int time) { + std::random_device dev; + std::mt19937 rng(dev()); + while (true) { + std::uniform_int_distribution pos( + 0, commons::screen_size - 1); + std::uniform_int_distribution color(30, 37); + + auto msg = std::make_shared(); + msg->pixel_.value = '#'; + msg->pixel_.color_fg = + static_cast(color(rng)); + msg->pixel_.color_bg = + static_cast(color(rng) + 10); + msg->position = commons::protocol::vec2{pos(rng), pos(rng)}; + msg->size = commons::protocol::vec2{pos(rng), pos(rng)}; + + spdlog::debug("DRAWRECTsending fg {}, bg {}, posx {}, posy {}", + static_cast(msg->pixel_.color_fg), + static_cast(msg->pixel_.color_bg), msg->position.x, + msg->position.y); + // used as lifetime expansion so that msg doesnt go out of scope + // till it actually was sent + client->send( + msg->serialize(), + std::bind(&commons::protocol::generic_message_base::handle_sent, msg, + std::placeholders::_1)); + std::this_thread::sleep_for(std::chrono::milliseconds(time)); + spdlog::info("send new"); + } +} + +void draw_random_pixel(std::shared_ptr client, int time) { + std::random_device dev; + std::mt19937 rng(dev()); + while (true) { + std::uniform_int_distribution pos( + 0, commons::screen_size - 1); + std::uniform_int_distribution color(30, 37); + + auto msg = std::make_shared(); + msg->pixel_.value = '#'; + msg->pixel_.color_fg = + static_cast(color(rng)); + msg->pixel_.color_bg = + static_cast(color(rng) + 10); + msg->position = commons::protocol::vec2{pos(rng), pos(rng)}; + + spdlog::debug("sending fg {}, bg {}, posx {}, posy {}", + static_cast(msg->pixel_.color_fg), + static_cast(msg->pixel_.color_bg), msg->position.x, + msg->position.y); + // used as lifetime expansion so that msg doesnt go out of scope + // till it actually was sent + client->send( + msg->serialize(), + std::bind(&commons::protocol::generic_message_base::handle_sent, msg, + std::placeholders::_1)); + std::this_thread::sleep_for(std::chrono::milliseconds(time)); + spdlog::info("send new"); + } +} + +void draw(std::shared_ptr client, int time) { + while (true) { + auto msg = std::make_shared(); + spdlog::debug("sending draw"); + client->send( + msg->serialize(), + std::bind(&commons::protocol::generic_message_base::handle_sent, msg, + std::placeholders::_1)); + std::this_thread::sleep_for(std::chrono::milliseconds(time)); + } +} + int main(int argc, char* argv[]) { init_spdlog(); @@ -31,41 +106,14 @@ int main(int argc, char* argv[]) { boost::asio::io_context io_context; std::unique_ptr server = nullptr; std::shared_ptr client = nullptr; - std::thread t; + std::vector jobs; if (argc == 1) { client = run_client(io_context); - - t = std::thread([client]() { - std::random_device dev; - std::mt19937 rng(dev()); - while (true) { - std::uniform_int_distribution pos( - 0, commons::screen_size - 1); - std::uniform_int_distribution color(30, - 37); - - auto msg = std::make_shared(); - msg->pixel_.value = '@'; - msg->pixel_.color_fg = - static_cast(color(rng)); - msg->pixel_.color_bg = - static_cast(color(rng) + 10); - msg->position = commons::protocol::vec2{pos(rng), pos(rng)}; - - spdlog::debug("sending fg {}, bg {}, posx {}, posy {}", - static_cast(msg->pixel_.color_fg), - static_cast(msg->pixel_.color_bg), msg->position.x, - msg->position.y); - // used as lifetime expansion so that msg doesnt go out of scope - // till it actually was sent - client->send( - msg->serialize(), - std::bind(&commons::protocol::generic_message_base::handle_sent, - msg, std::placeholders::_1)); - std::this_thread::sleep_for(std::chrono::milliseconds(20)); - spdlog::info("send new"); - } - }); + jobs.push_back( + std::thread([client]() { draw_random_rect(client, 500); })); + jobs.push_back( + std::thread([client]() { draw_random_pixel(client, 50); })); + jobs.push_back(std::thread([client]() { draw(client, 100); })); } if (argc == 2) { @@ -82,7 +130,9 @@ int main(int argc, char* argv[]) { context.join(); } - t.join(); + for (auto& job : jobs) { + job.join(); + } } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n";