aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/utility/Utilities.hpp
blob: 6eeb2859221274dc44e8a10ebaab0463ec198f4e (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
#pragma once

#include <godot_cpp/classes/font_file.hpp>
#include <godot_cpp/classes/image.hpp>

#include <openvic-simulation/types/Colour.hpp>
#include <openvic-simulation/types/Date.hpp>
#include <openvic-simulation/types/Vector.hpp>

#define ERR(x) ((x) ? OK : FAILED)

namespace OpenVic::Utilities {

   inline std::string godot_to_std_string(godot::String const& str) {
      return str.ascii().get_data();
   }

   inline godot::String std_to_godot_string(std::string const& str) {
      return str.c_str();
   }

   inline godot::String std_view_to_godot_string(std::string_view str) {
      return std_to_godot_string(static_cast<std::string>(str));
   }

   inline godot::StringName std_to_godot_string_name(std::string const& str) {
      return str.c_str();
   }

   inline godot::StringName std_view_to_godot_string_name(std::string_view str) {
      return std_to_godot_string_name(static_cast<std::string>(str));
   }

   godot::String int_to_formatted_string(int64_t val);

   godot::String float_to_formatted_string(float val);

   godot::String date_to_formatted_string(Date date);

   inline godot::Color to_godot_color(colour_t colour) {
      return {
         colour_byte_to_float((colour >> 16) & 0xFF),
         colour_byte_to_float((colour >> 8) & 0xFF),
         colour_byte_to_float(colour & 0xFF)
      };
   }

   inline godot::Vector2i to_godot_ivec2(ivec2_t vec) {
      return { vec.x, vec.y };
   }

   inline godot::Vector2 to_godot_fvec2(fvec2_t vec) {
      return { vec.x, vec.y };
   }

   /* Loads a Resource from a file in the Godot project directory given by a path beginning with "res://". */
   godot::Ref<godot::Resource> load_resource(godot::String const& path, godot::String const& type_hint = {});

   /* Load an Image from anywhere on the machine, using Godot's image-loading function or, in the case of
    * ".dds" image files which Godot is unable to load at runtime, GLI's DDS loading function. */
   godot::Ref<godot::Image> load_godot_image(godot::String const& path);

   /* Load a Font from anywhere on the machine, combining the ".fnt" file loaded from the given path with the
    * already-loaded image file containing the actual characters. */
   godot::Ref<godot::FontFile> load_godot_font(godot::String const& fnt_path, godot::Ref<godot::Image> const& image);
}