Skip to content

Commit

Permalink
Use ERR_FAIL macros where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Dec 28, 2023
1 parent c21e9b7 commit ad94f23
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Error GFXMaskedFlagTexture::_generate_combined_image() {
) {
const Color mask_image_colour = mask_image->get_pixelv(mask_image_point);
// Rescale from mask_image to flag_image coordinates.
const Vector2i flag_image_point = mask_image_point * flag_image->get_size() / mask_image->get_size();
const Vector2i flag_image_point = (2 * mask_image_point + Vector2i { 1, 1 }) * flag_image->get_size() / mask_image->get_size() / 2;
Color flag_image_colour = flag_image->get_pixelv(flag_image_point);
flag_image_colour.a = mask_image_colour.a;
combined_image->set_pixelv(combined_image_point, flag_image_colour.blend(overlay_image_colour));
Expand Down Expand Up @@ -76,11 +76,8 @@ Ref<GFXMaskedFlagTexture> GFXMaskedFlagTexture::make_gfx_masked_flag_texture(GFX
Ref<GFXMaskedFlagTexture> masked_flag_texture;
masked_flag_texture.instantiate();
ERR_FAIL_NULL_V(masked_flag_texture, nullptr);
if (masked_flag_texture->set_gfx_masked_flag(gfx_masked_flag) == OK) {
return masked_flag_texture;
} else {
return nullptr;
}
ERR_FAIL_COND_V(masked_flag_texture->set_gfx_masked_flag(gfx_masked_flag) != OK, nullptr);
return masked_flag_texture;
}

void GFXMaskedFlagTexture::clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,8 @@ Ref<GFXPieChartTexture> GFXPieChartTexture::make_gfx_pie_chart_texture(GFX::PieC
Ref<GFXPieChartTexture> pie_chart_texture;
pie_chart_texture.instantiate();
ERR_FAIL_NULL_V(pie_chart_texture, nullptr);
if (pie_chart_texture->set_gfx_pie_chart(gfx_pie_chart) == OK) {
return pie_chart_texture;
} else {
return nullptr;
}
ERR_FAIL_COND_V(pie_chart_texture->set_gfx_pie_chart(gfx_pie_chart) != OK, nullptr);
return pie_chart_texture;
}

void GFXPieChartTexture::clear() {
Expand Down
1 change: 1 addition & 0 deletions extension/src/openvic-extension/classes/GUINode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void GUINode::_bind_methods() {
}

GUINode::GUINode() {
//set_anchors_preset(PRESET_FULL_RECT);
set_mouse_filter(MOUSE_FILTER_IGNORE);
}

Expand Down
72 changes: 27 additions & 45 deletions extension/src/openvic-extension/singletons/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,66 +32,50 @@ AssetManager::~AssetManager() {
_singleton = nullptr;
}

Ref<Image> AssetManager::_load_image(StringName path) {
Ref<Image> AssetManager::_load_image(StringName const& 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;
}
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);
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;
}
ERR_FAIL_COND_V_MSG(
image.is_null() || image->is_empty(), nullptr, vformat("Failed to load image: %s (looked up: %s)", path, lookedup_path)
);
return image;
}

AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName path) {
AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName const& 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();
}
ERR_FAIL_NULL_V(image, image_assets.end());
return image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first;
}

Ref<Image> AssetManager::get_image(StringName path, bool cache) {
Ref<Image> AssetManager::get_image(StringName const& 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;
}
ERR_FAIL_COND_V(it == image_assets.end(), nullptr);
return it->second.image;
} else {
return _load_image(path);
}
}

Ref<ImageTexture> AssetManager::get_texture(StringName path) {
Ref<ImageTexture> AssetManager::get_texture(StringName const& 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;
ERR_FAIL_COND_V(it == image_assets.end(), nullptr);
if (it->second.texture.is_null()) {
it->second.texture = ImageTexture::create_from_image(it->second.image);
ERR_FAIL_NULL_V_MSG(it->second.texture, nullptr, vformat("Failed to turn image into texture: %s", path));
}
return it->second.texture;
}

Ref<Font> AssetManager::get_font(StringName name) {
Ref<Font> AssetManager::get_font(StringName const& name) {
const font_map_t::const_iterator it = fonts.find(name);
if (it != fonts.end()) {
return it->second;
Expand All @@ -101,21 +85,19 @@ Ref<Font> AssetManager::get_font(StringName name) {
static const String font_ext = ".fnt";
static const String image_ext = ".tga";

const String image_path = font_dir + name + image_ext;
const StringName 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;
}
ERR_FAIL_NULL_V_MSG(image, 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 =
std_to_godot_string(game_singleton->get_dataloader().lookup_file(godot_to_std_string(font_dir + name + font_ext)).string());
std_to_godot_string(game_singleton->get_dataloader().lookup_file(godot_to_std_string(font_path)).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;
}
ERR_FAIL_NULL_V_MSG(
font, nullptr,
vformat("Failed to load font file %s (looked up: %s) for the font named %s", font_path, lookedup_font_path, name)
);
fonts.emplace(std::move(name), font);
return font;
}
10 changes: 5 additions & 5 deletions extension/src/openvic-extension/singletons/AssetManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace OpenVic {
image_asset_map_t image_assets;
font_map_t fonts;

static godot::Ref<godot::Image> _load_image(godot::StringName path);
image_asset_map_t::iterator _get_image_asset(godot::StringName path);
static godot::Ref<godot::Image> _load_image(godot::StringName const& path);
image_asset_map_t::iterator _get_image_asset(godot::StringName const& path);

protected:
static void _bind_methods();
Expand All @@ -37,15 +37,15 @@ namespace OpenVic {

/* Search for and load an image at the specified path relative to the game defines, first checking the AssetManager's
* image cache (if cache is true) in case it has already been loaded, and returning nullptr if image loading fails. */
godot::Ref<godot::Image> get_image(godot::StringName path, bool cache = true);
godot::Ref<godot::Image> get_image(godot::StringName const& path, bool cache = true);

/* Create a texture from an image found at the specified path relative to the game defines, fist checking
* AssetManager's texture cache in case it has already been loaded, and returning nullptr if image loading
* or texture creation fails. */
godot::Ref<godot::ImageTexture> get_texture(godot::StringName path);
godot::Ref<godot::ImageTexture> get_texture(godot::StringName const& path);

/* Search for and load a font with the specified name from the game defines' font directory, first checking the
* AssetManager's font cache in case it has already been loaded, and returning nullptr if font loading fails. */
godot::Ref<godot::Font> get_font(godot::StringName name);
godot::Ref<godot::Font> get_font(godot::StringName const& name);
};
}
Loading

0 comments on commit ad94f23

Please sign in to comment.