diff options
| author | Mel <einebeere@gmail.com> | 2024-04-14 22:47:08 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2024-04-14 22:47:08 +0200 |
| commit | dc21ce532b56b56b1b60ff1bbe5726eff83e4dd5 (patch) | |
| tree | 6166201c0543e72ecb77c715105e9a4bba06e796 /src | |
| parent | 0b2474a476b8d54967c7362c2d6bdfc76af1f05b (diff) | |
| download | meowcraft-dc21ce532b56b56b1b60ff1bbe5726eff83e4dd5.tar.zst meowcraft-dc21ce532b56b56b1b60ff1bbe5726eff83e4dd5.zip | |
Fix SIOF [1] in asset system and rework for better flexibility
[1]: https://en.cppreference.com/w/cpp/language/siof
Diffstat (limited to 'src')
| -rw-r--r-- | src/AllAssets.hpp | 16 | ||||
| -rw-r--r-- | src/Assets.cpp | 19 | ||||
| -rw-r--r-- | src/Assets.hpp | 56 | ||||
| -rw-r--r-- | src/Common/Unique.hpp | 12 | ||||
| -rw-r--r-- | src/GFX/Resources.hpp | 21 | ||||
| -rw-r--r-- | src/Render.cpp | 3 | ||||
| -rw-r--r-- | src/meson.build | 1 |
7 files changed, 84 insertions, 44 deletions
diff --git a/src/AllAssets.hpp b/src/AllAssets.hpp new file mode 100644 index 0000000..00e7100 --- /dev/null +++ b/src/AllAssets.hpp @@ -0,0 +1,16 @@ +#pragma once + +#define ALL_ASSETS \ + X(Shaders::terrain, vertex, shaders_terrain_vert_glsl) \ + X(Shaders::terrain, fragment, shaders_terrain_frag_glsl) \ + \ + X(Shaders::clouds, vertex, shaders_clouds_vert_glsl) \ + X(Shaders::clouds, fragment, shaders_clouds_frag_glsl) \ + \ + X(Shaders::block_outline, vertex, shaders_block_outline_vert_glsl) \ + X(Shaders::block_outline, fragment, shaders_block_outline_frag_glsl) \ + \ + X(Shaders::image_viewer, vertex, shaders_image_viewer_vert_glsl) \ + X(Shaders::image_viewer, fragment, shaders_image_viewer_frag_glsl) \ + \ + X(Images, atlas, images_atlas_ppm) diff --git a/src/Assets.cpp b/src/Assets.cpp deleted file mode 100644 index 7cf361c..0000000 --- a/src/Assets.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "Assets.hpp" - -#define ASSET(ns, name, raw_name) \ - namespace ns { Char const* name = ::MC::Assets::Files::raw_name; } - -#define SHADER_ASSET(name) \ - ASSET(Shaders::name, vertex, shaders_##name##_vertex) \ - ASSET(Shaders::name, fragment, shaders_##name##_fragment) - -namespace MC::Assets { - -SHADER_ASSET(terrain) -SHADER_ASSET(clouds) -SHADER_ASSET(block_outline) -SHADER_ASSET(image_viewer) - -ASSET(Images, atlas, images_atlas_ppm) - -} \ No newline at end of file diff --git a/src/Assets.hpp b/src/Assets.hpp index 0de6d90..0d1c635 100644 --- a/src/Assets.hpp +++ b/src/Assets.hpp @@ -1,30 +1,48 @@ #pragma once #include "Common/Sizes.hpp" +#include "Common/Assert.hpp" +#include "Common/Unique.hpp" -#define MC_DECLARE_ASSET(ns, name, raw_name) \ - namespace Files { extern char const* raw_name; } \ - namespace ns { extern Char const* name; } +#include "AllAssets.hpp" -#define MC_DECLARE_SHADER_ASSET(name) \ - MC_DECLARE_ASSET(Shaders::name, vertex, shaders_##name##_vertex) \ - MC_DECLARE_ASSET(Shaders::name, fragment, shaders_##name##_fragment) +namespace MC { -namespace MC::Assets { +using Asset = Char const* const; +using AssetToken = U64; -MC_DECLARE_SHADER_ASSET(terrain) -MC_DECLARE_SHADER_ASSET(clouds) -MC_DECLARE_SHADER_ASSET(block_outline) -MC_DECLARE_SHADER_ASSET(image_viewer) +#define X(ns, _name, file_path) namespace Assets::Files { extern Asset file_path; } +ALL_ASSETS +#undef X -MC_DECLARE_ASSET(Images, atlas, images_atlas_ppm) +namespace Assets { + constexpr AssetToken invalid_asset = 0; + #define X(ns, name, _file_path) namespace ns { constexpr AssetToken name = UNIQUE_NUMBER; } + ALL_ASSETS + #undef X } -// NOTE: We need to declare dependencies both in Assets.cpp and and here -// in Assets.hpp but only because we rename the variable name from it's -// file name to a more readable name. If we didn't do that we could just -// declare the variable in the hpp file, without needing another compilation -// unit at all. -// This would work easily if there was a using x = y; directive that didn't -// just work for types but for variables as well. But there isn't, I think.. :( \ No newline at end of file +constexpr Asset lookup(AssetToken token) { + switch (token) { + #define X(ns, name, file_path) case Assets::ns::name: return Assets::Files::file_path; + ALL_ASSETS + #undef X + + default: UNREACHABLE("Invalid asset token."); + } +} + +template <AssetToken T = Assets::invalid_asset> +constexpr Asset asset(AssetToken t = Assets::invalid_asset) { + static_assert(T != Assets::invalid_asset, "Invalid asset token."); + ASSERT(t == Assets::invalid_asset || t == T, "Can't lookup asset both dynamically and statically at the same time."); + + return lookup(T); +} + +template <> inline Asset asset<Assets::invalid_asset>(AssetToken token) { + return lookup(token); +} + +} diff --git a/src/Common/Unique.hpp b/src/Common/Unique.hpp new file mode 100644 index 0000000..147fb0e --- /dev/null +++ b/src/Common/Unique.hpp @@ -0,0 +1,12 @@ +#pragma once + +// TODO: Put this macro in it's own Common header, +// it's useful often. +#define _UNIQUE_CONCAT(a, b) _UNIQUE_CONCAT2(a, b) +#define _UNIQUE_CONCAT2(a, b) a##b + +#define _UNIQUE_NAME_PREFIX _unique_name_ +#define UNIQUE_NAME _UNIQUE_CONCAT(_UNIQUE_NAME_PREFIX, __COUNTER__) + +#define _UNIQUE_NUMBER_PREFIX 0xAEF +#define UNIQUE_NUMBER _UNIQUE_CONCAT(_UNIQUE_NUMBER_PREFIX, __COUNTER__) diff --git a/src/GFX/Resources.hpp b/src/GFX/Resources.hpp index 4206e33..8f9a79a 100644 --- a/src/GFX/Resources.hpp +++ b/src/GFX/Resources.hpp @@ -24,10 +24,23 @@ private: }; static inline std::array<ProgramMetadata, ProgramCount> const m_program_metadata = {{ - {Program::Terrain, Assets::Shaders::terrain::vertex, Assets::Shaders::terrain::fragment}, - {Program::Clouds, Assets::Shaders::clouds::vertex, Assets::Shaders::clouds::fragment}, - {Program::ImageViewer, Assets::Shaders::image_viewer::vertex, Assets::Shaders::image_viewer::fragment}, - {Program::BlockOutline, Assets::Shaders::block_outline::vertex, Assets::Shaders::block_outline::fragment}, + { + Program::Terrain, + asset<Assets::Shaders::terrain::vertex>(), + asset<Assets::Shaders::terrain::fragment>() + }, { + Program::Clouds, + asset<Assets::Shaders::clouds::vertex>(), + asset<Assets::Shaders::clouds::fragment>() + }, { + Program::ImageViewer, + asset<Assets::Shaders::image_viewer::vertex>(), + asset<Assets::Shaders::image_viewer::fragment>() + }, { + Program::BlockOutline, + asset<Assets::Shaders::block_outline::vertex>(), + asset<Assets::Shaders::block_outline::fragment>() + }, }}; Bool m_initialized = false; diff --git a/src/Render.cpp b/src/Render.cpp index 58d5a42..c4ed147 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -26,7 +26,8 @@ void Render::run() { glViewport(0, 0, w, h); }); - auto image = GFX::Image::PPMParser(Assets::Images::atlas).parse(); + auto atlas_asset = MC::asset<MC::Assets::Images::atlas>(); + auto image = GFX::Image::PPMParser(atlas_asset).parse(); auto texture = GFX::Texture(image); glEnable(GL_DEPTH_TEST); diff --git a/src/meson.build b/src/meson.build index 9a33eed..498274c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -5,7 +5,6 @@ source_files = files([ 'Input.cpp', 'Time.cpp', 'Transform.cpp', - 'Assets.cpp', 'Math/AABB.cpp', 'Math/Grid.cpp', |
