summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/asset_manager.cc20
-rw-r--r--src/util/asset_manager.h24
-rw-r--r--src/util/asset_manager_dcl.h16
-rw-r--r--src/util/asset_manager_utils.h32
-rw-r--r--src/util/mini_math.h1
5 files changed, 72 insertions, 21 deletions
diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc
index d4dd5c7..650f220 100644
--- a/src/util/asset_manager.cc
+++ b/src/util/asset_manager.cc
@@ -2,6 +2,7 @@
// It implements the generic asset retrieval logic with runtime caching.
#include "util/asset_manager.h"
+#include "util/asset_manager_utils.h"
#if defined(USE_TEST_ASSETS)
#include "test_assets.h"
#else
@@ -153,6 +154,25 @@ TextureAsset GetTextureAsset(AssetId asset_id) {
return {(int)header[0], (int)header[1], data + 8};
}
+MeshAsset GetMeshAsset(AssetId asset_id) {
+ size_t size = 0;
+ const uint8_t* data = GetAsset(asset_id, &size);
+ if (!data || size < 8) {
+ return {0, nullptr, 0, nullptr};
+ }
+
+ const uint8_t* ptr = data;
+ uint32_t num_vertices = *reinterpret_cast<const uint32_t*>(ptr);
+ ptr += sizeof(uint32_t);
+ const MeshVertex* vertices = reinterpret_cast<const MeshVertex*>(ptr);
+ ptr += num_vertices * sizeof(MeshVertex);
+ uint32_t num_indices = *reinterpret_cast<const uint32_t*>(ptr);
+ ptr += sizeof(uint32_t);
+ const uint32_t* indices = reinterpret_cast<const uint32_t*>(ptr);
+
+ return {num_vertices, vertices, num_indices, indices};
+}
+
void DropAsset(AssetId asset_id, const uint8_t* asset) {
uint16_t index = (uint16_t)asset_id;
if (index >= (uint16_t)AssetId::ASSET_LAST_ID) {
diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h
index a78447d..ed7f1aa 100644
--- a/src/util/asset_manager.h
+++ b/src/util/asset_manager.h
@@ -1,17 +1,9 @@
// This file is part of the 64k demo project.
-// It defines the core structures and interface for asset management.
-// Used for efficient retrieval of embedded binary resources.
+// Core asset management interface for basic asset retrieval.
+// For typed helpers (TextureAsset, MeshAsset), include asset_manager_utils.h
#pragma once
-#include <cstddef>
-#include <cstdint>
-
-enum class AssetId : uint16_t; // Forward declaration
-
-// Type for procedural generation functions: (buffer, width, height, params,
-// num_params)
-// Returns true on success, false on failure.
-typedef bool (*ProcGenFunc)(uint8_t*, int, int, const float*, int);
+#include "asset_manager_dcl.h"
struct AssetRecord {
const uint8_t* data; // Pointer to asset data (static or dynamic)
@@ -32,13 +24,3 @@ struct AssetRecord {
// - 'out_size' returns the original asset size (excluding the null terminator).
const uint8_t* GetAsset(AssetId asset_id, size_t* out_size = nullptr);
void DropAsset(AssetId asset_id, const uint8_t* asset);
-
-struct TextureAsset {
- int width;
- int height;
- const uint8_t* pixels;
-};
-
-// Helper to retrieve and parse a simple texture asset (from packer's
-// [w][h][pixels] format)
-TextureAsset GetTextureAsset(AssetId asset_id);
diff --git a/src/util/asset_manager_dcl.h b/src/util/asset_manager_dcl.h
new file mode 100644
index 0000000..bf618f0
--- /dev/null
+++ b/src/util/asset_manager_dcl.h
@@ -0,0 +1,16 @@
+// This file is part of the 64k demo project.
+// Forward declarations for asset management system.
+// Use this header when you only need AssetId type.
+
+#pragma once
+#include <cstddef>
+#include <cstdint>
+
+enum class AssetId : uint16_t; // Forward declaration
+
+// Type for procedural generation functions: (buffer, width, height, params,
+// num_params)
+// Returns true on success, false on failure.
+typedef bool (*ProcGenFunc)(uint8_t*, int, int, const float*, int);
+
+struct AssetRecord; // Forward declaration (opaque)
diff --git a/src/util/asset_manager_utils.h b/src/util/asset_manager_utils.h
new file mode 100644
index 0000000..b738692
--- /dev/null
+++ b/src/util/asset_manager_utils.h
@@ -0,0 +1,32 @@
+// This file is part of the 64k demo project.
+// Typed asset helpers for specialized asset retrieval.
+// Only include this if you need TextureAsset or MeshAsset structs.
+
+#pragma once
+#include "asset_manager.h"
+
+struct TextureAsset {
+ int width;
+ int height;
+ const uint8_t* pixels;
+};
+
+struct MeshVertex {
+ float p[3];
+ float n[3];
+ float u[2];
+};
+
+struct MeshAsset {
+ uint32_t num_vertices;
+ const MeshVertex* vertices;
+ uint32_t num_indices;
+ const uint32_t* indices;
+};
+
+// Helper to retrieve and parse a simple texture asset (from packer's
+// [w][h][pixels] format)
+TextureAsset GetTextureAsset(AssetId asset_id);
+
+// Helper to retrieve and parse a mesh asset (from packer's binary format)
+MeshAsset GetMeshAsset(AssetId asset_id);
diff --git a/src/util/mini_math.h b/src/util/mini_math.h
index 2307087..f847cc1 100644
--- a/src/util/mini_math.h
+++ b/src/util/mini_math.h
@@ -350,6 +350,7 @@ struct quat {
}
static quat from_axis(vec3 a, float ang) {
+ a = a.normalize(); // Normalize axis to prevent scaling artifacts
float s = std::sin(ang * 0.5f);
return {a.x * s, a.y * s, a.z * s, std::cos(ang * 0.5f)};
}