implement dummy cli renderer for testing

This commit is contained in:
2022-04-26 22:16:06 +02:00
parent 14d0716607
commit 4df991650d
3 changed files with 132 additions and 19 deletions

View File

@@ -6,12 +6,30 @@
#include <cstring>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <iostream>
#include <map>
#include <memory>
#include "spdlog/spdlog.h"
namespace commons::protocol {
namespace commons {
static constexpr size_t screen_size = 30;
namespace protocol {
struct vec2;
struct pixel;
} // namespace protocol
class render_interface {
public:
virtual ~render_interface() = default;
virtual void update_pixel(commons::protocol::vec2 position,
commons::protocol::pixel pixel) = 0;
virtual void draw() = 0;
};
namespace protocol {
enum class Type {
DRAW_RECTANGLE = 0,
@@ -42,6 +60,7 @@ class generic_message_base
}
virtual void handle_message() = 0;
virtual void handle_message(render_interface* renderer){};
virtual void create_message() {
spdlog::debug("create_message is not implemented yet");
}
@@ -87,6 +106,34 @@ struct vec3 {
int x, y, z;
};
enum class background_color {
BLACK = 40,
RED,
GREEN,
YELlOW,
BLUE,
MAGENTA,
CYAN,
WHITE,
};
enum class foreground_color {
BLACK = 30,
RED,
GREEN,
YELlOW,
BLUE,
MAGENTA,
CYAN,
WHITE,
};
struct pixel {
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"); }
@@ -118,10 +165,48 @@ class draw_pixel : public generic_message<Type::DRAW_PIXEL> {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
virtual void handle_message(render_interface* renderer) override {
renderer->update_pixel(position, pixel_);
}
vec2 position;
pixel pixel_;
vec3 color;
};
class Serializer {};
} // namespace commons::protocol
} // namespace protocol
class cli_renderer : public render_interface {
public:
cli_renderer(size_t 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 draw() override {
std::stringstream str;
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;
}
str << "\033[30;40m\n";
}
str << "\033[30;40m\n";
std::cout << str.str() << '\n';
}
private:
std::vector<std::vector<protocol::pixel>> screen;
};
} // namespace commons

View File

@@ -4,6 +4,7 @@
#include <cstdlib>
#include <future>
#include <iostream>
#include <sstream>
#include "protocol.hpp"
#include "spdlog/spdlog.h"
@@ -25,7 +26,8 @@ class fake_thread_pool {
class server {
public:
server(boost::asio::io_context& io_context, short port)
: socket_(io_context, udp::endpoint(udp::v4(), port)) {
: socket_(io_context, udp::endpoint(udp::v4(), port)),
renderer(std::make_unique<cli_renderer>(commons::screen_size)) {
do_receive();
}
@@ -38,8 +40,8 @@ class server {
std::shared_ptr<protocol::generic_message_base> msg_obj =
deserialize(data_.data(), bytes_recvd);
fake_thread_pool::add_task([msg_obj = std::move(msg_obj)]() {
msg_obj->handle_message();
fake_thread_pool::add_task([this, msg_obj = std::move(msg_obj)]() {
msg_obj->handle_message(renderer.get());
});
// do_send(bytes_recvd);
@@ -64,6 +66,8 @@ class server {
udp::endpoint sender_endpoint_;
enum { max_length = 1024 };
std::array<char, max_length> data_;
std::unique_ptr<render_interface> renderer;
};
} // namespace commons