3 #include "nta/SpriteFont.h"
4 #include "nta/Random.h"
9 for (
int i = 0; i < NUM_PRINTABLE_CHARS; i++) {
17 float minX = 999, maxX = -999, minY = 999, maxY = -999;
19 minX = std::min(r.topLeft.x, minX);
20 maxX = std::max(r.topLeft.x+r.dimensions.x, maxX);
21 minY = std::min(r.topLeft.y-r.dimensions.y, minY);
22 maxY = std::max(r.topLeft.y, maxY);
24 return glm::vec2(maxX-minX, maxY-minY);
29 glm::vec4 rectC(rect.topLeft.x+rect.dimensions.x/2, rect.topLeft.y-rect.dimensions.y/2, rect.dimensions.x/2.f, rect.dimensions.y/2.f);
30 glm::vec4 rC(r.topLeft.x+r.dimensions.x/2, r.topLeft.y-r.dimensions.y/2, r.dimensions.x/2.f, r.dimensions.y/2.f);
31 glm::vec4 displacement = rectC-rC;
32 if (fabs(displacement.x) < rectC[2]+rC[2] && fabs(displacement.y) < rectC[3]+rC[3]) {
39 void FontMap::addRect(
char c, crvec2 dimensions) {
41 if (!std::any_of(m_rects, m_rects+NUM_PRINTABLE_CHARS, [](
const CharRect& cr){
return cr.addedToMap;})) {
42 newRect = {glm::vec2(0), dimensions,
true};
46 existingRect = m_rects[Random::randInt(NUM_PRINTABLE_CHARS)];
47 }
while(!existingRect.addedToMap);
48 float width = dimensions.x;
49 float height = dimensions.y;
50 int relPos = Random::randInt(4);
53 case 0: newRect = {glm::vec2(existingRect.topLeft.x, existingRect.topLeft.y+height), dimensions,
true};
break;
55 case 1: newRect = {glm::vec2(existingRect.topLeft.x, existingRect.topLeft.y-existingRect.dimensions.y), dimensions,
true};
break;
57 case 2: newRect = {glm::vec2(existingRect.topLeft.x-width, existingRect.topLeft.y), dimensions,
true};
break;
59 case 3: newRect = {glm::vec2(existingRect.topLeft.x+existingRect.dimensions.x, existingRect.topLeft.y), dimensions,
true};
break;
61 }
while (isOverlapping(newRect));
62 m_rects[c-FIRST_PRINTABLE_CHAR] = newRect;
64 void FontMap::position() {
65 glm::vec2 topLeft = m_rects[0].topLeft;
66 std::for_each(m_rects, m_rects+NUM_PRINTABLE_CHARS, [&](
const CharRect& rect) {
67 if (rect.topLeft.x < topLeft.x) {
68 topLeft.x = rect.topLeft.x;
70 if (rect.topLeft.y > topLeft.y) {
71 topLeft.y = rect.topLeft.y;
75 std::for_each(m_rects, m_rects+NUM_PRINTABLE_CHARS, [&](
CharRect& rect) {
76 rect.topLeft -= topLeft;