Try reformatting asset code with clang-format
This commit is contained in:
parent
e2d29f34f2
commit
069318dc0b
5 changed files with 173 additions and 192 deletions
|
@ -1,28 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <array>
|
||||
|
||||
#include "file.hpp"
|
||||
#include "assetptr.hpp"
|
||||
#include "asset_types.hpp"
|
||||
#include "assetptr.hpp"
|
||||
#include "file.hpp"
|
||||
#include "string_utils.hpp"
|
||||
|
||||
template<typename T>
|
||||
std::unique_ptr<T> load_asset(const prism::path& p);
|
||||
template<typename T> std::unique_ptr<T> load_asset(const prism::path& p);
|
||||
|
||||
template<typename T>
|
||||
bool can_load_asset(const prism::path& p);
|
||||
template<typename T> bool can_load_asset(const prism::path& p);
|
||||
|
||||
template<class AssetType>
|
||||
using AssetStore = std::unordered_map<prism::path, std::unique_ptr<AssetType>>;
|
||||
template<class AssetType> using AssetStore = std::unordered_map<prism::path, std::unique_ptr<AssetType>>;
|
||||
|
||||
template<class... Assets>
|
||||
class AssetPool : public AssetStore<Assets>... {
|
||||
template<class... Assets> class AssetPool : public AssetStore<Assets>... {
|
||||
public:
|
||||
template<typename T>
|
||||
AssetPtr<T> add() {
|
||||
template<typename T> AssetPtr<T> add() {
|
||||
const auto p = prism::path();
|
||||
auto reference_block = get_reference_block(p);
|
||||
|
||||
|
@ -31,13 +26,9 @@ public:
|
|||
return AssetPtr<T>(AssetStore<T>::at(p).get(), reference_block);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
AssetPtr<T> get(const prism::path path) {
|
||||
return fetch<T>(path, get_reference_block(path));
|
||||
}
|
||||
template<typename T> AssetPtr<T> get(const prism::path path) { return fetch<T>(path, get_reference_block(path)); }
|
||||
|
||||
template<typename T>
|
||||
std::vector<T*> get_all() {
|
||||
template<typename T> std::vector<T*> get_all() {
|
||||
std::vector<T*> assets;
|
||||
for (auto iter = AssetStore<T>::begin(); iter != AssetStore<T>::end(); iter++) {
|
||||
auto& [p, asset] = *iter;
|
||||
|
@ -48,8 +39,7 @@ public:
|
|||
return assets;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
AssetPtr<T> fetch(const prism::path path, ReferenceBlock* reference_block) {
|
||||
template<typename T> AssetPtr<T> fetch(const prism::path path, ReferenceBlock* reference_block) {
|
||||
if (!AssetStore<T>::count(path))
|
||||
AssetStore<T>::try_emplace(path, load_asset<T>(path));
|
||||
|
||||
|
@ -89,8 +79,7 @@ private:
|
|||
return reference_blocks[path].get();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) {
|
||||
template<typename T> void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) {
|
||||
if (can_load_asset<T>(path)) {
|
||||
if (!AssetStore<T>::count(path))
|
||||
AssetStore<T>::try_emplace(path, load_asset<T>(path));
|
||||
|
@ -100,8 +89,7 @@ private:
|
|||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void delete_asset(const prism::path path) {
|
||||
template<typename T> void delete_asset(const prism::path path) {
|
||||
auto iter = AssetStore<T>::find(path);
|
||||
if (iter != AssetStore<T>::end()) {
|
||||
auto& [_, asset] = *iter;
|
||||
|
@ -123,8 +111,7 @@ std::unique_ptr<Texture> load_texture(const prism::path& path);
|
|||
|
||||
void save_material(Material* material, const prism::path& path);
|
||||
|
||||
template<typename T>
|
||||
std::unique_ptr<T> load_asset(const prism::path& path) {
|
||||
template<typename T> std::unique_ptr<T> load_asset(const prism::path& path) {
|
||||
if constexpr (std::is_same_v<T, Mesh>) {
|
||||
return load_mesh(path);
|
||||
} else if constexpr (std::is_same_v<T, Material>) {
|
||||
|
@ -134,8 +121,7 @@ std::unique_ptr<T> load_asset(const prism::path& path) {
|
|||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool can_load_asset(const prism::path& path) {
|
||||
template<typename T> bool can_load_asset(const prism::path& path) {
|
||||
if constexpr (std::is_same_v<T, Mesh>) {
|
||||
return path.extension() == ".model";
|
||||
} else if constexpr (std::is_same_v<T, Material>) {
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
#include <map>
|
||||
|
||||
#include "aabb.hpp"
|
||||
#include "assetptr.hpp"
|
||||
#include "math.hpp"
|
||||
#include "aabb.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
class GFXBuffer;
|
||||
|
@ -75,8 +75,8 @@ struct Bone {
|
|||
|
||||
class Mesh : public Asset {
|
||||
public:
|
||||
// meshes are rendered in parts if we cannot batch it in one call, i.e. a mesh
|
||||
// with multiple materials with different textures, etc
|
||||
// meshes are rendered in parts if we cannot batch it in one call, i.e. a
|
||||
// mesh with multiple materials with different textures, etc
|
||||
struct Part {
|
||||
std::string name;
|
||||
prism::aabb bounding_box;
|
||||
|
@ -84,7 +84,8 @@ public:
|
|||
GFXBuffer* bone_batrix_buffer = nullptr;
|
||||
std::vector<Matrix4x4> offset_matrices;
|
||||
|
||||
uint32_t index_offset = 0, index_count = 0;;
|
||||
uint32_t index_offset = 0, index_count = 0;
|
||||
;
|
||||
int32_t vertex_offset = 0;
|
||||
int32_t material_override = -1;
|
||||
std::string material_hint;
|
||||
|
|
|
@ -12,13 +12,10 @@ public:
|
|||
std::string path;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct AssetPtr {
|
||||
template<class T> struct AssetPtr {
|
||||
AssetPtr() = default;
|
||||
|
||||
AssetPtr(T* ptr, ReferenceBlock* block) : handle(ptr), block(block) {
|
||||
block->references++;
|
||||
}
|
||||
AssetPtr(T* ptr, ReferenceBlock* block) : handle(ptr), block(block) { block->references++; }
|
||||
|
||||
AssetPtr(const AssetPtr& rhs) {
|
||||
handle = rhs.handle;
|
||||
|
@ -56,23 +53,13 @@ struct AssetPtr {
|
|||
T* handle = nullptr;
|
||||
ReferenceBlock* block = nullptr;
|
||||
|
||||
explicit operator bool() const{
|
||||
return handle != nullptr;
|
||||
}
|
||||
explicit operator bool() const { return handle != nullptr; }
|
||||
|
||||
T* operator->() {
|
||||
return handle;
|
||||
}
|
||||
T* operator->() { return handle; }
|
||||
|
||||
T* operator->() const {
|
||||
return handle;
|
||||
}
|
||||
T* operator->() const { return handle; }
|
||||
|
||||
T* operator*() {
|
||||
return handle;
|
||||
}
|
||||
T* operator*() { return handle; }
|
||||
|
||||
T* operator*() const {
|
||||
return handle;
|
||||
}
|
||||
T* operator*() const { return handle; }
|
||||
};
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
#include "asset.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <stb_image.h>
|
||||
|
||||
#include "log.hpp"
|
||||
#include "assertions.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "gfx.hpp"
|
||||
#include "json_conversions.hpp"
|
||||
#include "gfx_commandbuffer.hpp"
|
||||
#include "assertions.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "input.hpp"
|
||||
#include "physics.hpp"
|
||||
#include "imgui_backend.hpp"
|
||||
#include "input.hpp"
|
||||
#include "json_conversions.hpp"
|
||||
#include "log.hpp"
|
||||
#include "physics.hpp"
|
||||
#include "renderer.hpp"
|
||||
|
||||
std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||
Expects(!path.empty());
|
||||
|
@ -50,7 +50,8 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
Expects(numVertices > 0);
|
||||
|
||||
const auto read_buffer = [&f = file.value(), numVertices](unsigned int size) -> GFXBuffer* {
|
||||
auto buffer = engine->get_gfx()->create_buffer(nullptr, size * static_cast<unsigned int>(numVertices), false, GFXBufferUsage::Vertex);
|
||||
auto buffer = engine->get_gfx()->create_buffer(
|
||||
nullptr, size * static_cast<unsigned int>(numVertices), false, GFXBufferUsage::Vertex);
|
||||
auto buffer_ptr = reinterpret_cast<unsigned char*>(engine->get_gfx()->get_buffer_contents(buffer));
|
||||
if (buffer_ptr != nullptr) {
|
||||
f.read(buffer_ptr, size * static_cast<unsigned int>(numVertices));
|
||||
|
@ -78,7 +79,8 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
|
||||
Expects(numIndices > 0);
|
||||
|
||||
mesh->index_buffer = engine->get_gfx()->create_buffer(nullptr, sizeof(uint32_t) * numIndices, false, GFXBufferUsage::Index);
|
||||
mesh->index_buffer =
|
||||
engine->get_gfx()->create_buffer(nullptr, sizeof(uint32_t) * numIndices, false, GFXBufferUsage::Index);
|
||||
auto index_ptr = reinterpret_cast<uint32_t*>(engine->get_gfx()->get_buffer_contents(mesh->index_buffer));
|
||||
if (index_ptr != nullptr) {
|
||||
file->read(index_ptr, sizeof(uint32_t) * numIndices);
|
||||
|
@ -172,7 +174,8 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
int numBones = 0;
|
||||
file->read(&numBones);
|
||||
|
||||
p.bone_batrix_buffer = engine->get_gfx()->create_buffer(nullptr, sizeof(Matrix4x4) * 128, true, GFXBufferUsage::Storage);
|
||||
p.bone_batrix_buffer =
|
||||
engine->get_gfx()->create_buffer(nullptr, sizeof(Matrix4x4) * 128, true, GFXBufferUsage::Storage);
|
||||
|
||||
if (numBones > 0) {
|
||||
p.offset_matrices.resize(numBones);
|
||||
|
@ -210,7 +213,8 @@ std::unique_ptr<Texture> load_texture(const prism::path& path) {
|
|||
file->read_all();
|
||||
|
||||
int width, height, channels;
|
||||
unsigned char* data = stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4);
|
||||
unsigned char* data =
|
||||
stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4);
|
||||
if (!data) {
|
||||
prism::log("Failed to load texture from {}!", path.string());
|
||||
return nullptr;
|
||||
|
@ -229,7 +233,10 @@ std::unique_ptr<Texture> load_texture(const prism::path& path) {
|
|||
createInfo.width = width;
|
||||
createInfo.height = height;
|
||||
createInfo.format = GFXPixelFormat::R8G8B8A8_UNORM;
|
||||
createInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst | GFXTextureUsage::TransferSrc; // src and dst are needed for copy tex data -> image and mipmap gen (from image data) respectively
|
||||
createInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst |
|
||||
GFXTextureUsage::TransferSrc; // src and dst are needed for copy tex
|
||||
// data -> image and mipmap gen (from
|
||||
// image data) respectively
|
||||
|
||||
if (should_generate_mipmaps)
|
||||
createInfo.mip_count = std::floor(std::log2(std::max(width, height))) + 1;
|
||||
|
|
Reference in a new issue