| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "font.h" |
| 20 | |
| 21 | #include "fonthand.h" |
| 22 | #include "sprite.h" |
| 23 | |
| 24 | /** |
| 25 | * \file font.c |
| 26 | * \brief Text drawing routines. |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * \brief Draws text in a color specified by hue and value and with a drop |
| 31 | * shadow. |
| 32 | * |
| 33 | * A '~' in the text is not drawn but instead toggles highlighting which |
| 34 | * increases \c value by 4. |
| 35 | * |
| 36 | * \li like JE_dString() if (black == false && shadow_dist == 2 && hue == 15) |
| 37 | * \li like JE_textShade() with PART_SHADE if (black == true && shadow_dist == 1) |
| 38 | * \li like JE_outTextAndDarken() if (black == false && shadow_dist == 1) |
| 39 | * \li like JE_outTextAdjust() with shadow if (black == false && shadow_dist == 2) |
| 40 | * |
| 41 | * @param surface destination surface |
| 42 | * @param x initial x-position in pixels; which direction(s) the text is drawn |
| 43 | * from this position depends on the alignment |
| 44 | * @param y initial upper y-position in pixels |
| 45 | * @param text text to be drawn |
| 46 | * @param font style/size of text |
| 47 | * @param alignment left_aligned, centered, or right_aligned |
| 48 | * @param hue hue component of text color |
| 49 | * @param value value component of text color |
| 50 | * @param black if true the shadow is drawn as solid black, if false the shadow |
| 51 | * is drawn by darkening the pixels of the destination surface |
| 52 | * @param shadow_dist distance in pixels that the shadow will be drawn away from |
| 53 | * the text. (This is added to both the x and y positions, so a value of |
| 54 | * 1 causes the shadow to be drawn 1 pixel right and 1 pixel lower than |
| 55 | * the text.) |
| 56 | */ |
| 57 | void draw_font_hv_shadow(SDL_Surface *surface, int x, int y, const char *text, Font font, FontAlignment alignment, Uint8 hue, Sint8 value, bool black, int shadow_dist) |
| 58 | { |
| 59 | draw_font_dark(surface, x + shadow_dist, y + shadow_dist, text, font, alignment, black); |
| 60 | |
| 61 | draw_font_hv(surface, x, y, text, font, alignment, hue, value); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * \brief Draws text in a color specified by hue and value and with a |
| 66 | * surrounding shadow. |
| 67 | * |
| 68 | * A '~' in the text is not drawn but instead toggles highlighting which |
| 69 | * increases \c value by 4. |
| 70 | * |
| 71 | * \li like JE_textShade() with FULL_SHADE if (black == true && shadow_dist == 1) |
| 72 | * |
| 73 | * @param surface destination surface |
| 74 | * @param x initial x-position in pixels; which direction(s) the text is drawn |
| 75 | * from this position depends on the alignment |
| 76 | * @param y initial upper y-position in pixels |
| 77 | * @param text text to be drawn |
| 78 | * @param font style/size of text |
| 79 | * @param alignment left_aligned, centered, or right_aligned |
| 80 | * @param hue hue component of text color |
| 81 | * @param value value component of text color |
| 82 | * @param black if true the shadow is drawn as solid black, if false the shadow |
| 83 | * is drawn by darkening the pixels of the destination surface |
| 84 | * @param shadow_dist distance in pixels that the shadows will be drawn away |
| 85 | * from the text. (This distance is separately added to and subtracted |
| 86 | * from the x position and y position, resulting in four shadows -- one |
| 87 | * in each cardinal direction. If this shadow distance is small enough, |
| 88 | * this produces a shadow that outlines the text.) |
| 89 | */ |
| 90 | void draw_font_hv_full_shadow(SDL_Surface *surface, int x, int y, const char *text, Font font, FontAlignment alignment, Uint8 hue, Sint8 value, bool black, int shadow_dist) |
| 91 | { |
| 92 | draw_font_dark(surface, x, y - shadow_dist, text, font, alignment, black); |
| 93 | draw_font_dark(surface, x + shadow_dist, y, text, font, alignment, black); |
| 94 | draw_font_dark(surface, x, y + shadow_dist, text, font, alignment, black); |
| 95 | draw_font_dark(surface, x - shadow_dist, y, text, font, alignment, black); |
| 96 | |
| 97 | draw_font_hv(surface, x, y, text, font, alignment, hue, value); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * \brief Draws text in a color specified by hue and value. |
| 102 | * |
| 103 | * A '~' in the text is not drawn but instead toggles highlighting which |
| 104 | * increases \c value by 4. |
| 105 | * |
| 106 | * \li like JE_outText() with (brightness >= 0) |
| 107 | * \li like JE_outTextAdjust() without shadow |
| 108 | * |
| 109 | * @param surface destination surface |
| 110 | * @param x initial x-position in pixels; which direction(s) the text is drawn |
| 111 | * from this position depends on the alignment |
| 112 | * @param y initial upper y-position in pixels |
| 113 | * @param text text to be drawn |
| 114 | * @param font style/size of text |
| 115 | * @param alignment left_aligned, centered, or right_aligned |
| 116 | * @param hue hue component of text color |
| 117 | * @param value value component of text color |
| 118 | */ |
| 119 | void draw_font_hv(SDL_Surface *surface, int x, int y, const char *text, Font font, FontAlignment alignment, Uint8 hue, Sint8 value) |
| 120 | { |
| 121 | switch (alignment) |
| 122 | { |
| 123 | case left_aligned: |
| 124 | break; |
| 125 | case centered: |
| 126 | x -= JE_textWidth(text, font) / 2; |
| 127 | break; |
| 128 | case right_aligned: |
| 129 | x -= JE_textWidth(text, font); |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | bool highlight = false; |
| 134 | |
| 135 | for (; *text != '\0'; ++text) |
| 136 | { |
| 137 | int sprite_id = font_ascii[(unsigned char)*text]; |
| 138 | |
| 139 | switch (*text) |
| 140 | { |
| 141 | case ' ': |
| 142 | x += 6; |
| 143 | break; |
| 144 | |
| 145 | case '~': |
| 146 | highlight = !highlight; |
| 147 | if (highlight) |
| 148 | value += 4; |
| 149 | else |
| 150 | value -= 4; |
| 151 | break; |
| 152 | |
| 153 | default: |
| 154 | if (sprite_id != -1 && sprite_exists(font, sprite_id)) |
| 155 | { |
| 156 | blit_sprite_hv(surface, x, y, font, sprite_id, hue, value); |
| 157 | |
| 158 | x += sprite(font, sprite_id)->width + 1; |
| 159 | } |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * \brief Draws blended text in a color specified by hue and value. |
| 167 | * |
| 168 | * Corresponds to blit_sprite_hv_blend() |
| 169 | * |
| 170 | * \li like JE_outTextModify() |
| 171 | * |
| 172 | * @param surface destination surface |
| 173 | * @param x initial x-position in pixels; which direction(s) the text is drawn |
| 174 | * from this position depends on the alignment |
| 175 | * @param y initial upper y-position in pixels |
| 176 | * @param text text to be drawn |
| 177 | * @param font style/size of text |
| 178 | * @param alignment left_aligned, centered, or right_aligned |
| 179 | * @param hue hue component of text color |
| 180 | * @param value value component of text color |
| 181 | */ |
| 182 | void draw_font_hv_blend(SDL_Surface *surface, int x, int y, const char *text, Font font, FontAlignment alignment, Uint8 hue, Sint8 value) |
| 183 | { |
| 184 | switch (alignment) |
| 185 | { |
| 186 | case left_aligned: |
| 187 | break; |
| 188 | case centered: |
| 189 | x -= JE_textWidth(text, font) / 2; |
| 190 | break; |
| 191 | case right_aligned: |
| 192 | x -= JE_textWidth(text, font); |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | for (; *text != '\0'; ++text) |
| 197 | { |
| 198 | int sprite_id = font_ascii[(unsigned char)*text]; |
| 199 | |
| 200 | switch (*text) |
| 201 | { |
| 202 | case ' ': |
| 203 | x += 6; |
| 204 | break; |
| 205 | |
| 206 | case '~': |
| 207 | break; |
| 208 | |
| 209 | default: |
| 210 | if (sprite_id != -1 && sprite_exists(font, sprite_id)) |
| 211 | { |
| 212 | blit_sprite_hv_blend(surface, x, y, font, sprite_id, hue, value); |
| 213 | |
| 214 | x += sprite(font, sprite_id)->width + 1; |
| 215 | } |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * \brief Draws darkened text. |
| 223 | * |
| 224 | * Corresponds to blit_sprite_dark() |
| 225 | * |
| 226 | * \li like JE_outText() with (brightness < 0) if (black == true) |
| 227 | * |
| 228 | * @param surface destination surface |
| 229 | * @param x initial x-position in pixels; which direction(s) the text is drawn |
| 230 | * from this position depends on the alignment |
| 231 | * @param y initial upper y-position in pixels |
| 232 | * @param text text to be drawn |
| 233 | * @param font style/size of text |
| 234 | * @param alignment left_aligned, centered, or right_aligned |
| 235 | * @param black if true text is drawn as solid black, if false text is drawn by |
| 236 | * darkening the pixels of the destination surface |
| 237 | */ |
| 238 | void draw_font_dark(SDL_Surface *surface, int x, int y, const char *text, Font font, FontAlignment alignment, bool black) |
| 239 | { |
| 240 | switch (alignment) |
| 241 | { |
| 242 | case left_aligned: |
| 243 | break; |
| 244 | case centered: |
| 245 | x -= JE_textWidth(text, font) / 2; |
| 246 | break; |
| 247 | case right_aligned: |
| 248 | x -= JE_textWidth(text, font); |
| 249 | break; |
| 250 | } |
| 251 | |
| 252 | for (; *text != '\0'; ++text) |
| 253 | { |
| 254 | int sprite_id = font_ascii[(unsigned char)*text]; |
| 255 | |
| 256 | switch (*text) |
| 257 | { |
| 258 | case ' ': |
| 259 | x += 6; |
| 260 | break; |
| 261 | |
| 262 | case '~': |
| 263 | break; |
| 264 | |
| 265 | default: |
| 266 | if (sprite_id != -1 && sprite_exists(font, sprite_id)) |
| 267 | { |
| 268 | blit_sprite_dark(surface, x, y, font, sprite_id, black); |
| 269 | |
| 270 | x += sprite(font, sprite_id)->width + 1; |
| 271 | } |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | } |