Motor de sprites/animacions/surfaces i jugador centrat (v0.2.0)

This commit is contained in:
JailDesigner committed 2026-04-18 21:02:55 +02:00
1 parent 7bb1edd08a
commit de5a670fdb
34 files changed
+16137 -81

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
#include "core/resources/resource_cache.hpp"
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include "core/resources/resource_helper.hpp"
#include "utils/utils.hpp"
namespace Resource {
auto Cache::get() -> Cache& {
static Cache instance_;
return instance_;
}
void Cache::loadSurface(const std::string& name, const std::string& file_path) {
auto surface = std::make_shared<Surface>(file_path);
surfaces_.emplace_back(SurfaceResource{.name = name, .surface = std::move(surface)});
}
void Cache::loadAnimation(const std::string& name, const std::string& file_path) {
auto bytes = Helper::loadFile(file_path);
if (bytes.empty()) {
throw std::runtime_error("Cache: cannot load animation " + file_path);
}
animations_.emplace_back(AnimationResource{.name = name, .yaml_data = std::move(bytes)});
printWithDots("Animation : ", getFileName(file_path), "[ LOADED ]");
}
auto Cache::getSurface(const std::string& name) -> std::shared_ptr<Surface> {
auto it = std::ranges::find_if(surfaces_, [&name](const auto& s) { return s.name == name; });
if (it == surfaces_.end()) {
std::cerr << "Cache: surface not found: " << name << '\n';
throw std::runtime_error("Surface not found: " + name);
}
return it->surface;
}
auto Cache::getAnimationData(const std::string& name) -> const AnimationResource& {
auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; });
if (it == animations_.end()) {
std::cerr << "Cache: animation not found: " << name << '\n';
throw std::runtime_error("Animation not found: " + name);
}
return *it;
}
} // namespace Resource
+40
View File
@@ -0,0 +1,40 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "core/resources/resource_types.hpp"
namespace Resource {
// Cache mínim per a la fase 2: només surfaces i animacions, càrrega
// explícita per nom + path. Per ara la cerca és lineal sobre vector.
// Meyers singleton: la destrucció estàtica només toca smart pointers, no
// depén d'SDL ni de Screen, així que és segura sense ordenació explícita.
class Cache {
public:
static auto get() -> Cache&;
Cache(const Cache&) = delete;
Cache(Cache&&) = delete;
auto operator=(const Cache&) -> Cache& = delete;
auto operator=(Cache&&) -> Cache& = delete;
// Carrega una surface des de fitxer i la registra amb el nom donat.
void loadSurface(const std::string& name, const std::string& file_path);
// Llig el YAML d'animació en cru i el registra amb el nom donat.
void loadAnimation(const std::string& name, const std::string& file_path);
auto getSurface(const std::string& name) -> std::shared_ptr<Surface>;
auto getAnimationData(const std::string& name) -> const AnimationResource&;
private:
Cache() = default;
~Cache() = default;
std::vector<SurfaceResource> surfaces_;
std::vector<AnimationResource> animations_;
};
} // namespace Resource
+24
View File
@@ -0,0 +1,24 @@
#include "core/resources/resource_helper.hpp"
#include <fstream>
#include <iostream>
namespace Resource::Helper {
auto loadFile(const std::string& filepath) -> std::vector<std::uint8_t> {
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "Resource::Helper: cannot open " << filepath << '\n';
return {};
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::uint8_t> data(static_cast<std::size_t>(size));
if (!file.read(reinterpret_cast<char*>(data.data()), size)) {
std::cerr << "Resource::Helper: failed to read " << filepath << '\n';
return {};
}
return data;
}
} // namespace Resource::Helper
+12
View File
@@ -0,0 +1,12 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
namespace Resource::Helper {
// Carga un fitxer del filesystem (a aquesta fase encara no hi ha pack).
auto loadFile(const std::string& filepath) -> std::vector<std::uint8_t>;
} // namespace Resource::Helper
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "core/rendering/surface.hpp"
struct SurfaceResource {
std::string name;
std::shared_ptr<Surface> surface;
};
struct AnimationResource {
std::string name;
std::vector<std::uint8_t> yaml_data;
};