diff --git a/include/Defines.h b/include/Defines.h new file mode 100644 index 0000000..ce5af68 --- /dev/null +++ b/include/Defines.h @@ -0,0 +1,52 @@ +#pragma once + +namespace td { + +template +struct Vec2 { + union { + T x; + T r; + }; + + union { + T y; + T g; + }; + + Vec2(T X = 0, T Y = 0) : x(X), y(Y) {} + + friend bool operator==(const Vec2& vec2, const Vec2& other) { return vec2.x == other.x && vec2.y == other.y; } +}; + +template +struct Vec3 { + union { + T x; + T r; + }; + + union { + T y; + T g; + }; + + union { + T z; + T b; + }; + + Vec3(T X = 0, T Y = 0, T Z = 0) : x(X), y(Y), z(Z) {} +}; + +using Vec2i = Vec2; +using Vec2u = Vec2; +using Vec2f = Vec2; +using Vec2d = Vec2; + +using Vec3i = Vec3; +using Vec3u = Vec3; +using Vec3f = Vec3; +using Vec3d = Vec3; + +} // namespace td diff --git a/include/game/Mobs.h b/include/game/Mobs.h index 6a409f2..90b4660 100644 --- a/include/game/Mobs.h +++ b/include/game/Mobs.h @@ -1,5 +1,6 @@ #pragma once +#include "Defines.h" #include "Towers.h" #include "Types.h" #include "Team.h" @@ -9,8 +10,6 @@ #include #include -#include - namespace td { namespace game { @@ -48,13 +47,13 @@ class MobStats { private: float m_Damage; float m_Speed; - glm::vec2 m_Size; + Vec2f m_Size; std::uint16_t m_MoneyCost; std::uint16_t m_ExpCost; std::uint16_t m_MaxLife; std::uint16_t m_ExpReward; public: - MobStats(float damage, float speed, glm::vec2 size, std::uint16_t moneyCost, + MobStats(float damage, float speed, Vec2f size, std::uint16_t moneyCost, std::uint16_t expCost, std::uint16_t expReward, std::uint16_t maxLife) : m_Damage(damage), m_Speed(speed), m_Size(size), m_MoneyCost(moneyCost), m_ExpCost(expCost), @@ -63,7 +62,7 @@ public: float GetDamage() const { return m_Damage; } float GetMovementSpeed() const { return m_Speed; } - const glm::vec2& GetSize() const { return m_Size; } + const Vec2f& GetSize() const { return m_Size; } std::uint16_t GetMoneyCost() const { return m_MoneyCost; } std::uint16_t GetExpCost() const { return m_ExpCost; } std::uint16_t GetExpReward() const { return m_ExpReward; } diff --git a/include/game/World.h b/include/game/World.h index a45bcf4..6c5ef5b 100644 --- a/include/game/World.h +++ b/include/game/World.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "Mobs.h" @@ -196,10 +195,10 @@ public: return m_TilePalette.at(index - 1); } - bool CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID player) const; - bool CanPlaceBigTower(const glm::vec2& worldPos, PlayerID player) const; + bool CanPlaceLittleTower(const Vec2f& worldPos, PlayerID player) const; + bool CanPlaceBigTower(const Vec2f& worldPos, PlayerID player) const; - TowerPtr GetTower(const glm::vec2& position) const; // returns null if no tower is here + TowerPtr GetTower(const Vec2f& position) const; // returns null if no tower is here const std::unordered_map& GetChunks() const { return m_Chunks; } diff --git a/include/game/client/Client.h b/include/game/client/Client.h index c31abae..f3555cf 100644 --- a/include/game/client/Client.h +++ b/include/game/client/Client.h @@ -44,7 +44,7 @@ public: void SelectTeam(game::TeamColor team); void SendMobs(const std::vector& mobSends); - void PlaceTower(game::TowerType type, const glm::vec2& position); + void PlaceTower(game::TowerType type, const Vec2f& position); void UpgradeTower(game::TowerID tower, game::TowerLevel level); void RemoveTower(game::TowerID tower); private: diff --git a/include/render/Renderer.h b/include/render/Renderer.h index 2e3550b..f0103fe 100644 --- a/include/render/Renderer.h +++ b/include/render/Renderer.h @@ -1,6 +1,6 @@ #pragma once -#include +#include "Defines.h" #include #include "loader/GLLoader.h" #include "render/shaders/WorldShader.h" @@ -15,17 +15,17 @@ public: struct Model { GL::VertexArray* vao; - glm::vec2 positon; + Vec2f positon; }; private: std::unique_ptr m_WorldShader; std::unique_ptr m_EntityShader; - glm::vec3 m_BackgroundColor; + Vec3f m_BackgroundColor; bool m_IsometricView = true; float m_IsometricShade = m_IsometricView; - glm::vec2 m_CamPos{}; + Vec2f m_CamPos{}; public: Renderer(); ~Renderer(); @@ -39,13 +39,13 @@ public: void RenderModel(const Model& model); void SetZoom(float zoom); - void SetCamMovement(const glm::vec2& mov); - void SetCamPos(const glm::vec2& newPos); + void SetCamMovement(const Vec2f& mov); + void SetCamPos(const Vec2f& newPos); void SetIsometricView(bool isometric); // false = 2D true = Isometric - void SetBackgroundColor(const glm::vec3& color) { m_BackgroundColor = color; } + void SetBackgroundColor(const Vec3f& color) { m_BackgroundColor = color; } - glm::vec2 GetCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight); + Vec2f GetCursorWorldPos(const Vec2f& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight); private: void UpdateIsometricView(); void UpdateIsometricFade(); diff --git a/include/render/WorldRenderer.h b/include/render/WorldRenderer.h index 5eac84a..273b189 100644 --- a/include/render/WorldRenderer.h +++ b/include/render/WorldRenderer.h @@ -11,8 +11,6 @@ #include "render/gui/imgui/imgui.h" -#include - namespace td { namespace client { @@ -30,10 +28,10 @@ private: Renderer* m_Renderer; game::World* m_World; std::unique_ptr m_WorldVao, m_MobVao, m_SelectTileVao; - glm::vec2 m_CamPos; - glm::vec2 m_CursorPos; - glm::vec2 m_HoldCursorPos; - glm::vec2 m_LastClicked; + Vec2f m_CamPos; + Vec2f m_CursorPos; + Vec2f m_HoldCursorPos; + Vec2f m_LastClicked; float m_Zoom; float m_CamSensibility = 1; bool m_PopupOpened = false; @@ -77,8 +75,8 @@ private: void DetectCastleHovering() const; void RenderTooltips() const; void RemoveTower(); - glm::vec2 GetCursorWorldPos() const; - glm::vec2 GetClickWorldPos() const; + Vec2f GetCursorWorldPos() const; + Vec2f GetClickWorldPos() const; void UpdateCursorPos(); }; diff --git a/include/render/gui/TowerPlacePopup.h b/include/render/gui/TowerPlacePopup.h index ebd49ed..96a2d7d 100644 --- a/include/render/gui/TowerPlacePopup.h +++ b/include/render/gui/TowerPlacePopup.h @@ -2,20 +2,20 @@ #include "GuiWidget.h" -#include +#include "Defines.h" namespace td { namespace gui { class TowerPlacePopup : public GuiWidget { private: - glm::vec2 m_ClickWorldPos; + Vec2f m_ClickWorldPos; public: TowerPlacePopup(client::Client* client); virtual void Render(); - void SetClickPos(const glm::vec2& worldPos); + void SetClickPos(const Vec2f& worldPos); private: static constexpr float m_TowerPopupTileWidth = 200.0f; static constexpr float m_TowerPopupTileHeight = 200.0f; diff --git a/include/render/shaders/EntityShader.h b/include/render/shaders/EntityShader.h index 3a280ff..824ade6 100644 --- a/include/render/shaders/EntityShader.h +++ b/include/render/shaders/EntityShader.h @@ -19,10 +19,10 @@ public: EntityShader(); void LoadShader(); - void SetCamPos(const glm::vec2& camPos); + void SetCamPos(const Vec2f& camPos); void SetZoom(float zoom); void SetAspectRatio(float aspectRatio); - void SetModelPos(const glm::vec2& modelPos); + void SetModelPos(const Vec2f& modelPos); void SetIsometricView(float isometric); }; diff --git a/include/render/shaders/ShaderProgram.h b/include/render/shaders/ShaderProgram.h index e9cd4b1..31f36d5 100755 --- a/include/render/shaders/ShaderProgram.h +++ b/include/render/shaders/ShaderProgram.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include "Defines.h" #include "render/GL.h" namespace td { @@ -24,11 +24,9 @@ protected: void LoadFloat(unsigned int location, float value) const; void LoadInt(unsigned int location, int value) const; - void LoadVector(unsigned int location, const glm::vec2& vector) const; - void LoadVector(unsigned int location, const glm::vec3& vector) const; - void LoadVector(unsigned int location, const glm::vec4& vector) const; + void LoadVector(unsigned int location, const Vec2f& vector) const; + void LoadVector(unsigned int location, const Vec3f& vector) const; void LoadBoolean(unsigned int location, bool value) const; - void LoadMatrix(unsigned int location, const glm::mat4& matrix) const; void CleanUp() const; private: diff --git a/include/render/shaders/WorldShader.h b/include/render/shaders/WorldShader.h index 001f9e3..58e972a 100644 --- a/include/render/shaders/WorldShader.h +++ b/include/render/shaders/WorldShader.h @@ -13,7 +13,7 @@ protected: public: WorldShader(); void LoadShader(); - void SetCamPos(const glm::vec2& camPos); + void SetCamPos(const Vec2f& camPos); void SetZoom(float zoom); void SetAspectRatio(float aspectRatio); void SetIsometricView(float isometric); diff --git a/src/game/World.cpp b/src/game/World.cpp index 25ea578..4ec503b 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -160,7 +160,7 @@ const Color* World::GetTileColor(TilePtr tile) const { return nullptr; } -bool World::CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID playerID) const { +bool World::CanPlaceLittleTower(const Vec2f& worldPos, PlayerID playerID) const { TilePtr tile = GetTile(worldPos.x, worldPos.y); const Player& player = m_Game->GetPlayers()[playerID]; @@ -186,7 +186,7 @@ bool World::CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID playerID) co return false; } -bool World::CanPlaceBigTower(const glm::vec2& worldPos, PlayerID playerID) const { +bool World::CanPlaceBigTower(const Vec2f& worldPos, PlayerID playerID) const { if (!CanPlaceLittleTower(worldPos, playerID)) return false; TilePtr tile = GetTile(worldPos.x, worldPos.y); @@ -224,7 +224,7 @@ void World::CleanDeadMobs() { } } -TowerPtr World::GetTower(const glm::vec2& position) const { +TowerPtr World::GetTower(const Vec2f& position) const { for (TowerPtr tower : m_Towers) { if (tower->GetSize() == TowerSize::Big) { if (tower->GetCenterX() - 2.5f < position.x && tower->GetCenterX() + 2.5f > position.x && diff --git a/src/game/client/Client.cpp b/src/game/client/Client.cpp index 8f57517..d30439c 100644 --- a/src/game/client/Client.cpp +++ b/src/game/client/Client.cpp @@ -59,7 +59,7 @@ void Client::SendMobs(const std::vector& mobSends) { m_Connexion.SendPacket(&packet); } -void Client::PlaceTower(game::TowerType type, const glm::vec2& position) { +void Client::PlaceTower(game::TowerType type, const Vec2f& position) { protocol::PlaceTowerPacket packet(position.x, position.y, type); m_Connexion.SendPacket(&packet); } diff --git a/src/game/server/ServerConnexion.cpp b/src/game/server/ServerConnexion.cpp index 2b80a7b..5960f86 100644 --- a/src/game/server/ServerConnexion.cpp +++ b/src/game/server/ServerConnexion.cpp @@ -151,11 +151,13 @@ void ServerConnexion::HandlePacket(const protocol::PlaceTowerPacket* packet) { const game::TowerInfo& towerInfo = game::GetTowerInfo(towerType); server::ServerWorld* world = m_Server->GetGame().GetServerWorld(); - if (!world->CanPlaceLittleTower({ packet->GetTowerX(), packet->GetTowerY() }, m_ID)) + Vec2f towerPos = { static_cast(packet->GetTowerX()), static_cast(packet->GetTowerY()) }; + + if (!world->CanPlaceLittleTower(towerPos, m_ID)) return; if (towerInfo.IsBigTower()) - if (!world->CanPlaceBigTower({ packet->GetTowerX(), packet->GetTowerY() }, m_ID)) + if (!world->CanPlaceBigTower(towerPos, m_ID)) return; game::TowerPtr tower = world->PlaceTowerAt(towerType, packet->GetTowerX(), packet->GetTowerY(), m_ID); diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index bbdb4ea..29c7198 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -39,6 +39,8 @@ void Renderer::InitShaders() { UpdateIsometricView(); } +// TODO : change loader check + bool Renderer::Init() { #if __has_include() glbinding::initialize(); @@ -110,13 +112,13 @@ void Renderer::SetZoom(float zoom) { m_EntityShader->SetZoom(zoom); } -void Renderer::SetCamMovement(const glm::vec2& mov) { +void Renderer::SetCamMovement(const Vec2f& mov) { m_CamPos.x += mov.x * (1 - m_IsometricView) + (0.5 * mov.x - mov.y) * m_IsometricView; m_CamPos.y += -mov.y * (1 - m_IsometricView) + (-0.5 * mov.x - mov.y) * m_IsometricView; SetCamPos(m_CamPos); } -void Renderer::SetCamPos(const glm::vec2& newPos) { +void Renderer::SetCamPos(const Vec2f& newPos) { m_CamPos = newPos; m_WorldShader->Start(); m_WorldShader->SetCamPos(newPos); @@ -128,7 +130,7 @@ void Renderer::SetIsometricView(bool isometric) { m_IsometricView = isometric; } -glm::vec2 Renderer::GetCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight) { +Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight) { float isometricEased = utils::EaseInOutExpo(m_IsometricShade); float relativeX = (cursorPos.x / windowWidth * 2) - 1; diff --git a/src/render/WorldRenderer.cpp b/src/render/WorldRenderer.cpp index 1c0ea4e..c1fad78 100644 --- a/src/render/WorldRenderer.cpp +++ b/src/render/WorldRenderer.cpp @@ -70,7 +70,7 @@ void WorldRenderer::Update() { } void WorldRenderer::RemoveTower() { - glm::vec2 cursorPos = GetCursorWorldPos(); + Vec2f cursorPos = GetCursorWorldPos(); game::TowerPtr clickedTower = m_World->GetTower(cursorPos); @@ -104,7 +104,7 @@ void WorldRenderer::RenderTileSelect() const { Renderer::Model tileSelectModel; tileSelectModel.vao = m_SelectTileVao.get(); - tileSelectModel.positon = { (int)m_CursorPos.x, (int)m_CursorPos.y }; + tileSelectModel.positon = { std::floor(m_CursorPos.x), std::floor(m_CursorPos.y) }; m_Renderer->RenderModel(tileSelectModel); } @@ -237,7 +237,7 @@ void WorldRenderer::RenderTowerUpgradePopup() { void WorldRenderer::DetectClick() { ImGuiIO& io = ImGui::GetIO(); if (ImGui::IsMouseReleased(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemFocused()) { - glm::vec2 cursorPos = { io.MousePos.x, io.MousePos.y }; + Vec2f cursorPos = { io.MousePos.x, io.MousePos.y }; if (cursorPos == m_HoldCursorPos) { m_LastClicked = m_HoldCursorPos; Click(); @@ -260,7 +260,7 @@ void WorldRenderer::RenderCastleTooltip() const { } void WorldRenderer::DetectMobHovering() const { - glm::vec2 cursorWorldPos = GetCursorWorldPos(); + Vec2f cursorWorldPos = GetCursorWorldPos(); for (game::MobPtr mob : m_World->GetMobList()) { if (mob->CollidesWith({ cursorWorldPos.x, cursorWorldPos.y })) { m_MobTooltip->SetMob(mob.get()); @@ -271,7 +271,7 @@ void WorldRenderer::DetectMobHovering() const { } void WorldRenderer::DetectCastleHovering() const { - glm::vec2 cursorWorldPos = GetCursorWorldPos(); + Vec2f cursorWorldPos = GetCursorWorldPos(); for (const game::Team& team : m_World->GetTeams()) { if (team.GetCastle().CollidesWith({ cursorWorldPos.x, cursorWorldPos.y })) { m_CastleTooltip->SetCastle(&team.GetCastle()); @@ -292,12 +292,12 @@ void WorldRenderer::OnTowerRemove(game::TowerPtr tower) { m_TowersCache.UpdateVertexArray(); } -glm::vec2 WorldRenderer::GetCursorWorldPos() const { +Vec2f WorldRenderer::GetCursorWorldPos() const { ImGuiIO& io = ImGui::GetIO(); return m_Renderer->GetCursorWorldPos({ io.MousePos.x, io.MousePos.y }, Display::GetAspectRatio(), m_Zoom, Display::GetWindowWidth(), Display::GetWindowHeight()); } -glm::vec2 WorldRenderer::GetClickWorldPos() const { +Vec2f WorldRenderer::GetClickWorldPos() const { return m_Renderer->GetCursorWorldPos(m_LastClicked, Display::GetAspectRatio(), m_Zoom, Display::GetWindowWidth(), Display::GetWindowHeight()); } diff --git a/src/render/gui/TowerPlacePopup.cpp b/src/render/gui/TowerPlacePopup.cpp index f06a508..432b759 100644 --- a/src/render/gui/TowerPlacePopup.cpp +++ b/src/render/gui/TowerPlacePopup.cpp @@ -57,7 +57,7 @@ void TowerPlacePopup::Render() { } } -void TowerPlacePopup::SetClickPos(const glm::vec2& worldPos) { +void TowerPlacePopup::SetClickPos(const Vec2f& worldPos) { m_ClickWorldPos = worldPos; } diff --git a/src/render/shaders/EntityShader.cpp b/src/render/shaders/EntityShader.cpp index 8c684e2..950ae99 100644 --- a/src/render/shaders/EntityShader.cpp +++ b/src/render/shaders/EntityShader.cpp @@ -107,7 +107,7 @@ void EntityShader::GetAllUniformLocation() { m_LocationViewtype = static_cast(GetUniformLocation("isometricView")); } -void EntityShader::SetCamPos(const glm::vec2& camPos) { +void EntityShader::SetCamPos(const Vec2f& camPos) { LoadVector(m_LocationCam, camPos); } void EntityShader::SetZoom(float zoom) { @@ -116,7 +116,7 @@ void EntityShader::SetZoom(float zoom) { void EntityShader::SetAspectRatio(float aspectRatio) { LoadFloat(m_LocationAspectRatio, aspectRatio); } -void EntityShader::SetModelPos(const glm::vec2& modelPos) { +void EntityShader::SetModelPos(const Vec2f& modelPos) { LoadVector(m_LocationTranslation, modelPos); } void EntityShader::SetIsometricView(float isometric) { diff --git a/src/render/shaders/ShaderProgram.cpp b/src/render/shaders/ShaderProgram.cpp index 23d6db2..4815c42 100755 --- a/src/render/shaders/ShaderProgram.cpp +++ b/src/render/shaders/ShaderProgram.cpp @@ -14,8 +14,6 @@ #include #include -#include - #ifdef __ANDROID__ #include #endif @@ -56,28 +54,19 @@ void ShaderProgram::LoadInt(unsigned int location, int value) const { } void ShaderProgram::LoadVector(unsigned int location, - const glm::vec2& vector) const { + const Vec2f& vector) const { glUniform2f(static_cast(location), vector.x, vector.y); } void ShaderProgram::LoadVector(unsigned int location, - const glm::vec3& vector) const { + const Vec3f& vector) const { glUniform3f(static_cast(location), vector.x, vector.y, vector.z); } -void ShaderProgram::LoadVector(unsigned int location, - const glm::vec4& vector) const { - glUniform4f(static_cast(location), vector.x, vector.y, vector.z, vector.w); -} - void ShaderProgram::LoadBoolean(unsigned int location, bool value) const { glUniform1i(static_cast(location), value); } -void ShaderProgram::LoadMatrix(unsigned int location, const glm::mat4& matrix) const { - glUniformMatrix4fv(static_cast(location), 1, false, glm::value_ptr(matrix)); -} - void ShaderProgram::CleanUp() const { Stop(); glDetachShader(m_ProgramID, m_VertexShaderID); diff --git a/src/render/shaders/WorldShader.cpp b/src/render/shaders/WorldShader.cpp index 51c4e91..26780c2 100644 --- a/src/render/shaders/WorldShader.cpp +++ b/src/render/shaders/WorldShader.cpp @@ -100,7 +100,7 @@ void WorldShader::GetAllUniformLocation() { m_LocationViewtype = static_cast(GetUniformLocation("isometricView")); } -void WorldShader::SetCamPos(const glm::vec2& camPos) { +void WorldShader::SetCamPos(const Vec2f& camPos) { LoadVector(m_LocationCam, camPos); } void WorldShader::SetZoom(float zoom) { diff --git a/xmake.lua b/xmake.lua index ecfca4c..ce24d65 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,7 +1,7 @@ add_rules("mode.debug", "mode.release") add_defines("TD_IMPL_OPENGL_LOADER_GLEW"); -add_requires("libsdl >= 2", "zlib", "glew", "glm") +add_requires("libsdl >= 2", "zlib", "glew") target("TowerDefense") set_kind("binary")