add rendering routines for devices

This commit is contained in:
Robert 2023-06-30 20:19:26 +02:00
parent f99ae219bb
commit 7465b18ae8
6 changed files with 93 additions and 2 deletions

View file

@ -3,10 +3,12 @@ add_subdirectory(3rdparty/imgui)
add_executable(netsim
"src/main.cpp"
"src/NetworkRenderer.cpp"
"src/DeviceRenderer.cpp"
)
target_sources(netsim PRIVATE ${IMGUI_SOURCES})
target_include_directories(netsim PRIVATE ${IMGUI_INCLUDE_DIR})
target_include_directories(netsim PRIVATE ${IMGUI_INCLUDE_DIR} include)
target_link_libraries(netsim PRIVATE
libnetsim

View file

@ -0,0 +1,20 @@
#pragma once
#include <memory>
#include <SDL.h>
#include "Device.hpp"
struct SDL_Renderer;
class DeviceRenderer {
public:
DeviceRenderer(std::shared_ptr<Device> device, uint16_t x, uint16_t y);
void Render(SDL_Renderer* renderer) const;
void Deselect();
private:
SDL_FRect bbox;
bool selected;
std::shared_ptr<Device> device;
};

View file

@ -0,0 +1,17 @@
#pragma once
#include <vector>
#include "Network.hpp"
#include "DeviceRenderer.hpp"
class NetworkRenderer {
public:
NetworkRenderer(Network& network);
void AddDevice(std::shared_ptr<Device> device, uint16_t x, uint16_t y);
void Render(SDL_Renderer* renderer) const;
private:
Network& network;
std::vector<DeviceRenderer> devices;
};

View file

@ -0,0 +1,26 @@
#include "DeviceRenderer.hpp"
#include <SDL.h>
DeviceRenderer::DeviceRenderer(std::shared_ptr<Device> device, uint16_t x, uint16_t y) :
device(device), selected(false)
{
bbox.x = x;
bbox.y = y;
bbox.w = 50;
bbox.h = 50;
}
void DeviceRenderer::Deselect() {
selected = false;
}
void DeviceRenderer::Render(SDL_Renderer* renderer) const {
SDL_SetRenderDrawColor(renderer, 150, 150, 150, 255);
SDL_RenderFillRect(renderer, &bbox);
if (selected) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &bbox);
}
}

View file

@ -0,0 +1,19 @@
#include "NetworkRenderer.hpp"
#include "DeviceRenderer.hpp"
NetworkRenderer::NetworkRenderer(Network& network) :
network(network)
{
}
void NetworkRenderer::AddDevice(std::shared_ptr<Device> device, uint16_t x, uint16_t y) {
devices.push_back(DeviceRenderer(device, x, y));
}
void NetworkRenderer::Render(SDL_Renderer* renderer) const {
for (const DeviceRenderer& device : devices) {
device.Render(renderer);
}
}

View file

@ -6,6 +6,7 @@
#include "imgui_impl_sdlrenderer3.h"
#include "Netsim.hpp"
#include "NetworkRenderer.hpp"
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO);
@ -27,6 +28,7 @@ int main(int argc, char** argv) {
ImGui::StyleColorsDark();
Network network("Testnet");
NetworkRenderer networkRenderer(network);
bool shouldClose = false;
SDL_Event event;
@ -46,6 +48,8 @@ int main(int argc, char** argv) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
networkRenderer.Render(renderer);
ImGui_ImplSDL3_NewFrame();
ImGui_ImplSDLRenderer3_NewFrame();
ImGui::NewFrame();
@ -54,7 +58,10 @@ int main(int argc, char** argv) {
if (ImGui::BeginMenu("New...")) {
if (ImGui::MenuItem("Host")) {
network.addDevice(Device::create("12:34:56:78:9A:BC"));
auto device = Device::create("12:34:56:78:9A:BC");
network.addDevice(device);
networkRenderer.AddDevice(device, 400, 400);
};
ImGui::EndMenu();