-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin_qe3.c
650 lines (509 loc) · 14.7 KB
/
win_qe3.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
// win_qe3.c
#include "qe3.h"
//#include "mru.h"
// for the logging part
//#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
int screen_width;
int screen_height;
qboolean have_quit;
int update_bits;
HANDLE bsp_process;
//===========================================
void Sys_SetTitle (char *text)
{
SetWindowText (g_qeglobals.d_hwndMain, text);
}
HCURSOR waitcursor;
void Sys_BeginWait (void)
{
waitcursor = SetCursor (LoadCursor (NULL, IDC_WAIT));
}
void Sys_EndWait (void)
{
if (waitcursor)
{
SetCursor (waitcursor);
waitcursor = NULL;
}
}
void Sys_GetCursorPos (int *x, int *y)
{
POINT lpPoint;
GetCursorPos (&lpPoint);
*x = lpPoint.x;
*y = lpPoint.y;
}
void Sys_SetCursorPos (int x, int y)
{
SetCursorPos (x, y);
}
void Sys_UpdateWindows (int bits)
{
// Sys_Printf("updating 0x%X\n", bits);
update_bits |= bits;
//update_bits = -1;
}
void Sys_Beep (void)
{
MessageBeep (MB_ICONASTERISK);
}
#define BUFF_SIZE 65536
char *TranslateString (char *buf)
{
static char buf2[BUFF_SIZE];
int i, l;
char *out;
l = strlen(buf);
out = buf2;
for (i=0 ; i<l ; i++)
{
if (buf[i] == '\n')
{
*out++ = '\r';
*out++ = '\n';
}
else
*out++ = buf[i];
}
*out++ = 0;
return buf2;
}
// called whenever we need to open/close/check the console log file
void Sys_LogFile (void)
{
if (g_qeglobals.d_savedinfo.logconsole && !g_qeglobals.d_logfile)
{
// open a file to log the console (if user prefs say so)
// the file handle is g_qeglobals.d_logfile
// the log file is erased
char name[_MAX_PATH];
char path[_MAX_PATH];
GetCurrentDirectory (_MAX_PATH, path);
sprintf (name, "%s/qe3.log", path);
g_qeglobals.d_logfile = _open (name, _O_TRUNC | _O_CREAT | _O_WRONLY, _S_IREAD | _S_IWRITE);
if (g_qeglobals.d_logfile)
Sys_Printf("Started logging\n");
else
MessageBox( g_qeglobals.d_hwndMain, "Failed to create log file, check write permissions in qe3 directory.\n", "Console logging", MB_OK );
}
else if (g_qeglobals.d_logfile)
{
Sys_Printf("Closing log file\n");
_close (g_qeglobals.d_logfile);
g_qeglobals.d_logfile = 0;
}
}
void Sys_ClearPrintf (void)
{
char text[4];
text[0] = 0;
SendMessage (g_qeglobals.d_hwndConsole, WM_SETTEXT, 0, (LPARAM)text);
}
#define SCROLLBACK_MAX_LINES 600 // PGM
#define SCROLLBACK_DEL_CHARS 500 // PGM
void Sys_Printf (char *text, ...)
{
va_list argptr;
char buf[BUFF_SIZE];
char *out;
LRESULT result; // PGM
DWORD oldPosS, oldPosE; // PGM
va_start (argptr,text);
vsprintf (buf, text,argptr);
va_end (argptr);
if (g_qeglobals.d_logfile)
{
_write(g_qeglobals.d_logfile, buf, strlen(buf));
_commit(g_qeglobals.d_logfile);
}
out = TranslateString (buf);
#ifdef LATER
Sys_Status(out);
#else
//PGM
result = SendMessage (g_qeglobals.d_hwndConsole, EM_GETLINECOUNT, 0, 0);
if(result >= SCROLLBACK_MAX_LINES)
{
char replaceText[5];
replaceText[0] = '\0';
SendMessage (g_qeglobals.d_hwndConsole, WM_SETREDRAW, (WPARAM)0, (LPARAM)0);
SendMessage (g_qeglobals.d_hwndConsole, EM_GETSEL, (WPARAM)&oldPosS, (LPARAM)&oldPosE);
SendMessage (g_qeglobals.d_hwndConsole, EM_SETSEL, 0, SCROLLBACK_DEL_CHARS);
SendMessage (g_qeglobals.d_hwndConsole, EM_REPLACESEL, (WPARAM)0, (LPARAM)replaceText);
SendMessage (g_qeglobals.d_hwndConsole, EM_SETSEL, oldPosS, oldPosE);
SendMessage (g_qeglobals.d_hwndConsole, WM_SETREDRAW, (WPARAM)1, (LPARAM)0);
}
//PGM
SendMessage (g_qeglobals.d_hwndConsole, EM_REPLACESEL, 0, (LPARAM)out);
SendMessage (g_qeglobals.d_hwndConsole, EM_SCROLLCARET, 0, 0); // EER1
#endif
}
double Sys_DoubleTime (void)
{
return clock()/ 1000.0;
}
void PrintPixels (HDC hDC)
{
int i;
PIXELFORMATDESCRIPTOR p[64];
printf ("### flags color layer\n");
for (i=1 ; i<64 ; i++)
{
if (!DescribePixelFormat ( hDC, i, sizeof(p[0]), &p[i]))
break;
printf ("%3i %5i %5i %5i\n", i,
p[i].dwFlags,
p[i].cColorBits,
p[i].bReserved);
}
printf ("%i modes\n", i-1);
}
//==========================================================================
void QEW_StopGL( HWND hWnd, HGLRC hGLRC, HDC hDC )
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hGLRC );
ReleaseDC( hWnd, hDC );
}
int QEW_SetupPixelFormat(HDC hDC, qboolean zbuffer )
{
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // depth bits
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int pixelformat = 0;
zbuffer = true;
if ( !zbuffer )
pfd.cDepthBits = 0;
if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
{
printf("%d",GetLastError());
Error ("ChoosePixelFormat failed");
}
if (!SetPixelFormat(hDC, pixelformat, &pfd))
Error ("SetPixelFormat failed");
return pixelformat;
}
/*
=================
Error
For abnormal program terminations
=================
*/
void Error (char *error, ...)
{
va_list argptr;
char text[1024];
char text2[1024];
int err;
err = GetLastError ();
va_start (argptr,error);
vsprintf (text, error,argptr);
va_end (argptr);
sprintf (text2, "%s\nGetLastError() = %i", text, err);
MessageBox(g_qeglobals.d_hwndMain, text2, "Error", 0 /* MB_OK */ );
// close logging if necessary
g_qeglobals.d_savedinfo.logconsole = false;
Sys_LogFile ();
exit (1);
}
/*
======================================================================
FILE DIALOGS
======================================================================
*/
qboolean ConfirmModified (void)
{
if (!modified)
return true;
if (MessageBox (g_qeglobals.d_hwndMain, "This will lose changes to the map"
, "warning", MB_OKCANCEL) == IDCANCEL)
return false;
return true;
}
static OPENFILENAME ofn; /* common dialog box structure */
static char szDirName[MAX_PATH]; /* directory string */
static char szFile[260]; /* filename string */
static char szFileTitle[260]; /* file title string */
static char szFilter[260] = /* filter string */
"QuakeEd file (*.map)\0*.map\0\0";
static char szProjectFilter[260] = /* filter string */
"QuakeEd project (*.qe3)\0*.qe3\0\0";
static char chReplace; /* string separator for szFilter */
static int i, cbString; /* integer count variables */
static HANDLE hf; /* file handle */
void OpenDialog (void)
{
/*
* Obtain the system directory name and
* store it in szDirName.
*/
strcpy (szDirName, ValueForKey (g_qeglobals.d_project_entity, "mapspath") );
if (strlen(szDirName) == 0)
{
strcpy (szDirName, ValueForKey (g_qeglobals.d_project_entity, "basepath") );
strcat (szDirName, "/maps");
}
/* Place the terminating null character in the szFile. */
szFile[0] = '\0';
/* Set the members of the OPENFILENAME structure. */
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = g_qeglobals.d_hwndCamera;
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = sizeof(szFileTitle);
ofn.lpstrInitialDir = szDirName;
ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
OFN_FILEMUSTEXIST;
/* Display the Open dialog box. */
if (!GetOpenFileName(&ofn))
return; // canceled
// Add the file in MRU.
AddNewItem( g_qeglobals.d_lpMruMenu, ofn.lpstrFile);
// Refresh the File menu.
PlaceMenuMRUItem(g_qeglobals.d_lpMruMenu,GetSubMenu(GetMenu(g_qeglobals.d_hwndMain),0),
ID_FILE_EXIT);
/* Open the file. */
Map_LoadFile (ofn.lpstrFile);
}
void ProjectDialog (void)
{
/*
* Obtain the system directory name and
* store it in szDirName.
*/
strcpy (szDirName, ValueForKey(g_qeglobals.d_project_entity, "basepath") );
strcat (szDirName, "/scripts");
/* Place the terminating null character in the szFile. */
szFile[0] = '\0';
/* Set the members of the OPENFILENAME structure. */
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = g_qeglobals.d_hwndCamera;
ofn.lpstrFilter = szProjectFilter;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = sizeof(szFileTitle);
ofn.lpstrInitialDir = szDirName;
ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
OFN_FILEMUSTEXIST;
/* Display the Open dialog box. */
if (!GetOpenFileName(&ofn))
return; // canceled
// Refresh the File menu.
PlaceMenuMRUItem(g_qeglobals.d_lpMruMenu,GetSubMenu(GetMenu(g_qeglobals.d_hwndMain),0),
ID_FILE_EXIT);
/* Open the file. */
if (!QE_LoadProject(ofn.lpstrFile))
Error ("Couldn't load project file");
}
void SaveAsDialog (void)
{
strcpy (szDirName, ValueForKey (g_qeglobals.d_project_entity, "basepath") );
strcat (szDirName, "/maps");
/* Place the terminating null character in the szFile. */
szFile[0] = '\0';
/* Set the members of the OPENFILENAME structure. */
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = g_qeglobals.d_hwndCamera;
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = sizeof(szFileTitle);
ofn.lpstrInitialDir = szDirName;
ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;
/* Display the Open dialog box. */
if (!GetSaveFileName(&ofn))
return; // canceled
DefaultExtension (ofn.lpstrFile, ".map");
strcpy (currentmap, ofn.lpstrFile);
// Add the file in MRU.
AddNewItem(g_qeglobals.d_lpMruMenu, ofn.lpstrFile);
// Refresh the File menu.
PlaceMenuMRUItem(g_qeglobals.d_lpMruMenu,GetSubMenu(GetMenu(g_qeglobals.d_hwndMain),0),
ID_FILE_EXIT);
Map_SaveFile (ofn.lpstrFile, false); // ignore region
}
/*
=======================================================
Menu modifications
=======================================================
*/
/*
==================
FillBSPMenu
==================
*/
char *bsp_commands[256];
void FillBSPMenu (void)
{
HMENU hmenu;
epair_t *ep;
int i;
static int count;
hmenu = GetSubMenu (GetMenu(g_qeglobals.d_hwndMain), MENU_BSP);
for (i=0 ; i<count ; i++)
DeleteMenu (hmenu, CMD_BSPCOMMAND+i, MF_BYCOMMAND);
count = 0;
i = 0;
for (ep = g_qeglobals.d_project_entity->epairs ; ep ; ep=ep->next)
{
if (ep->key[0] == 'b' && ep->key[1] == 's' && ep->key[2] == 'p')
{
bsp_commands[i] = ep->key;
AppendMenu (hmenu, MF_ENABLED|MF_STRING,
CMD_BSPCOMMAND+i, (LPCTSTR)ep->key);
i++;
}
}
count = i;
}
//==============================================
/*
===============
CheckBspProcess
See if the BSP is done yet
===============
*/
void CheckBspProcess (void)
{
char outputpath[1024];
char temppath[1024];
DWORD exitcode;
char *out;
BOOL ret;
if (!bsp_process)
return;
ret = GetExitCodeProcess (bsp_process, &exitcode);
if (!ret)
Error ("GetExitCodeProcess failed");
if (exitcode == STILL_ACTIVE)
return;
bsp_process = 0;
GetTempPath(1024, temppath);
sprintf (outputpath, "%sjunk.txt", temppath);
LoadFile (outputpath, (void **)&out);
Sys_Printf ("%s", out);
Sys_Printf ("\ncompleted.\n");
free (out);
Sys_Beep ();
Pointfile_Check ();
}
extern int cambuttonstate;
/*
==================
WinMain
==================
*/
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance
,LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
double time, oldtime, delta;
HACCEL accelerators;
g_qeglobals.d_hInstance = hInstance;
InitCommonControls ();
screen_width = GetSystemMetrics (SM_CXFULLSCREEN);
screen_height = GetSystemMetrics (SM_CYFULLSCREEN);
// hack for broken NT 4.0 dual screen
if (screen_width > 2*screen_height)
screen_width /= 2;
accelerators = LoadAccelerators (hInstance
, MAKEINTRESOURCE(IDR_ACCELERATOR1));
if (!accelerators)
Error ("LoadAccelerators failed");
Main_Create (hInstance);
Sys_LogFile ();
WCam_Create (hInstance);
WXY_Create (hInstance);
WZ_Create (hInstance);
CreateEntityWindow(hInstance);
// the project file can be specified on the command line,
// or implicitly found in the scripts directory
if (lpCmdLine && strlen(lpCmdLine))
{
ParseCommandLine (lpCmdLine);
if (!QE_LoadProject(argv[1]))
Error ("Couldn't load %s project file", argv[1]);
}
else if (!QE_LoadProject("scripts/quake.qe3"))
Error ("Couldn't load scripts/quake.qe3 project file");
QE_Init ();
Sys_Printf ("Entering message loop\n");
oldtime = Sys_DoubleTime ();
while (!have_quit)
{
Sys_EndWait (); // remove wait cursor if active
while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if (!TranslateAccelerator(g_qeglobals.d_hwndMain, accelerators, &msg) )
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
if (msg.message == WM_QUIT)
have_quit = true;
}
CheckBspProcess ();
time = Sys_DoubleTime ();
delta = time - oldtime;
oldtime = time;
if (delta > 0.2)
delta = 0.2;
// run time dependant behavior
Cam_MouseControl (delta);
// update any windows now
if (update_bits & W_CAMERA)
{
InvalidateRect(g_qeglobals.d_hwndCamera, NULL, false);
UpdateWindow (g_qeglobals.d_hwndCamera);
}
if (update_bits & (W_Z | W_Z_OVERLAY) )
{
InvalidateRect(g_qeglobals.d_hwndZ, NULL, false);
UpdateWindow (g_qeglobals.d_hwndZ);
}
if ( update_bits & W_TEXTURE )
{
InvalidateRect(g_qeglobals.d_hwndTexture, NULL, false);
UpdateWindow (g_qeglobals.d_hwndEntity);
}
if (update_bits & (W_XY | W_XY_OVERLAY))
{
InvalidateRect(g_qeglobals.d_hwndXY, NULL, false);
UpdateWindow (g_qeglobals.d_hwndXY);
}
update_bits = 0;
if (cambuttonstate != MK_RBUTTON && !have_quit) // EER1 fix
{ // if not driving in the camera view, block
WaitMessage ();
}
}
/* return success of application */
return TRUE;
}