From 6b2149fe57bfde2118495319c127974dba0acb92 Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Sun, 12 Nov 2023 23:03:59 +0100 Subject: [PATCH] Fix crashes on building or tech sprites Create a dummy "error" pixmap when the sprites are missing. --- client/tileset/tilespec.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/client/tileset/tilespec.cpp b/client/tileset/tilespec.cpp index 2336b12e35..c9f8d5bbbe 100644 --- a/client/tileset/tilespec.cpp +++ b/client/tileset/tilespec.cpp @@ -1172,14 +1172,22 @@ void tilespec_reread_frozen_refresh(const QString &name) menus_init(); } +/** + * Makes a dummy "error" pixmap to prevent crashes. + */ +static QPixmap *make_error_pixmap() +{ + auto s = new QPixmap(20, 20); + s->fill(Qt::red); + return s; +} + /** Loads the given graphics file (found in the data path) into a newly allocated sprite. */ static QPixmap *load_gfx_file(const char *gfx_filename) { - QPixmap *s; - // Try out all supported file extensions to find one that works. auto supported = QImageReader::supportedImageFormats(); @@ -1197,17 +1205,14 @@ static QPixmap *load_gfx_file(const char *gfx_filename) if (!real_full_name.isEmpty()) { log_debug("trying to load gfx file \"%s\".", qUtf8Printable(real_full_name)); - s = load_gfxfile(qUtf8Printable(real_full_name)); - if (s) { + if (const auto s = load_gfxfile(qUtf8Printable(real_full_name)); s) { return s; } } } qCritical("Could not load gfx file \"%s\".", gfx_filename); - s = new QPixmap(20, 20); - s->fill(Qt::red); - return s; + return make_error_pixmap(); } /** @@ -3163,7 +3168,9 @@ void tileset_setup_impr_type(struct tileset *t, struct impr_type *pimprove) pimprove->graphic_alt, "improvement", improvement_rule_name(pimprove), false); - // should maybe do something if nullptr, eg generic default? + if (!t->sprites.building[improvement_index(pimprove)]) { + t->sprites.building[improvement_index(pimprove)] = make_error_pixmap(); + } } /** @@ -3177,7 +3184,9 @@ void tileset_setup_tech_type(struct tileset *t, struct advance *padvance) t, LOG_VERBOSE, padvance->graphic_str, padvance->graphic_alt, "technology", advance_rule_name(padvance), false); - // should maybe do something if nullptr, eg generic default? + if (!t->sprites.tech[advance_index(padvance)]) { + t->sprites.tech[advance_index(padvance)] = make_error_pixmap(); + } } else { t->sprites.tech[advance_index(padvance)] = nullptr; }