diff --git a/include/game/World.h b/include/game/World.h index 61c78ec..53eb4c6 100644 --- a/include/game/World.h +++ b/include/game/World.h @@ -56,6 +56,13 @@ struct Color { std::uint8_t r, g, b; }; +static constexpr Color BLACK{ 0, 0, 0 }; +static constexpr Color WHITE{ 255, 255, 255 }; + +static constexpr Color RED{ 255, 0, 0 }; +static constexpr Color GREEN{ 0, 255, 0 }; +static constexpr Color BLUE{ 0, 0, 255 }; + struct Tile { virtual TileType getType() const = 0; }; @@ -134,6 +141,7 @@ protected: TowerTileColorPalette m_TowerPlacePalette; Color m_WalkablePalette; std::vector m_DecorationPalette; + Color m_Background; std::unordered_map m_Chunks; @@ -172,6 +180,7 @@ public: const TowerTileColorPalette& getTowerTileColorPalette() const { return m_TowerPlacePalette; } const Color& getWalkableTileColor() const { return m_WalkablePalette; } const std::vector& getDecorationPalette() const { return m_DecorationPalette; } + const Color& getBackgroundColor() const { return m_Background; } const TilePalette& getTilePalette() const { return m_TilePalette; } diff --git a/include/game/client/ClientGame.h b/include/game/client/ClientGame.h index cb11294..04d0bc7 100644 --- a/include/game/client/ClientGame.h +++ b/include/game/client/ClientGame.h @@ -37,7 +37,7 @@ public: Client* getClient() const { return m_Client; } render::Renderer* getRenderer() const { return m_Renderer; } - WorldClient& getWorld() { return m_WorldClient; } + WorldClient& getWorldClient() { return m_WorldClient; } virtual void HandlePacket(const protocol::ConnexionInfoPacket* packet); virtual void HandlePacket(const protocol::PlayerJoinPacket* packet); diff --git a/include/protocol/Protocol.h b/include/protocol/Protocol.h index 2ce34fb..6e0a8ba 100644 --- a/include/protocol/Protocol.h +++ b/include/protocol/Protocol.h @@ -45,6 +45,7 @@ struct WorldHeader { game::TowerTileColorPalette m_TowerPlacePalette; game::Color m_WalkablePalette; std::vector m_DecorationPalette; + game::Color m_Background; game::SpawnColorPalette m_SpawnColorPalette; @@ -128,6 +129,7 @@ public: const game::TowerTileColorPalette& getTowerTilePalette() const { return m_Header.m_TowerPlacePalette; } const game::Color& getWalkableTileColor() const { return m_Header.m_WalkablePalette; } const std::vector& getDecorationPalette() const { return m_Header.m_DecorationPalette; } + const game::Color& getBackgroundColor() const { return m_Header.m_Background; } const game::Spawn& getRedSpawn() const { return m_Header.m_RedSpawn; } const game::Spawn& getBlueSpawn() const { return m_Header.m_BlueSpawn; } diff --git a/include/render/Renderer.h b/include/render/Renderer.h index 5e85437..6c1e087 100644 --- a/include/render/Renderer.h +++ b/include/render/Renderer.h @@ -29,6 +29,8 @@ private: std::unique_ptr m_WorldShader; std::unique_ptr m_EntityShader; + glm::vec3 m_BackgroundColor; + bool m_IsometricView = true; float m_IsometricShade = m_IsometricView; glm::vec2 m_CamPos{}; @@ -49,6 +51,8 @@ public: void setCamPos(const glm::vec2& newPos); void setIsometricView(bool isometric); // false = 2D true = Isometric + void setBackgroundColor(const glm::vec3& color) { m_BackgroundColor = color; } + glm::vec2 getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight); private: void updateIsometricView(); diff --git a/src/game/World.cpp b/src/game/World.cpp index fa8a995..7b7f9db 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -37,6 +37,7 @@ bool World::loadMap(const protocol::WorldBeginDataPacket* worldHeader) { m_TowerPlacePalette = worldHeader->getTowerTilePalette(); m_WalkablePalette = worldHeader->getWalkableTileColor(); m_DecorationPalette = worldHeader->getDecorationPalette(); + m_Background = worldHeader->getBackgroundColor(); getRedTeam().getSpawn() = worldHeader->getRedSpawn(); getBlueTeam().getSpawn() = worldHeader->getBlueSpawn(); diff --git a/src/game/client/ClientGame.cpp b/src/game/client/ClientGame.cpp index 793cfa5..4afa04c 100644 --- a/src/game/client/ClientGame.cpp +++ b/src/game/client/ClientGame.cpp @@ -93,6 +93,7 @@ void ClientGame::HandlePacket(const protocol::UpdateMoneyPacket* packet) { void ClientGame::HandlePacket(const protocol::DisconnectPacket* packet) { m_GameState = game::GameState::Disconnected; + m_Renderer->setBackgroundColor({ 0, 0, 0 }); } void ClientGame::HandlePacket(const protocol::WorldDataPacket* packet) { diff --git a/src/game/client/WorldClient.cpp b/src/game/client/WorldClient.cpp index ca54f7c..d7a1a20 100644 --- a/src/game/client/WorldClient.cpp +++ b/src/game/client/WorldClient.cpp @@ -17,6 +17,11 @@ WorldClient::WorldClient(ClientGame* game) : game::World(game), protocol::Packet void WorldClient::HandlePacket(const protocol::WorldBeginDataPacket* packet) { loadMap(packet); + if (m_Game->getGameState() == game::GameState::Game) { + const game::Color& backgroundColor = getBackgroundColor(); + m_Game->getRenderer()->setBackgroundColor({ static_cast(backgroundColor.r / 255.0f), static_cast(backgroundColor.g / 255.0f), + static_cast(backgroundColor.b / 255.0f) }); + } } void WorldClient::HandlePacket(const protocol::WorldDataPacket* packet) { diff --git a/src/protocol/Protocol.cpp b/src/protocol/Protocol.cpp index c779a99..a436cc1 100644 --- a/src/protocol/Protocol.cpp +++ b/src/protocol/Protocol.cpp @@ -91,6 +91,8 @@ DataBuffer WorldBeginDataPacket::SerializeCustom() const { memcpy((std::uint8_t*)data.data() + bufferSize, decoTilePalette.data(), decoTilePalette.size() * sizeof(game::Color)); + data << m_Header.m_Background; + const game::Spawn& redSpawn = m_Header.m_RedSpawn, blueSpawn = m_Header.m_BlueSpawn; const game::TeamCastle& redCastle = m_Header.m_RedCastle, blueCastle = m_Header.m_BlueCastle; @@ -123,6 +125,8 @@ DataBuffer WorldBeginDataPacket::Serialize() const { memcpy((std::uint8_t*)data.data() + bufferSize, decoTilePalette.data(), decoTilePalette.size() * sizeof(game::Color)); + data << m_Header.m_World->getBackgroundColor(); + const game::Spawn& redSpawn = m_Header.m_World->getRedTeam().getSpawn(), blueSpawn = m_Header.m_World->getBlueTeam().getSpawn(); const game::TeamCastle& redCastle = m_Header.m_World->getRedTeam().getCastle(), blueCastle = m_Header.m_World->getBlueTeam().getCastle(); @@ -155,6 +159,8 @@ void WorldBeginDataPacket::Deserialize(DataBuffer& data) { data.SetReadOffset(data.GetReadOffset() + decoPalletteSizeByte); + data >> m_Header.m_Background; + utils::shape::Rectangle redCastle, blueCastle; data >> m_Header.m_RedSpawn >> redCastle; diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index 23f6930..db3af9d 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -17,7 +17,7 @@ namespace td { namespace render { -Renderer::Renderer() { +Renderer::Renderer() : m_BackgroundColor(0, 0, 0) { } @@ -86,7 +86,7 @@ void Renderer::updateIsometricFade() { void Renderer::prepare() { glClear(GL_COLOR_BUFFER_BIT); - glClearColor(0, 0, 0, 0); + glClearColor(m_BackgroundColor.r, m_BackgroundColor.g, m_BackgroundColor.b, 0); updateIsometricFade(); } diff --git a/src/render/WorldRenderer.cpp b/src/render/WorldRenderer.cpp index 125f9ac..74fbd53 100644 --- a/src/render/WorldRenderer.cpp +++ b/src/render/WorldRenderer.cpp @@ -38,7 +38,7 @@ WorldRenderer::WorldRenderer(game::World* world, client::ClientGame* client) : m m_TowerPlacePopup = std::make_unique(m_Client->getClient()); m_MobTooltip = std::make_unique(m_Client->getClient()); m_CastleTooltip = std::make_unique(m_Client->getClient()); - m_Client->getWorld().getWorldNotifier().bindListener(this); + m_Client->getWorldClient().getWorldNotifier().bindListener(this); } void WorldRenderer::updateCursorPos() {