diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-04 20:45:18 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-04 20:45:18 -0500 |
| commit | 871943d6a90bdb92b3cc495d4d927199611f8c6b (patch) | |
| tree | 9be125438747f7370cfa56e3f3e42f8c68982852 /src/font.cpp | |
| parent | 138e0a8f83e82c6109bfc387ac7417d4f41711b4 (diff) | |
| download | tanetane-871943d6a90bdb92b3cc495d4d927199611f8c6b.tar.gz tanetane-871943d6a90bdb92b3cc495d4d927199611f8c6b.tar.bz2 tanetane-871943d6a90bdb92b3cc495d4d927199611f8c6b.zip | |
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.
Diffstat (limited to 'src/font.cpp')
| -rw-r--r-- | src/font.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
| 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 @@ | |||
| 1 | #include "font.h" | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include "renderer.h" | ||
| 5 | |||
| 6 | inline int charToIndex(char ch) { | ||
| 7 | return static_cast<int>(ch) - static_cast<int>('!'); | ||
| 8 | } | ||
| 9 | |||
| 10 | Font::Font(std::string_view filename, Renderer& renderer) { | ||
| 11 | std::ifstream fontfile(filename.data()); | ||
| 12 | if (!fontfile.is_open()) { | ||
| 13 | throw std::invalid_argument("Can't find font file: " + std::string(filename)); | ||
| 14 | } | ||
| 15 | |||
| 16 | std::string imagefilename; | ||
| 17 | fontfile >> imagefilename; | ||
| 18 | textureId_ = renderer.loadImageFromFile(imagefilename); | ||
| 19 | |||
| 20 | std::string line; | ||
| 21 | char ch; | ||
| 22 | |||
| 23 | int numChars; | ||
| 24 | fontfile >> numChars; | ||
| 25 | std::getline(fontfile, line); // characters | ||
| 26 | |||
| 27 | fontfile >> columns_; | ||
| 28 | std::getline(fontfile, line); // characters per row | ||
| 29 | |||
| 30 | fontfile >> tileSize_.x(); | ||
| 31 | fontfile >> ch; // x | ||
| 32 | fontfile >> tileSize_.y(); | ||
| 33 | std::getline(fontfile, line); // tile size | ||
| 34 | |||
| 35 | std::getline(fontfile, line); // ignore the baseline location | ||
| 36 | std::getline(fontfile, line); // blank | ||
| 37 | |||
| 38 | for (int i=0; i<numChars; i++) { | ||
| 39 | fontfile >> ch; // the character | ||
| 40 | |||
| 41 | int width; | ||
| 42 | fontfile >> width; | ||
| 43 | |||
| 44 | widths_.push_back(width); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | vec2i Font::getCharacterLocation(char ch) const { | ||
| 49 | int index = charToIndex(ch); | ||
| 50 | return (vec2i { index % columns_, index / columns_ }) * tileSize_; | ||
| 51 | } | ||
| 52 | |||
| 53 | vec2i Font::getCharacterSize(char ch) const { | ||
| 54 | int index = charToIndex(ch); | ||
| 55 | return { widths_.at(index), tileSize_.h() }; | ||
| 56 | } | ||
| 57 | |||
| 58 | int Font::getCharacterWidth(char ch) const { | ||
| 59 | if (ch == ' ') { | ||
| 60 | return 3; | ||
| 61 | } | ||
| 62 | return widths_.at(charToIndex(ch)); | ||
| 63 | } | ||
