* Calculates the real size and position of each character in * the surface and stores them in SDL_Rect's for future use * by other classes. */
| 113 | * by other classes. |
| 114 | */ |
| 115 | void Font::init() |
| 116 | { |
| 117 | _surface->lock(); |
| 118 | int length = (_surface->getWidth() / _width); |
| 119 | if (_monospace) |
| 120 | { |
| 121 | for (size_t i = 0; i < _index.length(); ++i) |
| 122 | { |
| 123 | SDL_Rect rect; |
| 124 | int startX = i % length * _width; |
| 125 | int startY = i / length * _height; |
| 126 | rect.x = startX; |
| 127 | rect.y = startY; |
| 128 | rect.w = _width; |
| 129 | rect.h = _height; |
| 130 | _chars[_index[i]] = rect; |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | for (size_t i = 0; i < _index.length(); ++i) |
| 136 | { |
| 137 | SDL_Rect rect; |
| 138 | int left = -1, right = -1; |
| 139 | int startX = i % length * _width; |
| 140 | int startY = i / length * _height; |
| 141 | for (int x = startX; x < startX + _width; ++x) |
| 142 | { |
| 143 | for (int y = startY; y < startY + _height && left == -1; ++y) |
| 144 | { |
| 145 | Uint8 pixel = _surface->getPixel(x, y); |
| 146 | if (pixel != 0) |
| 147 | { |
| 148 | left = x; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | for (int x = startX + _width - 1; x >= startX; --x) |
| 153 | { |
| 154 | for (int y = startY + _height; y-- != startY && right == -1;) |
| 155 | { |
| 156 | Uint8 pixel = _surface->getPixel(x, y); |
| 157 | if (pixel != 0) |
| 158 | { |
| 159 | right = x; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | rect.x = left; |
| 164 | rect.y = startY; |
| 165 | rect.w = right - left + 1; |
| 166 | rect.h = _height; |
| 167 | |
| 168 | _chars[_index[i]] = rect; |
| 169 | } |
| 170 | } |
| 171 | _surface->unlock(); |
| 172 | } |