Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more cpp 20 stuff #307

Merged
merged 15 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ jobs:
cmake -G Ninja \
-B ${{github.workspace}}/build \
-DJT_ENABLE_UNITTESTS=OFF \
-DJT_ENABLE_DEBUG=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DJT_ENABLE_DEMOS=OFF \
-DJT_ENABLE_LTO_OPTIMIZATION=ON \
Expand Down Expand Up @@ -125,6 +126,7 @@ jobs:
emcmake cmake \
-B ${{github.workspace}}/build \
-DCMAKE_BUILD_TYPE=Release \
-DJT_ENABLE_DEBUG=OFF \
-DJT_ENABLE_LTO_OPTIMIZATION=ON \
-DJT_ENABLE_DEMOS=OFF \
-DJT_ENABLE_UNITTESTS=OFF \
Expand Down Expand Up @@ -189,6 +191,7 @@ jobs:
run: |
cmake \
-DJT_ENABLE_DEMOS=OFF \
-DJT_ENABLE_DEBUG=OFF \
-DJT_ENABLE_UNITTESTS=OFF \
-DCMAKE_BUILD_TYPE=Release \
-B ${{github.workspace}}/build .
Expand Down
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ if (JT_ENABLE_CLANG_TIDY)
set(CMAKE_CXX_CLANG_TIDY "clang-tidy")
endif ()


if (JT_ENABLE_LTO_OPTIMIZATION)
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GL /LTCG")
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=auto -fwhole-program -g0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=auto -fwhole-program -g0 -march=native -mtune=native")
endif ()

if (NOT CMAKE_BUILD_TYPE STREQUAL "Release")
Expand All @@ -50,6 +51,8 @@ if (JT_ENABLE_WEB)
"SHELL:--preload-file assets")
elseif (MSVC)
add_compile_definitions(NOMINMAX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++20")

elseif (NOT CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lstdc++fs")
endif ()
Expand Down
6 changes: 4 additions & 2 deletions impl/game/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#include <action_commands/action_command_manager.hpp>
#include <action_commands/basic_action_commands.hpp>
#include <audio/audio/audio_impl.hpp>
#include <audio/audio/audio_null.hpp>
#include <audio/audio/logging_audio.hpp>
#include <audio/fades/logging_sound_fade_manager.hpp>
#include <cache_impl.hpp>
#include <cache/cache_impl.hpp>
#include <camera.hpp>
#include <game.hpp>
#include <game_properties.hpp>
Expand Down Expand Up @@ -61,7 +62,8 @@ int main(int /*argc*/, char* /*argv*/[])
jt::InputManager input { mouse, keyboard, { gamepad0 } };

jt::SoundFadeManager fades;
jt::AudioImpl audio { std::make_unique<jt::LoggingSoundFadeManager>(fades, logger) };
// jt::AudioImpl audio { std::make_unique<jt::LoggingSoundFadeManager>(fades, logger) };
jt::null_objects::AudioNull audio {};
jt::LoggingAudio loggingAudio { audio, logger };

jt::StateManager stateManager { std::make_shared<StateIntro>() };
Expand Down
47 changes: 23 additions & 24 deletions impl/gamelib/state_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <input/input_manager.hpp>
#include <lerp.hpp>
#include <log/license_info.hpp>
#include <math_helper.hpp>
#include <algorithm>
#include <screeneffects/vignette.hpp>
#include <state_game.hpp>
#include <state_manager/state_manager_transition_fade_to_black.hpp>
Expand Down Expand Up @@ -113,22 +113,20 @@ void StateMenu::createTweens()

void StateMenu::createInstructionTweenColor1()
{
auto tc = jt::TweenColor::create(
auto const tc = jt::TweenColor::create(
m_textStart, 0.5f, GP::PaletteFontFront(), GP::PalleteFrontHighlight());
tc->addCompleteCallback([this]() { createInstructionTweenColor2(); });
tc->setAgePercentConversion([](float age) {
return jt::Lerp::cubic(0.0f, 1.0f, jt::MathHelper::clamp(age, 0.0f, 1.0f));
});
tc->setAgePercentConversion(
[](float age) { return jt::Lerp::cubic(0.0f, 1.0f, std::clamp(age, 0.0f, 1.0f)); });
add(tc);
}

void StateMenu::createInstructionTweenColor2()
{
auto tc = jt::TweenColor::create(
auto const tc = jt::TweenColor::create(
m_textStart, 0.45f, GP::PalleteFrontHighlight(), GP::PaletteFontFront());
tc->setAgePercentConversion([](float age) {
return jt::Lerp::cubic(0.0f, 1.0f, jt::MathHelper::clamp(age, 0.0f, 1.0f));
});
tc->setAgePercentConversion(
[](float age) { return jt::Lerp::cubic(0.0f, 1.0f, std::clamp(age, 0.0f, 1.0f)); });
tc->setStartDelay(0.2f);
tc->addCompleteCallback([this]() { createInstructionTweenColor1(); });
add(tc);
Expand All @@ -139,46 +137,47 @@ void StateMenu::createTweenExplanation()
auto const start = m_textStart->getPosition() + jt::Vector2f { -1000, 0 };
auto const end = m_textStart->getPosition();

auto tween = jt::TweenPosition::create(m_textStart, 0.5f, start, end);
auto const tween = jt::TweenPosition::create(m_textStart, 0.5f, start, end);
tween->setStartDelay(0.3f);
tween->setSkipFrames();
tween->setSkipTicks();

tween->addCompleteCallback([this]() { createInstructionTweenColor1(); });
add(tween);
}

void StateMenu::createTweenTitleAlpha()
{
auto tween = jt::TweenAlpha::create(m_textTitle, 0.55f, 0, 255);
auto const tween = jt::TweenAlpha::create(m_textTitle, 0.55f, 0, 255);
tween->setStartDelay(0.2f);
tween->setSkipFrames();
tween->setSkipTicks();
add(tween);
}

void StateMenu::createTweenOverlayAlpha()
{
auto tween = jt::TweenAlpha::create(m_overlay, 0.5f, std::uint8_t { 255 }, std::uint8_t { 0 });
tween->setSkipFrames();
auto const tween
= jt::TweenAlpha::create(m_overlay, 0.5f, std::uint8_t { 255 }, std::uint8_t { 0 });
tween->setSkipTicks();
add(tween);
}

void StateMenu::createTweenCreditsPosition()
{
auto creditsPositionStart = m_textCredits->getPosition() + jt::Vector2f { 0, 150 };
auto creditsPositionEnd = m_textCredits->getPosition();
auto const creditsPositionStart = m_textCredits->getPosition() + jt::Vector2f { 0, 150 };
auto const creditsPositionEnd = m_textCredits->getPosition();

auto tweenCredits
auto const tweenCredits
= jt::TweenPosition::create(m_textCredits, 0.75f, creditsPositionStart, creditsPositionEnd);
tweenCredits->setStartDelay(0.8f);
tweenCredits->setSkipFrames();
tweenCredits->setSkipTicks();
add(tweenCredits);

auto versionPositionStart = m_textVersion->getPosition() + jt::Vector2f { 0, 150 };
auto versionPositionEnd = m_textVersion->getPosition();
auto tweenVersion
auto const versionPositionStart = m_textVersion->getPosition() + jt::Vector2f { 0, 150 };
auto const versionPositionEnd = m_textVersion->getPosition();
auto const tweenVersion
= jt::TweenPosition::create(m_textVersion, 0.75f, versionPositionStart, versionPositionEnd);
tweenVersion->setStartDelay(0.8f);
tweenVersion->setSkipFrames();
tweenVersion->setSkipTicks();
add(tweenVersion);
}

Expand All @@ -204,7 +203,7 @@ void StateMenu::checkForTransitionToStateGame()
{
auto const keysToTriggerTransition = { jt::KeyCode::Space, jt::KeyCode::Enter };

if (std::any_of(keysToTriggerTransition.begin(), keysToTriggerTransition.end(),
if (std::any_of(std::begin(keysToTriggerTransition),std::end(keysToTriggerTransition) ,
[this](auto const k) { return getGame()->input().keyboard()->justPressed(k); })) {
startTransitionToStateGame();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ std::vector<std::string> jt::ActionCommandManager::getArguments(

void jt::ActionCommandManager::removeUnusedCommands()
{
jt::SystemHelper::erase_if(m_registeredCommands, [this](auto const& kvp) {
std::erase_if(m_registeredCommands, [this](auto const& kvp) {
auto shared_state = std::get<0>(kvp.second);
if (shared_state.expired()) {
m_logger.info("remove command '" + kvp.first + "'");
Expand All @@ -66,6 +66,7 @@ std::shared_ptr<bool> jt::ActionCommandManager::registerTemporaryCommand(
m_logger.info("registered command '" + trimmedCommand + "'");
return sharedState;
}

void jt::ActionCommandManager::update() { removeUnusedCommands(); }

std::vector<std::string> jt::ActionCommandManager::getAllCommands()
Expand Down
29 changes: 16 additions & 13 deletions impl/jamtemplate/common/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void jt::Animation::add(std::string const& fileName, std::string const& animName
throw std::invalid_argument { "different sizes for frametimes and frame indices" };
}

if (m_frames.count(animName) != 0) {
if (m_frames.contains(animName)) {
std::cout << "Warning: Overwriting old animation with name: " << animName << std::endl;
}

Expand Down Expand Up @@ -187,7 +187,7 @@ void jt::Animation::loadFromAseprite(

bool jt::Animation::hasAnimation(std::string const& animationName) const
{
return (m_frames.count(animationName) != 0);
return (m_frames.contains(animationName));
}

std::vector<std::string> jt::Animation::getAllAvailableAnimationNames() const
Expand All @@ -207,7 +207,7 @@ std::string jt::Animation::getRandomAnimationName() const
"can not get random animation name if no animation has been added"
};
}
return jt::SystemHelper::select_randomly(m_frames.cbegin(), m_frames.cend())->first;
return jt::SystemHelper::select_randomly(m_frames).first;
}

void jt::Animation::play(std::string const& animationName, size_t startFrameIndex, bool restart)
Expand Down Expand Up @@ -334,7 +334,6 @@ void jt::Animation::doFlashImpl(float t, jt::Color col)

void jt::Animation::doUpdate(float elapsed)
{
// check if valid
if (!m_isValid) {
std::cout << "Warning: Update Animation with invalid animName: '" + m_currentAnimName
+ "'\n";
Expand All @@ -343,15 +342,19 @@ void jt::Animation::doUpdate(float elapsed)

// proceed time
m_frameTime += elapsed * m_animationplaybackSpeed;
while (m_frameTime >= m_time[m_currentAnimName][m_currentIdx]) {
m_frameTime -= m_time[m_currentAnimName][m_currentIdx];

auto const frame_time = m_time[m_currentAnimName][m_currentIdx];
// increase index
while (m_frameTime >= frame_time) {
m_frameTime -= frame_time;
m_currentIdx++;
if (m_currentIdx >= m_frames.at(m_currentAnimName).size()) {
if (getCurrentAnimationIsLooping()) {
m_currentIdx = 0;
} else {
m_currentIdx = m_frames.at(m_currentAnimName).size() - 1;
}
}
// wrap index or fix index at last frame
if (m_currentIdx >= m_frames.at(m_currentAnimName).size()) {
if (getCurrentAnimationIsLooping()) {
m_currentIdx = 0;
} else {
m_currentIdx = m_frames.at(m_currentAnimName).size() - 1;
}
}

Expand Down Expand Up @@ -446,7 +449,7 @@ std::size_t jt::Animation::getCurrentAnimationFrameIndex() const { return m_curr
void jt::Animation::setFrameTimes(
std::string const& animationName, std::vector<float> const& frameTimes)
{
if (m_frames.count(animationName) == 0) {
if (!m_frames.contains(animationName)) {
throw std::invalid_argument { "cannot set frame times for invalid animation: "
+ animationName };
}
Expand Down
2 changes: 1 addition & 1 deletion impl/jamtemplate/common/audio/audio/audio_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ oalpp::SoundContextInterface& jt::AudioImpl::getContext() { return m_context; }

std::shared_ptr<jt::SoundInterface> jt::AudioImpl::getPermanentSound(std::string const& identifier)
{
if (m_permanentSounds.count(identifier) == 0) {
if (!m_permanentSounds.contains(identifier)) {
return nullptr;
}
return m_permanentSounds[identifier];
Expand Down
2 changes: 1 addition & 1 deletion impl/jamtemplate/common/audio/audio/audio_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace jt {

class AudioInterface {
public:
/// Update sounds in the the audio interface
/// Update sounds in the audio interface
virtual void update(float elapsed) = 0;

/// Add a temporary sound that will be removed once the shared pointer count is zero
Expand Down
7 changes: 4 additions & 3 deletions impl/jamtemplate/common/audio/fades/sound_fade_info.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "sound_fade_info.hpp"
#include <math_helper.hpp>
#include <algorithm>

jt::SoundFadeInfo::SoundFadeInfo(std::weak_ptr<jt::SoundInterface> const& sound, float duration,
float startVolume, float endVolume)
Expand All @@ -15,14 +15,15 @@ void jt::SoundFadeInfo::update(float elapsed)
m_age += elapsed;

auto agePercent = m_age / m_duration; // 0-1
agePercent = jt::MathHelper::clamp(agePercent, 0.0f, 1.0f);
agePercent = std::clamp(agePercent, 0.0f, 1.0f);
auto const value = (1.0f - agePercent) * m_startVolume + agePercent * m_endVolume;

auto const snd = m_sound.lock();
if (snd) {
if (snd) [[likely]] {
snd->setVolume(value);
}
}

bool jt::SoundFadeInfo::isAlive() const { return m_age < m_duration; }

bool jt::SoundFadeInfo::hasValidSound() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jt::IntroLoopingSoundWithEffect::IntroLoopingSoundWithEffect(std::string const&
std::string const& loopingFileName, jt::SoundBufferManagerInterface& soundBufferManager,
oalpp::effects::MonoEffectInterface& effect)
: m_introSound { std::make_unique<jt::SoundWithEffect>(
introFileName, soundBufferManager, effect) }
introFileName, soundBufferManager, effect) }
, m_loopingSound { std::make_unique<jt::SoundWithEffect>(
loopingFileName, soundBufferManager, effect) }
{
Expand Down
2 changes: 1 addition & 1 deletion impl/jamtemplate/common/audio/sound/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ float jt::Sound::getPosition() const { return m_sound.getCurrentOffsetInSeconds(

void jt::Sound::setPitch(float pitch)
{
if (pitch <= 0.0f) {
if (pitch <= 0.0f) [[unlikely]] {
throw std::invalid_argument { "Pitch has to be greater than 0." };
}
m_sound.setPitch(pitch);
Expand Down
2 changes: 1 addition & 1 deletion impl/jamtemplate/common/audio/sound/sound_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void jt::SoundBase::setVolume(float newVolume) { m_volume = newVolume; }

void jt::SoundBase::setBlend(float blend)
{
if (blend < 0.0f || blend > 1.0f) {
if (blend < 0.0f || blend > 1.0f) [[unlikely]] {
throw std::invalid_argument { "Blend has to be between [0, 1]." };
}
m_blend = 1.0f - blend;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ float jt::GroupVolumeManager::getVolume(std::string const& groupName) const
if (groupName == "") {
return 1.0f;
}
if (m_volumeGroups.count(groupName) == 0) {
if (!m_volumeGroups.contains(groupName)) {
m_volumeGroups[groupName] = 1.0f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ void jt::Box2DContactManager::unregisterCallback(std::string const& callbackIden
void jt::Box2DContactManager::BeginContact(b2Contact* contact)
{
for (auto const& kvp : m_callbacks) {
if (kvp.second) {
if (kvp.second) [[likely]] {
if (kvp.second->getEnabled()) {
kvp.second->onBeginContact(contact);
}
}
}
}

void jt::Box2DContactManager::EndContact(b2Contact* contact)
{
for (auto const& kvp : m_callbacks) {
if (kvp.second) {
if (kvp.second) [[likely]] {
if (kvp.second->getEnabled()) {
kvp.second->onEndContact(contact);
}
}
}
}

std::vector<std::string> jt::Box2DContactManager::getAllCallbackIdentifiers() const
{
std::vector<std::string> identifiers;
Expand Down
5 changes: 3 additions & 2 deletions impl/jamtemplate/common/box2dwrapper/box2d_joint.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "box2d_joint.hpp"

jt::Box2DJoint::Box2DJoint(std::shared_ptr<Box2DWorldInterface> world, const b2JointDef* def)
jt::Box2DJoint::Box2DJoint(std::shared_ptr<Box2DWorldInterface> world, b2JointDef const* def)
{
if (!world) {
if (!world) [[unlikely]] {
throw std::invalid_argument { "cannot create joint for nullptr world" };
}
m_world = world;
Expand All @@ -19,4 +19,5 @@ void jt::Box2DJoint::doDestroy()
}

void jt::Box2DJoint::setB2Joint(b2Joint* joint) { m_joint = joint; }

b2Joint* jt::Box2DJoint::getB2Joint() const { return m_joint; }
Loading