-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
hu_msg.c
357 lines (290 loc) · 8.52 KB
/
hu_msg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**\file hu_msg.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2011 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*\author Copyright © 1993-1996 by id Software, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* Important state change messages.
*/
// HEADER FILES ------------------------------------------------------------
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#if __JDOOM__
# include "jdoom.h"
#elif __JDOOM64__
# include "jdoom64.h"
#elif __JHERETIC__
# include "jheretic.h"
#elif __JHEXEN__
# include "jhexen.h"
#endif
#include "hu_msg.h"
#include "hu_menu.h"
#include "hu_stuff.h"
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
D_CMD(MsgResponse);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
ccmdtemplate_t msgCCmds[] = {
{"messageyes", "", CCmdMsgResponse},
{"messageno", "", CCmdMsgResponse},
{"messagecancel", "", CCmdMsgResponse},
{NULL}
};
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static boolean awaitingResponse;
static int messageToPrint; // 1 = message to be printed.
static msgresponse_t messageResponse;
static msgtype_t msgType;
static msgfunc_t msgCallback;
static char* msgText;
static void* msgContext;
static char yesNoMessage[160];
// CODE --------------------------------------------------------------------
/**
* Called during the PreInit of each game during start up.
* Register Cvars and CCmds for the important messages.
*/
void Hu_MsgRegister(void)
{
int i;
for(i = 0; msgCCmds[i].name; ++i)
Con_AddCommand(msgCCmds + i);
}
/**
* Called during init.
*/
void Hu_MsgInit(void)
{
awaitingResponse = false;
messageToPrint = 0; // 1 = message to be printed.
messageResponse = MSG_CANCEL;
msgCallback = NULL;
msgText = NULL;
msgContext = NULL;
}
/**
* Called during engine shutdown.
*/
void Hu_MsgShutdown(void)
{
if(msgText)
free(msgText);
msgText = NULL;
}
static void stopMessage(void)
{
#if __JDOOM__ || __JDOOM64__
# define SFX_ENDMESSAGE SFX_SWTCHX
#elif __JHERETIC__
# define SFX_ENDMESSAGE SFX_CHAT
#elif __JHEXEN__
# define SFX_ENDMESSAGE SFX_DOOR_LIGHT_CLOSE
#endif
messageToPrint = 0;
awaitingResponse = false;
if(msgText)
free(msgText);
msgText = NULL;
S_LocalSound(SFX_ENDMESSAGE, NULL);
// Disable the message binding context.
DD_Executef(true, "deactivatebcontext message");
#undef SFX_ENDMESSAGE
}
/**
* \todo: Query the bindings to determine the actual controls bound to the
* message response commands.
*/
static void composeYesNoMessage(void)
{
char* buf = yesNoMessage, *in, tmp[2];
buf[0] = 0;
tmp[1] = 0;
// Get the message template.
in = PRESSYN;
for(; *in; in++)
{
if(in[0] == '%')
{
if(in[1] == '1')
{
strcat(buf, "Y");
in++;
continue;
}
if(in[1] == '2')
{
strcat(buf, "N");
in++;
continue;
}
if(in[1] == '%')
in++;
}
tmp[0] = *in;
strcat(buf, tmp);
}
}
static void drawMessage(void)
{
#define LEADING (0)
short textFlags = MN_MergeMenuEffectWithDrawTextFlags(0);
int x = SCREENWIDTH/2, y = SCREENHEIGHT/2;
const char* questionString;
switch(msgType)
{
case MSG_ANYKEY: questionString = PRESSKEY; break;
case MSG_YESNO: questionString = yesNoMessage; break;
default:
Con_Error("drawMessage: Internal error, unknown message type %i.\n", (int) msgType);
}
DGL_Enable(DGL_TEXTURE_2D);
DGL_Color4f(cfg.menuTextColors[MENU_COLOR4][CR], cfg.menuTextColors[MENU_COLOR4][CG], cfg.menuTextColors[MENU_COLOR4][CB], 1);
FR_SetFont(FID(GF_FONTA));
FR_LoadDefaultAttrib();
FR_SetLeading(LEADING);
FR_SetShadowStrength(cfg.menuTextGlitter);
FR_SetGlitterStrength(cfg.menuShadow);
FR_DrawText(msgText, x, y, ALIGN_TOP, textFlags);
y += FR_TextHeight(msgText);
// An additional blank line between the message and response prompt.
y += FR_CharHeight('A') * (1+LEADING);
FR_DrawTextFragment2(questionString, x, y, ALIGN_TOP, textFlags);
DGL_Disable(DGL_TEXTURE_2D);
#undef LEADING
}
/**
* Draw any active message.
*/
void Hu_MsgDrawer(void)
{
borderedprojectionstate_t bp;
if(!messageToPrint) return;
GL_ConfigureBorderedProjection(&bp, 0, SCREENWIDTH, SCREENHEIGHT, Get(DD_WINDOW_WIDTH), Get(DD_WINDOW_HEIGHT), cfg.menuScaleMode);
GL_BeginBorderedProjection(&bp);
DGL_MatrixMode(DGL_MODELVIEW);
DGL_PushMatrix();
DGL_Translatef(SCREENWIDTH/2, SCREENHEIGHT/2, 0);
DGL_Scalef(cfg.menuScale, cfg.menuScale, 1);
DGL_Translatef(-(SCREENWIDTH/2), -(SCREENHEIGHT/2), 0);
drawMessage();
DGL_MatrixMode(DGL_MODELVIEW);
DGL_PopMatrix();
GL_EndBorderedProjection(&bp);
}
/**
* Updates on Game Tick.
*/
void Hu_MsgTicker(timespan_t time)
{
// Check if there has been a response to a message.
if(!messageToPrint || awaitingResponse)
return;
// We can now stop the message.
stopMessage();
if(msgType != MSG_ANYKEY && msgCallback)
msgCallback(messageResponse, msgContext);
}
/**
* If an "any key" message is active, respond to the event.
*/
int Hu_MsgResponder(event_t* ev)
{
if(!messageToPrint || msgType != MSG_ANYKEY)
return false;
// We are only interested in key downs.
if(ev->state == EVS_DOWN &&
(ev->type == EV_KEY || ev->type == EV_MOUSE_BUTTON ||
ev->type == EV_JOY_BUTTON))
{
stopMessage();
return true;
}
return true;
}
boolean Hu_IsMessageActive(void)
{
return messageToPrint;
}
/**
* Begin a new message.
*/
void Hu_MsgStart(msgtype_t type, const char* msg, msgfunc_t callback, void* context)
{
assert(msg);
awaitingResponse = true;
messageResponse = 0;
messageToPrint = 1;
msgType = type;
msgCallback = callback;
msgContext = context;
// Take a copy of the message string.
msgText = calloc(1, strlen(msg)+1);
strncpy(msgText, msg, strlen(msg));
if(msgType == MSG_YESNO)
composeYesNoMessage();
FR_ResetTypeInTimer();
// If the console is open, close it. This message must be noticed!
Con_Open(false);
// Enable the message binding context.
DD_Execute(true, "activatebcontext message");
}
/**
* Handles responses to messages requiring input.
*/
D_CMD(MsgResponse)
{
if(messageToPrint)
{
// Handle "Press any key to continue" messages.
if(messageToPrint && msgType == MSG_ANYKEY)
{
stopMessage();
return true;
}
if(!stricmp(argv[0], "messageyes"))
{
awaitingResponse = false;
messageResponse = MSG_YES;
return true;
}
else if(!stricmp(argv[0], "messageno"))
{
awaitingResponse = false;
messageResponse = MSG_NO;
return true;
}
else if(!stricmp(argv[0], "messagecancel"))
{
awaitingResponse = false;
messageResponse = MSG_CANCEL;
return true;
}
}
return false;
}