From 871943d6a90bdb92b3cc495d4d927199611f8c6b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 4 Feb 2021 20:45:18 -0500 Subject: Added text boxes Text now reveals itself and scrolls! Yay! It even plays speaker beeps. TODO: the arror indicating an A press is needed. Bullets on lines that need bullets. The header that says who the speaker is, if relevant. --- src/font.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/font.cpp (limited to 'src/font.cpp') diff --git a/src/font.cpp b/src/font.cpp new file mode 100644 index 0000000..cfaad24 --- /dev/null +++ b/src/font.cpp @@ -0,0 +1,63 @@ +#include "font.h" +#include +#include +#include "renderer.h" + +inline int charToIndex(char ch) { + return static_cast(ch) - static_cast('!'); +} + +Font::Font(std::string_view filename, Renderer& renderer) { + std::ifstream fontfile(filename.data()); + if (!fontfile.is_open()) { + throw std::invalid_argument("Can't find font file: " + std::string(filename)); + } + + std::string imagefilename; + fontfile >> imagefilename; + textureId_ = renderer.loadImageFromFile(imagefilename); + + std::string line; + char ch; + + int numChars; + fontfile >> numChars; + std::getline(fontfile, line); // characters + + fontfile >> columns_; + std::getline(fontfile, line); // characters per row + + fontfile >> tileSize_.x(); + fontfile >> ch; // x + fontfile >> tileSize_.y(); + std::getline(fontfile, line); // tile size + + std::getline(fontfile, line); // ignore the baseline location + std::getline(fontfile, line); // blank + + for (int i=0; i> ch; // the character + + int width; + fontfile >> width; + + widths_.push_back(width); + } +} + +vec2i Font::getCharacterLocation(char ch) const { + int index = charToIndex(ch); + return (vec2i { index % columns_, index / columns_ }) * tileSize_; +} + +vec2i Font::getCharacterSize(char ch) const { + int index = charToIndex(ch); + return { widths_.at(index), tileSize_.h() }; +} + +int Font::getCharacterWidth(char ch) const { + if (ch == ' ') { + return 3; + } + return widths_.at(charToIndex(ch)); +} -- cgit 1.4.1