Skip to content

Commit

Permalink
Handle missing core fonts more gracefully
Browse files Browse the repository at this point in the history
Ref #26
  • Loading branch information
soulweaver91 committed Sep 16, 2016
1 parent c0e2264 commit 738bbb5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/CarrotQt5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,19 @@ CarrotQt5::CarrotQt5(QWidget *parent) : QMainWindow(parent), initialized(false),
}

// Read the main font
smallFont = std::make_shared<BitmapFont>("Data/Assets/ui/font_small.png", 17, 18, 15, 32, 256, -2);
mainFont = std::make_shared<BitmapFont>("Data/Assets/ui/font_medium.png", 29, 31, 15, 32, 256, -1);
largeFont = std::make_shared<BitmapFont>("Data/Assets/ui/font_large.png", 57, 63, 15, 32, 256, -1);
try {
smallFont = std::make_shared<BitmapFont>("Data/Assets/ui/font_small.png", 17, 18, 15, 32, 256, -2);
mainFont = std::make_shared<BitmapFont>("Data/Assets/ui/font_medium.png", 29, 31, 15, 32, 256, -1);
largeFont = std::make_shared<BitmapFont>("Data/Assets/ui/font_large.png", 57, 63, 15, 32, 256, -1);
} catch (const std::exception& ex) {
QMessageBox::critical(nullptr, "Could not load core bitmap fonts", QString(
"One or more of the three core fonts could not be loaded! Verify that you have extracted the resources "
"from your Jazz Jackrabbit 2 installation with Project Carrot Anims Extractor.\n\n"
"%1"
).arg(ex.what()));
exit(EXIT_FAILURE);
return;
}

// Initialize the spawner
eventSpawner = std::make_unique<EventSpawner>();
Expand Down
9 changes: 6 additions & 3 deletions src/graphics/BitmapFont.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
#include "BitmapFont.h"

#include <QFile>
#include <exception>

BitmapFont::BitmapFont(const QString& filename, unsigned width, unsigned height,
unsigned cols, unsigned first, unsigned last, int defaultSpacing)
: charHeight(height), defaultSpacing(defaultSpacing) {
if (!(fontTexture.loadFromFile(filename.toUtf8().data()))) {
throw 0;
throw std::runtime_error("Font image '" + filename.toStdString() + "' could not be loaded!");
}

char widthFromFileTable[256];
std::fill_n(widthFromFileTable, 256, static_cast<char>(width % 256));

QFile widthFile(filename + ".config");
if (!(widthFile.open(QIODevice::ReadOnly))) {
// oops, something went wrong
// Could not open the character width file.
// As a result, each character is considered to be the same width as
// the horizontal offset of characters on the image file.
} else {
widthFile.read(widthFromFileTable,256);
widthFile.read(widthFromFileTable, 256);
}
widthFile.close();

Expand Down

0 comments on commit 738bbb5

Please sign in to comment.