Refactor and client connection
This commit is contained in:
parent
29b106d002
commit
c943057fa2
@ -28,7 +28,8 @@ sudo apt install \
|
||||
libglm-dev \
|
||||
libspdlog-dev \
|
||||
libfmt-dev \
|
||||
libgl1-mesa-dev
|
||||
libgl1-mesa-dev \
|
||||
libimgui-dev
|
||||
```
|
||||
|
||||
## Building
|
||||
|
||||
@ -1 +0,0 @@
|
||||
#
|
||||
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
void begin_connection_to_universe(const char *address, int port);
|
||||
void shutdown_universe();
|
||||
bool is_universe_running();
|
||||
}
|
||||
@ -1,5 +1,7 @@
|
||||
#include "deerith/graphics/window.h"
|
||||
#include "primitive_internals.h"
|
||||
#include "../network/network_internals.h"
|
||||
#include <deerith/universe/universe.h>
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
@ -71,6 +73,7 @@ int deerith::init_window()
|
||||
|
||||
init_primitives();
|
||||
init_imgui();
|
||||
init_network();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -128,6 +131,8 @@ void deerith::shutdown_window()
|
||||
ImGui::DestroyContext();
|
||||
|
||||
shutdown_primitives();
|
||||
shutdown_universe();
|
||||
shutdown_network();
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
63
deerith_engine_client/src/network/network.cpp
Normal file
63
deerith_engine_client/src/network/network.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
#include <deerith/core/log.h>
|
||||
#include <enet/enet.h>
|
||||
|
||||
#include <deerith/core/log.h>
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "network_internals.h"
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
ENetHost *client;
|
||||
ENetPeer *client_peer;
|
||||
}
|
||||
|
||||
void deerith::connect_to_server(const char *address, int port)
|
||||
{
|
||||
ENetAddress eaddress = {0};
|
||||
eaddress.port = port;
|
||||
|
||||
int result = enet_address_set_host(&eaddress, address);
|
||||
deerith_client_assert(result == 0, "connect_to_server invalid address");
|
||||
|
||||
client = enet_host_create(NULL, 1, 2, 0, 0);
|
||||
deerith_client_assert(client, "connect_to_server failed to create host");
|
||||
|
||||
client_peer = enet_host_connect(client, &eaddress, 2, 0);
|
||||
deerith_client_assert(client_peer, "enet_host_connect failed on connect_to_server");
|
||||
}
|
||||
|
||||
int deerith::init_network()
|
||||
{
|
||||
if (enet_initialize() != 0)
|
||||
{
|
||||
deerith_client_error("An error occurred while initializing ENet.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void deerith::shutdown_network()
|
||||
{
|
||||
enet_deinitialize();
|
||||
}
|
||||
|
||||
bool deerith::wait_till_connected_to_server()
|
||||
{
|
||||
ENetEvent event;
|
||||
if (enet_host_service(client, &event, 5000) > 0 &&
|
||||
event.type == ENET_EVENT_TYPE_CONNECT)
|
||||
{
|
||||
deerith_client_info("Connected to server");
|
||||
return true;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
enet_peer_reset(client_peer);
|
||||
deerith_client_error("Failed to connect to server");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
10
deerith_engine_client/src/network/network_internals.h
Normal file
10
deerith_engine_client/src/network/network_internals.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
int init_network();
|
||||
void shutdown_network();
|
||||
|
||||
void connect_to_server(const char *address, int port);
|
||||
bool wait_till_connected_to_server();
|
||||
}
|
||||
60
deerith_engine_client/src/universe/universe.cpp
Normal file
60
deerith_engine_client/src/universe/universe.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include <deerith/universe/universe.h>
|
||||
#include <deerith/core/log.h>
|
||||
|
||||
#include "../network/network_internals.h"
|
||||
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
std::unique_ptr<std::thread> universe_thread = {};
|
||||
std::atomic<bool> universe_running = false;
|
||||
|
||||
void universe_simulation();
|
||||
}
|
||||
|
||||
void deerith::begin_connection_to_universe(const char *address, int port)
|
||||
{
|
||||
deerith_core_assert(!universe_running, "begin_connection_to_universe failed because there is a current running universe");
|
||||
|
||||
if (universe_thread && universe_thread->joinable())
|
||||
universe_thread->join();
|
||||
|
||||
universe_thread.reset();
|
||||
|
||||
connect_to_server(address, port);
|
||||
|
||||
universe_running = true;
|
||||
universe_thread = std::make_unique<std::thread>(universe_simulation);
|
||||
}
|
||||
|
||||
bool deerith::is_universe_running()
|
||||
{
|
||||
return universe_running;
|
||||
}
|
||||
|
||||
void deerith::universe_simulation()
|
||||
{
|
||||
bool connected = wait_till_connected_to_server();
|
||||
|
||||
if (connected)
|
||||
{
|
||||
while (universe_running)
|
||||
{
|
||||
deerith_client_trace("another thread");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
}
|
||||
|
||||
deerith_client_trace("Thread stop");
|
||||
universe_running = false;
|
||||
}
|
||||
|
||||
void deerith::shutdown_universe()
|
||||
{
|
||||
if (universe_thread && universe_thread->joinable())
|
||||
universe_thread->join();
|
||||
universe_running = false;
|
||||
universe_thread.reset();
|
||||
}
|
||||
@ -1,5 +1,18 @@
|
||||
add_library(DeerithCore)
|
||||
|
||||
find_path(ENET_INCLUDE_DIR enet/enet.h)
|
||||
find_library(ENET_LIBRARY enet)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
FetchContent_Declare(
|
||||
EnTT
|
||||
GIT_REPOSITORY https://github.com/skypjack/entt.git
|
||||
GIT_TAG v3.16.0
|
||||
)
|
||||
|
||||
FetchContent_MakeAvailable(EnTT)
|
||||
|
||||
find_package(spdlog REQUIRED)
|
||||
find_package(fmt REQUIRED)
|
||||
find_package(glm REQUIRED)
|
||||
@ -17,6 +30,7 @@ target_sources(DeerithCore
|
||||
target_include_directories(DeerithCore
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
${ENET_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(DeerithCore
|
||||
@ -24,4 +38,5 @@ target_link_libraries(DeerithCore
|
||||
spdlog::spdlog
|
||||
fmt::fmt
|
||||
glm::glm
|
||||
${ENET_LIBRARY}
|
||||
)
|
||||
@ -2,6 +2,7 @@
|
||||
#include <deerith/graphics/primitive.h>
|
||||
#include <deerith/graphics/input.h>
|
||||
#include <deerith/core/core.h>
|
||||
#include <deerith/universe/universe.h>
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <imgui.h>
|
||||
@ -10,6 +11,9 @@
|
||||
|
||||
glm::vec2 player_pos(5, 5);
|
||||
|
||||
char net_address[16] = "127.0.0.1";
|
||||
int port = 8080;
|
||||
|
||||
float vel = 1;
|
||||
|
||||
int main(int args_count, const char **args)
|
||||
@ -37,8 +41,22 @@ int main(int args_count, const char **args)
|
||||
{
|
||||
ImGui::SliderFloat("Velocity", &vel, 0, 4);
|
||||
|
||||
ImGui::End();
|
||||
if (deerith::is_universe_running())
|
||||
{
|
||||
ImGui::Text("Universe running");
|
||||
}
|
||||
else
|
||||
{
|
||||
ImGui::InputText("Net Address", net_address, 16);
|
||||
ImGui::InputInt("Net port", &port);
|
||||
|
||||
if (ImGui::Button("Connect"))
|
||||
{
|
||||
deerith::begin_connection_to_universe(net_address, port);
|
||||
}
|
||||
}
|
||||
}
|
||||
ImGui::End();
|
||||
|
||||
deerith::finish_window_display();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user