Skip to content

Commit

Permalink
Adapt to newest SFML
Browse files Browse the repository at this point in the history
vittorioromeo committed May 28, 2024
1 parent 59e948b commit 8998d7f
Showing 6 changed files with 47 additions and 31 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ set(SFML_ENABLE_PCH true)
CPMAddPackage(
NAME SFML
GITHUB_REPOSITORY vittorioromeo/SFML
GIT_TAG 5415b77a2fc6fb109a0cc4a7b5fb93ae5a69b720
GIT_TAG 1774e83b5fbd068151aabd7a4fa2b0bd4af4e0bd
)

set_target_properties(sfml-system PROPERTIES UNITY_BUILD OFF)
@@ -187,7 +187,7 @@ if(NOT SSVOH_ANDROID)
CPMAddPackage(
NAME imgui-sfml
GITHUB_REPOSITORY SFML/imgui-sfml
GIT_TAG 7758bfd06326af08dc919198c13f9f0e64e70b1a
GIT_TAG 4727cf135a0e412d1f5b2e7fa402283dce0c91da
)

set_target_properties(ImGui-SFML PROPERTIES UNITY_BUILD ON)
1 change: 0 additions & 1 deletion include/SSVOpenHexagon/Global/Assets.hpp
Original file line number Diff line number Diff line change
@@ -58,7 +58,6 @@ class HGAssets
[[nodiscard]] sf::Texture& getTextureOrNullTexture(const std::string& mId);

[[nodiscard]] sf::Font& getFont(const std::string& mId);
[[nodiscard]] sf::Font& getFontOrNullFont(const std::string& mId);

[[nodiscard]] bool isValidLevelId(
const std::string& mLevelId) const noexcept;
4 changes: 2 additions & 2 deletions src/SSVOpenHexagon/Core/HexagonGame.cpp
Original file line number Diff line number Diff line change
@@ -280,8 +280,8 @@ HexagonGame::HexagonGame(Steam::steam_manager* mSteamManager,
: steamManager(mSteamManager),
discordManager(mDiscordManager),
assets(mAssets),
font{assets.getFontOrNullFont("OpenSquare-Regular.ttf")},
fontBold{assets.getFontOrNullFont("OpenSquare-Bold.ttf")},
font{assets.getFont("OpenSquare-Regular.ttf")},
fontBold{assets.getFont("OpenSquare-Bold.ttf")},
audio(mAudio),
window(mGameWindow),
hexagonClient{mHexagonClient},
9 changes: 6 additions & 3 deletions src/SSVOpenHexagon/Core/main.cpp
Original file line number Diff line number Diff line change
@@ -312,15 +312,18 @@ getFirstCompressedReplayFilenameFromArgs(const std::vector<std::string>& args)
{
const auto resetIcon = [&window]
{
sf::Image icon;
if(!icon.loadFromFile("Assets/icon.png"))
const std::optional icon =
sf::Image::loadFromFile("Assets/icon.png");

if(!icon.has_value())
{
ssvu::lo("::main") << "Failed to load icon image\n";
return;
}

window->getRenderWindow().setIcon(
{icon.getSize().x, icon.getSize().y}, icon.getPixelsPtr());
{icon->getSize().x, icon->getSize().y},
icon->getPixelsPtr());
};

window->onRecreation += resetIcon;
22 changes: 20 additions & 2 deletions src/SSVOpenHexagon/Global/AssetStorage.cpp
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

#include "SSVOpenHexagon/Global/Assert.hpp"

#include "SSVOpenHexagon/Global/Macros.hpp"
#include "SSVOpenHexagon/Utils/Concat.hpp"
#include "SSVOpenHexagon/Utils/UniquePtr.hpp"

@@ -61,13 +62,30 @@ class AssetStorage::AssetStorageImpl

[[nodiscard]] bool loadFont(const std::string& id, const std::string& path)
{
return tryEmplaceAndThenLoadFromFile(_fonts, id, path);
std::optional font = sf::Font::loadFromFile(path);

if(!font.has_value())
{
return false;
}

auto [it, inserted] = _fonts.emplace(id, *SSVOH_MOVE(font));
return inserted;
}

[[nodiscard]] bool loadSoundBuffer(
const std::string& id, const std::string& path)
{
return tryEmplaceAndThenLoadFromFile(_soundBuffers, id, path);
std::optional soundBuffer = sf::SoundBuffer::loadFromFile(path);

if(!soundBuffer.has_value())
{
return false;
}

auto [it, inserted] =
_soundBuffers.emplace(id, *SSVOH_MOVE(soundBuffer));
return inserted;
}

[[nodiscard]] sf::Texture* getTexture(const std::string& id) noexcept
38 changes: 17 additions & 21 deletions src/SSVOpenHexagon/Global/Assets.cpp
Original file line number Diff line number Diff line change
@@ -40,6 +40,8 @@
#include <SFML/Audio/Music.hpp>

#include <chrono>
#include <iostream>
#include <exception>

namespace hg {

@@ -131,7 +133,6 @@ class HGAssets::HGAssetsImpl
[[nodiscard]] sf::Texture& getTextureOrNullTexture(const std::string& mId);

[[nodiscard]] sf::Font& getFont(const std::string& mId);
[[nodiscard]] sf::Font& getFontOrNullFont(const std::string& mId);

[[nodiscard]] bool isValidLevelId(
const std::string& mLevelId) const noexcept;
@@ -576,19 +577,15 @@ HGAssets::HGAssetsImpl::~HGAssetsImpl()

[[nodiscard]] sf::Font& HGAssets::HGAssetsImpl::getFont(const std::string& mId)
{
sf::Font* ptr = assetStorage->getFont(mId);
SSVOH_ASSERT(ptr);

return *ptr;
}
if(sf::Font* ptr = assetStorage->getFont(mId))
{
return *ptr;
}

[[nodiscard]] sf::Font& HGAssets::HGAssetsImpl::getFontOrNullFont(
const std::string& mId)
{
static sf::Font nullFont;
sf::Font* ptr = assetStorage->getFont(mId);
std::cerr << "Fatal error: missing sound file '" << mId << '\''
<< std::endl;

return ptr ? *ptr : nullFont;
std::terminate();
}

[[nodiscard]] bool HGAssets::HGAssetsImpl::isValidLevelId(
@@ -884,21 +881,25 @@ void HGAssets::HGAssetsImpl::loadPackAssets_loadShaders(
{
for(const auto& p : scanSingleByExt(mPath + "Shaders/", extension))
{
auto shader = Utils::makeUnique<sf::Shader>();
std::optional shader =
sf::Shader::loadFromFile(p.getStr(), shaderType);

if(!shader->loadFromFile(p.getStr(), shaderType))
if(!shader.has_value())
{
ssvu::lo("hg::loadPackAssets_loadShaders")
<< "Failed to load shader '" << p << "'\n";

continue;
}

shadersById.push_back(shader.get());
auto shaderUptr =
Utils::makeUnique<sf::Shader>(*SSVOH_MOVE(shader));

shadersById.push_back(shaderUptr.get());
SSVOH_ASSERT(shadersById.size() > 0);
const std::size_t shaderId = shadersById.size() - 1;

LoadedShader ls{.shader{SSVOH_MOVE(shader)},
LoadedShader ls{.shader{SSVOH_MOVE(shaderUptr)},
.path{p},
.shaderType{shaderType},
.id{shaderId}};
@@ -1632,11 +1633,6 @@ sf::Font& HGAssets::getFont(const std::string& mId)
return _impl->getFont(mId);
}

sf::Font& HGAssets::getFontOrNullFont(const std::string& mId)
{
return _impl->getFontOrNullFont(mId);
}

bool HGAssets::isValidLevelId(const std::string& mLevelId) const noexcept
{
return _impl->isValidLevelId(mLevelId);

0 comments on commit 8998d7f

Please sign in to comment.