about summary refs log tree commit diff stats
path: root/data/maps/the_bearer/README
diff options
context:
space:
mode:
Diffstat (limited to 'data/maps/the_bearer/README')
0 files changed, 0 insertions, 0 deletions
#include "font.h"
#include <fstream>
#include <iostream>
#include "renderer.h"

inline int charToIndex(char ch) {
  return static_cast<int>(ch) - static_cast<int>('!');
}

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<numChars; i++) {
    fontfile >> 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));
}