aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/singletons/AssetManager.cpp
blob: 581c7fa4ebb3b306af541064b96908b3046ca122 (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
#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;

using OpenVic::Utilities::godot_to_std_string;
using OpenVic::Utilities::std_to_godot_string;

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

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 path) {
   GameSingleton* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, nullptr);
   const String lookedup_path =
      std_to_godot_string(game_singleton->get_dataloader().lookup_image_file(godot_to_std_string(path)).string());
   if (lookedup_path.is_empty()) {
      UtilityFunctions::push_error("Failed to look up image: ", path);
      return nullptr;
   }
   const Ref<Image> image = Utilities::load_godot_image(lookedup_path);
   if (image.is_null() || image->is_empty()) {
      UtilityFunctions::push_error("Failed to load image: ", lookedup_path, " (looked up from ", path, ")");
      return nullptr;
   } else {
      return image;
   }
}

AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName path) {
   const image_asset_map_t::iterator it = image_assets.find(path);
   if (it != image_assets.end()) {
      return it;
   }
   const Ref<Image> image = _load_image(path);
   if (image.is_valid()) {
      return image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first;
   } else {
      return image_assets.end();
   }
}

Ref<Image> AssetManager::get_image(StringName path, bool cache) {
   if (cache) {
      const image_asset_map_t::const_iterator it = _get_image_asset(path);
      if (it != image_assets.end()) {
         return it->second.image;
      } else {
         return nullptr;
      }
   } else {
      return _load_image(path);
   }
}

Ref<ImageTexture> AssetManager::get_texture(StringName path) {
   const image_asset_map_t::iterator it = _get_image_asset(path);
   if (it != image_assets.end()) {
      if (it->second.texture.is_null()) {
         it->second.texture = ImageTexture::create_from_image(it->second.image);
         if (it->second.texture.is_null()) {
            UtilityFunctions::push_error("Failed to turn image into texture: ", path);
         }
      }
      return it->second.texture;
   } else {
      return nullptr;
   }
}

Ref<Font> AssetManager::get_font(StringName name) {
   const font_map_t::const_iterator it = fonts.find(name);
   if (it != fonts.end()) {
      return it->second;
   }

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

   const String image_path = font_dir + name + image_ext;
   const Ref<Image> image = get_image(image_path);
   if (image.is_null()) {
      UtilityFunctions::push_error("Failed to load font image: ", image_path, " for the font named ", name);
      return nullptr;
   }
   GameSingleton* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, nullptr);
   const String lookedup_font_path =
      std_to_godot_string(game_singleton->get_dataloader().lookup_file(godot_to_std_string(font_dir + name + font_ext)).string());
   const Ref<Font> font = Utilities::load_godot_font(lookedup_font_path, image);
   if (font.is_null()) {
      UtilityFunctions::push_error("Failed to load font file ", lookedup_font_path, " for the font named ", name);
      return nullptr;
   }
   fonts.emplace(std::move(name), font);
   return font;
}