Skip to content

Commit

Permalink
add boilerplate for in game chat
Browse files Browse the repository at this point in the history
  • Loading branch information
fallahn committed Aug 24, 2023
1 parent d14a32e commit 0ee1313
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 13 deletions.
4 changes: 2 additions & 2 deletions samples/golf/buildnumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef BUILD_NUMBER_H_
#define BUILD_NUMBER_H_

#define BUILDNUMBER 1371
#define BUILDNUMBER_STR "1371"
#define BUILDNUMBER 1376
#define BUILDNUMBER_STR "1376"

#endif /* BUILD_NUMBER_H_ */
2 changes: 2 additions & 0 deletions samples/golf/golf.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@
<ClCompile Include="src\golf\TerrainBuilder.cpp" />
<ClCompile Include="src\golf\TerrainChunks.cpp" />
<ClCompile Include="src\golf\TerrainDepthmap.cpp" />
<ClCompile Include="src\golf\TextChat.cpp" />
<ClCompile Include="src\golf\TrophyDisplaySystem.cpp" />
<ClCompile Include="src\golf\TrophyState.cpp" />
<ClCompile Include="src\golf\TutorialDirector.cpp" />
Expand Down Expand Up @@ -804,6 +805,7 @@
<ClInclude Include="src\golf\TerrainChunks.hpp" />
<ClInclude Include="src\golf\TerrainDepthmap.hpp" />
<ClInclude Include="src\golf\TextAnimCallback.hpp" />
<ClInclude Include="src\golf\TextChat.hpp" />
<ClInclude Include="src\golf\Treeset.hpp" />
<ClInclude Include="src\golf\TrophyDisplaySystem.hpp" />
<ClInclude Include="src\golf\TrophyState.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions samples/golf/golf.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@
<ClCompile Include="src\golf\GolfStateScoring.cpp">
<Filter>Source Files\golf\client\states</Filter>
</ClCompile>
<ClCompile Include="src\golf\TextChat.cpp">
<Filter>Source Files\golf\client</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\ErrorCheck.hpp">
Expand Down Expand Up @@ -770,6 +773,9 @@
<ClInclude Include="src\golf\GcState.hpp">
<Filter>Header Files\golf\client\states</Filter>
</ClInclude>
<ClInclude Include="src\golf\TextChat.hpp">
<Filter>Header Files\golf\client</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="src\golf\WaterShader.inl">
Expand Down
9 changes: 0 additions & 9 deletions samples/golf/src/GolfGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,6 @@ void GolfGame::handleEvent(const cro::Event& evt)
case SDLK_KP_PLUS:
togglePixelScale(m_sharedData, true);
break;
#ifdef USE_GNS
case SDLK_F8:
if (evt.key.keysym.mod & KMOD_SHIFT)
{
//Social::toggleChat();
//cro::App::getWindow().setMouseCaptured(false);
}
break;
#endif
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions samples/golf/src/golf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ set(GOLF_SRC
${PROJECT_DIR}/golf/TerrainBuilder.cpp
${PROJECT_DIR}/golf/TerrainChunks.cpp
${PROJECT_DIR}/golf/TerrainDepthmap.cpp
${PROJECT_DIR}/golf/TextChat.cpp
${PROJECT_DIR}/golf/TrophyDisplaySystem.cpp
${PROJECT_DIR}/golf/TrophyState.cpp
${PROJECT_DIR}/golf/TutorialDirector.cpp
Expand Down
12 changes: 11 additions & 1 deletion samples/golf/src/golf/GolfState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ GolfState::GolfState(cro::StateStack& stack, cro::State::Context context, Shared
m_skyScene (context.appInstance.getMessageBus(), 512),
m_uiScene (context.appInstance.getMessageBus(), 1024),
m_trophyScene (context.appInstance.getMessageBus()),
m_textChat (m_uiScene, sd),
m_inputParser (sd, &m_gameScene),
m_cpuGolfer (m_inputParser, m_currentPlayer, m_collisionMesh),
m_wantsGameState (true),
Expand Down Expand Up @@ -522,6 +523,12 @@ bool GolfState::handleEvent(const cro::Event& evt)
case SDLK_F6:
logCSV();
break;
case SDLK_F8:
if (evt.key.keysym.mod & KMOD_SHIFT)
{
m_textChat.toggleWindow();
}
break;
#ifdef CRO_DEBUG_
case SDLK_F2:
m_sharedData.clientConnection.netClient.sendPacket(PacketID::ServerCommand, std::uint8_t(ServerCommand::NextHole), net::NetFlag::Reliable);
Expand Down Expand Up @@ -5456,7 +5463,7 @@ void GolfState::spawnBullsEye(const BullsEye& b)
}
}

float scale = cro::Util::Easing::easeOutElastic(progress) * targetScale;
float scale = cro::Util::Easing::easeOutBack(progress) * targetScale;
e.getComponent<cro::Transform>().setScale(glm::vec3(scale));
e.getComponent<cro::Transform>().rotate(cro::Transform::Y_AXIS, dt * 0.2f);
};
Expand All @@ -5483,6 +5490,9 @@ void GolfState::handleNetEvent(const net::NetEvent& evt)
switch (evt.packet.getID())
{
default: break;
case PacketID::ChatMessage:
m_textChat.handlePacket(evt.packet);
break;
case PacketID::FlagHit:
{
auto data = evt.packet.as<BullHit>();
Expand Down
3 changes: 3 additions & 0 deletions samples/golf/src/golf/GolfState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ source distribution.
#include "TerrainChunks.hpp"
#include "MinimapZoom.hpp"
#include "BallTrail.hpp"
#include "TextChat.hpp"
#include "server/ServerPacketData.hpp"

#include <crogine/core/State.hpp>
Expand Down Expand Up @@ -122,6 +123,8 @@ class GolfState final : public cro::State, public cro::GuiClient, public cro::Co
cro::Scene m_trophyScene;
TerrainDepthmap m_depthMap;

TextChat m_textChat;

InputParser m_inputParser;
CPUGolfer m_cpuGolfer;
cro::Clock m_turnTimer;
Expand Down
15 changes: 14 additions & 1 deletion samples/golf/src/golf/PacketIDs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ source distribution.

#include <cstdint>
#include <string>
#include <array>

namespace ScoreType
{
Expand Down Expand Up @@ -89,6 +90,17 @@ namespace MaxStrokeID
};
}

struct TextMessage final
{
std::uint8_t client = 0;

//this is the max message bytes - total message length is
//this plus 1 for nullterm
static constexpr std::size_t MaxBytes = 8192;
std::array<char, MaxBytes + 1> messageData = {};
TextMessage() { std::fill(messageData.begin(), messageData.end(), 0); }
};

namespace PacketID
{
enum
Expand Down Expand Up @@ -158,7 +170,8 @@ namespace PacketID
Emote, //< uint32 00|client|player|emoteID
LevelUp, //< uint64 00|00|client|player|level (level is 4 bytes)
BallPrediction, //< InputUpdate if from client, vec3 if from server
PlayerXP //<uint16 level << 8 | client - used to share client xp/level info
PlayerXP, //<uint16 level << 8 | client - used to share client xp/level info
ChatMessage //TextMessage struct
};
}

Expand Down
80 changes: 80 additions & 0 deletions samples/golf/src/golf/TextChat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*-----------------------------------------------------------------------
Matt Marchant 2023
http://trederia.blogspot.com
Super Video Golf - zlib licence.
This software is provided 'as-is', without any express or
implied warranty.In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions :
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
-----------------------------------------------------------------------*/

#include "TextChat.hpp"
#include "PacketIDs.hpp"

TextChat::TextChat(cro::Scene& s, SharedStateData& sd)
: m_scene (s),
m_sharedData(sd),
m_visible (false)
{
registerWindow([&]()
{
if (m_visible)
{
ImGui::SetNextWindowSize({ 400.f, 100.f });
if (ImGui::Begin("Chat Window", &m_visible))
{


}
ImGui::End();
}
});
}

//public
void TextChat::handlePacket(const net::NetEvent::Packet& pkt)
{
const auto msg = pkt.as<TextMessage>();
//only one person can type on a connected computer anyway
//so we'll always assume it's player 0
const auto& name = m_sharedData.connectionData[msg.client].playerData[0].name;

std::string textStr = msg.messageData.data();

//TODO process any emotes such as /me and choose colour

m_displayBuffer.emplace_back(textStr, ImVec4(1.f, 0.f, 0.f, 0.f));
if (m_displayBuffer.size() > MaxLines)
{
m_displayBuffer.pop_front();
}

cro::String screenMsg = cro::String::fromUtf8(textStr.begin(), textStr.end());
//TODO create an entity to temporarily show the message on screen
}


//private
void TextChat::sendTextChat()
{

}
69 changes: 69 additions & 0 deletions samples/golf/src/golf/TextChat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*-----------------------------------------------------------------------
Matt Marchant 2023
http://trederia.blogspot.com
Super Video Golf - zlib licence.
This software is provided 'as-is', without any express or
implied warranty.In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions :
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
-----------------------------------------------------------------------*/

#pragma once

#include "SharedStateData.hpp"

#include <crogine/ecs/Scene.hpp>
#include <crogine/gui/Gui.hpp>
#include <crogine/gui/GuiClient.hpp>

#include <deque>

class TextChat final : public cro::GuiClient
{
public:
TextChat(cro::Scene&, SharedStateData&);

void handlePacket(const net::NetEvent::Packet&);

void toggleWindow() { m_visible = !m_visible; }

private:

cro::Scene& m_scene;
SharedStateData& m_sharedData;
bool m_visible;

struct BufferLine final
{
const std::string str;
ImVec4 colour;

BufferLine(const std::string& s, ImVec4 c)
: str(s), colour(c) {}
};

static constexpr std::size_t MaxLines = 24;
std::deque<BufferLine> m_displayBuffer;
std::string m_inputBuffer;

void sendTextChat();
};

0 comments on commit 0ee1313

Please sign in to comment.