diff --git a/.clang-format b/.clang-format index 897422b..01a519d 100644 --- a/.clang-format +++ b/.clang-format @@ -1,7 +1,7 @@ --- Language: Cpp # BasedOnStyle: Google -AccessModifierOffset: -1 +AccessModifierOffset: -4 AlignAfterOpenBracket: Align AlignArrayOfStructures: None AlignConsecutiveAssignments: diff --git a/CMakeLists.txt b/CMakeLists.txt index 67a87bc..5f0dc53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,70 +43,69 @@ set(SRC_FILES "src/libs/weldmesh.c" "src/libs/weldmesh.h" "src/renderer.cpp" - "src/resources/font.cpp" - "src/resources/material.cpp" - "src/resources/mesh.cpp" - "src/resources/shader.cpp" - "src/resources/texture.cpp" + "src/resource_font.cpp" + "src/resource_material.cpp" + "src/resource_mesh.cpp" + "src/resource_shader.cpp" + "src/resource_texture.cpp" "src/scene.cpp" "src/scene_manager.cpp" - "src/systems/collisions.cpp" - "src/systems/custom_behaviour.cpp" - "src/systems/mesh_render_system.cpp" - "src/systems/ui_render_system.cpp" - "src/systems/transform.cpp" - "src/util/files.cpp" - "src/util/gen_tangents.cpp" - "src/util/gltf_loader.cpp" - "src/vulkan/device.cpp" - "src/vulkan/device.h" - "src/vulkan/gpu_allocator.cpp" - "src/vulkan/gpu_allocator.h" - "src/vulkan/instance.cpp" - "src/vulkan/instance.h" - "src/vulkan/swapchain.cpp" - "src/vulkan/swapchain.h" + "src/system_collisions.cpp" + "src/system_custom_behaviour.cpp" + "src/system_mesh_render.cpp" + "src/system_transform.cpp" + "src/files.cpp" + "src/gen_tangents.cpp" + "src/gltf_loader.cpp" + "src/vulkan_device.cpp" + "src/vulkan_allocator.cpp" + "src/vulkan_instance.cpp" + "src/vulkan_swapchain.cpp" "src/window.cpp" - "src/util/file_dialog.cpp") + "src/file_dialog.cpp" +) set(INCLUDE_FILES "include/application.h" "include/application_component.h" - "include/components/collider.h" - "include/components/custom.h" - "include/components/mesh_renderable.h" - "include/components/ui_renderable.h" - "include/components/transform.h" + "include/component_collider.h" + "include/component_custom.h" + "include/component_mesh.h" + "include/component_transform.h" "include/ecs.h" - "include/engine_api.h" + "include/vulkan_allocator.h" + "include/vulkan_device.h" + "include/vulkan_instance.h" + "include/vulkan_swapchain.h" "include/event_system.h" "include/gfx.h" "include/gfx_device.h" "include/input_manager.h" - "include/inputs/keyboard.h" - "include/inputs/mouse.h" + "include/input_keys.h" + "include/input_mouse.h" "include/log.h" "include/logger.h" "include/renderer.h" "include/resource_manager.h" - "include/resources/font.h" - "include/resources/material.h" - "include/resources/mesh.h" - "include/resources/shader.h" - "include/resources/texture.h" + "include/resource_font.h" + "include/resource_material.h" + "include/resource_mesh.h" + "include/resource_shader.h" + "include/resource_texture.h" "include/scene.h" "include/scene_manager.h" - "include/systems/collisions.h" - "include/systems/custom_behaviour.h" - "include/systems/mesh_render_system.h" - "include/systems/ui_render_system.h" - "include/systems/transform.h" + "include/system_collisions.h" + "include/system_custom_behaviour.h" + "include/system_mesh_render.h" + "include/system_transform.h" "include/util.h" - "include/util/files.h" - "include/util/gen_tangents.h" - "include/util/gltf_loader.h" + "include/files.h" + "include/gen_tangents.h" + "include/gltf_loader.h" "include/window.h" - "include/util/file_dialog.h") + "include/file_dialog.h" +) + file(GLOB_RECURSE RES_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/res/engine/*") diff --git a/CODE_STYLE.TXT b/CODE_STYLE.TXT index 9ded391..8860367 100644 --- a/CODE_STYLE.TXT +++ b/CODE_STYLE.TXT @@ -119,6 +119,19 @@ private: }; ` +Structs are laid out as follows: +` +struct Y { + /* static data members */ + /* non-static data members */ + + /* static methods */ + /* non-static methods */ +}; +` +Avoid having access specifiers in structs. +Structs should almost always be POD. If not, consider using a class instead. + Inlined methods are to be avoided as much as possible. Even for trivial getters/setters. Inline methods are only used when doing so provides a tangible performance gain. @@ -126,4 +139,23 @@ Classes that should not be copied and/or moved should have their copy and/or mov and copy/move assignment operators explicitly deleted. Ensure that constructors with a single argument are marked `explicit` unless implicit construction -is intended. \ No newline at end of file +is intended. + + + +################################################################################ + + + +###################### +# INCLUDE DIRECTIVES # +###################### + +Header files should be included in the following order: +- Header corresponding to source file +- C standard library includes (as, e.g., cstdint) +- C++ STL includes +- External libraries from, e.g., 'dependencies/' directory or Vulkan SDK. Use angle brackets. +- Libraries in the source tree. Use double quotes. (TODO: Move these into 'dependencies/') +- Header files from the current project. Use double quotes. +Each section should be sorted alphabetically (In VS: highlight the includes, right-click -> #include directives -> sort). \ No newline at end of file diff --git a/include/components/collider.h b/include/component_collider.h similarity index 100% rename from include/components/collider.h rename to include/component_collider.h diff --git a/include/component_custom.h b/include/component_custom.h new file mode 100644 index 0000000..f60d260 --- /dev/null +++ b/include/component_custom.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace engine { + +struct CustomComponent { + std::function on_init; // void on_init(void); + std::function on_update; // void on_update(float ts); +}; + +} // namespace engine \ No newline at end of file diff --git a/include/components/mesh_renderable.h b/include/component_mesh.h similarity index 100% rename from include/components/mesh_renderable.h rename to include/component_mesh.h diff --git a/include/components/transform.h b/include/component_transform.h similarity index 100% rename from include/components/transform.h rename to include/component_transform.h diff --git a/include/components/custom.h b/include/components/custom.h deleted file mode 100644 index 9f2fbd7..0000000 --- a/include/components/custom.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -namespace engine { - -struct CustomComponent { - std::function onInit; // void onInit(void); - std::function onUpdate; // void onUpdate(float ts); -}; - -} // namespace engine \ No newline at end of file diff --git a/include/components/ui_renderable.h b/include/components/ui_renderable.h deleted file mode 100644 index f532b03..0000000 --- a/include/components/ui_renderable.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -namespace engine { - -struct UIRenderableComponent { - int x; -}; - -} // namespace engine \ No newline at end of file diff --git a/include/engine_api.h b/include/engine_api.h deleted file mode 100644 index dd45887..0000000 --- a/include/engine_api.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -/* -#ifndef ENGINE_API -# ifdef _MSC_VER -# ifdef ENGINE_EXPORTS -# define ENGINE_API __declspec(dllexport) -# else -# define ENGINE_API __declspec(dllimport) -# endif -# else -# define ENGINE_API -# endif -#endif -*/ - -#define ENGINE_API \ No newline at end of file diff --git a/include/util/file_dialog.h b/include/file_dialog.h similarity index 100% rename from include/util/file_dialog.h rename to include/file_dialog.h diff --git a/include/util/files.h b/include/files.h similarity index 92% rename from include/util/files.h rename to include/files.h index 6039f5a..2496cb5 100644 --- a/include/util/files.h +++ b/include/files.h @@ -1,11 +1,12 @@ #pragma once +#include + #include #include #include namespace engine { -namespace util { std::unique_ptr> ReadTextFile(const std::string& path); std::unique_ptr> ReadBinaryFile(const std::string& path); @@ -14,5 +15,4 @@ std::unique_ptr> ReadBinaryFile(const std::string& path); // minimum. Output format is R8G8B8A8_UINT std::unique_ptr> ReadImageFile(const std::string& path, int& width, int& height); -} // namespace util } // namespace engine \ No newline at end of file diff --git a/include/util/gen_tangents.h b/include/gen_tangents.h similarity index 100% rename from include/util/gen_tangents.h rename to include/gen_tangents.h diff --git a/include/util/gltf_loader.h b/include/gltf_loader.h similarity index 100% rename from include/util/gltf_loader.h rename to include/gltf_loader.h diff --git a/include/inputs/keyboard.h b/include/input_keys.h similarity index 99% rename from include/inputs/keyboard.h rename to include/input_keys.h index 85b4ccc..dccb284 100644 --- a/include/inputs/keyboard.h +++ b/include/input_keys.h @@ -2,8 +2,7 @@ // Keyboard scancodes, taken from SDL_scancode.h -namespace engine { -namespace inputs { +namespace engine::inputs { enum class Key : int { K_UNKNOWN = 0, @@ -372,5 +371,4 @@ enum class Key : int { for array bounds */ }; -} // namespace inputs -} // namespace engine \ No newline at end of file +} // namespace engine::inputs \ No newline at end of file diff --git a/include/inputs/mouse.h b/include/input_mouse.h similarity index 67% rename from include/inputs/mouse.h rename to include/input_mouse.h index e5db720..c998f3b 100644 --- a/include/inputs/mouse.h +++ b/include/input_mouse.h @@ -1,11 +1,9 @@ #pragma once -namespace engine { -namespace inputs { +namespace engine::inputs { enum class MouseButton : int { M_LEFT, M_MIDDLE, M_RIGHT, M_X1, M_X2, M_INVALID = 7, M_SIZE = 7 }; enum class MouseAxis : int { X, Y, X_SCR, Y_SCR }; -} // namespace inputs -} // namespace engine \ No newline at end of file +} // namespace engine::inputs \ No newline at end of file diff --git a/include/resources/font.h b/include/resource_font.h similarity index 50% rename from include/resources/font.h rename to include/resource_font.h index abac082..2152c62 100644 --- a/include/resources/font.h +++ b/include/resource_font.h @@ -10,20 +10,22 @@ namespace engine { class Font { - public: - Font(const std::string& path); - ~Font(); + std::unique_ptr m_font_info{}; + std::unique_ptr> m_font_buffer{}; + std::map m_unicode_to_glyph{}; + +public: + explicit Font(const std::string& path); Font(const Font&) = delete; + + ~Font(); + Font& operator=(const Font&) = delete; - std::unique_ptr> GetTextBitmap(const std::string& text, float height_px, int& width_out, int& height_out); + std::unique_ptr> getTextBitmap(const std::string& text, float height_px, int& width_out, int& height_out); - private: - std::unique_ptr font_info_{}; - std::unique_ptr> font_buffer_{}; - std::map unicode_to_glyph_{}; - - int GetGlyphIndex(int unicode_codepoint); +private: + int getGlyphIndex(int unicode_codepoint); }; } // namespace engine \ No newline at end of file diff --git a/include/resource_material.h b/include/resource_material.h new file mode 100644 index 0000000..d3bb774 --- /dev/null +++ b/include/resource_material.h @@ -0,0 +1,36 @@ +#pragma once + +#include + +#include "resources/shader.h" +#include "resources/texture.h" + +// a material is just a shader with assigned textures/parameters + +namespace engine { + +// copyable +class Material { + const std::shared_ptr m_shader; + std::shared_ptr m_texture_albedo; + std::shared_ptr m_texture_normal; + std::shared_ptr m_texture_occlusion_roughness_metallic; + const gfx::DescriptorSet* m_material_set = nullptr; + Renderer* const m_renderer; + +public: + Material(Renderer* renderer, std::shared_ptr shader); + Material(const Material&) = delete; + + ~Material(); + + Material& operator=(const Material&) = delete; + + void setAlbedoTexture(std::shared_ptr texture); + void setNormalTexture(std::shared_ptr texture); + void setOcclusionRoughnessMetallicTexture(std::shared_ptr texture); + const gfx::DescriptorSet* getDescriptorSet() { return m_material_set; } + Shader* getShader() { return m_shader.get(); } +}; + +} // namespace engine \ No newline at end of file diff --git a/include/resources/mesh.h b/include/resource_mesh.h similarity index 64% rename from include/resources/mesh.h rename to include/resource_mesh.h index ad23244..5f24b5d 100644 --- a/include/resources/mesh.h +++ b/include/resource_mesh.h @@ -1,9 +1,10 @@ #pragma once +#include + #include #include #include -#include #include "gfx.h" #include "gfx_device.h" @@ -15,33 +16,31 @@ struct Vertex { glm::vec3 norm; glm::vec4 tangent; // w component flips binormal if -1. w should be 1 or -1 glm::vec2 uv; - static constexpr int FloatsPerVertex() { return static_cast(sizeof(Vertex) / sizeof(float)); } + + static constexpr int floatsPerVertex() { return static_cast(sizeof(Vertex) / sizeof(float)); } }; -} // namespace engine - -namespace engine { - class Mesh { - public: + GFXDevice* const m_gfx; + const gfx::Buffer* m_vb; + const gfx::Buffer* m_ib; + uint32_t m_count; + +public: Mesh(GFXDevice* gfx, const std::vector& vertices); Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices); - ~Mesh(); Mesh(const Mesh&) = delete; + + ~Mesh(); + Mesh& operator=(const Mesh&) = delete; - const gfx::Buffer* GetVB(); - const gfx::Buffer* GetIB(); - uint32_t GetCount(); + const gfx::Buffer* getVB(); + const gfx::Buffer* getIB(); + uint32_t getCount(); - private: - GFXDevice* const gfx_; - - const gfx::Buffer* vb_; - const gfx::Buffer* ib_; - uint32_t count_; - - void InitMesh(const std::vector& vertices, const std::vector& indices); +private: + void initMesh(const std::vector& vertices, const std::vector& indices); }; } // namespace engine \ No newline at end of file diff --git a/include/resources/shader.h b/include/resource_shader.h similarity index 100% rename from include/resources/shader.h rename to include/resource_shader.h diff --git a/include/resources/texture.h b/include/resource_texture.h similarity index 100% rename from include/resources/texture.h rename to include/resource_texture.h diff --git a/include/resources/material.h b/include/resources/material.h deleted file mode 100644 index b035720..0000000 --- a/include/resources/material.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include - -#include "resources/shader.h" -#include "resources/texture.h" - -// a material is just a shader with assigned textures/parameters - -namespace engine { - -// copyable -class Material { - public: - Material(Renderer* renderer, std::shared_ptr shader); - ~Material(); - Material& operator=(const Material&) = delete; - - void SetAlbedoTexture(std::shared_ptr texture); - void SetNormalTexture(std::shared_ptr texture); - void SetOcclusionRoughnessMetallicTexture(std::shared_ptr texture); - - const gfx::DescriptorSet* GetDescriptorSet() { return material_set_; } - Shader* GetShader() { return shader_.get(); } - - private: - const std::shared_ptr shader_; - std::shared_ptr texture_albedo_; - std::shared_ptr texture_normal_; - std::shared_ptr texture_occlusion_roughness_metallic_; - - const gfx::DescriptorSet* material_set_ = nullptr; - - Renderer* const renderer_; -}; - -} // namespace engine \ No newline at end of file diff --git a/include/systems/collisions.h b/include/system_collisions.h similarity index 100% rename from include/systems/collisions.h rename to include/system_collisions.h diff --git a/include/systems/custom_behaviour.h b/include/system_custom_behaviour.h similarity index 100% rename from include/systems/custom_behaviour.h rename to include/system_custom_behaviour.h diff --git a/include/systems/mesh_render_system.h b/include/system_mesh_render.h similarity index 100% rename from include/systems/mesh_render_system.h rename to include/system_mesh_render.h diff --git a/include/systems/transform.h b/include/system_transform.h similarity index 100% rename from include/systems/transform.h rename to include/system_transform.h diff --git a/include/systems/ui_render_system.h b/include/systems/ui_render_system.h deleted file mode 100644 index 31c7791..0000000 --- a/include/systems/ui_render_system.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include - -#include "ecs.h" -#include "scene.h" -#include "gfx.h" - -namespace engine { - -class UIRenderSystem : public System { - public: - UIRenderSystem(Scene* scene); - ~UIRenderSystem(); - - void OnComponentInsert(Entity entity) override; - void OnUpdate(float ts) override; - - private: -}; - -} // namespace engine diff --git a/src/vulkan/gpu_allocator.h b/include/vulkan_allocator.h similarity index 100% rename from src/vulkan/gpu_allocator.h rename to include/vulkan_allocator.h diff --git a/src/vulkan/device.h b/include/vulkan_device.h similarity index 100% rename from src/vulkan/device.h rename to include/vulkan_device.h diff --git a/src/vulkan/instance.h b/include/vulkan_instance.h similarity index 100% rename from src/vulkan/instance.h rename to include/vulkan_instance.h diff --git a/src/vulkan/swapchain.h b/include/vulkan_swapchain.h similarity index 100% rename from src/vulkan/swapchain.h rename to include/vulkan_swapchain.h diff --git a/src/application.cpp b/src/application.cpp index 019360d..818c6b7 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -1,12 +1,11 @@ #include "application.h" -#include #include #include +#include #include #include #include -#include #include @@ -14,6 +13,8 @@ #include "imgui/imgui_impl_sdl2.h" #include "imgui/imgui_impl_vulkan.h" +#include "components/collider.h" +#include "components/transform.h" #include "gfx.h" #include "gfx_device.h" #include "input_manager.h" @@ -23,15 +24,13 @@ #include "resources/mesh.h" #include "resources/shader.h" #include "resources/texture.h" -#include "systems/mesh_render_system.h" -#include "components/transform.h" -#include "components/collider.h" #include "scene.h" #include "scene_manager.h" -#include "window.h" -#include "util/gltf_loader.h" +#include "systems/collisions.h" +#include "systems/mesh_render_system.h" #include "util/file_dialog.h" -#include +#include "util/gltf_loader.h" +#include "window.h" static struct ImGuiThings { ImGuiContext* context; @@ -145,9 +144,9 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph /* default materials */ { auto defaultMaterial = std::make_unique(renderer(), GetResource("builtin.fancy")); - defaultMaterial->SetAlbedoTexture(GetResource("builtin.white")); - defaultMaterial->SetNormalTexture(GetResource("builtin.normal")); - defaultMaterial->SetOcclusionRoughnessMetallicTexture(GetResource("builtin.mr")); + defaultMaterial->setAlbedoTexture(GetResource("builtin.white")); + defaultMaterial->setNormalTexture(GetResource("builtin.normal")); + defaultMaterial->setOcclusionRoughnessMetallicTexture(GetResource("builtin.mr")); GetResourceManager()->AddPersistent("builtin.default", std::move(defaultMaterial)); } } diff --git a/src/util/file_dialog.cpp b/src/file_dialog.cpp similarity index 100% rename from src/util/file_dialog.cpp rename to src/file_dialog.cpp diff --git a/src/util/files.cpp b/src/files.cpp similarity index 100% rename from src/util/files.cpp rename to src/files.cpp diff --git a/src/util/gen_tangents.cpp b/src/gen_tangents.cpp similarity index 98% rename from src/util/gen_tangents.cpp rename to src/gen_tangents.cpp index 278a208..c5bfdee 100644 --- a/src/util/gen_tangents.cpp +++ b/src/gen_tangents.cpp @@ -65,7 +65,7 @@ std::vector engine::util::GenTangents(std::vector& ver std::vector vertex_data_out(vertices.size()); // initialised to zeros const int new_vertex_count = WeldMesh(reinterpret_cast(remap_table.data()), reinterpret_cast(vertex_data_out.data()), reinterpret_cast(vertices.data()), - static_cast(vertices.size()), Vertex::FloatsPerVertex()); + static_cast(vertices.size()), Vertex::floatsPerVertex()); assert(new_vertex_count >= 0); // get new vertices into the vector. diff --git a/src/util/gltf_loader.cpp b/src/gltf_loader.cpp similarity index 98% rename from src/util/gltf_loader.cpp rename to src/gltf_loader.cpp index 42c5fce..517294d 100644 --- a/src/util/gltf_loader.cpp +++ b/src/gltf_loader.cpp @@ -248,10 +248,10 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) materials.emplace_back(std::make_shared(scene.app()->renderer(), scene.app()->GetResource("builtin.fancy"))); // base color - materials.back()->SetAlbedoTexture(scene.app()->GetResource("builtin.white")); + materials.back()->setAlbedoTexture(scene.app()->GetResource("builtin.white")); if (material.pbrMetallicRoughness.baseColorTexture.index != -1) { if (material.pbrMetallicRoughness.baseColorTexture.texCoord == 0) { - materials.back()->SetAlbedoTexture(textures.at(material.pbrMetallicRoughness.baseColorTexture.index)); + materials.back()->setAlbedoTexture(textures.at(material.pbrMetallicRoughness.baseColorTexture.index)); } else { LOG_WARN("Material {} base color texture specifies a UV channel other than zero which is unsupported.", material.name); @@ -269,16 +269,16 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) samplerInfo.anisotropic_filtering = false; colour_textures.emplace(std::make_pair(c, std::make_shared(scene.app()->renderer(), pixel, 1, 1, samplerInfo, true))); } - materials.back()->SetAlbedoTexture(colour_textures.at(c)); + materials.back()->setAlbedoTexture(colour_textures.at(c)); } // occlusion roughness metallic - materials.back()->SetOcclusionRoughnessMetallicTexture( + materials.back()->setOcclusionRoughnessMetallicTexture( scene.app()->GetResource("builtin.white")); // default ao = 1.0, rough = 1.0, metal = 1.0 if (material.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) { if (material.pbrMetallicRoughness.metallicRoughnessTexture.texCoord == 0) { LOG_DEBUG("Setting occlusion roughness metallic texture!"); - materials.back()->SetOcclusionRoughnessMetallicTexture(textures.at(material.pbrMetallicRoughness.metallicRoughnessTexture.index)); + materials.back()->setOcclusionRoughnessMetallicTexture(textures.at(material.pbrMetallicRoughness.metallicRoughnessTexture.index)); } else { LOG_WARN("Material {} metallic roughness texture specifies a UV channel other than zero which is unsupported.", material.name); @@ -298,7 +298,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) samplerInfo.anisotropic_filtering = false; metal_rough_textures.emplace(std::make_pair(mr, std::make_shared(scene.app()->renderer(), pixel, 1, 1, samplerInfo, false))); } - materials.back()->SetOcclusionRoughnessMetallicTexture(metal_rough_textures.at(mr)); + materials.back()->setOcclusionRoughnessMetallicTexture(metal_rough_textures.at(mr)); } // occlusion texture @@ -316,10 +316,10 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) } // normal map - materials.back()->SetNormalTexture(scene.app()->GetResource("builtin.normal")); + materials.back()->setNormalTexture(scene.app()->GetResource("builtin.normal")); if (material.normalTexture.index != -1) { if (material.normalTexture.texCoord == 0) { - materials.back()->SetNormalTexture(textures.at(material.normalTexture.index)); + materials.back()->setNormalTexture(textures.at(material.normalTexture.index)); } else { LOG_WARN("Material {} normal texture specifies a UV channel other than zero which is unsupported.", material.name); @@ -521,7 +521,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) std::vector vertex_data_out(num_indices); // initialised to zeros const int num_unq_vertices = WeldMesh(remap_table.data(), reinterpret_cast(vertex_data_out.data()), - reinterpret_cast(vertices.data()), static_cast(num_indices), Vertex::FloatsPerVertex()); + reinterpret_cast(vertices.data()), static_cast(num_indices), Vertex::floatsPerVertex()); assert(num_unq_vertices >= 0); // get new vertices into the vector diff --git a/src/imgui/imgui_tables.cpp b/src/imgui/imgui_tables.cpp index 7fbf618..e5654cc 100644 --- a/src/imgui/imgui_tables.cpp +++ b/src/imgui/imgui_tables.cpp @@ -585,7 +585,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG return true; } -// For reference, the average total _allocation count_ for a table is: +// For reference, the average total _allocation m_count for a table is: // + 0 (for ImGuiTable instance, we are pooling allocations in g.Tables[]) // + 1 (for table->RawData allocated below) // + 1 (for table->ColumnsNames, if names are used) diff --git a/src/resources/font.cpp b/src/resource_font.cpp similarity index 75% rename from src/resources/font.cpp rename to src/resource_font.cpp index f80bd97..879d70d 100644 --- a/src/resources/font.cpp +++ b/src/resource_font.cpp @@ -12,10 +12,10 @@ namespace engine { Font::Font(const std::string& path) { - font_buffer_ = util::ReadBinaryFile(path); - font_info_ = std::make_unique(); + m_font_buffer = util::ReadBinaryFile(path); + m_font_info = std::make_unique(); - if (stbtt_InitFont(font_info_.get(), font_buffer_->data(), 0) == 0) { + if (stbtt_InitFont(m_font_info.get(), m_font_buffer->data(), 0) == 0) { throw std::runtime_error("Failed to initialise font!"); } @@ -24,12 +24,12 @@ Font::Font(const std::string& path) Font::~Font() { LOG_DEBUG("Destroyed font"); } -std::unique_ptr> Font::GetTextBitmap(const std::string& text, float height_px, int& width_out, int& height_out) +std::unique_ptr> Font::getTextBitmap(const std::string& text, float height_px, int& width_out, int& height_out) { - const float sf = stbtt_ScaleForPixelHeight(font_info_.get(), height_px); + const float sf = stbtt_ScaleForPixelHeight(m_font_info.get(), height_px); int ascent, descent, line_gap; - stbtt_GetFontVMetrics(font_info_.get(), &ascent, &descent, &line_gap); + stbtt_GetFontVMetrics(m_font_info.get(), &ascent, &descent, &line_gap); struct CharacterRenderInfo { int advance; // bitmap advance @@ -42,10 +42,10 @@ std::unique_ptr> Font::GetTextBitmap(const std::string& tex int width = 0; for (size_t i = 0; i < text.size(); i++) { - const int glyph_index = GetGlyphIndex(static_cast(text.at(i))); + const int glyph_index = getGlyphIndex(static_cast(text.at(i))); int advanceWidth, leftSideBearing; - stbtt_GetGlyphHMetrics(font_info_.get(), glyph_index, &advanceWidth, &leftSideBearing); + stbtt_GetGlyphHMetrics(m_font_info.get(), glyph_index, &advanceWidth, &leftSideBearing); if (i == 0 && leftSideBearing < 0) { // if the character extends before the current point @@ -57,7 +57,7 @@ std::unique_ptr> Font::GetTextBitmap(const std::string& tex renderInfo.advance = static_cast(static_cast(advanceWidth) * sf); - if (stbtt_IsGlyphEmpty(font_info_.get(), glyph_index) == 0) { + if (stbtt_IsGlyphEmpty(m_font_info.get(), glyph_index) == 0) { renderInfo.isEmpty = false; } else { @@ -66,7 +66,7 @@ std::unique_ptr> Font::GetTextBitmap(const std::string& tex if (!renderInfo.isEmpty) { renderInfo.bitmap = - stbtt_GetGlyphBitmap(font_info_.get(), sf, sf, glyph_index, &renderInfo.width, &renderInfo.height, &renderInfo.xoff, &renderInfo.yoff); + stbtt_GetGlyphBitmap(m_font_info.get(), sf, sf, glyph_index, &renderInfo.width, &renderInfo.height, &renderInfo.xoff, &renderInfo.yoff); } characterRenderInfos.push_back(renderInfo); @@ -114,17 +114,17 @@ std::unique_ptr> Font::GetTextBitmap(const std::string& tex return bitmap; } -int Font::GetGlyphIndex(int unicode_codepoint) +int Font::getGlyphIndex(int unicode_codepoint) { - if (unicode_to_glyph_.contains(unicode_codepoint)) { - return unicode_to_glyph_.at(unicode_codepoint); + if (m_unicode_to_glyph.contains(unicode_codepoint)) { + return m_unicode_to_glyph.at(unicode_codepoint); } else { - const int glyph_index = stbtt_FindGlyphIndex(font_info_.get(), unicode_codepoint); + const int glyph_index = stbtt_FindGlyphIndex(m_font_info.get(), unicode_codepoint); if (glyph_index == 0) { throw std::runtime_error("Glyph not found in font!"); } - unicode_to_glyph_.emplace(std::make_pair(unicode_codepoint, glyph_index)); + m_unicode_to_glyph.emplace(std::make_pair(unicode_codepoint, glyph_index)); return glyph_index; } } diff --git a/src/resource_material.cpp b/src/resource_material.cpp new file mode 100644 index 0000000..9c206bf --- /dev/null +++ b/src/resource_material.cpp @@ -0,0 +1,33 @@ +#include "resources/material.h" + +#include "resources/shader.h" + +namespace engine { + +Material::Material(Renderer* renderer, std::shared_ptr shader) : m_shader(shader), m_renderer(renderer) +{ + m_material_set = renderer->GetDevice()->AllocateDescriptorSet(renderer->GetMaterialSetLayout()); + LOG_DEBUG("Created material"); +} + +Material::~Material() { m_renderer->GetDevice()->FreeDescriptorSet(m_material_set); LOG_DEBUG("Destroyed material"); } + +void Material::setAlbedoTexture(std::shared_ptr texture) +{ + m_renderer->GetDevice()->UpdateDescriptorCombinedImageSampler(m_material_set, 0, texture->GetImage(), texture->GetSampler()); + m_texture_albedo = texture; +} + +void Material::setNormalTexture(std::shared_ptr texture) +{ + m_renderer->GetDevice()->UpdateDescriptorCombinedImageSampler(m_material_set, 1, texture->GetImage(), texture->GetSampler()); + m_texture_normal = texture; +} + +void Material::setOcclusionRoughnessMetallicTexture(std::shared_ptr texture) +{ + m_renderer->GetDevice()->UpdateDescriptorCombinedImageSampler(m_material_set, 2, texture->GetImage(), texture->GetSampler()); + m_texture_occlusion_roughness_metallic = texture; +} + +} // namespace engine diff --git a/src/resource_mesh.cpp b/src/resource_mesh.cpp new file mode 100644 index 0000000..63e0948 --- /dev/null +++ b/src/resource_mesh.cpp @@ -0,0 +1,40 @@ +#include "resources/mesh.h" + +#include "log.h" +#include "gfx_device.h" + +namespace engine { + +Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices) : m_gfx(gfx) +{ + std::vector indices(vertices.size()); + for (uint32_t i = 0; i < indices.size(); i++) { + indices[i] = i; + } + initMesh(vertices, indices); +} + +Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices) : m_gfx(gfx) { initMesh(vertices, indices); } + +Mesh::~Mesh() +{ + m_gfx->DestroyBuffer(m_ib); + m_gfx->DestroyBuffer(m_vb); + LOG_DEBUG("Destroyed mesh"); +} + +const gfx::Buffer* Mesh::getVB() { return m_vb; } + +const gfx::Buffer* Mesh::getIB() { return m_ib; } + +uint32_t Mesh::getCount() { return m_count; } + +void Mesh::initMesh(const std::vector& vertices, const std::vector& indices) +{ + m_vb = m_gfx->CreateBuffer(gfx::BufferType::kVertex, vertices.size() * sizeof(Vertex), vertices.data()); + m_ib = m_gfx->CreateBuffer(gfx::BufferType::kIndex, indices.size() * sizeof(uint32_t), indices.data()); + m_count = (uint32_t)indices.size(); + LOG_DEBUG("Created mesh, vertices: {}, indices: {}", vertices.size(), indices.size()); +} + +} // namespace engine diff --git a/src/resources/shader.cpp b/src/resource_shader.cpp similarity index 100% rename from src/resources/shader.cpp rename to src/resource_shader.cpp diff --git a/src/resources/texture.cpp b/src/resource_texture.cpp similarity index 100% rename from src/resources/texture.cpp rename to src/resource_texture.cpp diff --git a/src/resources/material.cpp b/src/resources/material.cpp deleted file mode 100644 index 6a6a5da..0000000 --- a/src/resources/material.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "resources/material.h" - -#include "resources/shader.h" - -namespace engine { - -Material::Material(Renderer* renderer, std::shared_ptr shader) : shader_(shader), renderer_(renderer) -{ - material_set_ = renderer->GetDevice()->AllocateDescriptorSet(renderer->GetMaterialSetLayout()); - LOG_DEBUG("Created material"); -} - -Material::~Material() { renderer_->GetDevice()->FreeDescriptorSet(material_set_); LOG_DEBUG("Destroyed material"); } - -void Material::SetAlbedoTexture(std::shared_ptr texture) -{ - renderer_->GetDevice()->UpdateDescriptorCombinedImageSampler(material_set_, 0, texture->GetImage(), texture->GetSampler()); - texture_albedo_ = texture; -} - -void Material::SetNormalTexture(std::shared_ptr texture) -{ - renderer_->GetDevice()->UpdateDescriptorCombinedImageSampler(material_set_, 1, texture->GetImage(), texture->GetSampler()); - texture_normal_ = texture; -} - -void Material::SetOcclusionRoughnessMetallicTexture(std::shared_ptr texture) -{ - renderer_->GetDevice()->UpdateDescriptorCombinedImageSampler(material_set_, 2, texture->GetImage(), texture->GetSampler()); - texture_occlusion_roughness_metallic_ = texture; -} - -} // namespace engine diff --git a/src/resources/mesh.cpp b/src/resources/mesh.cpp deleted file mode 100644 index 1059cc7..0000000 --- a/src/resources/mesh.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "resources/mesh.h" - -#include "log.h" -#include "gfx_device.h" - -namespace engine { - -Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices) : gfx_(gfx) -{ - std::vector indices(vertices.size()); - for (uint32_t i = 0; i < indices.size(); i++) { - indices[i] = i; - } - InitMesh(vertices, indices); -} - -Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices) : gfx_(gfx) { InitMesh(vertices, indices); } - -Mesh::~Mesh() -{ - gfx_->DestroyBuffer(ib_); - gfx_->DestroyBuffer(vb_); - LOG_DEBUG("Destroyed mesh"); -} - -const gfx::Buffer* Mesh::GetVB() { return vb_; } - -const gfx::Buffer* Mesh::GetIB() { return ib_; } - -uint32_t Mesh::GetCount() { return count_; } - -void Mesh::InitMesh(const std::vector& vertices, const std::vector& indices) -{ - vb_ = gfx_->CreateBuffer(gfx::BufferType::kVertex, vertices.size() * sizeof(Vertex), vertices.data()); - ib_ = gfx_->CreateBuffer(gfx::BufferType::kIndex, indices.size() * sizeof(uint32_t), indices.data()); - count_ = (uint32_t)indices.size(); - LOG_DEBUG("Created mesh, vertices: {}, indices: {}", vertices.size(), indices.size()); -} - -} // namespace engine diff --git a/src/systems/collisions.cpp b/src/system_collisions.cpp similarity index 100% rename from src/systems/collisions.cpp rename to src/system_collisions.cpp diff --git a/src/systems/custom_behaviour.cpp b/src/system_custom_behaviour.cpp similarity index 74% rename from src/systems/custom_behaviour.cpp rename to src/system_custom_behaviour.cpp index 879e60f..6b788c5 100644 --- a/src/systems/custom_behaviour.cpp +++ b/src/system_custom_behaviour.cpp @@ -22,12 +22,12 @@ void CustomBehaviourSystem::OnUpdate(float ts) { assert(c != nullptr); bool& entity_initialised = entity_is_initialised_.at(entity); if (entity_initialised == false) { - if (c->onInit == nullptr) throw std::runtime_error("CustomComponent::onInit not set! Entity: " + std::to_string(entity)); - if (c->onUpdate == nullptr) throw std::runtime_error("CustomComponent::onUpdate not set! Entity: " + std::to_string(entity)); - c->onInit(); + if (c->on_init == nullptr) throw std::runtime_error("CustomComponent::on_init not set! Entity: " + std::to_string(entity)); + if (c->on_update == nullptr) throw std::runtime_error("CustomComponent::on_update not set! Entity: " + std::to_string(entity)); + c->on_init(); entity_initialised = true; } - c->onUpdate(ts); + c->on_update(ts); } } diff --git a/src/systems/mesh_render_system.cpp b/src/system_mesh_render.cpp similarity index 93% rename from src/systems/mesh_render_system.cpp rename to src/system_mesh_render.cpp index 750f81c..5025c04 100644 --- a/src/systems/mesh_render_system.cpp +++ b/src/system_mesh_render.cpp @@ -52,17 +52,17 @@ void MeshRenderSystem::BuildRenderList(RenderList& render_list, bool with_static if (renderable->visible == false) continue; - const gfx::Pipeline* pipeline = renderable->material->GetShader()->GetPipeline(); + const gfx::Pipeline* pipeline = renderable->material->getShader()->GetPipeline(); render_list.emplace_back(RenderListEntry{.pipeline = pipeline, - .vertex_buffer = renderable->mesh->GetVB(), - .index_buffer = renderable->mesh->GetIB(), - .material_set = renderable->material->GetDescriptorSet(), + .vertex_buffer = renderable->mesh->getVB(), + .index_buffer = renderable->mesh->getIB(), + .material_set = renderable->material->getDescriptorSet(), .model_matrix = transform->world_matrix, - .index_count = renderable->mesh->GetCount()}); + .index_count = renderable->mesh->getCount()}); if (render_orders.contains(pipeline) == false) { - render_orders.emplace(pipeline, renderable->material->GetShader()->GetRenderOrder()); + render_orders.emplace(pipeline, renderable->material->getShader()->GetRenderOrder()); } } diff --git a/src/systems/transform.cpp b/src/system_transform.cpp similarity index 100% rename from src/systems/transform.cpp rename to src/system_transform.cpp diff --git a/src/systems/ui_render_system.cpp b/src/systems/ui_render_system.cpp deleted file mode 100644 index c94149a..0000000 --- a/src/systems/ui_render_system.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "systems/ui_render_system.h" - -#include "components/transform.h" -#include "components/ui_renderable.h" - -namespace engine { - - UIRenderSystem::UIRenderSystem(Scene* scene) - : System(scene, { typeid(TransformComponent).hash_code(), - typeid(UIRenderableComponent).hash_code() }) {} - - UIRenderSystem::~UIRenderSystem() {} - - void UIRenderSystem::OnComponentInsert(Entity entity) { - (void)entity; - } - - void UIRenderSystem::OnUpdate(float ts) { - (void)ts; - } - -} // namespace engine diff --git a/src/vulkan/gpu_allocator.cpp b/src/vulkan_allocator.cpp similarity index 100% rename from src/vulkan/gpu_allocator.cpp rename to src/vulkan_allocator.cpp diff --git a/src/vulkan/device.cpp b/src/vulkan_device.cpp similarity index 100% rename from src/vulkan/device.cpp rename to src/vulkan_device.cpp diff --git a/src/vulkan/instance.cpp b/src/vulkan_instance.cpp similarity index 100% rename from src/vulkan/instance.cpp rename to src/vulkan_instance.cpp diff --git a/src/vulkan/swapchain.cpp b/src/vulkan_swapchain.cpp similarity index 100% rename from src/vulkan/swapchain.cpp rename to src/vulkan_swapchain.cpp diff --git a/test/src/game.cpp b/test/src/game.cpp index a35b1d2..3c0836e 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -139,8 +139,8 @@ void PlayGame(GameSettings settings) cube_ren->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 1.0f, 1.0f, 1.0f); cube_ren->visible = true; auto cubeCustom = main_scene->AddComponent(cube); - cubeCustom->onInit = [] {}; - cubeCustom->onUpdate = [&main_scene, cube](float dt) { + cubeCustom->on_init = [] {}; + cubeCustom->on_update = [&main_scene, cube](float dt) { static float yaw = 0.0f; yaw += dt; main_scene->GetRotation(cube) = glm::angleAxis(yaw, glm::vec3{0.0f, 0.0f, 1.0f});