2 Commits

Author SHA1 Message Date
51dc910359 fastforward test in main 2025-08-01 13:28:53 +02:00
ced20ca991 less serialize code 2025-08-01 13:21:31 +02:00
15 changed files with 43 additions and 33 deletions

View File

@@ -61,7 +61,7 @@ class World {
TilePtr GetTilePtr(TileIndex index) const { TilePtr GetTilePtr(TileIndex index) const {
if (index == 0) if (index == 0)
return nullptr; return TilePtr(nullptr);
return m_TilePalette.at(index - 1); return m_TilePalette.at(index - 1);
} }

View File

@@ -67,7 +67,7 @@ using TileFactory = sp::MessageFactory<Tile, AllTiles>;
class TileHandler : public sp::GenericHandler<AllTiles> {}; class TileHandler : public sp::GenericHandler<AllTiles> {};
using TilePtr = std::shared_ptr<sp::SerializableMessage<TileFactory>>; using TilePtr = sp::SerializableMessage<TileFactory>;
// typedef std::shared_ptr<Tile> TilePtr; // typedef std::shared_ptr<Tile> TilePtr;
typedef std::vector<std::uint16_t> ChunkPalette; typedef std::vector<std::uint16_t> ChunkPalette;

View File

@@ -7,7 +7,6 @@
#include <memory> #include <memory>
#include <sp/common/GenericHandler.h> #include <sp/common/GenericHandler.h>
#include <sp/io/SerializableMessage.h>
#include <sp/protocol/ConcreteMessage.h> #include <sp/protocol/ConcreteMessage.h>
#include <sp/protocol/MessageDispatcher.h> #include <sp/protocol/MessageDispatcher.h>
#include <sp/protocol/MessageFactory.h> #include <sp/protocol/MessageFactory.h>
@@ -15,6 +14,8 @@
#include <td/common/NonCopyable.h> #include <td/common/NonCopyable.h>
#include <td/protocol/command/CommandData.h> #include <td/protocol/command/CommandData.h>
#include <sp/io/SerializableMessage.h>
namespace td { namespace td {
namespace protocol { namespace protocol {
@@ -58,9 +59,9 @@ using CommandDispatcher = sp::MessageDispatcher<CommandBase>;
using CommandFactory = sp::MessageFactory<CommandBase, AllCommands>; using CommandFactory = sp::MessageFactory<CommandBase, AllCommands>;
using LockStep = std::vector<std::shared_ptr<CommandBase>>; using CommandPtr = sp::SerializableMessage<CommandFactory>;
using CommandPtr = std::unique_ptr<sp::SerializableMessage<CommandFactory>>; using LockStep = std::vector<CommandPtr>;
} // namespace protocol } // namespace protocol
} // namespace td } // namespace td

View File

@@ -74,7 +74,7 @@ struct BeginGame {
struct LockSteps { struct LockSteps {
std::uint16_t m_FirstFrameNumber; std::uint16_t m_FirstFrameNumber;
Array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps; std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
}; };
struct WorldHeader { struct WorldHeader {

View File

@@ -40,8 +40,8 @@ namespace game {
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const TeamCastle& a_Castle); sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const TeamCastle& a_Castle);
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, TeamCastle& a_Castle); sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, TeamCastle& a_Castle);
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const ChunkPtr& a_Chunk); sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Spawn& a_Spawn);
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, ChunkPtr& a_Chunk); sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Spawn& a_Spawn);
} // namespace game } // namespace game
} // namespace td } // namespace td

View File

@@ -28,7 +28,7 @@ class RealTimeSimulation {
* \brief Replay constructor * \brief Replay constructor
* \param a_StepTime in ms * \param a_StepTime in ms
*/ */
RealTimeSimulation(game::World& a_World, const GameHistory& a_History, std::uint64_t a_StepTime); RealTimeSimulation(game::World& a_World, GameHistory&& a_History, std::uint64_t a_StepTime);
/** /**
* \brief Live update constructor (continuous game updates) * \brief Live update constructor (continuous game updates)

View File

@@ -82,10 +82,12 @@ td::sim::GameHistory GetCustomHistory() {
td::sim::GameHistory gh(MAX_COUNT); td::sim::GameHistory gh(MAX_COUNT);
auto spawn = std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0); auto spawn = td::protocol::CommandPtr(
std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
gh[0].push_back(spawn); gh[0].push_back(spawn);
auto tower = std::make_shared<td::protocol::commands::PlaceTowerCommand>(td::TowerType::Archer, 0, td::TowerCoords{77, 13}); auto tower = td::protocol::CommandPtr(
std::make_shared<td::protocol::commands::PlaceTowerCommand>(td::TowerType::Archer, 0, td::TowerCoords{77, 13}));
gh[0].push_back(tower); gh[0].push_back(tower);
return gh; return gh;
@@ -109,19 +111,21 @@ int main(int argc, char** argv) {
renderer.AddRenderer<td::render::EntityRenderer>(cam, w); renderer.AddRenderer<td::render::EntityRenderer>(cam, w);
renderer.AddRenderer<td::render::TowerRenderer>(cam, w); renderer.AddRenderer<td::render::TowerRenderer>(cam, w);
cam.SetCamPos({77, 5, 13}); cam.SetCamPos({77, 10, 13});
cam.UpdatePerspective(display.GetAspectRatio()); cam.UpdatePerspective(display.GetAspectRatio());
td::sim::RealTimeSimulation simulation(w, 500); td::sim::RealTimeSimulation simulation(w, 50);
display.OnKeyDown.Connect([&simulation](SDL_Keycode key) { display.OnKeyDown.Connect([&simulation](SDL_Keycode key) {
static int counter = 0;
if (key == SDLK_A) { if (key == SDLK_A) {
auto spawn = auto spawn = td::protocol::CommandPtr(
std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0); std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
td::Array<td::protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps{}; std::array<td::protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps{};
steps[0].push_back(spawn); steps[0].push_back(spawn);
td::protocol::packets::LockStepsPacket packet{0, steps}; td::protocol::packets::LockStepsPacket packet{counter * LOCKSTEP_BUFFER_SIZE * 3, steps};
simulation.HandlePacket(packet); simulation.HandlePacket(packet);
counter++;
} }
}); });

View File

@@ -2,6 +2,7 @@
#include <sp/common/DataBuffer.h> #include <sp/common/DataBuffer.h>
#include <sp/common/ByteSwapping.h> #include <sp/common/ByteSwapping.h>
#include <sp/common/DataBufferOperators.h>
namespace td { namespace td {

View File

@@ -3,17 +3,7 @@
namespace td { namespace td {
namespace game { namespace game {
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const ChunkPtr& a_Chunk) {
a_Buffer << a_Chunk->m_Palette;
a_Buffer << a_Chunk->m_Data;
return a_Buffer;
}
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, ChunkPtr& a_Chunk) {
a_Chunk = std::make_shared<td::game::Chunk>();
a_Buffer >> a_Chunk->m_Palette;
return a_Buffer >> a_Chunk->m_Data;
}
} // namespace game } // namespace game
} // namespace td } // namespace td

View File

@@ -1,6 +1,8 @@
#include <td/protocol/packet/PacketData.h> #include <td/protocol/packet/PacketData.h>
#include <td/protocol/packet/PacketSerialize.h> #include <td/protocol/packet/PacketSerialize.h>
#include <sp/common/DataBufferOperators.h>
namespace td { namespace td {
namespace game { namespace game {
@@ -15,5 +17,17 @@ sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, TeamCastle& a_Castle) {
return a_Buffer; return a_Buffer;
} }
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Spawn& a_Spawn) {
return a_Buffer << a_Spawn.GetCenterX() << a_Spawn.GetCenterY();
}
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Spawn& a_Spawn) {
float x, y;
a_Buffer >> x >> y;
a_Spawn.SetCenter({x, y});
return a_Buffer;
}
} // namespace game } // namespace game
} // namespace td } // namespace td

View File

@@ -27,7 +27,7 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) {
td::game::TileIndex tileIndex = chunk->GetTileIndex(tileNumber); td::game::TileIndex tileIndex = chunk->GetTileIndex(tileNumber);
td::game::TilePtr tile = world->GetTilePtr(tileIndex); td::game::TilePtr tile = world->GetTilePtr(tileIndex);
if (tile == nullptr) if (!tile)
continue; continue;
positions.insert( positions.insert(

View File

@@ -33,7 +33,7 @@ void GameHistory::FromPacket(protocol::pdata::LockSteps&& a_Steps) {
} }
protocol::packets::LockStepsPacket GameHistory::ToPacket(HistorySizeType a_StartIndex) { protocol::packets::LockStepsPacket GameHistory::ToPacket(HistorySizeType a_StartIndex) {
Array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps; std::array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps;
for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) { for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) {
steps[i] = GetLockStep(a_StartIndex + i); steps[i] = GetLockStep(a_StartIndex + i);
} }

View File

@@ -12,7 +12,7 @@ std::uint64_t GetTime() {
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count()); std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count());
} }
RealTimeSimulation::RealTimeSimulation(game::World& a_World, const GameHistory& a_History, std::uint64_t a_StepTime) : RealTimeSimulation::RealTimeSimulation(game::World& a_World, GameHistory&& a_History, std::uint64_t a_StepTime) :
m_StepTime(a_StepTime), m_StepTime(a_StepTime),
m_World(a_World), m_World(a_World),
m_CurrentTime(0), m_CurrentTime(0),
@@ -21,8 +21,8 @@ RealTimeSimulation::RealTimeSimulation(game::World& a_World, const GameHistory&
m_LastSnapshot(std::make_shared<WorldSnapshot>()), m_LastSnapshot(std::make_shared<WorldSnapshot>()),
m_LastValidStep(0) { m_LastValidStep(0) {
m_History.reserve(a_History.size()); m_History.reserve(a_History.size());
for (const auto& lockstep : a_History) { for (auto&& lockstep : a_History) {
m_History.emplace_back(lockstep); m_History.emplace_back(std::move(lockstep));
} }
Step(); Step();
} }

Binary file not shown.

View File

@@ -3,7 +3,7 @@ add_rules("mode.debug", "mode.release")
add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git") add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git")
add_requires("imgui 1.92.0", {configs = {sdl3 = true, opengl3 = true}}) add_requires("imgui 1.92.0", {configs = {sdl3 = true, opengl3 = true}})
add_requires("splib 2.1.0", "zlib") add_requires("splib 2.2.0", "zlib")
add_requires("libsdl3 3.2.16", "glew", "fpm", "enet6") add_requires("libsdl3 3.2.16", "glew", "fpm", "enet6")
set_languages("c++17") set_languages("c++17")