| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (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, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/hudmessage.cpp $ |
| 21 | * $Revision: 89 $ |
| 22 | * $Date: 9/22/01 7:14p $ |
| 23 | * $Author: Kevin $ |
| 24 | * |
| 25 | * <file to correctly render hud messages> |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/hudmessage.cpp $ |
| 28 | * |
| 29 | * 89 9/22/01 7:14p Kevin |
| 30 | * Print confirmation messages when sending a private message |
| 31 | * |
| 32 | * 88 4/19/00 5:31p Matt |
| 33 | * From Duane for 1.4 |
| 34 | * Set m_limit class member variable if it had not been properly |
| 35 | * initialized |
| 36 | * |
| 37 | * 87 7/16/99 11:17a Samir |
| 38 | * dirty rects for multi line persistent hud messages. |
| 39 | * |
| 40 | * 86 7/13/99 12:22a Jeff |
| 41 | * correctly externed HudInputMessage, increased size for new tokenized |
| 42 | * text macros |
| 43 | * |
| 44 | * 85 7/09/99 3:49p Jason |
| 45 | * fixed bugs/issues for patch |
| 46 | * |
| 47 | * 84 7/07/99 12:50a Jeff |
| 48 | * if a hud message gets broken up while typing, the state is saved and |
| 49 | * continued on the next line |
| 50 | * |
| 51 | * 83 5/20/99 5:48p Matt |
| 52 | * Added a HUD item flag, for use by Dallas-created items, that makes a |
| 53 | * HUD item persisitent for the duration of one level, but get cleared |
| 54 | * between levels. |
| 55 | * |
| 56 | * 82 5/17/99 6:29p Jeff |
| 57 | * only filter hud messages in multiplayer |
| 58 | * |
| 59 | * 81 5/17/99 6:22p Jeff |
| 60 | * added filtered HUD messages |
| 61 | * |
| 62 | * 80 5/17/99 5:48p Samir |
| 63 | * base scrolling off of time key is down. |
| 64 | * |
| 65 | * 79 5/10/99 10:22p Ardussi |
| 66 | * changes to compile on Mac |
| 67 | * |
| 68 | * 78 5/08/99 12:54p Matt |
| 69 | * When a HUD message is split over multiple lines, copy the color |
| 70 | * information to subsequent lines. |
| 71 | * |
| 72 | * 77 5/08/99 1:06a Samir |
| 73 | * create dirty rect when clearing persistent hud messages. |
| 74 | * |
| 75 | * 76 5/07/99 10:48p Matt |
| 76 | * Made add-HUD functions return true if the message was added, and false |
| 77 | * if it wasn't (because the previous message was the same). |
| 78 | * |
| 79 | * 75 5/05/99 6:36p Matt |
| 80 | * Added queue system for persistent HUD messages. |
| 81 | * |
| 82 | * 74 5/01/99 5:52p Samir |
| 83 | * removed RenderHudMessages, and redid RenderHUDMessages so it did what |
| 84 | * RenderHudMessages did, resets on screen hud messages. The HUD message |
| 85 | * console is resetted in ResetGameMessages. |
| 86 | * |
| 87 | * 73 4/28/99 2:14a Samir |
| 88 | * save and load Game messages in savegame. |
| 89 | * |
| 90 | * 72 4/27/99 12:32p Samir |
| 91 | * maybe fixed crash when resetting message lists. |
| 92 | * |
| 93 | * 71 4/24/99 10:38p Samir |
| 94 | * cleaned up hud text problems in 'small mode' |
| 95 | * |
| 96 | * 70 4/24/99 8:43p Samir |
| 97 | * when shrinking screen hud messages get rendered in black region. |
| 98 | * |
| 99 | * 69 4/17/99 6:15p Samir |
| 100 | * replaced gr.h with grdefs.h and fixed resulting compile bugs. |
| 101 | * |
| 102 | * 68 4/14/99 12:35p Samir |
| 103 | * localization issues. |
| 104 | * |
| 105 | * 67 4/14/99 2:50a Jeff |
| 106 | * fixed some case mismatched #includes |
| 107 | * |
| 108 | * 66 4/09/99 3:04p Kevin |
| 109 | * Set hud message length back to 40 -- I think I meant to change |
| 110 | * something else when I changed this. |
| 111 | * |
| 112 | * 65 3/10/99 2:25p Kevin |
| 113 | * Save/Load and Demo file fixes |
| 114 | * |
| 115 | * 64 3/05/99 3:01p Jeff |
| 116 | * fixed crash when a persistant message is displayed during a IGC |
| 117 | * |
| 118 | * 63 3/05/99 12:46p Matt |
| 119 | * Set text flags & alpha before drawing scrolling text and input message. |
| 120 | * |
| 121 | * 62 3/04/99 8:09p Samir |
| 122 | * when inputting text, don't use formatted print function to output it to |
| 123 | * screen. There's still a problem on the end though when printing out the |
| 124 | * message. |
| 125 | * |
| 126 | * 61 3/04/99 7:39p Matt |
| 127 | * Added sound effects to FreeSpace-style persistent HUD messages. |
| 128 | * |
| 129 | * 60 3/04/99 6:15p Matt |
| 130 | * Added groovy FreeSpace briefing-style text effect to the persistent HUD |
| 131 | * message. |
| 132 | * |
| 133 | * 59 3/03/99 5:34p Matt |
| 134 | * Added fade-out for goal complete messages |
| 135 | * |
| 136 | * 58 3/02/99 11:41p Kevin |
| 137 | * Fixed dedicated server |
| 138 | * |
| 139 | * 57 3/02/99 3:30p Samir |
| 140 | * took out ddio_KeyFlush which caused shift characters to be silenced |
| 141 | * when inputting message. This did not affect other key things as far as |
| 142 | * I know. |
| 143 | * |
| 144 | * 56 2/19/99 2:18p Samir |
| 145 | * AddGameMessage implicitly figures out time and level of message. |
| 146 | * |
| 147 | * 55 2/18/99 5:27p Matt |
| 148 | * Added color parm to AddPersistentHUDMessage() and fixed the timeout. |
| 149 | * |
| 150 | * 54 2/16/99 6:16p Jeff |
| 151 | * pause hud messages when in cinematics |
| 152 | * |
| 153 | * 53 2/16/99 3:52p Kevin |
| 154 | * Hud messages wrap on 70% of the screen width instead of 100% |
| 155 | * |
| 156 | * 52 2/09/99 6:52p Jeff |
| 157 | * implemented 'typing inidcator' in multiplayer...players that are typing |
| 158 | * messages have an icon on them |
| 159 | * |
| 160 | * 51 2/04/99 7:17p Jeff |
| 161 | * put in sounds for hud messages |
| 162 | * |
| 163 | * 50 1/31/99 7:26p Matt |
| 164 | * Renamed a bunch of functions to have HUD capitalized |
| 165 | * |
| 166 | * 49 1/29/99 6:34p Samir |
| 167 | * fixed booboo. |
| 168 | * |
| 169 | * 48 1/29/99 6:32p Samir |
| 170 | * implemented hud scrollback for hud messages. |
| 171 | * |
| 172 | * 47 1/29/99 2:08p Jeff |
| 173 | * localization |
| 174 | * |
| 175 | * 46 1/28/99 6:17p Jason |
| 176 | * added markers |
| 177 | * |
| 178 | * 45 1/27/99 6:05p Samir |
| 179 | * added scrollback for game messages on HUD. |
| 180 | * |
| 181 | * 44 1/23/99 2:33p Kevin |
| 182 | * Increased hud message length, because we clip to the real width of the |
| 183 | * hud. Also fixed up the multiline code a bit |
| 184 | * |
| 185 | * 43 1/22/99 4:19p Kevin |
| 186 | * Fixed some multiline hud stuff |
| 187 | * |
| 188 | * 42 1/22/99 4:06p Jeff |
| 189 | * added hud messages that can be sent to just teammates or individual |
| 190 | * people |
| 191 | * |
| 192 | * 41 1/21/99 11:43p Kevin |
| 193 | * Hud messages will now span multiple lines if needed (except for |
| 194 | * blinking) |
| 195 | * |
| 196 | * 40 1/08/99 2:55p Samir |
| 197 | * Ripped out OSIRIS1. |
| 198 | * |
| 199 | * 39 12/14/98 11:06a Jason |
| 200 | * changes for 1.1 |
| 201 | * |
| 202 | * 38 12/10/98 4:58p Jason |
| 203 | * fixed off-by-one hudmessage problem |
| 204 | * |
| 205 | * 37 11/01/98 1:57a Jeff |
| 206 | * converted the vsprintf calls to use the Pvsprintf, which is a safe |
| 207 | * vsprintf, no buffer overflows allowed |
| 208 | * |
| 209 | * 36 10/29/98 11:59a Jason |
| 210 | * fixed hud message issues |
| 211 | * |
| 212 | * 35 10/21/98 5:02p Jeff |
| 213 | * removed player from HUD if in observer nide |
| 214 | * |
| 215 | * 34 10/19/98 11:22p Samir |
| 216 | * made AddPersistentMessage screen res friendly. |
| 217 | * |
| 218 | * 33 10/17/98 2:46p Jason |
| 219 | * automatically start new line when eol is reaching in DoInputMessage |
| 220 | * |
| 221 | * 32 10/14/98 4:26p Samir |
| 222 | * added persistent hud messages. |
| 223 | * |
| 224 | * 31 10/13/98 8:44p Samir |
| 225 | * move outgoing message prompt down. |
| 226 | * |
| 227 | * 30 10/07/98 6:55p Jason |
| 228 | * made message rollback work |
| 229 | * |
| 230 | * 29 10/07/98 10:58a Jason |
| 231 | * flushed the key buffer when pressing escape while entering hud message |
| 232 | * |
| 233 | * 28 10/06/98 5:45p Kevin |
| 234 | * Added new configuration for demo |
| 235 | * |
| 236 | * 27 10/05/98 11:08a Jason |
| 237 | * implemented player message log |
| 238 | * |
| 239 | * 26 9/30/98 4:50p Jeff |
| 240 | * fixed mprintf if a hud message is too long |
| 241 | * |
| 242 | * 25 9/15/98 4:31p Jason |
| 243 | * added more functionality for the dedicated server |
| 244 | * |
| 245 | * 24 9/14/98 6:28p Jason |
| 246 | * first pass at getting dedicated server working |
| 247 | * |
| 248 | * 23 9/08/98 10:28a Samir |
| 249 | * added function to reset hud messages. |
| 250 | * |
| 251 | * 22 8/17/98 10:39a Jeff |
| 252 | * fixed bug with buffer length for hud messages |
| 253 | * |
| 254 | * 21 8/15/98 10:50p Matt |
| 255 | * Changed comment |
| 256 | * |
| 257 | * 20 8/13/98 3:10p Jeff |
| 258 | * key flush when done typing a hud message |
| 259 | * |
| 260 | * 19 7/28/98 3:17p Jason |
| 261 | * fixed buffer overrun problem |
| 262 | * |
| 263 | * 18 6/16/98 10:53a Jeff |
| 264 | * |
| 265 | * 17 5/05/98 5:35p Jason |
| 266 | * took off stupid drop shadow |
| 267 | * |
| 268 | * 16 4/23/98 4:13a Samir |
| 269 | * new hud system. |
| 270 | * |
| 271 | * 15 4/06/98 2:54p Jason |
| 272 | * yet more multiplayer changes |
| 273 | * |
| 274 | * 14 3/23/98 7:37p Jason |
| 275 | * added colored hud messages |
| 276 | * |
| 277 | * 13 3/23/98 4:51p Jason |
| 278 | * incremental checkin for multiplay |
| 279 | * |
| 280 | * 12 3/16/98 6:41p Jason |
| 281 | * added user definable messages to multiplayer |
| 282 | * |
| 283 | * 11 3/16/98 3:53p Jason |
| 284 | * added hud input message stuff |
| 285 | * |
| 286 | * 10 2/06/98 5:45p Samir |
| 287 | * Use hud font now. |
| 288 | * |
| 289 | * 9 1/19/98 4:37p Jason |
| 290 | * made hud message time longer |
| 291 | * |
| 292 | * 8 1/12/98 7:07p Samir |
| 293 | * Hud messages are in the briefing font for now. |
| 294 | * |
| 295 | * 7 1/12/98 5:23p Samir |
| 296 | * New hud font for messages. |
| 297 | * |
| 298 | * 6 12/29/97 5:47p Samir |
| 299 | * New text system and took out refs to Game_viewport. |
| 300 | * |
| 301 | * 5 12/16/97 4:27p Jason |
| 302 | * fixed time wrapping problem |
| 303 | * |
| 304 | * 4 8/29/97 5:42p Jason |
| 305 | * fixed hud rendering problems |
| 306 | * |
| 307 | * 6 4/25/97 10:49a Jason |
| 308 | * made timer stuff work with Gametime instead of timer_GetTime |
| 309 | * |
| 310 | * 5 3/05/97 3:03p Jason |
| 311 | * added blinking messages |
| 312 | * |
| 313 | * 4 3/04/97 12:58p Jason |
| 314 | * made hud messages scroll and fade |
| 315 | * |
| 316 | * 3 3/04/97 11:58a Jason |
| 317 | * checked in for Samir to debug |
| 318 | * |
| 319 | * 2 2/13/97 4:12p Jason |
| 320 | * added hud message test |
| 321 | * |
| 322 | * $NoKeywords: $ |
| 323 | */ |
| 324 | |
| 325 | #include <cstdarg> |
| 326 | #include <cstdio> |
| 327 | |
| 328 | #include <stdlib.h> |
| 329 | #include <string.h> |
| 330 | #include <ctype.h> |
| 331 | #include "grdefs.h" |
| 332 | #include "hud.h" |
| 333 | #include "game.h" |
| 334 | #include "ddio.h" |
| 335 | #include "gamefont.h" |
| 336 | #include "newui.h" |
| 337 | #include "multi.h" |
| 338 | #include "player.h" |
| 339 | #include "game2dll.h" |
| 340 | #include "stringtable.h" |
| 341 | #include "dedicated_server.h" |
| 342 | #include "AppConsole.h" |
| 343 | #include "demofile.h" |
| 344 | #include "mem.h" |
| 345 | #include "textaux.h" |
| 346 | #include "3d.h" |
| 347 | #include "marker.h" |
| 348 | #include "controls.h" |
| 349 | #include "Mission.h" |
| 350 | #include "sounds.h" |
| 351 | #include "hlsoundlib.h" |
| 352 | #include "args.h" |
| 353 | |
| 354 | #define HUD_MESSAGE_NORMAL 0 |
| 355 | #define HUD_MESSAGE_BLINKING 1 |
| 356 | |
| 357 | #define HUD_MESSAGE_NONE 0 |
| 358 | #define HUD_MESSAGE_GENERAL 1 |
| 359 | #define HUD_MESSAGE_TEAM 2 |
| 360 | |
| 361 | // How long the message stays up for (in seconds) |
| 362 | #define HUD_MESSAGE_TIME 5 |
| 363 | // How long the message scrolls for (in seconds) |
| 364 | #define HUD_SCROLL_TIME .5 |
| 365 | |
| 366 | char HudInputMessage[MAX_HUD_INPUT_LEN]; |
| 367 | int Doing_input_message = HUD_MESSAGE_NONE; |
| 368 | int HudInputMessageLen = 0; |
| 369 | static t_dirty_rect HUD_inmsg_dirty_rect; |
| 370 | |
| 371 | static char HUD_messages[MAX_HUD_MESSAGES][HUD_MESSAGE_LENGTH]; |
| 372 | static int HUD_message_type[MAX_HUD_MESSAGES]; |
| 373 | static ddgr_color HUD_message_color[MAX_HUD_MESSAGES]; |
| 374 | int Num_hud_messages = 0; |
| 375 | static int Hud_scroll_offset = 0; |
| 376 | static t_dirty_rect HUD_msg_dirty_rect; |
| 377 | |
| 378 | static float Hud_timer = 0.0f; |
| 379 | |
| 380 | static uint8_t Hud_persistent_msg_id = HUD_INVALID_ID; |
| 381 | static float Hud_persistent_msg_timer = 0.0f; |
| 382 | static int Hud_persistent_msg_flags = 0; |
| 383 | static int Hud_persistent_msg_current_len; |
| 384 | static float Hud_persistent_msg_char_timer; |
| 385 | static float Hud_persistent_msg_id2 = HUD_INVALID_ID; |
| 386 | static int Hud_persistent_msg_sound_handle = SOUND_NONE_INDEX; |
| 387 | static t_dirty_rect HUD_persist_dirty_rect[2]; |
| 388 | |
| 389 | tHUDItem *GetHUDItem(int id); |
| 390 | |
| 391 | // game message console |
| 392 | MsgListConsole Game_msg_con; |
| 393 | static tMsgList Game_msg_list; |
| 394 | static bool Game_msg_con_vis = false; |
| 395 | |
| 396 | // hud message console |
| 397 | MsgListConsole HUD_msg_con; |
| 398 | static tMsgList HUD_msg_list; |
| 399 | static bool HUD_msg_con_vis = false; |
| 400 | static bool Hud_messages_paused = false; |
| 401 | |
| 402 | #define GAME_MSGCON_W 416 |
| 403 | #define GAME_MSGCON_H 256 |
| 404 | #define GAME_MSGCON_X ((Max_window_w - GAME_MSGCON_W) / 2) |
| 405 | #define GAME_MSGCON_Y ((Max_window_h - GAME_MSGCON_H) / 2) |
| 406 | |
| 407 | // This function takes a HUD messages, ensures it isn't too long, and if it is, it correctly fixes it |
| 408 | static void CorrectHudMessage(char *str); |
| 409 | |
| 410 | /////////////////////////////////////////////////////////////////////////////// |
| 411 | // Functions |
| 412 | |
| 413 | // prints a string onto the debug consle |
| 414 | static void AddMessageToRollback(char *msg) { HUD_msg_list.add(msg); } |
| 415 | |
| 416 | // Adds a single line to the hud message list |
| 417 | // Returns true if message added, or false if message not (because the previous message was the same) |
| 418 | static bool AddLineToHUDMessages(char *temp_message, ddgr_color color = -1); |
| 419 | bool AddLineToHUDMessages(char *temp_message, ddgr_color color) { |
| 420 | if (Num_hud_messages > 0 && !strcmp(temp_message, HUD_messages[Num_hud_messages - 1])) |
| 421 | return 0; // this is the same message as before, don't print it twice! |
| 422 | |
| 423 | if (Dedicated_server) { |
| 424 | PrintDedicatedMessage("*%s\n", temp_message); |
| 425 | return 1; |
| 426 | } |
| 427 | |
| 428 | AddMessageToRollback(temp_message); |
| 429 | |
| 430 | if (color == -1) |
| 431 | color = GR_RGB(0, 255, 0); |
| 432 | |
| 433 | if (Num_hud_messages == MAX_HUD_MESSAGES) { |
| 434 | // Get rid of top message |
| 435 | |
| 436 | for (int i = 1; i < MAX_HUD_MESSAGES; i++) { |
| 437 | strcpy(HUD_messages[i - 1], HUD_messages[i]); |
| 438 | HUD_message_type[i - 1] = HUD_message_type[i]; |
| 439 | HUD_message_color[i - 1] = HUD_message_color[i]; |
| 440 | } |
| 441 | |
| 442 | Num_hud_messages--; |
| 443 | |
| 444 | Hud_scroll_offset = 0; |
| 445 | |
| 446 | if (!Hud_messages_paused) |
| 447 | Hud_timer = Gametime; |
| 448 | } |
| 449 | |
| 450 | if (Num_hud_messages == 0 && !Hud_messages_paused) |
| 451 | Hud_timer = Gametime; |
| 452 | |
| 453 | CorrectHudMessage(temp_message); |
| 454 | |
| 455 | strcpy(HUD_messages[Num_hud_messages], temp_message); |
| 456 | HUD_message_type[Num_hud_messages] = HUD_MESSAGE_NORMAL; |
| 457 | |
| 458 | HUD_message_color[Num_hud_messages] = color; |
| 459 | |
| 460 | Num_hud_messages++; |
| 461 | |
| 462 | return 1; |
| 463 | } |
| 464 | |
| 465 | // Adds line, splitting if too long |
| 466 | // Returns true if line added |
| 467 | static bool AddMultipleLinesToHUDMessages(char *temp_message, ddgr_color color = -1); |
| 468 | bool AddMultipleLinesToHUDMessages(char *temp_message, ddgr_color color) { |
| 469 | char ourstr[HUD_MESSAGE_LENGTH * 2]; |
| 470 | char *p; |
| 471 | // char word[HUD_MESSAGE_LENGTH*2]; |
| 472 | char nextword[HUD_MESSAGE_LENGTH * 2]; |
| 473 | static char thisline[HUD_MESSAGE_LENGTH * 2] = ""; |
| 474 | thisline[0] = '\0'; |
| 475 | strcpy(ourstr, temp_message); |
| 476 | p = strtok(ourstr, " "); |
| 477 | bool added = 0; |
| 478 | while (p) { |
| 479 | strcat(thisline, p); |
| 480 | strcat(thisline, " "); |
| 481 | // strcpy(word,p); |
| 482 | |
| 483 | p = strtok(NULL, " "); |
| 484 | if (p) { |
| 485 | strcpy(nextword, p); |
| 486 | if ((grtext_GetTextLineWidth(thisline) + grtext_GetTextLineWidth(nextword)) > (Game_window_w * .7)) { |
| 487 | if (thisline[strlen(thisline) - 1] == ' ') |
| 488 | thisline[strlen(thisline) - 1] = '\0'; |
| 489 | added |= AddLineToHUDMessages(thisline, color); |
| 490 | |
| 491 | // Scan for color information in the string we just added |
| 492 | char *c = NULL, *c2; |
| 493 | c2 = strchr(thisline, '\1'); |
| 494 | while (c2) { |
| 495 | c = c2; |
| 496 | c2 = strchr(c2 + 4, '\1'); |
| 497 | } |
| 498 | if (c) { // found one |
| 499 | for (int i = 0; i < 4; i++) |
| 500 | thisline[i] = c[i]; |
| 501 | thisline[4] = 0; |
| 502 | } else |
| 503 | thisline[0] = '\0'; |
| 504 | } |
| 505 | p = nextword; |
| 506 | } |
| 507 | } |
| 508 | if (thisline[strlen(thisline) - 1] == ' ') |
| 509 | thisline[strlen(thisline) - 1] = '\0'; |
| 510 | if (thisline[0]) |
| 511 | added |= AddLineToHUDMessages(thisline, color); |
| 512 | |
| 513 | return added; |
| 514 | } |
| 515 | |
| 516 | // adds a colored hud message to the list |
| 517 | // Returns true if message added, or false if message not (because the previous message was the same) |
| 518 | bool AddColoredHUDMessage(ddgr_color color, const char *format, ...) { |
| 519 | std::va_list args; |
| 520 | char *message = NULL; |
| 521 | char *last_message = NULL; |
| 522 | char temp_message[HUD_MESSAGE_LENGTH * 2]; |
| 523 | |
| 524 | va_start(args, format); |
| 525 | std::vsnprintf(temp_message, HUD_MESSAGE_LENGTH * 2, format, args); |
| 526 | va_end(args); |
| 527 | |
| 528 | if (Demo_flags == DF_RECORDING) { |
| 529 | DemoWriteHudMessage(color, false, temp_message); |
| 530 | } |
| 531 | |
| 532 | if (Dedicated_server) { |
| 533 | return AddLineToHUDMessages(temp_message, color); |
| 534 | } |
| 535 | |
| 536 | return AddMultipleLinesToHUDMessages(temp_message, color); |
| 537 | } |
| 538 | |
| 539 | // Adds a HUD message (similar to AddColoredHUDMessage), however can be filtered out by |
| 540 | // a "-playermessages" command line. |
| 541 | bool AddFilteredColoredHUDMessage(ddgr_color color, const char *format, ...) { |
| 542 | static int8_t checked_command_line = -1; |
| 543 | |
| 544 | if (checked_command_line == -1) { |
| 545 | if (FindArg("-playermessages") != 0) { |
| 546 | checked_command_line = 1; |
| 547 | } else { |
| 548 | checked_command_line = 0; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (Game_mode & GM_MULTI && checked_command_line) |
| 553 | return false; // filter this message |
| 554 | |
| 555 | std::va_list args; |
| 556 | char *message = NULL; |
| 557 | char *last_message = NULL; |
| 558 | char temp_message[HUD_MESSAGE_LENGTH * 2]; |
| 559 | |
| 560 | va_start(args, format); |
| 561 | std::vsnprintf(temp_message, HUD_MESSAGE_LENGTH * 2, format, args); |
| 562 | va_end(args); |
| 563 | |
| 564 | if (Demo_flags == DF_RECORDING) { |
| 565 | DemoWriteHudMessage(color, false, temp_message); |
| 566 | } |
| 567 | |
| 568 | if (Dedicated_server) { |
| 569 | return AddLineToHUDMessages(temp_message, color); |
| 570 | } |
| 571 | |
| 572 | return AddMultipleLinesToHUDMessages(temp_message, color); |
| 573 | } |
| 574 | |
| 575 | // Adds a HUD message (similar to AddHUDMessage), however can be filtered out by |
| 576 | // a "-playermessages" command line. |
| 577 | bool AddFilteredHUDMessage(const char *format, ...) { |
| 578 | static int8_t checked_command_line = -1; |
| 579 | |
| 580 | if (checked_command_line == -1) { |
| 581 | if (FindArg("-playermessages") != 0) { |
| 582 | checked_command_line = 1; |
| 583 | } else { |
| 584 | checked_command_line = 0; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | if (Game_mode & GM_MULTI && checked_command_line) |
| 589 | return false; // filter this message |
| 590 | |
| 591 | std::va_list args; |
| 592 | char *message = NULL; |
| 593 | char *last_message = NULL; |
| 594 | char temp_message[HUD_MESSAGE_LENGTH * 2]; |
| 595 | |
| 596 | va_start(args, format); |
| 597 | std::vsnprintf(temp_message, HUD_MESSAGE_LENGTH * 2, format, args); |
| 598 | va_end(args); |
| 599 | |
| 600 | if (Demo_flags == DF_RECORDING) { |
| 601 | DemoWriteHudMessage(0, false, temp_message); |
| 602 | } |
| 603 | |
| 604 | if (Dedicated_server) { |
| 605 | return AddLineToHUDMessages(temp_message); |
| 606 | } |
| 607 | |
| 608 | return AddMultipleLinesToHUDMessages(temp_message); |
| 609 | } |
| 610 | |
| 611 | // Adds a message to the HUD message list. If the list is already full, punt the |
| 612 | // top one and move the others up |
| 613 | // Returns true if message added, or false if message not (because the previous message was the same) |
| 614 | bool AddHUDMessage(const char *format, ...) { |
| 615 | std::va_list args; |
| 616 | char *message = NULL; |
| 617 | char *last_message = NULL; |
| 618 | char temp_message[HUD_MESSAGE_LENGTH * 2]; |
| 619 | |
| 620 | va_start(args, format); |
| 621 | std::vsnprintf(temp_message, HUD_MESSAGE_LENGTH * 2, format, args); |
| 622 | va_end(args); |
| 623 | |
| 624 | if (Demo_flags == DF_RECORDING) { |
| 625 | DemoWriteHudMessage(0, false, temp_message); |
| 626 | } |
| 627 | |
| 628 | if (Dedicated_server) { |
| 629 | return AddLineToHUDMessages(temp_message); |
| 630 | } |
| 631 | |
| 632 | return AddMultipleLinesToHUDMessages(temp_message); |
| 633 | } |
| 634 | |
| 635 | // Adds a blinking message to the HUD message list. If the list is already full, punt the |
| 636 | // top one and move the others up |
| 637 | // Returns true if message added, or false if message not (because the previous message was the same) |
| 638 | bool AddBlinkingHUDMessage(const char *format, ...) { |
| 639 | std::va_list args; |
| 640 | char *message = NULL; |
| 641 | char *last_message = NULL; |
| 642 | char temp_message[HUD_MESSAGE_LENGTH * 2]; |
| 643 | |
| 644 | va_start(args, format); |
| 645 | std::vsnprintf(temp_message, HUD_MESSAGE_LENGTH * 2, format, args); |
| 646 | va_end(args); |
| 647 | |
| 648 | if (Demo_flags == DF_RECORDING) { |
| 649 | DemoWriteHudMessage(0, true, temp_message); |
| 650 | } |
| 651 | |
| 652 | CorrectHudMessage(temp_message); |
| 653 | |
| 654 | if (Num_hud_messages > 0 && !strcmp(temp_message, HUD_messages[Num_hud_messages - 1])) |
| 655 | return 0; // this is the same message as before, don't print it twice! |
| 656 | |
| 657 | if (Num_hud_messages == MAX_HUD_MESSAGES) { |
| 658 | // Get rid of top message |
| 659 | |
| 660 | for (int i = 1; i < MAX_HUD_MESSAGES; i++) { |
| 661 | strcpy(HUD_messages[i - 1], HUD_messages[i]); |
| 662 | HUD_message_type[i - 1] = HUD_message_type[i]; |
| 663 | } |
| 664 | |
| 665 | Num_hud_messages--; |
| 666 | |
| 667 | Hud_scroll_offset = 0; |
| 668 | |
| 669 | if (!Hud_messages_paused) |
| 670 | Hud_timer = Gametime; |
| 671 | } |
| 672 | |
| 673 | if (Num_hud_messages == 0 && !Hud_messages_paused) |
| 674 | Hud_timer = Gametime; |
| 675 | |
| 676 | strcpy(HUD_messages[Num_hud_messages], temp_message); |
| 677 | HUD_message_type[Num_hud_messages] = HUD_MESSAGE_BLINKING; |
| 678 | |
| 679 | Num_hud_messages++; |
| 680 | |
| 681 | return 1; |
| 682 | } |
| 683 | |
| 684 | // This function takes a HUD messages, ensures it isn't too long, and if it is, it correctly fixes it |
| 685 | void CorrectHudMessage(char *str) { |
| 686 | // if (strlen(str)<=(HUD_MESSAGE_LENGTH-1)) |
| 687 | if (grtext_GetTextLineWidth(str) <= Game_window_w) |
| 688 | return; |
| 689 | |
| 690 | mprintf(0, "Message '%s' is too long!\n", str); |
| 691 | // the line is too long, we need to shorten it, but when we do, we need to make sure that it isn't in the middle |
| 692 | // of a color (0x01 0xFF 0xFF 0xFF) |
| 693 | str[HUD_MESSAGE_LENGTH - 1] = '\0'; |
| 694 | |
| 695 | // go back the last 3 characters and look for an 0x01, if we find one, than make it an '\0' to cut the string |
| 696 | for (int i = (HUD_MESSAGE_LENGTH - 2); i >= (HUD_MESSAGE_LENGTH - 4); i--) { |
| 697 | if (str[i] == 0x01) { |
| 698 | // we found a color start |
| 699 | str[i] = '\0'; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | // Called when the player hit the Multiplayer message key. |
| 705 | // Puts the player in input mode, or if already inputting resets the hud input message. |
| 706 | void StartHUDInputMessage() { |
| 707 | HudInputMessage[0] = 0; |
| 708 | Doing_input_message = HUD_MESSAGE_GENERAL; |
| 709 | HudInputMessageLen = 0; |
| 710 | SuspendControls(); |
| 711 | MultiSendRequestTypeIcon(true); |
| 712 | } |
| 713 | |
| 714 | // Called when the player hits the Multiplayer team-message key. |
| 715 | // Puts the player in input mode, or if already inputting resets the hud input message. |
| 716 | void StartTeamHUDInputMessage() { |
| 717 | // make sure that HUD_MESSAGE_TEAM is only set for team games |
| 718 | if (!Team_game) { |
| 719 | StartHUDInputMessage(); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | HudInputMessage[0] = 0; |
| 724 | Doing_input_message = HUD_MESSAGE_TEAM; |
| 725 | HudInputMessageLen = 0; |
| 726 | SuspendControls(); |
| 727 | } |
| 728 | |
| 729 | // Given a hud message, it will determine who should get this message. |
| 730 | // "name: message" = the player with the callsign name should get the message |
| 731 | // "team: message" = the Player_num's team should get the message |
| 732 | // "0-32: message" = the player with player num of 0-32 should get the message |
| 733 | // all other messages are for everyone else |
| 734 | // |
| 735 | // returns the starting position of the real message (past the :) |
| 736 | // destination will receive one of the following values: |
| 737 | // MULTI_SEND_MESSAGE_ALL = everyone should get this message |
| 738 | // MULTI_SEND_MESSAGE_RED_TEAM = only red team (0) should get this message |
| 739 | // MULTI_SEND_MESSAGE_BLUE_TEAM = only blue team (1) should get this message |
| 740 | // MULTI_SEND_MESSAGE_GREEN_TEAM = only green team (2) should get this message |
| 741 | // MULTI_SEND_MESSAGE_YELLOW_TEAM = only yellow team (3) should get this message |
| 742 | // 0-32 = player num of the player to get the message |
| 743 | const char *GetMessageDestination(const char *message, int *destination) { |
| 744 | int to_who = MULTI_SEND_MESSAGE_ALL; |
| 745 | const char *ret = message; |
| 746 | |
| 747 | // see if there is a colon in the string, and match that up with a name |
| 748 | const char *colon_pos = NULL; |
| 749 | colon_pos = strchr(message, ':'); |
| 750 | if (colon_pos > message) { |
| 751 | // this message might be for one person in particular |
| 752 | char buffer[256]; |
| 753 | memcpy(buffer, message, (colon_pos - message)); |
| 754 | buffer[colon_pos - message] = '\0'; |
| 755 | |
| 756 | // see if buffer is a number, in which case just send to that pnum |
| 757 | char *p = buffer; |
| 758 | bool is_num = true; |
| 759 | while (*p) { |
| 760 | if (!isdigit(*p)) { |
| 761 | is_num = false; |
| 762 | break; |
| 763 | } |
| 764 | p++; |
| 765 | } |
| 766 | |
| 767 | if (is_num) { |
| 768 | to_who = atoi(buffer); |
| 769 | if (to_who < 0 || to_who >= MAX_PLAYERS) |
| 770 | to_who = MULTI_SEND_MESSAGE_ALL; |
| 771 | } else { |
| 772 | int possible_match = -1; |
| 773 | int name_len = strlen(buffer); |
| 774 | int call_len; |
| 775 | |
| 776 | // it's not a number, see if it matches any of the player names |
| 777 | for (int pn = 0; pn < MAX_PLAYERS; pn++) { |
| 778 | if (NetPlayers[pn].flags & NPF_CONNECTED && NetPlayers[pn].sequence == NETSEQ_PLAYING) { |
| 779 | |
| 780 | call_len = strlen(Players[pn].callsign); |
| 781 | |
| 782 | if (call_len > name_len) { |
| 783 | // try to match a partial name |
| 784 | if (!strnicmp(Players[pn].callsign, buffer, name_len)) { |
| 785 | // possible match |
| 786 | possible_match = pn; |
| 787 | } |
| 788 | } else if (call_len == name_len) { |
| 789 | if (!stricmp(Players[pn].callsign, buffer)) { |
| 790 | // match! |
| 791 | to_who = pn; |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | } // end if |
| 796 | } // end for |
| 797 | |
| 798 | if (to_who < 0 && possible_match != -1) { |
| 799 | to_who = possible_match; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | if (to_who == MULTI_SEND_MESSAGE_ALL) { |
| 804 | // see if it is for the team |
| 805 | if (!stricmp(buffer, "team")) { |
| 806 | switch (Players[Player_num].team) { |
| 807 | case 0: |
| 808 | to_who = MULTI_SEND_MESSAGE_RED_TEAM; |
| 809 | break; |
| 810 | case 1: |
| 811 | to_who = MULTI_SEND_MESSAGE_BLUE_TEAM; |
| 812 | break; |
| 813 | case 2: |
| 814 | to_who = MULTI_SEND_MESSAGE_GREEN_TEAM; |
| 815 | break; |
| 816 | case 3: |
| 817 | to_who = MULTI_SEND_MESSAGE_YELLOW_TEAM; |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | ret = colon_pos + 1; |
| 824 | } |
| 825 | |
| 826 | *destination = to_who; |
| 827 | return ret; |
| 828 | } |
| 829 | |
| 830 | // Sends off the input message the player was typing |
| 831 | void SendOffHUDInputMessage() { |
| 832 | if (Doing_input_message == HUD_MESSAGE_NONE) |
| 833 | return; |
| 834 | |
| 835 | if (Game_mode & GM_MULTI) { |
| 836 | if (HudInputMessage[0] == '$') // special command |
| 837 | { |
| 838 | DLLInfo.input_string = HudInputMessage; |
| 839 | CallGameDLL(EVT_CLIENT_INPUT_STRING, &DLLInfo); |
| 840 | } else { |
| 841 | char str[255]; |
| 842 | snprintf(str, sizeof(str), TXT_HUDSAY, Players[Player_num].callsign, HudInputMessage); |
| 843 | |
| 844 | switch (Doing_input_message) { |
| 845 | case HUD_MESSAGE_GENERAL: { |
| 846 | int to_who; |
| 847 | |
| 848 | const char *colon_pos = GetMessageDestination(HudInputMessage, &to_who); |
| 849 | |
| 850 | if (to_who != MULTI_SEND_MESSAGE_ALL) { |
| 851 | if (to_who < 0) { |
| 852 | // to a team |
| 853 | snprintf(str, sizeof(str), "[%s]: %s", Players[Player_num].callsign, colon_pos); |
| 854 | } else { |
| 855 | // to a player |
| 856 | // cut out what's after the colon |
| 857 | snprintf(str, sizeof(str), "<%s>:%s", Players[Player_num].callsign, colon_pos); |
| 858 | AddHUDMessage("Sent private message to %s", Players[to_who].callsign); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | if (Netgame.local_role == LR_SERVER) |
| 863 | MultiSendMessageFromServer(GR_RGB(0, 128, 255), str, to_who); |
| 864 | else |
| 865 | MultiSendMessageToServer(0, str, to_who); |
| 866 | } break; |
| 867 | case HUD_MESSAGE_TEAM: { |
| 868 | int team = MULTI_SEND_MESSAGE_ALL; |
| 869 | switch (Players[Player_num].team) { |
| 870 | case 0: |
| 871 | team = MULTI_SEND_MESSAGE_RED_TEAM; |
| 872 | break; |
| 873 | case 1: |
| 874 | team = MULTI_SEND_MESSAGE_BLUE_TEAM; |
| 875 | break; |
| 876 | case 2: |
| 877 | team = MULTI_SEND_MESSAGE_GREEN_TEAM; |
| 878 | break; |
| 879 | case 3: |
| 880 | team = MULTI_SEND_MESSAGE_YELLOW_TEAM; |
| 881 | break; |
| 882 | } |
| 883 | |
| 884 | if (team != MULTI_SEND_MESSAGE_ALL) { |
| 885 | snprintf(str, sizeof(str), "[%s]: %s", Players[Player_num].callsign, HudInputMessage); |
| 886 | } |
| 887 | |
| 888 | if (Netgame.local_role == LR_SERVER) |
| 889 | MultiSendMessageFromServer(GR_RGB(0, 128, 255), str, team); |
| 890 | else |
| 891 | MultiSendMessageToServer(0, str, team); |
| 892 | } break; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | HudInputMessage[0] = 0; |
| 897 | HudInputMessageLen = 0; |
| 898 | |
| 899 | ddio_KeyFlush(); |
| 900 | ResumeControls(); |
| 901 | Doing_input_message = HUD_MESSAGE_NONE; |
| 902 | MultiSendRequestTypeIcon(false); |
| 903 | } |
| 904 | |
| 905 | // Breaks up the input message so that if we overflow it starts on the next line nicely |
| 906 | // Returns the number of characters in the next line |
| 907 | int BreakupHUDInputMessage(char *str) { |
| 908 | int cur = 0; |
| 909 | int last_space = -1; |
| 910 | int len = strlen(HudInputMessage); |
| 911 | |
| 912 | for (int i = 0; i < len; i++) { |
| 913 | if (HudInputMessage[i] == ' ') |
| 914 | last_space = i; |
| 915 | } |
| 916 | |
| 917 | if (last_space == -1) { |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | int leftover_len = len - last_space; |
| 922 | int leftover_size = leftover_len - 1; |
| 923 | if (leftover_len > 0) { |
| 924 | int to_who; |
| 925 | const char *colon_pos = GetMessageDestination(HudInputMessage, &to_who); |
| 926 | |
| 927 | if (to_who != MULTI_SEND_MESSAGE_ALL) { |
| 928 | char to_who_text[64]; |
| 929 | int to_who_size; |
| 930 | |
| 931 | to_who_size = colon_pos - HudInputMessage - 1; |
| 932 | strncpy(to_who_text, HudInputMessage, to_who_size); |
| 933 | to_who_text[to_who_size] = '\0'; |
| 934 | |
| 935 | sprintf(str, "%s:%s", to_who_text, &HudInputMessage[last_space + 1]); |
| 936 | leftover_size = strlen(str); |
| 937 | } else { |
| 938 | strcpy(str, &HudInputMessage[last_space + 1]); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | HudInputMessage[last_space] = 0; |
| 943 | |
| 944 | return leftover_size; |
| 945 | } |
| 946 | |
| 947 | // Handles all incoming keys for an inputted hud message |
| 948 | void DoHUDInputMessageKey(int key) { |
| 949 | if (key == KEY_F8) { |
| 950 | StartHUDInputMessage(); |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | if (Doing_input_message == HUD_MESSAGE_NONE) |
| 955 | return; |
| 956 | |
| 957 | // ddio_KeyFlush(); // took this out. screwed up shift-keys. don't need it anyway. |
| 958 | |
| 959 | if (key == KEY_ESC) { |
| 960 | Doing_input_message = HUD_MESSAGE_NONE; |
| 961 | MultiSendRequestTypeIcon(false); |
| 962 | Marker_message = 0; |
| 963 | HudInputMessage[0] = 0; |
| 964 | HudInputMessageLen = 0; |
| 965 | ResumeControls(); |
| 966 | } else if (key == KEY_BACKSP || key == KEY_DELETE) { |
| 967 | if (HudInputMessageLen > 0) |
| 968 | HudInputMessageLen--; |
| 969 | |
| 970 | HudInputMessage[HudInputMessageLen] = 0; |
| 971 | } else if (key == KEY_ENTER) { |
| 972 | if (Marker_message) { |
| 973 | DropMarker(HudInputMessage); |
| 974 | Doing_input_message = HUD_MESSAGE_NONE; |
| 975 | MultiSendRequestTypeIcon(false); |
| 976 | Marker_message = 0; |
| 977 | HudInputMessage[0] = 0; |
| 978 | HudInputMessageLen = 0; |
| 979 | ResumeControls(); |
| 980 | } else |
| 981 | SendOffHUDInputMessage(); |
| 982 | } else { |
| 983 | int ascii = ddio_KeyToAscii(key); |
| 984 | if (isdigit(ascii) || isalpha(ascii) || ispunct(ascii) || isspace(ascii)) { |
| 985 | if (Marker_message) { |
| 986 | if (HudInputMessageLen >= (MAX_MARKER_MESSAGE_LENGTH - 2)) |
| 987 | return; |
| 988 | } |
| 989 | |
| 990 | HudInputMessage[HudInputMessageLen++] = ascii; |
| 991 | HudInputMessage[HudInputMessageLen] = 0; |
| 992 | |
| 993 | if (!Marker_message && HudInputMessageLen >= (MAX_HUD_INPUT_LEN - 1)) { |
| 994 | // Break up our outgoing message onto the next line if need be |
| 995 | char str[MAX_HUD_INPUT_LEN]; |
| 996 | int str_len; |
| 997 | int old_style = Doing_input_message; |
| 998 | |
| 999 | str_len = BreakupHUDInputMessage(str); |
| 1000 | SendOffHUDInputMessage(); |
| 1001 | |
| 1002 | switch (old_style) { |
| 1003 | case HUD_MESSAGE_TEAM: |
| 1004 | StartTeamHUDInputMessage(); |
| 1005 | break; |
| 1006 | case HUD_MESSAGE_GENERAL: |
| 1007 | StartHUDInputMessage(); |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | if (str_len > 0) { |
| 1012 | strcpy(HudInputMessage, str); |
| 1013 | HudInputMessageLen = str_len; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | // Simply renders the hud input message if there is one |
| 1021 | void RenderHUDInputMessage() { |
| 1022 | char message[255]; |
| 1023 | int y, x, lw; |
| 1024 | |
| 1025 | ddgr_color text_color = HUD_COLOR; |
| 1026 | grtext_SetColor(text_color); |
| 1027 | |
| 1028 | if (Doing_input_message == HUD_MESSAGE_TEAM) { |
| 1029 | snprintf(message, sizeof(message), TXT_HUD_TEAMSAY, HudInputMessage); |
| 1030 | } else { |
| 1031 | if (Marker_message) |
| 1032 | snprintf(message, sizeof(message), TXT_HUD_MARKER, HudInputMessage); |
| 1033 | else |
| 1034 | snprintf(message, sizeof(message), TXT_MESSAGE, HudInputMessage); |
| 1035 | } |
| 1036 | |
| 1037 | grtext_SetAlpha(HUD_ALPHA); |
| 1038 | grtext_SetFlags(0); |
| 1039 | |
| 1040 | // RenderHUDTextNoFormatFlags(HUDTEXT_CENTERED, HUD_COLOR, HUD_ALPHA, 1, 0, y, message); |
| 1041 | lw = grtext_GetTextLineWidth(message); |
| 1042 | x = (Game_window_w - lw) / 2; |
| 1043 | if (Small_hud_flag) { |
| 1044 | y = Max_window_h - (int)(40 * ((float)Max_window_h / (float)DEFAULT_HUD_HEIGHT)); |
| 1045 | HUD_inmsg_dirty_rect.set(x, y, x + lw, y + grfont_GetHeight(HUD_FONT)); |
| 1046 | } else { |
| 1047 | y = (Game_window_h * 3 / 5) + Game_window_y; |
| 1048 | } |
| 1049 | grtext_Puts(x, y, message); |
| 1050 | } |
| 1051 | |
| 1052 | int testcolor = 200; |
| 1053 | |
| 1054 | float Hud_messages_paused_saved_Gametime; |
| 1055 | |
| 1056 | void HUDPauseMessages(void) { |
| 1057 | if (Hud_messages_paused) |
| 1058 | return; |
| 1059 | |
| 1060 | Hud_messages_paused = true; |
| 1061 | Hud_messages_paused_saved_Gametime = Gametime; |
| 1062 | } |
| 1063 | |
| 1064 | void HUDUnpauseMessages(void) { |
| 1065 | if (!Hud_messages_paused) |
| 1066 | return; |
| 1067 | |
| 1068 | Hud_messages_paused = false; |
| 1069 | float time_paused = Gametime - Hud_messages_paused_saved_Gametime; |
| 1070 | |
| 1071 | Hud_timer += time_paused; |
| 1072 | } |
| 1073 | |
| 1074 | // Renders all the messages we have in the message list. If HUD_MESSAGE_TIME has |
| 1075 | // elapsed, then punt the oldest message and move the others up one |
| 1076 | void RenderScrollingHUDMessages() { |
| 1077 | if (Hud_messages_paused) |
| 1078 | return; |
| 1079 | |
| 1080 | int shade; |
| 1081 | int text_height; |
| 1082 | int16_t l, t, r, b; |
| 1083 | int i; |
| 1084 | |
| 1085 | // Check for wraps |
| 1086 | if (Gametime < Hud_timer) |
| 1087 | Hud_timer = Gametime; |
| 1088 | |
| 1089 | grtext_SetFont(HUD_FONT); |
| 1090 | text_height = grfont_GetHeight(HUD_FONT); |
| 1091 | |
| 1092 | text_height += (3 * Hud_aspect_y); // add this for a spacer |
| 1093 | |
| 1094 | if (Doing_input_message > HUD_MESSAGE_NONE) |
| 1095 | RenderHUDInputMessage(); |
| 1096 | |
| 1097 | if (Num_hud_messages == 0) |
| 1098 | return; |
| 1099 | |
| 1100 | l = (Small_hud_flag) ? (Max_window_w / 2) : (Game_window_w / 2); |
| 1101 | r = l; |
| 1102 | t = 0; |
| 1103 | b = 0; |
| 1104 | for (i = 0; i < Num_hud_messages; i++) { |
| 1105 | int x; |
| 1106 | |
| 1107 | if (HUD_message_type[i] == HUD_MESSAGE_BLINKING) { |
| 1108 | float blink_time = Gametime; |
| 1109 | int iblink = blink_time; |
| 1110 | float frac_blink = blink_time - iblink; |
| 1111 | |
| 1112 | float answer = frac_blink / .5; |
| 1113 | |
| 1114 | iblink = answer; |
| 1115 | |
| 1116 | if (iblink == 1 || iblink == 3) |
| 1117 | continue; |
| 1118 | } |
| 1119 | |
| 1120 | if (i == 0 && Hud_scroll_offset != 0) { |
| 1121 | int scolor = text_height + Hud_scroll_offset; |
| 1122 | shade = (255 * scolor) / text_height; |
| 1123 | } else |
| 1124 | shade = 255; |
| 1125 | |
| 1126 | char *message = HUD_messages[i]; |
| 1127 | int text_width = grtext_GetTextLineWidth(message); |
| 1128 | int vp_width = Game_window_w; |
| 1129 | int y = (i * text_height) + Hud_scroll_offset; |
| 1130 | |
| 1131 | if (HUD_message_type[i] == HUD_MESSAGE_BLINKING) { |
| 1132 | ddgr_color text_color; |
| 1133 | text_color = (GR_RGB(shade, 0, 0)); |
| 1134 | grtext_SetColor(text_color); |
| 1135 | } else { |
| 1136 | ddgr_color text_color; |
| 1137 | text_color = HUD_message_color[i]; |
| 1138 | grtext_SetColor(text_color); |
| 1139 | } |
| 1140 | |
| 1141 | grtext_SetAlpha(HUD_ALPHA); |
| 1142 | grtext_SetFlags(0); |
| 1143 | |
| 1144 | if (Small_hud_flag) { |
| 1145 | x = (Max_window_w - text_width) / 2; |
| 1146 | if (x < l) |
| 1147 | l = x; |
| 1148 | if ((x + text_width) > r) |
| 1149 | r = x + text_width; |
| 1150 | b += text_height; |
| 1151 | } else { |
| 1152 | x = (Game_window_w - text_width) / 2; |
| 1153 | } |
| 1154 | grtext_Puts(x, y, message); |
| 1155 | } |
| 1156 | if (Small_hud_flag && (i > 0)) { |
| 1157 | HUD_msg_dirty_rect.set(l, t, r, b); |
| 1158 | } |
| 1159 | |
| 1160 | // Now see if the topmost message is old |
| 1161 | |
| 1162 | float new_time = Gametime; |
| 1163 | |
| 1164 | if ((new_time - Hud_timer) > HUD_MESSAGE_TIME) { |
| 1165 | for (int i = 1; i < MAX_HUD_MESSAGES; i++) { |
| 1166 | strcpy(HUD_messages[i - 1], HUD_messages[i]); |
| 1167 | HUD_message_type[i - 1] = HUD_message_type[i]; |
| 1168 | HUD_message_color[i - 1] = HUD_message_color[i]; |
| 1169 | } |
| 1170 | |
| 1171 | Num_hud_messages--; |
| 1172 | Hud_scroll_offset = 0; |
| 1173 | Hud_timer = new_time; |
| 1174 | |
| 1175 | } else if ((new_time - Hud_timer) > (HUD_MESSAGE_TIME - HUD_SCROLL_TIME)) { |
| 1176 | // scroll |
| 1177 | |
| 1178 | float cur_scroll_time = HUD_MESSAGE_TIME - HUD_SCROLL_TIME; |
| 1179 | float cur_hud_time = (new_time - Hud_timer) - cur_scroll_time; |
| 1180 | float fscroll; |
| 1181 | |
| 1182 | cur_hud_time /= HUD_SCROLL_TIME; |
| 1183 | // cur_hud_time is now normalized from 0 to 1 |
| 1184 | // 0 represents not scrolled, 1 represents a total scroll |
| 1185 | |
| 1186 | ASSERT(cur_hud_time >= 0 && cur_hud_time <= 1); |
| 1187 | |
| 1188 | fscroll = text_height * cur_hud_time; |
| 1189 | |
| 1190 | Hud_scroll_offset = -fscroll; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | #define FADEOUT_TIME 0.5f // fades out over this amount of time |
| 1195 | #define CHAR_DELAY 0.03f // delay between character |
| 1196 | |
| 1197 | // Renders all the messages we have in the message list. If HUD_MESSAGE_TIME has |
| 1198 | // elapsed, then punt the oldest message and move the others up one |
| 1199 | void RenderHUDMessages() { |
| 1200 | if (Hud_messages_paused) |
| 1201 | return; |
| 1202 | |
| 1203 | // Render & update the scrolling message list |
| 1204 | RenderScrollingHUDMessages(); |
| 1205 | |
| 1206 | // do persistent hud message system |
| 1207 | if (Hud_persistent_msg_id != HUD_INVALID_ID) { |
| 1208 | if (Hud_persistent_msg_timer != HUD_MSG_PERSISTENT_INFINITE) { |
| 1209 | tHUDItem *item = GetHUDItem(Hud_persistent_msg_id); |
| 1210 | |
| 1211 | // Update time |
| 1212 | Hud_persistent_msg_timer -= Frametime; |
| 1213 | |
| 1214 | // Do fadeout |
| 1215 | if (Hud_persistent_msg_flags & HPF_FADEOUT) { |
| 1216 | if (Hud_persistent_msg_timer < FADEOUT_TIME) { |
| 1217 | item->alpha = HUD_ALPHA * Hud_persistent_msg_timer / FADEOUT_TIME; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | if (item && Small_hud_flag) { |
| 1222 | grtext_SetFont(HUD_FONT); |
| 1223 | int16_t l = item->x * Max_window_w / DEFAULT_HUD_WIDTH; |
| 1224 | int16_t t = item->y * Max_window_h / DEFAULT_HUD_HEIGHT; |
| 1225 | int16_t r = l + grtext_GetTextLineWidth(item->data.text) + 10; |
| 1226 | int16_t b = t + grtext_GetTextHeight(item->data.text); |
| 1227 | item->dirty_rect.set(l, t, r, b); |
| 1228 | } |
| 1229 | |
| 1230 | // Do FreeSpace printing effect |
| 1231 | if (Hud_persistent_msg_id2 != HUD_INVALID_ID) { |
| 1232 | Hud_persistent_msg_char_timer -= Frametime; |
| 1233 | while (Hud_persistent_msg_char_timer < 0) { |
| 1234 | tHUDItem *item = GetHUDItem(Hud_persistent_msg_id); |
| 1235 | tHUDItem *item2 = GetHUDItem(Hud_persistent_msg_id2); |
| 1236 | |
| 1237 | item->data.text[Hud_persistent_msg_current_len] = item2->data.text[0]; |
| 1238 | Hud_persistent_msg_current_len++; |
| 1239 | |
| 1240 | // Check for more chars |
| 1241 | if (!item->data.text[Hud_persistent_msg_current_len]) { |
| 1242 | FreeHUDItem(Hud_persistent_msg_id2); |
| 1243 | Hud_persistent_msg_id2 = HUD_INVALID_ID; |
| 1244 | Sound_system.StopSoundLooping(Hud_persistent_msg_sound_handle); |
| 1245 | Hud_persistent_msg_sound_handle = SOUND_NONE_INDEX; |
| 1246 | break; // stop printing chars |
| 1247 | } else { // dp next char |
| 1248 | item2->data.text[0] = item->data.text[Hud_persistent_msg_current_len]; |
| 1249 | item->data.text[Hud_persistent_msg_current_len] = 0; |
| 1250 | item2->x = item->x + RenderHUDGetTextLineWidth(item->data.text); |
| 1251 | Hud_persistent_msg_char_timer += CHAR_DELAY; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | // If time out, clear message |
| 1257 | if (Hud_persistent_msg_timer <= 0.0f) { |
| 1258 | void ClearPersistentHUDMessage(); |
| 1259 | ClearPersistentHUDMessage(); |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | void RenderHUDMsgDirtyRects() { |
| 1266 | // clean hud message dirty rects |
| 1267 | HUD_msg_dirty_rect.fill(GR_BLACK); |
| 1268 | |
| 1269 | // clean input message dirty rects |
| 1270 | HUD_inmsg_dirty_rect.fill(GR_BLACK); |
| 1271 | |
| 1272 | // clean remnant persistant messages |
| 1273 | HUD_persist_dirty_rect[0].fill(GR_BLACK); |
| 1274 | HUD_persist_dirty_rect[1].fill(GR_BLACK); |
| 1275 | } |
| 1276 | |
| 1277 | // reset hud messages. |
| 1278 | void ResetHUDMessages() { |
| 1279 | Hud_timer = 0.0f; |
| 1280 | Num_hud_messages = 0; |
| 1281 | Hud_scroll_offset = 0; |
| 1282 | ResetPersistentHUDMessage(); |
| 1283 | HUDUnpauseMessages(); |
| 1284 | |
| 1285 | HUD_inmsg_dirty_rect.reset(); |
| 1286 | HUD_msg_dirty_rect.reset(); |
| 1287 | HUD_persist_dirty_rect[0].reset(); |
| 1288 | HUD_persist_dirty_rect[1].reset(); |
| 1289 | |
| 1290 | void ResetHUDLevelItems(); |
| 1291 | ResetHUDLevelItems(); |
| 1292 | } |
| 1293 | |
| 1294 | struct phud_message { |
| 1295 | char message[HUD_MESSAGE_LENGTH * 2]; |
| 1296 | ddgr_color color; |
| 1297 | int x, y; |
| 1298 | float time; |
| 1299 | int flags; |
| 1300 | int sound_index; |
| 1301 | }; |
| 1302 | |
| 1303 | #define PHUD_QUEUE_SIZE 3 |
| 1304 | |
| 1305 | phud_message PHUD_message_queue[PHUD_QUEUE_SIZE]; |
| 1306 | |
| 1307 | int Num_queued_PHUD_messages = 0; |
| 1308 | |
| 1309 | // adds a message to the persistant message queue |
| 1310 | void QueuePersistentHUDMessage(ddgr_color color, int x, int y, float time, int flags, int sound_index, char *message) { |
| 1311 | if (Num_queued_PHUD_messages == PHUD_QUEUE_SIZE) |
| 1312 | return; |
| 1313 | |
| 1314 | phud_message *pp = &PHUD_message_queue[Num_queued_PHUD_messages++]; |
| 1315 | |
| 1316 | pp->color = color; |
| 1317 | pp->x = x; |
| 1318 | pp->y = y; |
| 1319 | pp->time = time; |
| 1320 | pp->flags = flags; |
| 1321 | pp->sound_index = sound_index; |
| 1322 | strcpy(pp->message, message); |
| 1323 | } |
| 1324 | |
| 1325 | // Start playing a persistent HUD message |
| 1326 | void StartPersistentHUDMessage(ddgr_color color, int x, int y, float time, int flags, int sound_index, char *message) { |
| 1327 | if (Demo_flags == DF_RECORDING) { |
| 1328 | DemoWritePersistantHUDMessage(color, x, y, time, flags, sound_index, message); |
| 1329 | } |
| 1330 | |
| 1331 | // set x and y if special formatting. |
| 1332 | grtext_SetFont(HUD_FONT); |
| 1333 | if (x == HUD_MSG_PERSISTENT_CENTER) { |
| 1334 | x = Game_window_x + (Game_window_w - grtext_GetLineWidth(message)) / 2; |
| 1335 | } |
| 1336 | if (y == HUD_MSG_PERSISTENT_CENTER) { |
| 1337 | y = Game_window_y + (Game_window_h - grfont_GetHeight(HUD_FONT)) / 2; |
| 1338 | } |
| 1339 | |
| 1340 | // add custom text item |
| 1341 | tHUDItem huditem; |
| 1342 | |
| 1343 | memset(&huditem, 0, sizeof(huditem)); |
| 1344 | |
| 1345 | huditem.alpha = HUD_ALPHA; |
| 1346 | huditem.color = color; |
| 1347 | huditem.type = HUD_ITEM_CUSTOMTEXT; |
| 1348 | huditem.x = x * DEFAULT_HUD_WIDTH / Max_window_w; |
| 1349 | huditem.y = y * DEFAULT_HUD_HEIGHT / Max_window_h; |
| 1350 | huditem.data.text = message; |
| 1351 | huditem.stat = STAT_MESSAGES; |
| 1352 | huditem.flags = HUD_FLAG_PERSISTANT | HUD_FLAG_SMALL; // when hud is shrunk, this will draw outside of normal view |
| 1353 | |
| 1354 | AddHUDItem(&huditem); |
| 1355 | |
| 1356 | Hud_persistent_msg_id = huditem.id; |
| 1357 | Hud_persistent_msg_timer = time; |
| 1358 | Hud_persistent_msg_flags = flags; |
| 1359 | |
| 1360 | // Set up for FreeSpace-style effect |
| 1361 | if (flags & HPF_FREESPACE_DRAW) { |
| 1362 | |
| 1363 | // Create additional hud item for leading character |
| 1364 | static char single_char[2] = { 'a', '\0' }; ; // single-character string |
| 1365 | huditem.alpha = 255; |
| 1366 | huditem.data.text = single_char; |
| 1367 | huditem.saturation_count = 2; |
| 1368 | AddHUDItem(&huditem); |
| 1369 | Hud_persistent_msg_id2 = huditem.id; |
| 1370 | |
| 1371 | tHUDItem *item = GetHUDItem(Hud_persistent_msg_id); |
| 1372 | tHUDItem *item2 = GetHUDItem(Hud_persistent_msg_id2); |
| 1373 | |
| 1374 | item2->data.text[0] = item->data.text[0]; |
| 1375 | item->data.text[0] = 0; |
| 1376 | |
| 1377 | Hud_persistent_msg_current_len = 0; |
| 1378 | Hud_persistent_msg_char_timer = CHAR_DELAY; |
| 1379 | |
| 1380 | if (sound_index != SOUND_NONE_INDEX) |
| 1381 | Hud_persistent_msg_sound_handle = Sound_system.Play2dSound(sound_index); |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | // adds a persistent hud message that is timed, or infinite until removed |
| 1386 | // for infinite, time = -1.0f |
| 1387 | // for centering on an axis, set either x or y to -1. |
| 1388 | void AddPersistentHUDMessage(ddgr_color color, int x, int y, float time, int flags, int sound_index, const char *fmt, |
| 1389 | ...) { |
| 1390 | std::va_list args; |
| 1391 | char temp_message[HUD_MESSAGE_LENGTH * 2]; |
| 1392 | |
| 1393 | // start new message |
| 1394 | va_start(args, fmt); |
| 1395 | std::vsnprintf(temp_message, HUD_MESSAGE_LENGTH * 2, fmt, args); |
| 1396 | va_end(args); |
| 1397 | |
| 1398 | if (Hud_persistent_msg_id != HUD_INVALID_ID) // already one active, so queue the new one |
| 1399 | QueuePersistentHUDMessage(color, x, y, time, flags, sound_index, temp_message); |
| 1400 | else |
| 1401 | StartPersistentHUDMessage(color, x, y, time, flags, sound_index, temp_message); |
| 1402 | } |
| 1403 | |
| 1404 | // Clears the current message, and plays the next in the queue |
| 1405 | void ClearPersistentHUDMessage() { |
| 1406 | // free persistent message currently visible |
| 1407 | if (Hud_persistent_msg_id != HUD_INVALID_ID) { |
| 1408 | tHUDItem *item = GetHUDItem(Hud_persistent_msg_id); |
| 1409 | if (item) { |
| 1410 | t_dirty_rect *r = &item->dirty_rect; |
| 1411 | HUD_persist_dirty_rect[0].set(r->r[0].l, r->r[0].t, r->r[0].r, r->r[0].b); |
| 1412 | } |
| 1413 | FreeHUDItem(Hud_persistent_msg_id); |
| 1414 | Hud_persistent_msg_id = HUD_INVALID_ID; |
| 1415 | } |
| 1416 | if (Hud_persistent_msg_id2 != HUD_INVALID_ID) { |
| 1417 | tHUDItem *item = GetHUDItem(Hud_persistent_msg_id2); |
| 1418 | if (item) { |
| 1419 | t_dirty_rect *r = &item->dirty_rect; |
| 1420 | HUD_persist_dirty_rect[1].set(r->r[0].l, r->r[0].t, r->r[0].r, r->r[0].b); |
| 1421 | } |
| 1422 | FreeHUDItem(Hud_persistent_msg_id2); |
| 1423 | Hud_persistent_msg_id2 = HUD_INVALID_ID; |
| 1424 | } |
| 1425 | |
| 1426 | if (Hud_persistent_msg_sound_handle != SOUND_NONE_INDEX) { |
| 1427 | Sound_system.StopSoundLooping(Hud_persistent_msg_sound_handle); |
| 1428 | Hud_persistent_msg_sound_handle = SOUND_NONE_INDEX; |
| 1429 | } |
| 1430 | |
| 1431 | // Check for queued message |
| 1432 | if (Num_queued_PHUD_messages) { |
| 1433 | phud_message *pp = &PHUD_message_queue[0]; |
| 1434 | StartPersistentHUDMessage(pp->color, pp->x, pp->y, pp->time, pp->flags, pp->sound_index, pp->message); |
| 1435 | *pp = PHUD_message_queue[1]; |
| 1436 | Num_queued_PHUD_messages--; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | // Clears all the persistent hud messages |
| 1441 | void ResetPersistentHUDMessage() { |
| 1442 | Num_queued_PHUD_messages = 0; |
| 1443 | ClearPersistentHUDMessage(); |
| 1444 | } |
| 1445 | |
| 1446 | ///////////////////////////////////////////////////////////////////////// |
| 1447 | // HUD Message System |
| 1448 | ///////////////////////////////////////////////////////////////////////// |
| 1449 | |
| 1450 | // opens a hud rollback console. |
| 1451 | void OpenHUDMessageConsole() { |
| 1452 | if (Game_msg_con_vis) { |
| 1453 | CloseGameMessageConsole(); |
| 1454 | } |
| 1455 | |
| 1456 | HUD_msg_con_vis = HUD_msg_con.Open(TXT_HUDMSGPOPUP_TITLE, GAME_MSGCON_X, GAME_MSGCON_Y, GAME_MSGCON_W, GAME_MSGCON_H); |
| 1457 | } |
| 1458 | |
| 1459 | // closes hud rollback console |
| 1460 | void CloseHUDMessageConsole() { |
| 1461 | if (HUD_msg_con_vis) { |
| 1462 | HUD_msg_con.Close(); |
| 1463 | HUD_msg_con_vis = false; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | // toggles hud rollback console |
| 1468 | void ToggleHUDMessageConsole() { |
| 1469 | if (HUD_msg_con_vis) |
| 1470 | CloseHUDMessageConsole(); |
| 1471 | else |
| 1472 | OpenHUDMessageConsole(); |
| 1473 | } |
| 1474 | |
| 1475 | ///////////////////////////////////////////////////////////////////////// |
| 1476 | // Game Message System |
| 1477 | ///////////////////////////////////////////////////////////////////////// |
| 1478 | // resets game message list to no messages |
| 1479 | void ResetGameMessages() { |
| 1480 | Game_msg_list.reset(); |
| 1481 | Game_msg_con.AttachMsgList(&Game_msg_list); |
| 1482 | HUD_msg_list.reset(); |
| 1483 | HUD_msg_con.AttachMsgList(&HUD_msg_list); |
| 1484 | } |
| 1485 | |
| 1486 | void AddGameMessage(const char *msg) { |
| 1487 | int secs = (int)Gametime; |
| 1488 | |
| 1489 | Game_msg_list.add(msg, (uint8_t)Current_mission.cur_level, (secs / 3600), (secs / 60), secs % 60); |
| 1490 | } |
| 1491 | |
| 1492 | void SGSGameMessages(CFILE *fp) { |
| 1493 | int i = 0; |
| 1494 | const char *msg; |
| 1495 | |
| 1496 | cf_WriteShort(fp, (int16_t)Game_msg_list.m_nmsg); |
| 1497 | |
| 1498 | while ((msg = Game_msg_list.get(i++)) != NULL) { |
| 1499 | cf_WriteString(fp, msg); |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | void LGSGameMessages(CFILE *fp) { |
| 1504 | int n; |
| 1505 | char buf[512]; // some level designers can be nuts. |
| 1506 | |
| 1507 | n = (int)cf_ReadShort(fp); |
| 1508 | |
| 1509 | Game_msg_list.reset(); |
| 1510 | while (n) { |
| 1511 | n--; |
| 1512 | cf_ReadString(buf, sizeof(buf), fp); |
| 1513 | buf[511] = 0; |
| 1514 | Game_msg_list.add(buf); |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | // toggles game message console |
| 1519 | void ToggleGameMessageConsole() { |
| 1520 | if (Game_msg_con_vis) |
| 1521 | CloseGameMessageConsole(); |
| 1522 | else |
| 1523 | OpenGameMessageConsole(); |
| 1524 | } |
| 1525 | |
| 1526 | // game message console |
| 1527 | void OpenGameMessageConsole() { |
| 1528 | if (HUD_msg_con_vis) { |
| 1529 | CloseHUDMessageConsole(); |
| 1530 | } |
| 1531 | Game_msg_con_vis = |
| 1532 | Game_msg_con.Open(TXT_HUD_GAMEMESSAGES, GAME_MSGCON_X, GAME_MSGCON_Y, GAME_MSGCON_W, GAME_MSGCON_H); |
| 1533 | } |
| 1534 | |
| 1535 | void CloseGameMessageConsole() { |
| 1536 | Game_msg_con.Close(); |
| 1537 | Game_msg_con_vis = false; |
| 1538 | } |
| 1539 | |
| 1540 | ////////////////////////////////////////////////////////////////////////////// |
| 1541 | |
| 1542 | #define MSGL_BORDER_THICKNESS 4 |
| 1543 | |
| 1544 | tMsgList::tMsgList() { |
| 1545 | m_nmsg = 0; |
| 1546 | m_limit = 64; |
| 1547 | m_msg = NULL; |
| 1548 | } |
| 1549 | |
| 1550 | bool tMsgList::add(const char *msg, uint8_t lvl, uint8_t hr, uint8_t min, uint8_t sec) { |
| 1551 | char buf[2048]; |
| 1552 | |
| 1553 | if (!m_limit) |
| 1554 | m_limit = 64; |
| 1555 | |
| 1556 | if (m_msg == NULL) { |
| 1557 | m_msg = (char **)mem_malloc(sizeof(char *) * m_limit); |
| 1558 | m_nmsg = 0; |
| 1559 | } |
| 1560 | |
| 1561 | if (m_nmsg == m_limit && m_nmsg > 0) { |
| 1562 | int i; |
| 1563 | mem_free(m_msg[0]); |
| 1564 | for (i = 1; i < m_nmsg; i++) |
| 1565 | m_msg[i - 1] = m_msg[i]; |
| 1566 | m_nmsg--; |
| 1567 | } |
| 1568 | |
| 1569 | if (lvl > 0) { |
| 1570 | snprintf(buf, sizeof(buf), "[%d.%d.%d.%d] %s", lvl, hr, min, sec, msg); |
| 1571 | m_msg[m_nmsg++] = mem_strdup(buf); |
| 1572 | } else { |
| 1573 | m_msg[m_nmsg++] = mem_strdup(msg); |
| 1574 | } |
| 1575 | |
| 1576 | return true; |
| 1577 | } |
| 1578 | |
| 1579 | void tMsgList::reset() { |
| 1580 | if (m_msg) { |
| 1581 | while (m_nmsg) { |
| 1582 | m_nmsg--; |
| 1583 | if (m_msg[m_nmsg]) { |
| 1584 | mem_free(m_msg[m_nmsg]); |
| 1585 | } |
| 1586 | } |
| 1587 | mem_free(m_msg); |
| 1588 | m_msg = NULL; |
| 1589 | } |
| 1590 | m_nmsg = 0; |
| 1591 | } |
| 1592 | |
| 1593 | const char *tMsgList::get(int i) { |
| 1594 | if (m_msg) { |
| 1595 | if (i < m_nmsg) { |
| 1596 | return m_msg[i]; |
| 1597 | } |
| 1598 | } |
| 1599 | return NULL; |
| 1600 | } |
| 1601 | |
| 1602 | ////////////////////////////////////////////////////////////////////////////// |
| 1603 | |
| 1604 | MsgListConsole::MsgListConsole() { |
| 1605 | m_opened = false; |
| 1606 | m_list = NULL; |
| 1607 | m_buffer = NULL; |
| 1608 | m_bufline = 0; |
| 1609 | m_conlines = NULL; |
| 1610 | n_conlines = 0; |
| 1611 | } |
| 1612 | |
| 1613 | MsgListConsole::~MsgListConsole() {} |
| 1614 | |
| 1615 | // registers dimensions of box |
| 1616 | bool MsgListConsole::Open(const char *title, int x, int y, int w, int h) { |
| 1617 | m_x = x; |
| 1618 | m_y = y; |
| 1619 | m_w = w; |
| 1620 | m_h = h; |
| 1621 | |
| 1622 | if (!m_list) |
| 1623 | return false; |
| 1624 | |
| 1625 | // determine text buffer dimensions |
| 1626 | m_buflen = 2048; |
| 1627 | |
| 1628 | redo_copy: |
| 1629 | m_buffer = (char *)mem_malloc(m_buflen); |
| 1630 | if (m_buffer) { |
| 1631 | m_buffer[0] = 0; |
| 1632 | m_opened = true; |
| 1633 | } else { |
| 1634 | m_opened = false; |
| 1635 | return false; |
| 1636 | } |
| 1637 | |
| 1638 | // generate text buffer |
| 1639 | int i, len, j; |
| 1640 | char *bufptr = m_buffer; |
| 1641 | |
| 1642 | for (i = 0; i < m_list->m_nmsg; i++) { |
| 1643 | const char *msg = m_list->m_msg[i]; |
| 1644 | if ((strlen(m_buffer) + strlen(msg) + 2) >= (unsigned)(m_buflen)) { |
| 1645 | // reallocate buffer |
| 1646 | mem_free(m_buffer); |
| 1647 | m_buflen = m_buflen * 2; |
| 1648 | goto redo_copy; |
| 1649 | } |
| 1650 | |
| 1651 | textaux_WordWrap(msg, bufptr, m_w - MSGL_BORDER_THICKNESS * 2, HUD_FONT); |
| 1652 | bufptr += strlen(bufptr); |
| 1653 | |
| 1654 | // append newline character if there are more messages. |
| 1655 | if (i < (m_list->m_nmsg - 1)) { |
| 1656 | *bufptr = '\n'; |
| 1657 | bufptr++; |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | // allocate console line pointers |
| 1662 | int c = 0; |
| 1663 | int y2 = grfont_GetHeight(HUD_FONT) + MSGL_BORDER_THICKNESS + 3; |
| 1664 | while (y2 < (m_h - MSGL_BORDER_THICKNESS - grfont_GetHeight(HUD_FONT))) { |
| 1665 | y2 += (grfont_GetHeight(HUD_FONT) + 1); |
| 1666 | c++; |
| 1667 | } |
| 1668 | |
| 1669 | ASSERT(c > 0); |
| 1670 | m_conlines = (char **)mem_malloc(sizeof(char *) * c); |
| 1671 | n_conlines = c; |
| 1672 | memset(m_conlines, 0, sizeof(char *) * c); |
| 1673 | |
| 1674 | // get pointer to first visible line. |
| 1675 | c = 0; |
| 1676 | m_bufline = 0; |
| 1677 | len = strlen(m_buffer); |
| 1678 | bufptr = m_buffer; |
| 1679 | for (i = 0; i <= len; i++) { |
| 1680 | if (m_buffer[i] == '\n' || m_buffer[i] == 0) { |
| 1681 | if (c == n_conlines) { |
| 1682 | // scroll pointers up, store new one. |
| 1683 | for (j = 1; j < n_conlines; j++) |
| 1684 | m_conlines[j - 1] = m_conlines[j]; |
| 1685 | c--; |
| 1686 | } |
| 1687 | m_conlines[c] = bufptr; |
| 1688 | bufptr = &m_buffer[i + 1]; |
| 1689 | m_bufline++; |
| 1690 | c++; |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | m_numlines = m_bufline; |
| 1695 | m_keydowntime = 0.0f; |
| 1696 | |
| 1697 | strcpy(m_title, title); |
| 1698 | m_curmsgs = m_list->m_nmsg; |
| 1699 | |
| 1700 | SuspendControls(); |
| 1701 | |
| 1702 | return true; |
| 1703 | } |
| 1704 | |
| 1705 | void MsgListConsole::Close() { |
| 1706 | if (m_opened) { |
| 1707 | if (m_conlines) |
| 1708 | mem_free(m_conlines); |
| 1709 | if (m_buffer) |
| 1710 | mem_free(m_buffer); |
| 1711 | m_opened = false; |
| 1712 | ResumeControls(); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | void MsgListConsole::AttachMsgList(tMsgList *msglist) { |
| 1717 | m_list = msglist; |
| 1718 | m_bufline = 0; |
| 1719 | } |
| 1720 | |
| 1721 | void MsgListConsole::Draw() { |
| 1722 | g3Point *pntlist[4], points[4]; |
| 1723 | int y, i; |
| 1724 | |
| 1725 | if (!m_opened || !m_list) |
| 1726 | return; |
| 1727 | |
| 1728 | // change in buffer, do 'slow' update |
| 1729 | if (m_curmsgs != m_list->m_nmsg) { |
| 1730 | char buf[32]; |
| 1731 | strcpy(buf, m_title); |
| 1732 | this->Close(); |
| 1733 | this->Open(buf, m_x, m_y, m_w, m_h); |
| 1734 | } |
| 1735 | |
| 1736 | // draw background window. |
| 1737 | points[0].p3_sx = m_x; |
| 1738 | points[0].p3_sy = m_y; |
| 1739 | points[1].p3_sx = m_x + m_w; |
| 1740 | points[1].p3_sy = m_y; |
| 1741 | points[2].p3_sx = m_x + m_w; |
| 1742 | points[2].p3_sy = m_y + m_h; |
| 1743 | points[3].p3_sx = m_x; |
| 1744 | points[3].p3_sy = m_y + m_h; |
| 1745 | |
| 1746 | for (i = 0; i < 4; i++) { |
| 1747 | points[i].p3_z = 0; |
| 1748 | points[i].p3_flags = PF_PROJECTED; |
| 1749 | pntlist[i] = &points[i]; |
| 1750 | } |
| 1751 | |
| 1752 | rend_SetZBufferState(0); |
| 1753 | rend_SetTextureType(TT_FLAT); |
| 1754 | rend_SetAlphaType(AT_CONSTANT); |
| 1755 | rend_SetLighting(LS_NONE); |
| 1756 | rend_SetFlatColor(GR_BLACK); |
| 1757 | rend_SetAlphaValue(192); |
| 1758 | rend_DrawPolygon2D(0, pntlist, 4); |
| 1759 | |
| 1760 | // set up text rendering. |
| 1761 | grtext_Reset(); |
| 1762 | grtext_SetParameters(m_x + MSGL_BORDER_THICKNESS, m_y + MSGL_BORDER_THICKNESS - 1, m_x + m_w - MSGL_BORDER_THICKNESS, |
| 1763 | m_y + m_h - MSGL_BORDER_THICKNESS); |
| 1764 | grtext_SetFont(HUD_FONT); |
| 1765 | grtext_SetColor(GR_RGB(0, 255, 0)); |
| 1766 | grtext_Puts(m_x + MSGL_BORDER_THICKNESS + 4, m_y + MSGL_BORDER_THICKNESS - 1, m_title); |
| 1767 | |
| 1768 | y = m_y + grfont_GetHeight(HUD_FONT) + MSGL_BORDER_THICKNESS + 1; |
| 1769 | rend_SetFlatColor(GR_RGB(0, 255, 0)); |
| 1770 | rend_DrawLine(m_x + MSGL_BORDER_THICKNESS, y, m_x + m_w - MSGL_BORDER_THICKNESS, y); |
| 1771 | |
| 1772 | // draw all lines until bottom |
| 1773 | y += 2; |
| 1774 | for (i = 0; i < n_conlines; i++) { |
| 1775 | char *sptr = NULL; |
| 1776 | if (i < (n_conlines - 1)) { |
| 1777 | sptr = m_conlines[i + 1]; |
| 1778 | } |
| 1779 | if (sptr) { |
| 1780 | sptr[-1] = 0; |
| 1781 | } |
| 1782 | grtext_Puts(m_x + MSGL_BORDER_THICKNESS, y, m_conlines[i]); |
| 1783 | if (sptr) { |
| 1784 | sptr[-1] = '\n'; |
| 1785 | } |
| 1786 | y += (grfont_GetHeight(HUD_FONT) + 1); |
| 1787 | if (!sptr) { |
| 1788 | break; |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | grtext_Flush(); |
| 1793 | } |
| 1794 | |
| 1795 | void MsgListConsole::DoInput() { |
| 1796 | if (m_opened) { |
| 1797 | int i, offset_count = 0; // = ddio_KeyDownCount(KEY_DOWN) - ddio_KeyDownCount(KEY_UP); |
| 1798 | float key_time = ddio_KeyDownTime(KEY_DOWN) - ddio_KeyDownTime(KEY_UP); |
| 1799 | char *lineptr; |
| 1800 | |
| 1801 | if (m_numlines <= n_conlines) { |
| 1802 | offset_count = 0; // no need to offset if no need to scroll |
| 1803 | } |
| 1804 | |
| 1805 | if (key_time == 0.0f) { |
| 1806 | m_keydowntime = 0.0f; |
| 1807 | } else { |
| 1808 | m_keydowntime += key_time; |
| 1809 | } |
| 1810 | if (m_keydowntime < -0.1f) { |
| 1811 | offset_count = -1; |
| 1812 | m_keydowntime = 0.0f; |
| 1813 | } else if (m_keydowntime > 0.1f) { |
| 1814 | offset_count = 1; |
| 1815 | m_keydowntime = 0.0f; |
| 1816 | } |
| 1817 | |
| 1818 | // scroll offset_count amount. |
| 1819 | if (offset_count < 0) { |
| 1820 | // look back from m_conlines[0] |
| 1821 | if ((m_bufline + offset_count - n_conlines) < 0) |
| 1822 | offset_count = -(m_bufline - n_conlines); |
| 1823 | |
| 1824 | m_bufline = m_bufline + offset_count; |
| 1825 | |
| 1826 | if (offset_count < 0) { |
| 1827 | mprintf(0, "bufline=%d\n", m_bufline); |
| 1828 | } |
| 1829 | |
| 1830 | while (offset_count < 0) { |
| 1831 | offset_count++; |
| 1832 | for (i = n_conlines - 1; i > 0; i--) |
| 1833 | m_conlines[i] = m_conlines[i - 1]; |
| 1834 | |
| 1835 | // go back until hit previous newline of beginning of buffer. |
| 1836 | for (lineptr = m_conlines[0] - 1; lineptr != m_buffer && lineptr[-1] != '\n'; lineptr--) |
| 1837 | ; |
| 1838 | m_conlines[0] = lineptr; |
| 1839 | } |
| 1840 | } else if (offset_count > 0) { |
| 1841 | // look forward from m_conlines[n_conlines-1] |
| 1842 | if ((m_bufline + offset_count) > m_numlines) |
| 1843 | offset_count = m_numlines - m_bufline; |
| 1844 | |
| 1845 | m_bufline = m_bufline + offset_count; |
| 1846 | |
| 1847 | if (offset_count > 0) { |
| 1848 | mprintf(0, "bufline=%d\n", m_bufline); |
| 1849 | } |
| 1850 | |
| 1851 | while (offset_count > 0) { |
| 1852 | offset_count--; |
| 1853 | for (i = 1; i < n_conlines; i++) |
| 1854 | m_conlines[i - 1] = m_conlines[i]; |
| 1855 | |
| 1856 | // go forward until we hit a newline or end of buffer (if end, then error!) |
| 1857 | for (lineptr = m_conlines[n_conlines - 1]; lineptr[0] != '\n'; lineptr++) |
| 1858 | ; |
| 1859 | { |
| 1860 | if (lineptr[0] == 0) { |
| 1861 | Int3(); |
| 1862 | offset_count = 0; |
| 1863 | lineptr = m_conlines[n_conlines - 1]; |
| 1864 | break; |
| 1865 | } |
| 1866 | } |
| 1867 | if (lineptr[0]) |
| 1868 | lineptr++; |
| 1869 | m_conlines[n_conlines - 1] = lineptr; |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | } |