Skip to content

Commit

Permalink
Merge pull request #19 from Immortals-Robotics/udp-drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
lordhippo authored May 3, 2024
2 parents 5bae050 + 0c556ab commit 93044c6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 10 deletions.
1 change: 1 addition & 0 deletions source/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(SOURCE_FILES
renderer/renderer_ball.cpp
renderer/renderer_robot.cpp
renderer/renderer.cpp
renderer/renderer_drawudp.cpp
)

add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES})
Expand Down
33 changes: 30 additions & 3 deletions source/gui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ void Application::init(const int width, const int height)
ssl_field.set_penalty_area_depth(1800);
ssl_field.set_penalty_area_width(3600);

udp_client = std::make_unique<Common::UdpClient>(Common::NetworkAddress{"224.5.23.2", 10006});

udp_client = std::make_unique<Common::UdpClient>(Common::setting().vision_address);
udp_client_drawings = std::make_unique<Common::UdpClient>(Common::NetworkAddress{"127.0.0.1", 10066});
renderer->init();

vision_thread = std::thread(&Application::receiveVision, this);
vision_thread = std::thread(&Application::receiveVision, this);
drawing_thread = std::thread(&Application::receiveDrawings, this);
}

int Application::shutdown()
Expand All @@ -68,6 +69,7 @@ int Application::shutdown()

running = false;
vision_thread.join();
drawing_thread.join();

rlImGuiShutdown();

Expand Down Expand Up @@ -111,6 +113,12 @@ void Application::update()
udp_client->Update(updated_address);
}
vision_mutex.unlock();
drawing_mutex.lock();
renderer->drawCirclesUdp(debug_packet.dbg_draw().circle());
renderer->drawRectsUdp(debug_packet.dbg_draw().rect());
renderer->drawPointsUdp(debug_packet.dbg_draw().point());
renderer->drawLinesUdp(debug_packet.dbg_draw().line());
drawing_mutex.unlock();

renderer->applyShader();
rlImGuiImageRenderTextureFit(&renderer->shaderVisualizationTexture, true);
Expand Down Expand Up @@ -156,4 +164,23 @@ void Application::receiveVision()
}
};

void Application::receiveDrawings()
{
Protos::Immortals::Imm_DBG_wrapper tmp_drawing_packet;
while (running)
{
udp_client_drawings->receive(&tmp_drawing_packet);
if (tmp_drawing_packet.has_dbg_draw())
{
auto draw = tmp_drawing_packet.dbg_draw();
// Common::logDebug("draw darim");
debug_packet_off.clear_dbg_draw();
debug_packet_off.mutable_dbg_draw()->CopyFrom(draw);
}
drawing_mutex.lock();
std::swap(debug_packet, debug_packet_off);
drawing_mutex.unlock();
}
};

} // namespace Tyr::Gui
17 changes: 11 additions & 6 deletions source/gui/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@ class Application
bool shouldClose() const;

private:
Protos::SSL_GeometryFieldSize ssl_field;
Protos::SSL_WrapperPacket ssl_packet;
Protos::SSL_WrapperPacket ssl_packet_off;
Protos::Immortals::Debug_Draw world_state;
Protos::Immortals::Debug_Draw world_state_off;
Protos::SSL_GeometryFieldSize ssl_field;
Protos::SSL_WrapperPacket ssl_packet;
Protos::SSL_WrapperPacket ssl_packet_off;
Protos::Immortals::Imm_DBG_wrapper debug_packet;
Protos::Immortals::Imm_DBG_wrapper debug_packet_off;

std::unique_ptr<Renderer> renderer;
std::unique_ptr<ConfigMenu> config_menu;

std::unique_ptr<Common::UdpClient> udp_client;
Common::NetworkAddress updated_address;
std::unique_ptr<Common::UdpClient> udp_client_drawings;

Common::NetworkAddress updated_address;

std::mutex vision_mutex;
std::thread vision_thread;
std::mutex drawing_mutex;
std::thread drawing_thread;
std::atomic<bool> running = true;

void receiveVision();
void receiveDrawings();
};
} // namespace Tyr::Gui
8 changes: 7 additions & 1 deletion source/gui/renderer/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Renderer
unsigned char _transparency = 255);
void drawCircleSector(Common::Circle circle, Color _color, float _startAngle, float _endAngle, bool _isFilled,
unsigned char _transparency = 255);
void drawLineSegment(Common::LineSegment line_segment, Color _color, float _thickness = 1, unsigned char _transparency = 255);
void drawLineSegment(Common::LineSegment line_segment, Color _color, float _thickness = 1,
unsigned char _transparency = 255);
void drawText(Common::Vec2 _pos, std::string _str, int _fontSize = 12, Color _color = WHITE,
unsigned char _transparency = 255);

Expand All @@ -26,6 +27,11 @@ class Renderer
void drawRobot(const Protos::SSL_DetectionRobot &robot, Common::TeamColor color);
void drawBall(const Protos::SSL_DetectionBall &ball);

void drawCirclesUdp(const google::protobuf::RepeatedPtrField<Protos::Immortals::Debug_Circle> &circles);
void drawRectsUdp(const google::protobuf::RepeatedPtrField<Protos::Immortals::Debug_Rect> &rects);
void drawPointsUdp(const google::protobuf::RepeatedPtrField<Protos::Immortals::Debug_Point> &points);
void drawLinesUdp(const google::protobuf::RepeatedPtrField<Protos::Immortals::Debug_Line> &lines);

void applyShader();

RenderTexture visualizaionTexture, shaderVisualizationTexture;
Expand Down
72 changes: 72 additions & 0 deletions source/gui/renderer/renderer_drawudp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "renderer.h"

namespace Tyr::Gui
{
void Renderer::drawCirclesUdp(const google::protobuf::RepeatedPtrField<Protos::Immortals::Debug_Circle> &circles)
{
for (const auto &circle : circles)
{
auto position = Common::Vec2(circle.x(), circle.y());
auto rad = static_cast<float>(circle.r());

Color color = {.r = static_cast<unsigned char>(circle.col_r()),
.g = static_cast<unsigned char>(circle.col_g()),
.b = static_cast<unsigned char>(circle.col_b()),
.a = 255};

drawCircle({position, rad}, color, true, 255);
}
}

void Renderer::drawRectsUdp(const google::protobuf::RepeatedPtrField<Protos::Immortals::Debug_Rect> &rects)
{
for (const auto &rect : rects)
{
auto p1 = Common::Vec2(rect.x1(), rect.y1());
auto p2 = Common::Vec2(rect.x2(), rect.y2());

Common::Rect draw_rect(p1, p2);

Color color = {.r = static_cast<unsigned char>(rect.col_r()),
.g = static_cast<unsigned char>(rect.col_g()),
.b = static_cast<unsigned char>(rect.col_b()),
.a = 255};

drawRect(draw_rect, color, false, 1.f, 255);
}
}

void Renderer::drawPointsUdp(const google::protobuf::RepeatedPtrField<Protos::Immortals::Debug_Point> &points)
{
for (const auto &point : points)
{
auto position = Common::Vec2(point.x(), point.y());

Color color = {.r = static_cast<unsigned char>(point.col_r()),
.g = static_cast<unsigned char>(point.col_g()),
.b = static_cast<unsigned char>(point.col_b()),
.a = 255};

drawCircle({position, 20}, color, true, 255);
}
}

void Renderer::drawLinesUdp(const google::protobuf::RepeatedPtrField<Protos::Immortals::Debug_Line> &lines)
{
for (const auto &line : lines)
{
Common::LineSegment line_segment;

line_segment.start = Common::Vec2(line.x1(), line.y1());
line_segment.end = Common::Vec2(line.x2(), line.y2());

Color color = {.r = static_cast<unsigned char>(line.col_r()),
.g = static_cast<unsigned char>(line.col_g()),
.b = static_cast<unsigned char>(line.col_b()),
.a = 255};

drawLineSegment(line_segment, color);
}
}

} // namespace Tyr::Gui

0 comments on commit 93044c6

Please sign in to comment.