Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashes on missing building or tech sprites #2053

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions client/tileset/tilespec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}
}

/**
Expand All @@ -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;
}
Expand Down
Loading