aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/singletons/AssetManager.cpp
blob: eec1ada4087ab751700cf33a3ecf67af2d8d0306 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "AssetManager.hpp"

#include <godot_cpp/variant/utility_functions.hpp>

#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/Utilities.hpp"

using namespace godot;
using namespace OpenVic;

void AssetManager::_bind_methods() {
   OV_BIND_METHOD(AssetManager::get_image, { "path", "load_flags" }, DEFVAL(LOAD_FLAG_CACHE_IMAGE));
   OV_BIND_METHOD(AssetManager::get_texture, { "path", "load_flags" }, DEFVAL(LOAD_FLAG_CACHE_TEXTURE));
   OV_BIND_METHOD(AssetManager::get_font, { "name" });

   BIND_ENUM_CONSTANT(LOAD_FLAG_NONE);
   BIND_ENUM_CONSTANT(LOAD_FLAG_CACHE_IMAGE);
   BIND_ENUM_CONSTANT(LOAD_FLAG_CACHE_TEXTURE);
   BIND_ENUM_CONSTANT(LOAD_FLAG_FLIP_Y);
}

AssetManager* AssetManager::get_singleton() {
   return _singleton;
}

AssetManager::AssetManager() {
   ERR_FAIL_COND(_singleton != nullptr);
   _singleton = this;
}

AssetManager::~AssetManager() {
   ERR_FAIL_COND(_singleton != this);
   _singleton = nullptr;
}

Ref<Image> AssetManager::_load_image(StringName const& path, bool flip_y) {
   GameSingleton* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, nullptr);

   const String lookedup_path = Utilities::std_to_godot_string(
      game_singleton->get_dataloader().lookup_image_file(Utilities::godot_to_std_string(path)).string()
   );
   ERR_FAIL_COND_V_MSG(lookedup_path.is_empty(), nullptr, vformat("Failed to look up image: %s", path));

   const Ref<Image> image = Utilities::load_godot_image(lookedup_path);
   ERR_FAIL_COND_V_MSG(
      image.is_null() || image->is_empty(), nullptr,
      vformat("Failed to load image: %s (looked up: %s)", path, lookedup_path)
   );

   if (flip_y) {
      image->flip_y();
   }

   return image;
}

Ref<Image> AssetManager::get_image(StringName const& path, LoadFlags load_flags) {
   /* Check for an existing image entry indicating a previous load attempt, whether successful or not. */
   const image_asset_map_t::iterator it = image_assets.find(path);
   if (it != image_assets.end()) {
      std::optional<Ref<Image>> const& cached_image = it->second.image;

      if (cached_image.has_value()) {
         ERR_FAIL_NULL_V_MSG(*cached_image, nullptr, vformat("Failed to load image previously: %s", path));

         return *cached_image;
      }
   }

   /* No load attempt has been made yet, so we try now. */
   const Ref<Image> image = _load_image(path, load_flags & LOAD_FLAG_FLIP_Y);

   if (image.is_valid()) {
      if (load_flags & LOAD_FLAG_CACHE_IMAGE) {
         image_assets[path].image = image;
      }

      return image;
   } else {
      /* Mark both image and texture as failures, regardless of cache flags, in case of future load/creation attempts. */
      image_assets[path] = { nullptr, nullptr };

      ERR_FAIL_V_MSG(nullptr, vformat("Failed to load image: %s", path));
   }
}

Ref<ImageTexture> AssetManager::get_texture(StringName const& path, LoadFlags load_flags) {
   /* Check for an existing texture entry indicating a previous creation attempt, whether successful or not. */
   const image_asset_map_t::const_iterator it = image_assets.find(path);
   if (it != image_assets.end()) {
      std::optional<Ref<ImageTexture>> const& cached_texture = it->second.texture;

      if (cached_texture.has_value()) {
         ERR_FAIL_NULL_V_MSG(*cached_texture, nullptr, vformat("Failed to create texture previously: %s", path));

         return *cached_texture;
      }
   }

   /* No creation attempt has yet been made, so we try now starting by finding the corresponding image. */
   const Ref<Image> image = get_image(path, load_flags);
   ERR_FAIL_NULL_V_MSG(image, nullptr, vformat("Failed to load image for texture: %s", path));

   const Ref<ImageTexture> texture = ImageTexture::create_from_image(image);

   if (texture.is_valid()) {
      if (load_flags & LOAD_FLAG_CACHE_TEXTURE) {
         image_assets[path].texture = texture;
      }

      return texture;
   } else {
      /* Mark texture as a failure, regardless of cache flags, in case of future creation attempts. */
      image_assets[path].texture = nullptr;

      ERR_FAIL_V_MSG(nullptr, vformat("Failed to create texture: %s", path));
   }
}

Ref<Font> AssetManager::get_font(StringName const& name) {
   const font_map_t::const_iterator it = fonts.find(name);
   if (it != fonts.end()) {
      ERR_FAIL_NULL_V_MSG(it->second, nullptr, vformat("Failed to load font previously: %s", name));

      return it->second;
   }

   static const String font_dir = "gfx/fonts/";
   static const String font_ext = ".fnt";
   static const String image_ext = ".tga";

   const StringName image_path = font_dir + name + image_ext;
   const Ref<Image> image = get_image(image_path, LOAD_FLAG_NONE);
   if (image.is_null()) {
      fonts.emplace(name, nullptr);

      ERR_FAIL_V_MSG(nullptr, vformat("Failed to load font image %s for the font named %s", image_path, name));
   }

   GameSingleton* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, nullptr);

   const String font_path = font_dir + name + font_ext;
   const String lookedup_font_path = Utilities::std_to_godot_string(
      game_singleton->get_dataloader().lookup_file(Utilities::godot_to_std_string(font_path)).string()
   );
   if (lookedup_font_path.is_empty()) {
      fonts.emplace(name, nullptr);

      ERR_FAIL_V_MSG(nullptr, vformat("Failed to look up font: %s", font_path));
   }

   const Ref<Font> font = Utilities::load_godot_font(lookedup_font_path, image);
   if (font.is_null()) {
      fonts.emplace(name, nullptr);

      ERR_FAIL_V_MSG(
         nullptr,
         vformat("Failed to load font file %s (looked up: %s) for the font named %s", font_path, lookedup_font_path, name)
      );
   }

   fonts.emplace(name, font);
   return font;
}