Skip to content

Commit

Permalink
Added IsPausable, InitialState and game version to cache descript…
Browse files Browse the repository at this point in the history
…or, improved console on Windows, refactoring
  • Loading branch information
deathkiller committed Oct 24, 2023
1 parent 52cb7e5 commit f69bc6e
Show file tree
Hide file tree
Showing 37 changed files with 691 additions and 369 deletions.
2 changes: 2 additions & 0 deletions Sources/Jazz2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@
<ClInclude Include="Jazz2\UI\Menu\InGameMenu.h" />
<ClInclude Include="Jazz2\UI\Menu\InputDiagnosticsSection.h" />
<ClInclude Include="Jazz2\UI\Menu\LanguageSelectSection.h" />
<ClInclude Include="Jazz2\UI\Menu\LoadingSection.h" />
<ClInclude Include="Jazz2\UI\Menu\MainMenu.h" />
<ClInclude Include="Jazz2\UI\Menu\MenuSection.h" />
<ClInclude Include="Jazz2\UI\Menu\OptionsSection.h" />
Expand Down Expand Up @@ -730,6 +731,7 @@
<ClCompile Include="Jazz2\UI\Menu\InGameMenu.cpp" />
<ClCompile Include="Jazz2\UI\Menu\InputDiagnosticsSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\LanguageSelectSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\LoadingSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\MainMenu.cpp" />
<ClCompile Include="Jazz2\UI\Menu\OptionsSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\PauseSection.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions Sources/Jazz2.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,9 @@
<ClInclude Include="$(ExtensionLibraryPath)\Threading\Interlocked.h">
<Filter>Header Files\Shared\Threading</Filter>
</ClInclude>
<ClInclude Include="Jazz2\UI\Menu\LoadingSection.h">
<Filter>Header Files\Jazz2\UI\Menu</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cpp">
Expand Down Expand Up @@ -2252,6 +2255,9 @@
<ClCompile Include="Jazz2\Actors\RemoteActor.cpp">
<Filter>Source Files\Jazz2\Actors</Filter>
</ClCompile>
<ClCompile Include="Jazz2\UI\Menu\LoadingSection.cpp">
<Filter>Source Files\Jazz2\UI\Menu</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resources.rc">
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/Actors/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ namespace Jazz2::Actors
void Player::OnUpdate(float timeMult)
{
#if defined(DEATH_DEBUG)
if (_levelHandler->PlayerActionPressed(_playerIndex, PlayerActions::ChangeWeapon)) {
if (PreferencesCache::AllowCheats && _levelHandler->PlayerActionPressed(_playerIndex, PlayerActions::ChangeWeapon)) {
float moveDistance = (_levelHandler->PlayerActionPressed(_playerIndex, PlayerActions::Run) ? 400.0f : 100.0f);
if (_levelHandler->PlayerActionHit(_playerIndex, PlayerActions::Left)) {
MoveInstantly(Vector2f(-moveDistance, 0.0f), MoveType::Relative | MoveType::Force);
Expand Down
1 change: 1 addition & 0 deletions Sources/Jazz2/ILevelHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace Jazz2
virtual Tiles::TileMap* TileMap() = 0;

virtual GameDifficulty Difficulty() const = 0;
virtual bool IsPausable() const = 0;
virtual bool IsReforged() const = 0;
virtual Recti LevelBounds() const = 0;
virtual float ElapsedFrames() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/IRootController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Jazz2
virtual void ChangeLevel(LevelInitialization&& levelInit) = 0;

#if defined(WITH_MULTIPLAYER)
virtual bool ConnectToServer(const char* address, std::uint16_t port) = 0;
virtual bool ConnectToServer(const StringView& address, std::uint16_t port) = 0;
virtual bool CreateServer(std::uint16_t port) = 0;
#endif

Expand Down
45 changes: 26 additions & 19 deletions Sources/Jazz2/LevelHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,20 @@ namespace Jazz2
{
float timeMult = theApplication().timeMult();

UpdatePressedActions();
if (_pauseMenu == nullptr) {
UpdatePressedActions();

if (PlayerActionHit(0, PlayerActions::Menu) && _pauseMenu == nullptr && _nextLevelType == ExitType::None) {
PauseGame();
}
if (PlayerActionHit(0, PlayerActions::Menu) && _nextLevelType == ExitType::None) {
PauseGame();
}
#if defined(DEATH_DEBUG)
if (PlayerActionPressed(0, PlayerActions::ChangeWeapon) && PlayerActionHit(0, PlayerActions::Jump)) {
_cheatsUsed = true;
BeginLevelChange(ExitType::Warp | ExitType::FastTransition, nullptr);
}
if (PreferencesCache::AllowCheats && PlayerActionPressed(0, PlayerActions::ChangeWeapon) && PlayerActionHit(0, PlayerActions::Jump)) {
_cheatsUsed = true;
BeginLevelChange(ExitType::Warp | ExitType::FastTransition, nullptr);
}
#endif
}

#if defined(WITH_AUDIO)
// Destroy stopped players and resume music after Sugar Rush
if (_sugarRushMusic != nullptr && _sugarRushMusic->isStopped()) {
Expand All @@ -245,7 +248,7 @@ namespace Jazz2
}
#endif

if (_pauseMenu == nullptr) {
if (!IsPausable() || _pauseMenu == nullptr) {
if (_nextLevelType != ExitType::None) {
_nextLevelTime -= timeMult;

Expand Down Expand Up @@ -291,7 +294,7 @@ namespace Jazz2
}
}

if (_difficulty != GameDifficulty::Multiplayer) {
if (/*_difficulty != GameDifficulty::Multiplayer*/true) {
if (!_players.empty()) {
auto& pos = _players[0]->GetPos();
int32_t tx1 = (int32_t)pos.X / Tiles::TileSet::DefaultTileSize;
Expand Down Expand Up @@ -1613,22 +1616,26 @@ namespace Jazz2
{
// Show in-game pause menu
_pauseMenu = std::make_shared<UI::Menu::InGameMenu>(this);
// Prevent updating of all level objects
_rootNode->setUpdateEnabled(false);
if (IsPausable()) {
// Prevent updating of all level objects
_rootNode->setUpdateEnabled(false);
}

#if defined(WITH_AUDIO)
// Use low-pass filter on music and pause all SFX
if (_music != nullptr) {
_music->setLowPass(0.1f);
}
for (auto& sound : _playingSounds) {
if (sound->isPlaying()) {
sound->pause();
if (IsPausable()) {
for (auto& sound : _playingSounds) {
if (sound->isPlaying()) {
sound->pause();
}
}
// If Sugar Rush music is playing, pause it and play normal music instead
if (_sugarRushMusic != nullptr && _music != nullptr) {
_music->play();
}
}
// If Sugar Rush music is playing, pause it and play normal music instead
if (_sugarRushMusic != nullptr && _music != nullptr) {
_music->play();
}
#endif
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Jazz2/LevelHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ namespace Jazz2
return _difficulty;
}

bool IsPausable() const override {
return true;
}

bool IsReforged() const override {
return _isReforged;
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Jazz2/Multiplayer/MultiLevelHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace Jazz2::Multiplayer
MultiLevelHandler(IRootController* root, NetworkManager* networkManager, const LevelInitialization& levelInit);
~MultiLevelHandler() override;

bool IsPausable() const override {
return false;
}

float GetAmbientLight() const override;
void SetAmbientLight(float value) override;

Expand Down
6 changes: 4 additions & 2 deletions Sources/Jazz2/Multiplayer/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// Undefine it again after include
#undef far

#include <Containers/String.h>

#define MAX_CLIENTS 64

namespace Jazz2::Multiplayer
Expand All @@ -39,7 +41,7 @@ namespace Jazz2::Multiplayer
}
}

bool NetworkManager::CreateClient(INetworkHandler* handler, const char* address, std::uint16_t port, std::uint32_t clientData)
bool NetworkManager::CreateClient(INetworkHandler* handler, const StringView& address, std::uint16_t port, std::uint32_t clientData)
{
if (!_initialized || _host != nullptr) {
return false;
Expand All @@ -53,7 +55,7 @@ namespace Jazz2::Multiplayer
_state = NetworkState::Connecting;

ENetAddress addr = { };
enet_address_set_host(&addr, address);
enet_address_set_host(&addr, String::nullTerminatedView(address).data());
addr.port = port;

ENetPeer* peer = enet_host_connect(_host, &addr, (std::size_t)NetworkChannel::Count, clientData);
Expand Down
3 changes: 2 additions & 1 deletion Sources/Jazz2/Multiplayer/NetworkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../../nCine/Threading/ThreadSync.h"

#include <Containers/SmallVector.h>
#include <Containers/StringView.h>

struct _ENetHost;

Expand Down Expand Up @@ -39,7 +40,7 @@ namespace Jazz2::Multiplayer
NetworkManager();
~NetworkManager();

bool CreateClient(INetworkHandler* handler, const char* address, std::uint16_t port, std::uint32_t clientData);
bool CreateClient(INetworkHandler* handler, const StringView& address, std::uint16_t port, std::uint32_t clientData);
bool CreateServer(INetworkHandler* handler, std::uint16_t port);
void Dispose();

Expand Down
8 changes: 8 additions & 0 deletions Sources/Jazz2/PreferencesCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ using namespace Death::IO;

namespace Jazz2
{
#if defined(WITH_MULTIPLAYER)
String PreferencesCache::InitialState;
#endif
UnlockableEpisodes PreferencesCache::UnlockedEpisodes = UnlockableEpisodes::None;
RescaleMode PreferencesCache::ActiveRescaleMode = RescaleMode::None;
bool PreferencesCache::EnableFullscreen = false;
Expand Down Expand Up @@ -296,6 +299,11 @@ namespace Jazz2
} else if (arg == "/mute"_s) {
MasterVolume = 0.0f;
}
#if defined(WITH_MULTIPLAYER)
else if (InitialState.empty() && (arg == "/server"_s || arg.hasPrefix("/connect:"_s))) {
InitialState = arg;
}
#endif
}
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/Jazz2/PreferencesCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ namespace Jazz2
static constexpr std::int32_t UnlimitedFps = 0;
static constexpr std::int32_t UseVsync = -1;

#if defined(WITH_MULTIPLAYER)
static String InitialState;
#endif
static UnlockableEpisodes UnlockedEpisodes;

// Graphics
Expand Down Expand Up @@ -148,9 +151,7 @@ namespace Jazz2

static constexpr float TouchPaddingMultiplier = 0.003f;

/// Deleted copy constructor
PreferencesCache(const PreferencesCache&) = delete;
/// Deleted assignment operator
PreferencesCache& operator=(const PreferencesCache&) = delete;

static String _configPath;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/UI/Menu/BeginSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ namespace Jazz2::UI::Menu
// TODO: Multiplayer
case (int32_t)Item::TODO_ConnectTo:
// TODO: Hardcoded address and port
_root->ConnectToServer("127.0.0.1", 10666);
_root->ConnectToServer("127.0.0.1"_s, 7438);
break;
case (int32_t)Item::TODO_CreateServer:
// TODO: Hardcoded address and port
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/UI/Menu/IMenuContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Jazz2::UI::Menu
virtual void LeaveSection() = 0;
virtual void ChangeLevel(Jazz2::LevelInitialization&& levelInit) = 0;
#if defined(WITH_MULTIPLAYER)
virtual bool ConnectToServer(const char* address, std::uint16_t port) = 0;
virtual bool ConnectToServer(const StringView& address, std::uint16_t port) = 0;
virtual bool CreateServer(std::uint16_t port) = 0;
#endif
virtual void ApplyPreferencesChanges(ChangedPreferencesType type) = 0;
Expand Down
3 changes: 2 additions & 1 deletion Sources/Jazz2/UI/Menu/InGameMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ namespace Jazz2::UI::Menu
}

#if defined(WITH_MULTIPLAYER)
bool InGameMenu::ConnectToServer(const char* address, std::uint16_t port)
bool InGameMenu::ConnectToServer(const StringView& address, std::uint16_t port)
{
return _root->_root->ConnectToServer(address, port);
}
Expand Down Expand Up @@ -264,6 +264,7 @@ namespace Jazz2::UI::Menu
}

if ((type & ChangedPreferencesType::Language) == ChangedPreferencesType::Language) {
// All sections have to be recreated to load new language
_sections.clear();
SwitchToSection<PauseSection>();
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/UI/Menu/InGameMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Jazz2::UI::Menu
void LeaveSection() override;
void ChangeLevel(Jazz2::LevelInitialization&& levelInit) override;
#if defined(WITH_MULTIPLAYER)
bool ConnectToServer(const char* address, std::uint16_t port) override;
bool ConnectToServer(const StringView& address, std::uint16_t port) override;
bool CreateServer(std::uint16_t port) override;
#endif
void ApplyPreferencesChanges(ChangedPreferencesType type) override;
Expand Down
35 changes: 35 additions & 0 deletions Sources/Jazz2/UI/Menu/LoadingSection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "LoadingSection.h"

namespace Jazz2::UI::Menu
{
LoadingSection::LoadingSection(const StringView& message)
: _message(message)
{
}

LoadingSection::LoadingSection(String&& message)
: _message(std::move(message))
{

}

void LoadingSection::OnUpdate(float timeMult)
{
}

void LoadingSection::OnDraw(Canvas* canvas)
{
Vector2i viewSize = canvas->ViewSize;
Vector2f center = Vector2f(viewSize.X * 0.5f, viewSize.Y * 0.5f);

constexpr float topLine = 131.0f;
std::int32_t charOffset = 0;

_root->DrawStringShadow(_message, charOffset, center.X, topLine + 40.0f, IMenuContainer::FontLayer,
Alignment::Top, Font::DefaultColor, 1.2f, 0.4f, 0.6f, 0.6f, 0.6f, 0.9f, 1.2f);
}

void LoadingSection::OnTouchEvent(const nCine::TouchEvent& event, const Vector2i& viewSize)
{
}
}
20 changes: 20 additions & 0 deletions Sources/Jazz2/UI/Menu/LoadingSection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "MenuSection.h"

namespace Jazz2::UI::Menu
{
class LoadingSection : public MenuSection
{
public:
LoadingSection(const StringView& message);
LoadingSection(String&& message);

void OnUpdate(float timeMult) override;
void OnDraw(Canvas* canvas) override;
void OnTouchEvent(const nCine::TouchEvent& event, const Vector2i& viewSize) override;

private:
String _message;
};
}
Loading

0 comments on commit f69bc6e

Please sign in to comment.