-
Notifications
You must be signed in to change notification settings - Fork 1
Add asset manifest, its loader, and a runtime #31
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
570317e
`Resources`: add `store()`, logging
karnkaul 395db4c
Add `AssetManifest` and `ManifestLoader`
karnkaul a0b49d0
Move snake color to theme
karnkaul 2599062
Add `runtime::MainMenu`, rewire `Entrypoint`
karnkaul 22d6cd9
Add `ui::ProgressBar`
karnkaul 8daa160
Cleanup, fixes
karnkaul 1919ed3
Add `[[nodiscard]]`
karnkaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #pragma once | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace chomper { | ||
| struct AssetManifest { | ||
| std::vector<std::string> textures{}; | ||
| std::vector<std::string> audio{}; | ||
| std::vector<std::string> fonts{}; | ||
| }; | ||
| } // namespace chomper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #pragma once | ||
| #include "chomper/manifest/asset_manifest.hpp" | ||
| #include "chomper/resources.hpp" | ||
| #include <klib/log.hpp> | ||
| #include <le2d/asset/asset.hpp> | ||
| #include <le2d/asset/asset_loader.hpp> | ||
| #include <future> | ||
| #include <memory> | ||
| #include <span> | ||
|
|
||
| namespace chomper { | ||
| class ManifestLoader { | ||
| public: | ||
| struct Progress; | ||
|
|
||
| explicit ManifestLoader(le::AssetLoader asset_loader); | ||
|
|
||
| void startLoad(AssetManifest const& manifest); | ||
|
|
||
| Progress update(); | ||
| Progress getProgress() const; | ||
|
|
||
| void transferTo(Resources& out); | ||
|
|
||
| private: | ||
| struct Asset { | ||
| std::string uri{}; | ||
| std::unique_ptr<le::IAsset> asset{}; | ||
| }; | ||
|
|
||
| template <std::derived_from<le::IAsset> AssetTypeT> | ||
| void startLoads(std::span<std::string const> uris); | ||
|
|
||
| klib::TypedLogger<ManifestLoader> m_log{}; | ||
|
|
||
| le::AssetLoader m_asset_loader{}; | ||
|
|
||
| std::vector<std::future<Asset>> m_loading{}; // assets being loaded. | ||
| std::vector<Asset> m_loaded{}; // loaded assets. | ||
| }; | ||
|
|
||
| struct ManifestLoader::Progress { | ||
| [[nodiscard]] constexpr std::int64_t getTotal() const { | ||
| return remaining + loaded; | ||
| } | ||
|
|
||
| [[nodiscard]] constexpr float normalized() const { | ||
| auto const total = getTotal(); | ||
| if (total == 0) { | ||
| return 0.0f; | ||
| } | ||
| return float(loaded) / float(total); | ||
| } | ||
|
|
||
| [[nodiscard]] constexpr bool isLoading() const { | ||
| return remaining > 0; | ||
| } | ||
|
|
||
| std::int64_t remaining{}; | ||
| std::int64_t loaded{}; | ||
| }; | ||
| } // namespace chomper | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #pragma once | ||
| #include "chomper/engine.hpp" | ||
| #include "chomper/runtime.hpp" | ||
| #include <le2d/drawable/text.hpp> | ||
|
|
||
| namespace chomper::runtime { | ||
| class MainMenu : public IRuntime { | ||
| public: | ||
| explicit MainMenu(gsl::not_null<Engine*> engine); | ||
|
|
||
| private: | ||
| void tick(kvf::Seconds dt) final; | ||
| void render(le::IRenderer& renderer) const final; | ||
|
|
||
| void swingMainText(kvf::Seconds dt); | ||
|
|
||
| gsl::not_null<Engine*> m_engine; | ||
|
|
||
| le::drawable::Text m_mainText{}; | ||
| kvf::Seconds m_elapsed{}; | ||
| }; | ||
| } // namespace chomper::runtime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #pragma once | ||
| #include <le2d/drawable/shape.hpp> | ||
|
|
||
| namespace chomper::ui { | ||
| class ProgressBar { | ||
| public: | ||
| void setProgress(float value); | ||
|
|
||
| void draw(le::IRenderer& renderer) const { | ||
| quad.draw(renderer); | ||
| } | ||
|
|
||
| le::drawable::Quad quad{}; | ||
|
|
||
| glm::vec2 targetSize{300.0f, 50.0f}; | ||
| }; | ||
| } // namespace chomper::ui |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include "chomper/manifest/manifest_loader.hpp" | ||
| #include <klib/assert.hpp> | ||
| #include <le2d/resource/audio_buffer.hpp> | ||
| #include <le2d/resource/font.hpp> | ||
| #include <le2d/resource/texture.hpp> | ||
|
|
||
| namespace chomper { | ||
| ManifestLoader::ManifestLoader(le::AssetLoader asset_loader) : m_asset_loader(std::move(asset_loader)) {} | ||
|
|
||
| void ManifestLoader::startLoad(AssetManifest const& assetManifest) { | ||
| // call startLoads<AssetType>() for each field in the manifest. | ||
| startLoads<le::ITexture>(assetManifest.textures); | ||
| startLoads<le::IAudioBuffer>(assetManifest.audio); | ||
| startLoads<le::IFont>(assetManifest.fonts); | ||
| } | ||
|
|
||
| auto ManifestLoader::getProgress() const -> Progress { | ||
| return Progress{ | ||
| .remaining = std::int64_t(m_loading.size()), | ||
| .loaded = std::int64_t(m_loaded.size()), | ||
| }; | ||
| } | ||
|
|
||
| auto ManifestLoader::update() -> Progress { | ||
| auto const isReady = [this](std::future<Asset>& future) { | ||
| // should only have futures from std::async(). | ||
| KLIB_ASSERT(future.valid()); | ||
|
|
||
| if (future.wait_for(0s) == std::future_status::ready) { // if future is ready, | ||
| m_loaded.push_back(future.get()); // store loaded asset, and | ||
| return true; // erase corresponding future. | ||
| } | ||
| return false; // else do nothing. | ||
| }; | ||
| std::erase_if(m_loading, isReady); | ||
| return getProgress(); | ||
| } | ||
|
|
||
| void ManifestLoader::transferTo(Resources& out) { | ||
| // block until all pending loads have been completed. | ||
| for (auto& future : m_loading) { | ||
| m_loaded.push_back(future.get()); | ||
| } | ||
| m_loading.clear(); | ||
|
|
||
| // transfer valid loaded assets to out. | ||
| auto count = std::int64_t{}; | ||
| for (auto& asset : m_loaded) { | ||
| if (!asset.asset) { | ||
| continue; | ||
| } | ||
| out.store(std::move(asset.uri), std::move(asset.asset)); | ||
| ++count; | ||
| } | ||
| m_loaded.clear(); | ||
|
|
||
| m_log.info("{} asset(s) transferred to {}", count, klib::demangled_name(out)); | ||
| } | ||
|
|
||
| template <std::derived_from<le::IAsset> AssetTypeT> | ||
| void ManifestLoader::startLoads(std::span<std::string const> uris) { | ||
| for (auto const& uri : uris) { | ||
| if (uri.empty()) { | ||
| continue; | ||
| } | ||
| auto const loadAsset = [this, uri] { | ||
| return Asset{.uri = std::move(uri), .asset = m_asset_loader.load<AssetTypeT>(uri)}; | ||
| }; | ||
| m_loading.push_back(std::async(loadAsset)); | ||
| } | ||
| } | ||
| } // namespace chomper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.