Skip to content

Commit

Permalink
Add some files
Browse files Browse the repository at this point in the history
  • Loading branch information
xavieran committed Feb 29, 2024
1 parent 4470435 commit dd890b3
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bak/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ std::unordered_map<unsigned, Scene> LoadScenes(FileBuffer& fb)
}

logger.Debug() << ss.str() << "\n";
}
}

std::unordered_map<unsigned, Scene> scenes{};

Expand Down
2 changes: 1 addition & 1 deletion bak/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ std::ostream& operator<<(std::ostream&, const SceneIndex&);

struct ImageSlot
{
std::optional<std::string> mImage;
std::vector<std::string> mImage;
std::optional<unsigned> mPalette;
};

Expand Down
210 changes: 210 additions & 0 deletions gui/dynamicTTM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#include "gui/staticTTM.hpp"

#include "bak/textureFactory.hpp"

#include "com/assert.hpp"
#include "com/logger.hpp"

#include "graphics/types.hpp"

#include "gui/colors.hpp"

namespace Gui {

DynamicTTM::DynamicTTM(
Graphics::SpriteManager& spriteManager,
const BAK::Scene& sceneInit,
const BAK::Scene& sceneContent)
:
mSpriteSheet{spriteManager.AddTemporarySpriteSheet()},
mSceneFrame{
Graphics::DrawMode::Rect,
mSpriteSheet->mSpriteSheet,
Graphics::TextureIndex{0},
Graphics::ColorMode::SolidColor,
glm::vec4{0},
glm::vec2{0},
glm::vec2{1},
false
},
mDialogBackground{},
mSceneElements{},
mClipRegion{},
mLogger{Logging::LogState::GetLogger("Gui::DynamicTTM")}
{
mLogger.Debug() << "Loading scene: " << sceneInit << " with " << sceneContent << std::endl;
auto textures = Graphics::TextureStore{};
std::unordered_map<unsigned, unsigned> offsets{};

// Load all the image slots
for (const auto& scene : {sceneInit, sceneContent})
{
for (const auto& [imageKey, imagePal] : scene.mImages)
{
const auto& [image, palKey] = imagePal;
assert(scene.mPalettes.find(palKey) != scene.mPalettes.end());
const auto& palette = scene.mPalettes.find(palKey)->second;
mLogger.Debug() << "Loading image slot: " << imageKey
<< " (" << image << ") with palette: " << palKey << std::endl;
offsets[imageKey] = textures.GetTextures().size();

BAK::TextureFactory::AddToTextureStore(
textures,
image,
palette);
}
for (const auto& [screenKey, screenPal] : scene.mScreens)
{
const auto& [screen, palKey] = screenPal;
const auto& palette = scene.mPalettes.find(palKey)->second;
mLogger.Debug() << "Loading screen slot: " << screenKey
<< " (" << screen << ") with palette: " << palKey << std::endl;
offsets[25] = textures.GetTextures().size();

BAK::TextureFactory::AddScreenToTextureStore(
textures,
screen,
palette);
}
}

// Make sure all the refs are constant
mSceneElements.reserve(
sceneInit.mActions.size()
+ sceneContent.mActions.size());

// Some scenes will have a dialog background specified in slot 5
// Find and add it if so
constexpr auto DIALOG_BACKGROUND_SLOT = 5;
const auto dialogBackground = offsets.find(DIALOG_BACKGROUND_SLOT);
if (dialogBackground != offsets.end())
{
const auto texture = dialogBackground->second;
mDialogBackground.emplace(
Graphics::DrawMode::Sprite,
mSpriteSheet->mSpriteSheet,
Graphics::TextureIndex{texture},
Graphics::ColorMode::Texture,
glm::vec4{1},
glm::vec2{0},
glm::vec2{1},
false
);
}

// Add widgets for each scene action
for (const auto& scene : {sceneInit, sceneContent})
{
for (const auto& action : scene.mActions)
{
std::visit(
overloaded{
[&](const BAK::DrawScreen& sa){
mLogger.Info() << "DrawScreen: " << sa << "\n";
const auto screen = ConvertSceneAction(
sa,
textures,
offsets);

auto& elem = mSceneElements.emplace_back(
Graphics::DrawMode::Sprite,
mSpriteSheet->mSpriteSheet,
Graphics::TextureIndex{screen.mImage},
Graphics::ColorMode::Texture,
glm::vec4{1},
screen.mPosition,
screen.mScale,
false);

// Either add to the clip region or to the frame
// This doesn't work if the scene has more than one
// clip region...
if (mClipRegion)
mClipRegion->AddChildBack(&elem);
else
mSceneFrame.AddChildBack(&elem);
},
[&](const BAK::DrawSprite& sa){
const auto sceneSprite = ConvertSceneAction(
sa,
textures,
offsets);

auto& elem = mSceneElements.emplace_back(
Graphics::DrawMode::Sprite,
mSpriteSheet->mSpriteSheet,
Graphics::TextureIndex{sceneSprite.mImage},
Graphics::ColorMode::Texture,
glm::vec4{1},
sceneSprite.mPosition,
sceneSprite.mScale,
false);

// Either add to the clip region or to the frame
// This doesn't work if the scene has more than one
// clip region...
if (mClipRegion)
mClipRegion->AddChildBack(&elem);
else
mSceneFrame.AddChildBack(&elem);
},
[&](const BAK::DrawRect& sr){
constexpr auto FRAME_COLOR_INDEX = 6;
const auto [palKey, colorKey] = sr.mPaletteColor;
const auto sceneRect = SceneRect{
// Reddy brown frame color
colorKey == FRAME_COLOR_INDEX
? Gui::Color::frameMaroon
: Gui::Color::black,
glm::vec2{sr.mTopLeft.x, sr.mTopLeft.y},
// FIXME: This should be renamed dims not bottom right...
glm::vec2{sr.mBottomRight.x, sr.mBottomRight.y}};
// This really only works for "DrawFrame", not "DrawRect"
mSceneFrame.SetPosition(sceneRect.mPosition);
mSceneFrame.SetDimensions(sceneRect.mDimensions);
mSceneFrame.SetColor(sceneRect.mColor);

// DialogBackground will have same dims...
if (mDialogBackground)
{
mDialogBackground->SetPosition(sceneRect.mPosition + glm::vec2{1,1});
mDialogBackground->SetDimensions(sceneRect.mDimensions - glm::vec2{2, 2});
}
},
[&](const BAK::ClipRegion& a){
const auto clip = ConvertSceneAction(a);
mClipRegion.emplace(
ClipRegionTag{},
clip.mTopLeft,
clip.mDims,
false);
mSceneFrame.AddChildBack(&(*mClipRegion));
},
[&](const BAK::DisableClipRegion&){
// Doesn't really do anything...
// in the future maybe pop the clip region
// so we could add another one?
}
},
action
);
}
}

spriteManager
.GetSpriteSheet(mSpriteSheet->mSpriteSheet)
.LoadTexturesGL(textures);
}

Widget* DynamicTTM::GetScene()
{
return &mSceneFrame;
}

Widget* DynamicTTM::GetBackground()
{
ASSERT(mDialogBackground);
return &(*mDialogBackground);
}

}
37 changes: 37 additions & 0 deletions gui/dynamicTTM.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "bak/scene.hpp"

#include "com/logger.hpp"

#include "graphics/guiTypes.hpp"
#include "graphics/sprites.hpp"

#include "gui/scene.hpp"
#include "gui/core/widget.hpp"

namespace Gui {

class DynamicTTM
{
public:
DynamicTTM(
Graphics::SpriteManager& spriteManager,
const BAK::Scene& sceneInit,
const BAK::Scene& sceneContent);

Widget* GetScene();
Widget* GetBackground();

private:
Graphics::SpriteManager::TemporarySpriteSheet mSpriteSheet;
Widget mSceneFrame;
std::optional<Widget> mDialogBackground;

std::vector<Widget> mSceneElements;
std::optional<Widget> mClipRegion;

const Logging::Logger& mLogger;
};

}

0 comments on commit dd890b3

Please sign in to comment.