add draw and draw_rect message

This commit is contained in:
2022-04-27 00:11:12 +02:00
parent 4df991650d
commit 8994a06874
3 changed files with 128 additions and 48 deletions

View File

@@ -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

View File

@@ -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");
}

View File

@@ -24,6 +24,81 @@ auto run_server(boost::asio::io_context& context) {
return std::make_unique<commons::server>(context, 9000);
}
void draw_random_rect(std::shared_ptr<commons::client> client, int time) {
std::random_device dev;
std::mt19937 rng(dev());
while (true) {
std::uniform_int_distribution<std::mt19937::result_type> pos(
0, commons::screen_size - 1);
std::uniform_int_distribution<std::mt19937::result_type> color(30, 37);
auto msg = std::make_shared<commons::protocol::draw_rectangle>();
msg->pixel_.value = '#';
msg->pixel_.color_fg =
static_cast<commons::protocol::foreground_color>(color(rng));
msg->pixel_.color_bg =
static_cast<commons::protocol::background_color>(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<int>(msg->pixel_.color_fg),
static_cast<int>(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<commons::client> client, int time) {
std::random_device dev;
std::mt19937 rng(dev());
while (true) {
std::uniform_int_distribution<std::mt19937::result_type> pos(
0, commons::screen_size - 1);
std::uniform_int_distribution<std::mt19937::result_type> color(30, 37);
auto msg = std::make_shared<commons::protocol::draw_pixel>();
msg->pixel_.value = '#';
msg->pixel_.color_fg =
static_cast<commons::protocol::foreground_color>(color(rng));
msg->pixel_.color_bg =
static_cast<commons::protocol::background_color>(color(rng) + 10);
msg->position = commons::protocol::vec2{pos(rng), pos(rng)};
spdlog::debug("sending fg {}, bg {}, posx {}, posy {}",
static_cast<int>(msg->pixel_.color_fg),
static_cast<int>(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<commons::client> client, int time) {
while (true) {
auto msg = std::make_shared<commons::protocol::draw>();
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<commons::server> server = nullptr;
std::shared_ptr<commons::client> client = nullptr;
std::thread t;
std::vector<std::thread> 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<std::mt19937::result_type> pos(
0, commons::screen_size - 1);
std::uniform_int_distribution<std::mt19937::result_type> color(30,
37);
auto msg = std::make_shared<commons::protocol::draw_pixel>();
msg->pixel_.value = '@';
msg->pixel_.color_fg =
static_cast<commons::protocol::foreground_color>(color(rng));
msg->pixel_.color_bg =
static_cast<commons::protocol::background_color>(color(rng) + 10);
msg->position = commons::protocol::vec2{pos(rng), pos(rng)};
spdlog::debug("sending fg {}, bg {}, posx {}, posy {}",
static_cast<int>(msg->pixel_.color_fg),
static_cast<int>(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";