Sync
This commit is contained in:
parent
d592cef1a0
commit
9d2785541d
35
deerith_engine_core/include/deerith/network/sync_variable.h
Normal file
35
deerith_engine_core/include/deerith/network/sync_variable.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <deerith/engine.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
#define BUFFER_SIZE (NETWORK_DELAY + 20)
|
||||||
|
|
||||||
|
namespace deerith
|
||||||
|
{
|
||||||
|
struct sync_variable_base_t
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~sync_variable_base_t() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct sync_variable_entry_t
|
||||||
|
{
|
||||||
|
T value;
|
||||||
|
uint32_t tick_id;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class sync_variable_t
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~sync_buffer_t() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<sync_variable_entry_t<T>, BUFFER_SIZE> buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class sync_variable_t
|
||||||
|
}
|
||||||
@ -1,29 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <deerith/engine.h>
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <array>
|
|
||||||
|
|
||||||
#define BUFFER_SIZE (NETWORK_DELAY + 20)
|
|
||||||
|
|
||||||
namespace deerith
|
|
||||||
{
|
|
||||||
class sync_buffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual ~sync_buffer() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
class float_sync_data : public sync_buffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
private:
|
|
||||||
std::array<float, BUFFER_SIZE> float_buffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
class vec2_sync_data : public sync_buffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
private:
|
|
||||||
std::array<glm::vec2, BUFFER_SIZE> buffer;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -1,19 +1,29 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <deerith/universe/sync_buffer.h>
|
#include <deerith/network/sync_variable.h>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace deerith
|
namespace deerith
|
||||||
{
|
{
|
||||||
class user
|
class user_t
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void *user_data = nullptr;
|
void *user_data = nullptr;
|
||||||
|
|
||||||
void add_sync_buffer(sync_buffer *sync_buffer);
|
template <typename T>
|
||||||
|
void register_sync_variable(sync_variable_t<T> *var);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<sync_buffer *> sync_buffer_container;
|
std::vector<sync_variable_base_t *> buffers;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void register_user_sync_variables_init_callback(std::function<void(user_t &)>);
|
||||||
|
void register_user_sync_variables_shutdown_callback(std::function<void(user_t &)>);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void deerith::user_t::register_sync_variable(sync_variable_t<T> *var)
|
||||||
|
{
|
||||||
|
buffers.push_back(var);
|
||||||
}
|
}
|
||||||
@ -1,13 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <deerith/universe/user.h>
|
#include <deerith/universe/user.h>
|
||||||
#include <deerith/universe/sync_buffer.h>
|
#include <deerith/network/sync_variable.h>
|
||||||
#include <deerith/universe/universe_definition.h>
|
#include <deerith/universe/universe_definition.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct sandbox_user_data_t
|
struct sandbox_user_data_t
|
||||||
{
|
{
|
||||||
deerith::vec2_sync_data position;
|
deerith::sync_variable_t<glm::vec2> position_sync;
|
||||||
};
|
};
|
||||||
|
|
||||||
void define_user();
|
void define_user();
|
||||||
@ -1,16 +1,25 @@
|
|||||||
#include "common/common.h"
|
#include "common/common.h"
|
||||||
#include <deerith/universe/universe_definition.h>
|
#include <deerith/universe/user.h>
|
||||||
#include <deerith/core/log.h>
|
#include <deerith/core/log.h>
|
||||||
|
|
||||||
void user_definition_function(deerith::user &user_data)
|
void user_sync_variables_init(deerith::user_t &user)
|
||||||
{
|
{
|
||||||
deerith_trace("adding user data to new user");
|
deerith_trace("removing user data to new user");
|
||||||
|
|
||||||
sandbox_user_data_t *sandbox_user_data = new sandbox_user_data_t();
|
sandbox_user_data_t *sandbox_user_data = new sandbox_user_data_t();
|
||||||
user_data.add_sync_buffer(&sandbox_user_data->position);
|
|
||||||
|
user.user_data = sandbox_user_data;
|
||||||
|
user.register_sync_variable(&sandbox_user_data->position_sync);
|
||||||
|
}
|
||||||
|
|
||||||
|
void user_sync_variables_shutdown(deerith::user_t &user)
|
||||||
|
{
|
||||||
|
deerith_trace("removing user");
|
||||||
|
delete ((sandbox_user_data_t *)user.user_data);
|
||||||
|
user.user_data = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void client_definition()
|
void client_definition()
|
||||||
{
|
{
|
||||||
deerith::define_client_callback(user_definition_function);
|
deerith::register_user_sync_variables_init_callback(user_sync_variables_init);
|
||||||
|
deerith::register_user_sync_variables_shutdown_callback(user_sync_variables_shutdown);
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user