Devilution
Diablo devolved - magic behind the 1996 computer game
plrmsg.cpp
Go to the documentation of this file.
1 #include "all.h"
2 
4 
5 static BYTE plr_msg_slot;
7 
10 
11 void plrmsg_delay(BOOL delay)
12 {
13  int i;
14  _plrmsg *pMsg;
15  static DWORD plrmsg_ticks;
16 
17  if (delay) {
18  plrmsg_ticks = -SDL_GetTicks();
19  return;
20  }
21 
22  plrmsg_ticks += SDL_GetTicks();
23  pMsg = plr_msgs;
24  for (i = 0; i < PMSG_COUNT; i++, pMsg++)
25  pMsg->time += plrmsg_ticks;
26 }
27 
28 char *ErrorPlrMsg(const char *pszMsg)
29 {
30  char *result;
31  _plrmsg *pMsg = &plr_msgs[plr_msg_slot];
32  plr_msg_slot = (plr_msg_slot + 1) & (PMSG_COUNT - 1);
33  pMsg->player = MAX_PLRS;
34  pMsg->time = SDL_GetTicks();
35  result = strncpy(pMsg->str, pszMsg, sizeof(pMsg->str));
36  pMsg->str[sizeof(pMsg->str) - 1] = '\0';
37  return result;
38 }
39 
40 size_t EventPlrMsg(const char *pszFmt, ...)
41 {
42  _plrmsg *pMsg;
43  va_list va;
44 
45  va_start(va, pszFmt);
46  pMsg = &plr_msgs[plr_msg_slot];
47  plr_msg_slot = (plr_msg_slot + 1) & (PMSG_COUNT - 1);
48  pMsg->player = MAX_PLRS;
49  pMsg->time = SDL_GetTicks();
50  vsprintf(pMsg->str, pszFmt, va);
51  va_end(va);
52  return strlen(pMsg->str);
53 }
54 
55 void SendPlrMsg(int pnum, const char *pszStr)
56 {
57  _plrmsg *pMsg = &plr_msgs[plr_msg_slot];
58  plr_msg_slot = (plr_msg_slot + 1) & (PMSG_COUNT - 1);
59  pMsg->player = pnum;
60  pMsg->time = SDL_GetTicks();
61  strlen(plr[pnum]._pName); /* these are used in debug */
62  strlen(pszStr);
63  sprintf(pMsg->str, "%s (lvl %d): %s", plr[pnum]._pName, plr[pnum]._pLevel, pszStr);
64 }
65 
67 {
68  int i;
69  _plrmsg *pMsg = plr_msgs;
70  DWORD tick = SDL_GetTicks();
71 
72  for (i = 0; i < PMSG_COUNT; i++, pMsg++) {
73  if ((int)(tick - pMsg->time) > 10000)
74  pMsg->str[0] = '\0';
75  }
76 }
77 
78 void InitPlrMsg()
79 {
80  memset(plr_msgs, 0, sizeof(plr_msgs));
81  plr_msg_slot = 0;
82 }
83 
84 void DrawPlrMsg()
85 {
86  int i;
87  DWORD x = 74;
88  DWORD y = 230;
89  DWORD width = SCREEN_WIDTH - 20;
90  _plrmsg *pMsg;
91 
92  if (chrflag || questlog) {
93  x = 394;
94  width -= 300;
95  }
96  if (invflag || sbookflag)
97  width -= 300;
98 
99  if (width < 300)
100  return;
101 
102  if (width > 620)
103  width = 620;
104 
105  pMsg = plr_msgs;
106  for (i = 0; i < 8; i++) {
107  if (pMsg->str[0])
108  PrintPlrMsg(x, y, width, pMsg->str, text_color_from_player_num[pMsg->player]);
109  pMsg++;
110  y += 35;
111  }
112 }
113 
114 void PrintPlrMsg(DWORD x, DWORD y, DWORD width, const char *str, BYTE col)
115 {
116  int line = 0;
117 
118  while (*str) {
119  BYTE c;
120  int sx = x;
121  DWORD len = 0;
122  const char *sstr = str;
123  const char *endstr = sstr;
124 
125  while (1) {
126  if (*sstr) {
127  c = gbFontTransTbl[(BYTE)*sstr++];
128  c = fontframe[c];
129  len += fontkern[c] + 1;
130  if (!c) // allow wordwrap on blank glyph
131  endstr = sstr;
132  else if (len >= width)
133  break;
134  } else {
135  endstr = sstr;
136  break;
137  }
138  }
139 
140  while (str < endstr) {
141  c = gbFontTransTbl[(BYTE)*str++];
142  c = fontframe[c];
143  if (c)
144  PrintChar(sx, y, c, col);
145  sx += fontkern[c] + 1;
146  }
147 
148  y += 10;
149  line++;
150  if (line == 3)
151  break;
152  }
153 }
154 
_plrmsg::time
DWORD time
Definition: structs.h:1547
DrawPlrMsg
void DrawPlrMsg()
Definition: plrmsg.cpp:84
MAX_PLRS
#define MAX_PLRS
Definition: defs.h:16
_plrmsg::player
unsigned char player
Definition: structs.h:1548
plr_msgs
_plrmsg plr_msgs[PMSG_COUNT]
Definition: plrmsg.cpp:6
questlog
BOOL questlog
Definition: quests.cpp:6
COL_WHITE
@ COL_WHITE
Definition: enums.h:1993
COL_GOLD
@ COL_GOLD
Definition: enums.h:1996
PlayerStruct::_pLevel
char _pLevel
Definition: structs.h:266
all.h
SCREEN_WIDTH
#define SCREEN_WIDTH
Definition: defs.h:105
InitPlrMsg
void InitPlrMsg()
Definition: plrmsg.cpp:78
sbookflag
BOOL sbookflag
Definition: control.cpp:48
PMSG_COUNT
#define PMSG_COUNT
Definition: defs.h:80
DEVILUTION_END_NAMESPACE
#define DEVILUTION_END_NAMESPACE
Definition: types.h:10
ErrorPlrMsg
char * ErrorPlrMsg(const char *pszMsg)
Definition: plrmsg.cpp:28
plrmsg_delay
void plrmsg_delay(BOOL delay)
Definition: plrmsg.cpp:11
PrintPlrMsg
void PrintPlrMsg(DWORD x, DWORD y, DWORD width, const char *str, BYTE col)
Definition: plrmsg.cpp:114
fontkern
const BYTE fontkern[68]
Maps from smaltext.cel frame number to character width.
Definition: control.cpp:81
text_color_from_player_num
const char text_color_from_player_num[MAX_PLRS+1]
Maps from player_num to text colour, as used in chat messages.
Definition: plrmsg.cpp:9
_plrmsg
Definition: structs.h:1546
invflag
DEVILUTION_BEGIN_NAMESPACE BOOL invflag
Definition: inv.cpp:10
_plrmsg::str
char str[144]
Definition: structs.h:1549
EventPlrMsg
size_t EventPlrMsg(const char *pszFmt,...)
Definition: plrmsg.cpp:40
chrflag
BOOL chrflag
Definition: control.cpp:49
DEVILUTION_BEGIN_NAMESPACE
Definition: sha.cpp:10
SendPlrMsg
void SendPlrMsg(int pnum, const char *pszStr)
Definition: plrmsg.cpp:55
PrintChar
void PrintChar(int sx, int sy, int nCel, char col)
Print letter to the back buffer.
Definition: control.cpp:504
fontframe
const BYTE fontframe[128]
Maps from font index to smaltext.cel frame number.
Definition: control.cpp:65
gbFontTransTbl
const BYTE gbFontTransTbl[256]
Maps ASCII character code to font index, as used by the small, medium and large sized fonts; which co...
Definition: control.cpp:106
plr_msg_slot
static DEVILUTION_BEGIN_NAMESPACE BYTE plr_msg_slot
Definition: plrmsg.cpp:5
PlayerStruct::_pName
char _pName[PLR_NAME_LEN]
Definition: structs.h:237
plr
PlayerStruct plr[MAX_PLRS]
Definition: player.cpp:10
ClearPlrMsg
void ClearPlrMsg()
Definition: plrmsg.cpp:66