Files
Tower-Defense2/src/main.cpp

112 lines
2.7 KiB
C++

#include <iostream>
#include <sp/common/DataBuffer.h>
#include <sp/extensions/Compress.h>
#include <td/input/Display.h>
#include <td/game/World.h>
#include <td/protocol/packet/PacketSerialize.h>
#include <td/protocol/packet/Packets.h>
#include <td/render/renderer/EntityRenderer.h>
#include <td/render/renderer/WorldRenderer.h>
#include <td/simulation/RealTimeSimulation.h>
class WorldApply : public td::protocol::PacketHandler {
private:
td::game::World& m_World;
using td::protocol::PacketHandler::Handle;
public:
WorldApply(td::game::World& a_World) : m_World(a_World) {}
void Handle(const td::protocol::packets::WorldHeaderPacket& a_Header) override {
m_World.LoadMap(*a_Header);
}
void Handle(const td::protocol::packets::WorldDataPacket& a_Data) override {
m_World.LoadMap(*a_Data);
}
};
td::game::World GetWorld() {
sp::DataBuffer buffer;
buffer.ReadFile("test/tdmap.tdmap2");
sp::DataBuffer buffer1 = sp::zlib::Decompress(buffer, 84);
buffer.SetReadOffset(buffer.GetReadOffset() + 83);
sp::DataBuffer buffer2 = sp::zlib::Decompress(buffer, 511);
td::protocol::packets::WorldHeaderPacket header;
header.Read(buffer1);
td::protocol::packets::WorldDataPacket data;
data.Read(buffer2);
td::game::World w;
WorldApply wa(w);
td::protocol::PacketDispatcher d;
d.RegisterHandler(td::protocol::PacketID::WorldData, &wa);
d.RegisterHandler(td::protocol::PacketID::WorldHeader, &wa);
d.Dispatch(header);
d.Dispatch(data);
return w;
}
void FastForward(td::game::World& a_World, const td::sim::GameHistory& a_LockSteps) {
const td::FpFloat delta = td::FpFloat(1) / td::FpFloat(75);
for (const auto& lockstep : a_LockSteps) {
a_World.Tick(lockstep, delta);
}
}
td::sim::GameHistory GetCustomHistory() {
constexpr std::size_t MAX_COUNT = 20 * 60 * 40;
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);
gh[0].push_back(spawn);
return gh;
}
int main(int argc, char** argv) {
td::game::World w = GetWorld();
// init GL context
td::Display display(1920, 1080, "Tower-Defense 2");
td::render::Camera cam;
display.OnAspectRatioChange.Connect([&cam](float a_AspectRatio){
cam.UpdatePerspective(a_AspectRatio);
});
td::sim::GameHistory gh = GetCustomHistory();
td::render::RenderPipeline renderer;
renderer.AddRenderer<td::render::WorldRenderer>(cam, w);
renderer.AddRenderer<td::render::EntityRenderer>(cam, w);
cam.SetCamPos({77, 5, 13});
cam.UpdatePerspective(display.GetAspectRatio());
td::sim::RealTimeSimulation simulation(w, std::move(gh), 2000);
while (!display.IsCloseRequested()) {
display.PollEvents();
float lerp = simulation.Update();
renderer.Render(lerp);
display.Update();
}
return 0;
}