Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ x64/
*.json
imgui.ini
machine/include/
build/
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.16)
project(gctrl VERSION 0.0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Platform detection
if(WIN32)
set(GCTRL_PLATFORM_WINDOWS TRUE)
add_compile_definitions(GCTRL_PLATFORM_WINDOWS)
elseif(UNIX AND NOT APPLE)
set(GCTRL_PLATFORM_LINUX TRUE)
add_compile_definitions(GCTRL_PLATFORM_LINUX)
endif()

# Find dependencies
find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)

# Add subdirectories
add_subdirectory(lib)
add_subdirectory(engine)
69 changes: 69 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Main application source
set(ENGINE_SOURCES
main.cpp
)

# Platform-specific sources
if(GCTRL_PLATFORM_WINDOWS)
list(APPEND ENGINE_SOURCES
controls/network/udp_windows.cpp
platform/windows/process.cpp
platform/windows/dpi.cpp
platform/windows/font.cpp
)
elseif(GCTRL_PLATFORM_LINUX)
list(APPEND ENGINE_SOURCES
controls/network/udp_linux.cpp
platform/linux/process.cpp
platform/linux/dpi.cpp
platform/linux/font.cpp
)
endif()

add_executable(gctrl ${ENGINE_SOURCES})

target_include_directories(gctrl PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/lib
${CMAKE_SOURCE_DIR}/lib/nlohmann
${CMAKE_SOURCE_DIR}/lib/da0x
${CMAKE_SOURCE_DIR}/lib/IconFontCppHeaders
${CMAKE_SOURCE_DIR}/lib/L2DFileDialog/L2DFileDialog/src
)

target_link_libraries(gctrl PRIVATE
imgui
implot
imgui_node_editor
imgui_color_text_edit
SDL2::SDL2
OpenGL::GL
GLEW::GLEW
)

# Platform-specific linking and settings
if(GCTRL_PLATFORM_WINDOWS)
target_link_libraries(gctrl PRIVATE
ws2_32
dwmapi
shcore
)
# Windows GUI application (no console)
set_target_properties(gctrl PROPERTIES
WIN32_EXECUTABLE TRUE
)
elseif(GCTRL_PLATFORM_LINUX)
target_link_libraries(gctrl PRIVATE
pthread
)
endif()

# Copy font resources to build directory
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/fonts)
file(COPY
${CMAKE_CURRENT_SOURCE_DIR}/resources/fonts/cascadia-code.ttf
${CMAKE_CURRENT_SOURCE_DIR}/resources/fonts/consola.ttf
${CMAKE_CURRENT_SOURCE_DIR}/resources/fonts/fa-solid-900.otf
DESTINATION ${CMAKE_BINARY_DIR}/fonts
)
51 changes: 31 additions & 20 deletions engine/controls/debug/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

#pragma once


#include <map>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include "controls/network/udp.hpp"
#include <thread>
Expand All @@ -40,49 +40,60 @@ namespace controls {
std::thread listener;
};

static State state;
inline State& get_state() {
static State state;
return state;
}

void add_signal_value(uint64_t id, float value) {
inline void add_signal_value(uint64_t id, float value) {
auto& state = get_state();
std::lock_guard<std::mutex> lock(state.data_mutex);
if (state.signal_data[id].size() > 1000) {
state.signal_data[id].erase(state.signal_data[id].begin());
}
state.signal_data[id].push_back(value);
}

void listener_thread() {
network::udp::udp_listener listener("127.0.0.1", 8080);
inline void listener_thread() {
auto& state = get_state();
try {
network::udp::udp_listener listener("127.0.0.1", 8080);

listener.start([](uint64_t id, const std::vector<uint8_t>& data) {
if (data.size() < sizeof(float)) {
std::cerr << "Received data is too small: " << data.size() << " bytes\n";
return;
}
listener.start([](uint64_t id, const std::vector<uint8_t>& data) {
if (data.size() < sizeof(float)) {
std::cerr << "Received data is too small: " << data.size() << " bytes\n";
return;
}

float value;
std::memcpy(&value, data.data(), sizeof(float));
float value;
std::memcpy(&value, data.data(), sizeof(float));

std::cerr << "Received Signal ID: " << id << ", Value: " << value << "\n";
std::cerr << "Received Signal ID: " << id << ", Value: " << value << "\n";

add_signal_value(id, value);
add_signal_value(id, value);
});

while (state.keep_listening.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
while (state.keep_listening.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

listener.stop();
listener.stop();
} catch (const std::exception& e) {
std::cerr << "Debug listener failed: " << e.what() << "\n";
}
}

void begin() {
inline void begin() {
auto& state = get_state();
if (state.listener.joinable()) {
throw std::runtime_error("Listener thread already running!");
}
state.keep_listening.store(true);
state.listener = std::thread(listener_thread);
}

void end() {
inline void end() {
auto& state = get_state();
state.keep_listening.store(false);
if (state.listener.joinable()) {
state.listener.join();
Expand Down
6 changes: 3 additions & 3 deletions engine/controls/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ namespace controls {
}

void render_run_mode() {

std::lock_guard<std::mutex> lock(debug::state.data_mutex);
debug::viewer::render(machines, debug::state.signal_data);
auto& state = debug::get_state();
std::lock_guard<std::mutex> lock(state.data_mutex);
debug::viewer::render(machines, state.signal_data);
}

void view_menu(const std::string& label, const std::string& shortcut, ui::navigation::type navigation_type) {
Expand Down
19 changes: 14 additions & 5 deletions engine/controls/network/udp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@

#pragma once

#include "platform/platform.hpp"
#include <cstdint>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <functional>
#include <thread>
#include <mutex>
#include <string>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>

#pragma comment(lib, "Ws2_32.lib")
#if GCTRL_PLATFORM_WINDOWS
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
using socket_t = SOCKET;
constexpr socket_t INVALID_SOCKET_VALUE = INVALID_SOCKET;
#else
using socket_t = int;
constexpr socket_t INVALID_SOCKET_VALUE = -1;
#endif

namespace network {
namespace udp {
Expand All @@ -59,7 +68,7 @@ namespace network {
void stop();

private:
SOCKET socket_fd_;
socket_t socket_fd_;
bool stop_ = false;
std::thread listener_thread_;
void initialize_socket(const std::string& address, uint16_t port);
Expand All @@ -77,7 +86,7 @@ namespace network {
private:
std::string address_;
uint16_t port_;
SOCKET socket_fd_;
socket_t socket_fd_;
void initialize_socket();
void close_socket();
};
Expand Down
Loading