| 1 |
/** |
| 2 |
* Display game texts |
| 3 |
|
| 4 |
* Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson |
| 5 |
* Copyright (C) 2014 Sylvain Beucler |
| 6 |
|
| 7 |
* This file is part of GNU FreeDink |
| 8 |
|
| 9 |
* GNU FreeDink is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU General Public License as |
| 11 |
* published by the Free Software Foundation; either version 3 of the |
| 12 |
* License, or (at your option) any later version. |
| 13 |
|
| 14 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* General Public License for more details. |
| 18 |
|
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program. If not, see |
| 21 |
* <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include "text.h" |
| 29 |
|
| 30 |
#include "game_engine.h" |
| 31 |
#include "live_sprites_manager.h" |
| 32 |
#include "dinkini.h" /* check_seq_status */ |
| 33 |
#include "gfx.h" |
| 34 |
#include "gfx_fonts.h" |
| 35 |
#include "gfx_sprites.h" |
| 36 |
#include "text.h" |
| 37 |
#include "log.h" |
| 38 |
|
| 39 |
#define TEXT_MIN 2700 |
| 40 |
#define TEXT_TIMER 77 |
| 41 |
|
| 42 |
/* say_text, say_text_xy */ |
| 43 |
int add_text_sprite(char* text, int script, int sprite_owner, int mx, int my) |
| 44 |
{ |
| 45 |
int tsprite = add_sprite(mx, my, 8, 0, 0); |
| 46 |
if (tsprite == 0) |
| 47 |
{ |
| 48 |
log_error("Couldn't say something, out of sprites."); |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
strncpy(spr[tsprite].text, text, 200-1); // TODO: currently truncated to 199 chars |
| 53 |
spr[tsprite].text[200-1] = '\0'; |
| 54 |
|
| 55 |
*plast_text = tsprite; |
| 56 |
spr[tsprite].kill_ttl = strlen(text) * TEXT_TIMER; |
| 57 |
if (spr[tsprite].kill_ttl < TEXT_MIN) |
| 58 |
spr[tsprite].kill_ttl = TEXT_MIN; |
| 59 |
spr[tsprite].damage = -1; |
| 60 |
spr[tsprite].text_owner = sprite_owner; |
| 61 |
spr[tsprite].hard = 1; |
| 62 |
spr[tsprite].script = script; |
| 63 |
spr[tsprite].nohit = 1; |
| 64 |
|
| 65 |
return tsprite; |
| 66 |
} |
| 67 |
|
| 68 |
int say_text(char* text, int sprite_owner, int script) { |
| 69 |
int tsprite; |
| 70 |
if (sprite_owner == 1000) |
| 71 |
tsprite = add_text_sprite(text, script, 1000, 100, 100); |
| 72 |
else if (lsm_isValidSprite(sprite_owner)) |
| 73 |
tsprite = add_text_sprite(text, script, sprite_owner, |
| 74 |
spr[sprite_owner].x, spr[sprite_owner].y); |
| 75 |
else |
| 76 |
tsprite = add_text_sprite(text, script, sprite_owner, 100, 100); |
| 77 |
|
| 78 |
if (tsprite == 0) |
| 79 |
return 0; |
| 80 |
|
| 81 |
//set X offset for text, using strength var since it's unused |
| 82 |
spr[tsprite].strength = 75; |
| 83 |
if (lsm_isValidSprite(sprite_owner)) { |
| 84 |
check_seq_status(spr[sprite_owner].seq); |
| 85 |
int gfx_id = getpic(sprite_owner); |
| 86 |
if (gfx_id >= 0 && gfx_id < MAX_SPRITES) |
| 87 |
spr[tsprite].defense = (k[gfx_id].box.bottom - k[gfx_id].yoffset) + 100; |
| 88 |
else |
| 89 |
spr[tsprite].defense = 100; |
| 90 |
|
| 91 |
spr[tsprite].x = spr[sprite_owner].x - spr[tsprite].strength; |
| 92 |
spr[tsprite].y = spr[sprite_owner].y - spr[tsprite].defense; |
| 93 |
} |
| 94 |
return tsprite; |
| 95 |
} |
| 96 |
|
| 97 |
int say_text_xy(char* text, int mx, int my, int script) |
| 98 |
{ |
| 99 |
int sprite_owner = 1000; |
| 100 |
return add_text_sprite(text, script, sprite_owner, mx, my); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/* Get sprite #h, grab its text and display it */ |
| 105 |
void text_draw(int h, double brightness) { |
| 106 |
char crap[200]; |
| 107 |
char *cr; |
| 108 |
rect rcRect; |
| 109 |
int color = 0; |
| 110 |
|
| 111 |
if (spr[h].damage == -1) { |
| 112 |
sprintf(crap, "%s", spr[h].text); |
| 113 |
cr = &crap[0]; |
| 114 |
color = 14; |
| 115 |
while( cr[0] == '`') { |
| 116 |
//color code at top |
| 117 |
if (cr[1] == '#') color = 13; |
| 118 |
if (cr[1] == '1') color = 1; |
| 119 |
if (cr[1] == '2') color = 2; |
| 120 |
if (cr[1] == '3') color = 3; |
| 121 |
if (cr[1] == '5') color = 5; |
| 122 |
if (cr[1] == '6') color = 6; |
| 123 |
if (cr[1] == '7') color = 7; |
| 124 |
if (cr[1] == '8') color = 8; |
| 125 |
if (cr[1] == '9') color = 9; |
| 126 |
if (cr[1] == '0') color = 10; |
| 127 |
if (cr[1] == '$') color = 14; |
| 128 |
if (cr[1] == '%') color = 15; |
| 129 |
|
| 130 |
if (dversion >= 108) { |
| 131 |
//support for additional colors |
| 132 |
if (cr[1] == '@') |
| 133 |
color = 12; |
| 134 |
if (cr[1] == '!') |
| 135 |
color = 11; |
| 136 |
} |
| 137 |
|
| 138 |
if (cr[1] == '4') color = 4; |
| 139 |
cr = &cr[2]; |
| 140 |
} |
| 141 |
|
| 142 |
// Set size |
| 143 |
if (spr[h].text_owner == 1000) { |
| 144 |
rect_set(&rcRect, spr[h].x, spr[h].y, |
| 145 |
spr[h].x + 620, spr[h].y + 400); |
| 146 |
} else { |
| 147 |
rect_set(&rcRect, spr[h].x, spr[h].y, |
| 148 |
spr[h].x + 150, spr[h].y + 150); |
| 149 |
if ((spr[h].x + 150) > 620) |
| 150 |
rect_offset(&rcRect, ((spr[h].x+150)-620) - (((spr[h].x+150)-620) * 2), 0); |
| 151 |
} |
| 152 |
} else { |
| 153 |
// Display damage/experience points |
| 154 |
sprintf(crap, "%d", spr[h].damage); |
| 155 |
cr = &crap[0]; |
| 156 |
if (spr[h].brain_parm == 5000) // experience |
| 157 |
color = 14; |
| 158 |
|
| 159 |
if (spr[h].y < 0) |
| 160 |
spr[h].y = 0; |
| 161 |
rect_set(&rcRect, spr[h].x, spr[h].y, |
| 162 |
spr[h].x + 50, spr[h].y + 50); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
/* During a fadedown/fadeup, use white text to mimic v1.07 */ |
| 167 |
if (brightness < 256) |
| 168 |
color = 15; |
| 169 |
|
| 170 |
|
| 171 |
SDL_Color fg; |
| 172 |
// support for custom colors |
| 173 |
if (color >= 1 && color <= 15) |
| 174 |
fg = font_colors[color]; |
| 175 |
else |
| 176 |
fg = {255,255,255}; |
| 177 |
|
| 178 |
std::vector<TextCommand> cmds; |
| 179 |
|
| 180 |
// Clear cache if color changed (i.e. during truecolor fade_down()) |
| 181 |
if (spr[h].text_cache != NULL && memcmp(&spr[h].text_cache_color, &fg, sizeof(SDL_Color)) == 0) { |
| 182 |
delete spr[h].text_cache; |
| 183 |
spr[h].text_cache = NULL; |
| 184 |
} |
| 185 |
|
| 186 |
if (spr[h].text_cache == NULL) { |
| 187 |
SDL_Color bg = {8,14,21}; |
| 188 |
rect rel = rcRect; |
| 189 |
rect_offset(&rel, -rcRect.left, -rcRect.top); |
| 190 |
FONTS_SetTextColor(bg.r, bg.g, bg.b); |
| 191 |
print_text_wrap_getcmds(cr, &rel, 1, 0, FONT_DIALOG, &cmds); |
| 192 |
rect_offset(&rel,-2,0); |
| 193 |
print_text_wrap_getcmds(cr, &rel, 1, 0, FONT_DIALOG, &cmds); |
| 194 |
rect_offset(&rel,1,1); |
| 195 |
print_text_wrap_getcmds(cr, &rel, 1, 0, FONT_DIALOG, &cmds); |
| 196 |
rect_offset(&rel,0,-2); |
| 197 |
print_text_wrap_getcmds(cr, &rel, 1, 0, FONT_DIALOG, &cmds); |
| 198 |
|
| 199 |
FONTS_SetTextColor(fg.r, fg.g, fg.b); |
| 200 |
rect_offset(&rel,0,1); |
| 201 |
print_text_wrap_getcmds(cr, &rel, 1, 0, FONT_DIALOG, &cmds); |
| 202 |
|
| 203 |
if (cmds.size() > 0) { |
| 204 |
IOGfxSurface* surf = print_text_flatten_cmds(&cmds); |
| 205 |
spr[h].text_cache = surf; |
| 206 |
spr[h].text_cache_reldst = cmds[0].dst; |
| 207 |
print_text_cache(spr[h].text_cache, spr[h].text_cache_reldst, rcRect.left, rcRect.top); |
| 208 |
} |
| 209 |
} else { |
| 210 |
print_text_cache(spr[h].text_cache, spr[h].text_cache_reldst, rcRect.left, rcRect.top); |
| 211 |
} |
| 212 |
} |