Skip to content
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
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)

#options
option(ENABLE_COVERAGE "Enable coverage" OFF)

#bigobj
if (MSVC)
add_compile_options(/bigobj)
endif()

#add cmake script to download conan-cmake integrations
#include(cmake/conan_config.cmake)
Expand Down
4 changes: 3 additions & 1 deletion apps/asteroids/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ target_link_libraries(${PROJECT_NAME}
pgEngine::pgEngine
pgGame::pgGame
soundEngine::soundEngine
pgFoundation::pgFoundation

pgf::pgf
fmt::fmt
EnTT::EnTT
INTERFACE
pgFoundation::pgFoundation
)
target_include_directories(${PROJECT_NAME}
PRIVATE
Expand Down
3 changes: 1 addition & 2 deletions apps/galaxy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ file(GLOB_RECURSE CPP_FILES CONFIGURE_DEPENDS "*.cpp")

add_executable(${PROJECT_NAME} ${HEADER_FILES} ${CPP_FILES})

find_package(fmt REQUIRED)

target_link_libraries(${PROJECT_NAME}
PUBLIC
pgEngine::pgEngine
Expand All @@ -20,6 +18,7 @@ target_link_libraries(${PROJECT_NAME}
BT::behaviortree_cpp
soundEngine::soundEngine
pgf::pgf
INTERFACE
pgFoundation::pgFoundation
${ADDITION_LIBS}
)
Expand Down
2 changes: 2 additions & 0 deletions apps/galaxy/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct Galaxy
BackgoundParameters background;
ZoomParameters zoom;
Debugging debugging;
uint64_t stars_seed{0};
uint32_t num_stars{15000};
std::vector<Faction> factions = {
{.name{"Terra"}, .color{0, 0, 255, 255}},
//{.name{"Vogons"}, .color{255, 0, 0, 255}},
Expand Down
13 changes: 1 addition & 12 deletions apps/galaxy/components/SoundScape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,7 @@ struct SceneSoundScape
{
BackgroundMusic background_music{};
std::unordered_map<std::string, EventSound> event_sounds;
float master_volume{0.75f};
};

struct SoundConfig
{
float master_gain{1.0f};
// float music_gain{1.0f};
// float sfx_gain{1.0f};
// float voice_gain{1.0f};
// float ambiance_gain{1.0f};
// float ui_gain{1.0f};
};

;

} // namespace galaxy
45 changes: 45 additions & 0 deletions apps/galaxy/gui/InSceneOptionsWidget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
#include <gui/GameGuiWidget.hpp>
#include <pgGame/events/GameEvents.hpp>

namespace galaxy::gui {

class InSceneOptionsWidget : public galaxy::gui::GameGuiWidget
{
public:
using galaxy::gui::GameGuiWidget::GameGuiWidget;

void draw([[maybe_unused]] pg::Gui& gui) override
{
ImGui::Begin("In Scene Options", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
// HACK:
if (getGame().getCurrentSceneId() == "galaxy")
{
// background opacity
auto opacity = getGame().getCurrentScene().callGetter<float>("galaxy.background.opacity");
if (ImGui::SliderFloat("Opacity", &opacity, 0.f, 1.f))
{
getGame().getCurrentScene().callSetter<float>("galaxy.background.opacity", opacity);
}
// grid visible
if (getGame().getCurrentScene().hasAccessor<float>("galaxy.grid.opacity"))
{
float gridVisible = getGame().getCurrentScene().callGetter<float>("galaxy.grid.opacity");
if (ImGui::SliderFloat("Grid Visibility", &gridVisible, 0.f, 1.f))
{
getGame().getCurrentScene().callSetter<float>("galaxy.grid.opacity", gridVisible);
}
}
}

auto volume = getGame().getCurrentScene().callGetter<float>("scene.sound.volume");
if (ImGui::SliderFloat("Global Volume", &volume, 0.f, 1.f))
{
getGame().getCurrentScene().callSetter<float>("scene.sound.volume", volume);
}

ImGui::End();
}
};

} // namespace galaxy::gui
10 changes: 0 additions & 10 deletions apps/galaxy/gui/MainBarWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ class MainBarWidget : public galaxy::gui::GameGuiWidget
getGame().getGlobalDispatcher().enqueue<pg::game::events::TimeScaleEvent>(event);
}

// display second based time as

// TODO: structured time/GalaxyTime
// ImGui::Text(system_name.data());
// ImGui::Text(pos_str.data());
// ImGui::Text(system_state_str.data());
// ImGui::Text(faction_str.data());
//
// ImGui::Text(spectral_type.data());

ImGui::End();
}
};
Expand Down
44 changes: 40 additions & 4 deletions apps/galaxy/gui/SystemInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,34 @@
#include <components/Faction.hpp>
#include <magic_enum.hpp>
#include <pgEngine/generators/MarkovNameGen.hpp>
#include <pgEngine/math/Trigonometric.hpp>
#include <pgEngine/core/Constants.hpp>

namespace galaxy::gui {

auto sectorNameFromAngle(float angle_deg) -> std::string
{ // 0° is up
angle_deg += 90.0f;
// adjust for sector 0 starting at -15 degrees
angle_deg += 15.0f;

// normalize angle to 0-360
angle_deg = std::fmod(angle_deg + 360.0f, 360.0f);

// every 30 degrees, starting at -15 degrees. First sector is alpha, last is omega

// first sector is between -15 and +15
auto sector = static_cast<int>(std::floor((angle_deg) / 30.0f));

return pg::constants::greek_letter_names.at(sector);
}

// the remainder-angle inside a sector
auto inSectorAngle(float angle_deg) -> float
{
return std::fmod(angle_deg + 15.0f, 30.0f);
}

class SystemInfoWidget : public galaxy::gui::GameGuiWidget
{
public:
Expand All @@ -24,11 +49,12 @@ class SystemInfoWidget : public galaxy::gui::GameGuiWidget
// set up system name if not yet created
if (system.name.empty())
{
const auto& mfm = getGame().getSingleton<pg::generators::MarkovFrequencyMap<4>>("markovFrequencyMap");
system.name = pg::generators::markov::generate<4>(3, 8, mfm);
pg::SeedGenerator seed_gen(entt::to_integral(selected_entity));
const auto& mfm = getGame().getSingleton<pg::generators::MarkovFrequencyMap<4>>("markovFrequencyMap");
system.name = pg::generators::markov::generate<4>(3, 8, mfm, seed_gen);
}
auto name = buildSystemPositionStr(transform.pos);

auto pos_str = fmt::format("Pos: {},{}", transform.pos[0], transform.pos[1]);
auto system_state_str = fmt::format("State: {}", magic_enum::enum_name(system.colonizationStatus));
auto faction_str = fmt::format("Faction: {}", faction.name);
auto system_name = fmt::format("Name: {}", system.name);
Expand All @@ -40,14 +66,24 @@ class SystemInfoWidget : public galaxy::gui::GameGuiWidget
ImGui::Begin("System Info", nullptr, ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Text("System Info");
ImGui::Text(system_name.data());
ImGui::Text(pos_str.data());
ImGui::Text(name.data());
ImGui::Text(system_state_str.data());
ImGui::Text(faction_str.data());

ImGui::Text(spectral_type.data());

ImGui::End();
}

auto buildSystemPositionStr(pg::fVec2 pos) -> std::string
{
auto polar_pos = pg::math::toPolar(pos);

auto sector_name = sectorNameFromAngle(pg::math::toDegrees(std::get<1>(polar_pos)));
auto sector_angle = 10 * inSectorAngle(std::get<1>(polar_pos));
// name is sector - angle_in_sector:distance
return fmt::format("{}-{:.0f}:{:.0f}", sector_name, sector_angle, 10 * std::get<0>(polar_pos));
}
};

} // namespace galaxy::gui
111 changes: 96 additions & 15 deletions apps/galaxy/scenes/GalaxyScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <entt/entt.hpp>
#include <pgGame/components/singletons/RegisteredPreloaders.hpp>
#include <pgEngine/resources/SpriteResource.hpp>
#include <gui/InSceneOptionsWidget.hpp>

namespace galaxy {
using entt::literals::operator""_hs;
Expand All @@ -51,6 +52,8 @@ class GalaxyScene : public pg::game::Scene
auto& preLoaders = game.getSingleton<pg::singleton::RegisteredLoaders>(getSceneConfig().scene_id + ".loaders");
// for now, no images
#if 0
TODO: images can be loaded, but not added to the SDL in a background task. Doing so will interfere with the state while rendering.
This needs a two stage approach where adding to SDL is done protected by a mutex
std::vector<std::string> files = {
"../data/reticle.png", "../data/background/milky_way_blurred.png", "../data/circle_05.png"};

Expand Down Expand Up @@ -219,34 +222,46 @@ class GalaxyScene : public pg::game::Scene

pg::game::makeEntity<pg::game::GuiDrawable>(getSceneRegistry(),
{std::make_unique<galaxy::gui::MainBarWidget>(getGame())});

pg::game::makeEntity<pg::game::GuiDrawable>(
getSceneRegistry(),
{std::make_unique<galaxy::gui::InSceneOptionsWidget>(getGame()), pg::game::DRAWABLE_OVERLAY_MENU});
}

void setupGalaxy()
{
setupBackground();
setupMapMarkers();
setupStars();
}

void setupStars()
{
galaxyQuadtree = std::make_unique<pg::Quadtree<entt::entity>>(pg::fBox{{-750, -750}, {1500, 1500}});
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<float> d(0.0f, 200.0f);
std::normal_distribution<float> star_size_dist(0.0075f, 0.0025f);

// Random number generator setup
std::discrete_distribution<> star_class_dist(pgOrbit::star_class_probabilities.cbegin(),
pgOrbit::star_class_probabilities.cend());

auto dot_sprite = getGame().getResource<pg::Sprite>("../data/circle_05.png");
auto dot_sprite = getGame().getResource<pg::Sprite>("../data/circle_05.png");
auto& gen = pg::SeedGenerator(galaxyConfig.stars_seed).get();

for ([[maybe_unused]] auto i : std::ranges::iota_view{0, 15000})
for ([[maybe_unused]] auto i : std::ranges::iota_view{0u, galaxyConfig.num_stars})
{
// color state
auto rendererStates = pg::States{};
auto color = pg::Color{255, 0, 0, 255};
rendererStates.push(pg::TextureColorState{color});
rendererStates.push(pg::BlendModeState{SDL_BLENDMODE_ADD});

auto new_pos = pg::fVec2{d(gen), d(gen)};

auto spectral_type = pgOrbit::indexToSpectralType(star_class_dist(gen));
auto gammaCorrectedBrightness = pgOrbit::convertBrightnessByGamma(pgOrbit::perceivedBrightness, 2.6f);
auto gammaCorrectedBrightness = pgOrbit::adaptBrightnessByGamma(pgOrbit::perceivedBrightness, 2.6f);
auto new_size =
gammaCorrectedBrightness[magic_enum::enum_integer(spectral_type)] * pg::fVec2{1.0f, 1.0f} * 0.025f;

auto entity = pg::game::makeEntity<pg::Transform2D,
pg::game::Drawable,
galaxy::StarSystemState,
Expand All @@ -264,18 +279,84 @@ class GalaxyScene : public pg::game::Scene

galaxyQuadtree->insert({new_pos, new_size}, entity, galaxyQuadtree->root);
}
// add some background
addSingleton_as<const pg::Quadtree<entt::entity>&>("galaxy.quadtree", *galaxyQuadtree);
}

void setupMapMarkers()
{
auto rendererStates = pg::States{};
auto color = pg::Color{0, 0, 255, 255};
rendererStates.push(pg::ColorState{color});
rendererStates.push(pg::BlendModeState{SDL_BLENDMODE_NONE});
auto markers_group = std::make_shared<pg::Switch>();
for (auto i : std::ranges::iota_view{0, 12})
{
auto angle = pg::math::toRadians(-15.0f + 30.0f * i);

auto sector_pie = std::make_shared<pg::CircleSector>(
pg::fVec2{0, 0}, 750.0f, angle, angle + pg::math::toRadians(30.0f), 25);
auto inner_sector_pie = std::make_shared<pg::CircleSector>(
pg::fVec2{0, 0}, 250.f, angle, angle + pg::math::toRadians(30.0f), 25, 500.0f);
auto medium_sector_pie = std::make_shared<pg::CircleSector>(
pg::fVec2{0, 0}, 500.f, angle, angle + pg::math::toRadians(30.0f), 25, 500.0f);

auto group = std::make_shared<pg::Group>();
group->addPrimitive(sector_pie);
group->addPrimitive(inner_sector_pie);
group->addPrimitive(medium_sector_pie);
markers_group->addPrimitive(group);
}

auto entity =
pg::game::makeEntity<pg::Transform2D, pg::game::Drawable, pg::game::RenderState, pg::tags::GalaxyRenderTag>(
getGlobalRegistry(),
{.pos{}, .scale{1, 1}, .scaleSpace{pg::TransformScaleSpace::World}},
pg::game::Drawable{markers_group},
{std::move(rendererStates)},
{});

auto setOpacity = [entity, color, &registry = getGame().getGlobalRegistry()](float opacity) mutable {
auto& renderState = registry.get<pg::game::RenderState>(entity);

auto colorState = renderState.states.get<pg::ColorState>();
auto new_color = pg::vec_cast<uint8_t>(pg::vec_cast<float>(color) * opacity);

colorState->setColor(new_color);
};
auto getOpacity = [color, &registry = getGame().getGlobalRegistry(), entity]() {
auto& renderState = registry.get<pg::game::RenderState>(entity);
return renderState.states.get<pg::ColorState>()->getColor()[3] / 255.0f;
};
registerAccessor<float>("galaxy.grid.opacity", std::move(setOpacity), std::move(getOpacity));
}

void setupBackground()

{
// add background
auto background_sprite = getGame().getResource<pg::Sprite>("../data/background/milky_way_blurred.png");
auto states = pg::States{};
states.push(pg::TextureAlphaState{static_cast<uint8_t>(galaxyConfig.background.opacity * 255)});
pg::game::makeEntity<pg::Transform2D, pg::game::Drawable, pg::game::RenderState, pg::tags::GalaxyRenderTag>(
getGlobalRegistry(),
{.pos{0, 0}, .scale{0.5, 0.5}, .scaleSpace{pg::TransformScaleSpace::World}},
pg::game::Drawable{background_sprite},
{},
{});

addSingleton_as<const pg::Quadtree<entt::entity>&>("galaxy.quadtree", *galaxyQuadtree);
states.push(pg::TextureColorState{pg::Color{255, 255, 255, 255}});
states.push(pg::TextureBlendModeState{SDL_BLENDMODE_ADD});
auto entity =
pg::game::makeEntity<pg::Transform2D, pg::game::Drawable, pg::game::RenderState, pg::tags::GalaxyRenderTag>(
getGlobalRegistry(),
{.pos{0.0f, 0.0f}, .scale{0.5f, 0.5f}, .scaleSpace{pg::TransformScaleSpace::World}},
pg::game::Drawable{background_sprite},
{states},
{});

// lambda to modify the background opacity
auto setOpacity = [entity, &registry = getGame().getGlobalRegistry()](float opacity) mutable {
auto& renderState = registry.get<pg::game::RenderState>(entity);
renderState.states.replace<pg::TextureAlphaState>(static_cast<uint8_t>(opacity * 255));
};
auto getOpacity = [&registry = getGame().getGlobalRegistry(), entity]() {
auto& renderState = registry.get<pg::game::RenderState>(entity);
return renderState.states.get<pg::TextureAlphaState>()->getAlpha() / 255.0f;
};
registerAccessor<float>("galaxy.background.opacity", std::move(setOpacity), std::move(getOpacity));
}

void setupQuadtreeDebug()
Expand Down
2 changes: 0 additions & 2 deletions apps/galaxy/systems/DroneSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ void galaxy::DroneSystem::handleDroneFailed(galaxy::events::DroneFailedEvent eve

void galaxy::DroneSystem::createFactions(const pg::FrameStamp& frameStamp)
{
static std::random_device rd;
static std::mt19937 gen{rd()};
// setup for all factions
auto galaxy_config = _game.getCurrentScene().getSingleton<const galaxy::config::Galaxy&>("galaxy.config");

Expand Down
2 changes: 1 addition & 1 deletion apps/galaxy/systems/PickingSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PickingSystem : public pg::game::SystemInterface

auto& quadtree = _game.getCurrentScene().getSingleton<const pg::Quadtree<entt::entity>&>("galaxy.quadtree");
auto& marker = _game.getCurrentScene().getSingleton<entt::entity>("galaxy.debug.marker");
auto& transform = _game.getGlobalRegistry().get<pg::Transform2D>(marker);
auto& transform = _game.getCurrentSceneRegistry().get<pg::Transform2D>(marker);
auto scaled_range = pg::fVec2{5, 5} * (1.0f / pick.scale);

auto results = quadtree.rangeQuery(pg::fBox{pick.world_position - scaled_range, 2.0f * scaled_range});
Expand Down
Loading
Loading