-
Notifications
You must be signed in to change notification settings - Fork 7
/
wdriver.cpp
1628 lines (1426 loc) · 46.5 KB
/
wdriver.cpp
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Daedalus (Version 3.4) File: wdriver.cpp
** By Walter D. Pullen, [email protected], http://www.astrolog.org/labyrnth.htm
**
** IMPORTANT NOTICE: Daedalus and all Maze generation and general
** graphics routines used in this program are Copyright (C) 1998-2023 by
** Walter D. Pullen. Permission is granted to freely use, modify, and
** distribute these routines provided these credits and notices remain
** unmodified with any altered or distributed versions of the program.
** The user does have all rights to Mazes and other graphic output
** they make in Daedalus, like a novel created in a word processor.
**
** More formally: 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 and inspiring, 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, a copy of which is in the
** LICENSE.HTM included with Daedalus, and at http://www.gnu.org
**
** This file contains the Windows driver for Daedalus as a whole, and
** code to execute menu commands and Windows events.
**
** Created: 4/8/2013.
** Last code change: 8/29/2023.
*/
#include <windows.h>
#include <stdio.h>
#include <time.h>
#define APSTUDIO_INVOKED
#include "resource.h"
#include "util.h"
#include "graphics.h"
#include "color.h"
#include "threed.h"
#include "maze.h"
#include "draw.h"
#include "daedalus.h"
#include "wdriver.h"
#ifdef WIN
// Global variables
WI wi = {NULL, NULL, NULL, NULL, NULL, NULL,
{0, 0, 640, 480}, NULL, NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL};
// Global variables for common dialogs
OPENFILENAME ofn = {sizeof(OPENFILENAME), (HWND)NULL, (HINSTANCE)NULL, NULL,
NULL, 0, 1, NULL, cchSzMaxFile, NULL, cchSzMaxFile, NULL, NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 0, 0, NULL, 0L, NULL, NULL};
PRINTDLG prd = {
sizeof(PRINTDLG), (HWND)NULL, (HGLOBAL)NULL, (HGLOBAL)NULL, (HDC)NULL,
PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC | PD_USEDEVMODECOPIES,
0, 0, 0, 0, 1, (HINSTANCE)NULL, 0L, NULL, NULL, (LPCSTR)NULL, (LPCSTR)NULL,
(HGLOBAL)NULL, (HGLOBAL)NULL};
CHOOSECOLOR cc = {sizeof(CHOOSECOLOR), (HWND)NULL, (HWND)NULL, kvGray, NULL,
CC_FULLOPEN | CC_RGBINIT, 0L, NULL, NULL};
/*
******************************************************************************
** Function Hooks
******************************************************************************
*/
// Bring up a message box and return the button pressed.
int PrintSzCore(CONST char *sz, int nPriority)
{
char szTitle[cchSzDef], szT[cchSzDef], *pchTitle, *pchT;
UINT nIcon, nType = MB_OK;
int nRet = IDOK;
flag fSav;
// Don't show normal priority messages if Ignore Messages is active.
if (ws.fIgnorePrint && FBetween(nPriority, 0, ws.nIgnorePrint))
goto LDone;
if (ws.fSaverRun) {
ws.fSaverMouse = fFalse;
ShowCursor(fTrue);
}
// Figure out what text to show in the title bar of the message box.
pchTitle = NULL;
if (ws.nTitleMessage >= 0) {
pchT = SzVar(ws.nTitleMessage);
if (pchT != NULL) {
CopySz(pchT, szT, cchSzDef);
pchTitle = szT;
}
}
if (pchTitle == NULL || *pchTitle == chNull)
pchTitle = ws.szAppName;
switch (nPriority) {
case nPrintWarning:
sprintf(S(szTitle), "%s Warning", pchTitle);
nIcon = MB_ICONSTOP;
break;
case nPrintError:
sprintf(S(szTitle), "%s Error", pchTitle);
nIcon = MB_ICONEXCLAMATION;
break;
default:
sprintf(S(szTitle), "%s", pchTitle);
nIcon = MB_ICONINFORMATION;
break;
}
// Figure out what buttons to include in the message box.
if (nPriority < 0) {
nIcon = MB_ICONQUESTION;
switch (nPriority) {
case nAskOkCancel: nType = MB_OKCANCEL; break;
case nAskRetryCancel: nType = MB_RETRYCANCEL; break;
case nAskYesNo: nType = MB_YESNO; break;
case nAskYesNoCancel: nType = MB_YESNOCANCEL; break;
case nAskAbortRetryIgnore: nType = MB_ABORTRETRYIGNORE; break;
}
}
// Actually bring up the message box and wait for user to press a button.
if (ws.fCopyMessage)
FSzCopy(sz);
fSav = ws.fInSpree; ws.fInSpree = fTrue;
nRet = MessageBox(wi.hwndMain, sz,
ws.szTitle != NULL ? ws.szTitle : szTitle, nType | nIcon | MB_TASKMODAL);
ws.fInSpree = fSav;
ws.cDialog++;
LDone:
ws.szTitle = NULL;
if (ws.fSaverRun)
ShowCursor(fFalse);
if (nRet == IDCANCEL || nRet == IDABORT)
nRet = -1;
else if (nRet == IDNO || nRet == IDIGNORE)
nRet = 0;
else
nRet = 1;
return nRet;
}
// Allocate a memory buffer of a given size, in a Windows specific manner.
void *PAllocate(long lcb)
{
char sz[cchSzMax];
void *pv;
#ifdef DEBUG
pv = GlobalAlloc(GMEM_FIXED, lcb + sizeof(dword)*3);
#else
pv = GlobalAlloc(GMEM_FIXED, lcb);
#endif
// Handle success or failure of the allocation.
if (pv == NULL) {
sprintf(S(sz), "Failed to allocate memory (%ld bytes).\n", lcb);
PrintSz_E(sz);
} else {
us.cAlloc++;
us.cAllocTotal++;
us.cAllocSize += lcb;
}
#ifdef DEBUG
// Put sentinels at ends of allocation to check for buffer overruns.
*(dword *)pv = dwCanary;
*(dword *)((byte *)pv + sizeof(dword)) = lcb;
*(dword *)((byte *)pv + sizeof(dword)*2 + lcb) = dwCanary;
return (byte *)pv + sizeof(dword)*2;
#else
return pv;
#endif
}
// Free a memory buffer allocated with PAllocate.
void DeallocateP(void *pv)
{
Assert(pv != NULL);
#ifdef DEBUG
// Ensure buffer wasn't overrun during its existence.
void *pvSys;
dword lcb, dw;
pvSys = (byte *)pv - sizeof(dword)*2;
Assert(pvSys != NULL);
dw = *(dword *)pvSys;
Assert(dw == dwCanary);
lcb = *(dword *)((byte *)pvSys + sizeof(dword));
dw = *(dword *)((byte *)pv + lcb);
Assert(dw == dwCanary);
GlobalFree((HGLOBAL)pvSys);
#else
GlobalFree((HGLOBAL)pv);
#endif
us.cAlloc--;
}
// Redraw the screen, if Allow Partial Screen Updates is on. An action that
// modifies the bitmap has reached an intermediate point, where the screen
// may be updated here. This is half way between not updating the screen until
// the action has been completed, and updating after every pixel change.
void UpdateDisplay()
{
int xlSav, ylSav, xhSav, yhSav;
if (!ws.fAllowUpdate && !gs.fTraceDot)
return;
xlSav = xl; ylSav = yl; xhSav = xh; yhSav = yh;
DirtyView();
xl = xlSav; yl = ylSav; xh = xhSav; yh = yhSav;
}
// Draw a monochrome or color pixel on the window. Used to update the screen
// when one pixel of the bitmap changes, to avoid redrawing the whole bitmap.
void ScreenDot(int x, int y, bit o, KV kv)
{
HDC hdc;
HBRUSH hbr, hbrPrev;
int xpDot, ypDot, i;
MSG msg;
flag fColor = (kv != ~0);
if (dr.fInside || !ws.fNoDirty)
return;
if (!fColor)
SetPixel(wi.hdcBmp, x, y, o ? kvWhite : kvBlack);
else
SetPixel(wi.hdcBmp, x, y, kv);
hdc = GetDC(wi.hwnd);
InitDC(hdc);
if (bm.xa > 0 && bm.ya > 0 && bm.nHow < 3) {
if (!fColor)
hbr = CreateSolidBrush(o ? dr.kvOn : dr.kvOff);
else
hbr = CreateSolidBrush(kv);
hbrPrev = (HBRUSH)SelectObject(hdc, hbr);
xpDot = bm.xpOrigin + (x - bm.xaOrigin) * bm.xpPoint;
ypDot = bm.ypOrigin + (y - bm.yaOrigin) * bm.ypPoint;
PatBlt(hdc, xpDot, ypDot, bm.xpPoint, bm.ypPoint, PATCOPY);
SelectObject(hdc, hbrPrev);
DeleteObject(hbr);
}
ReleaseDC(wi.hwnd, hdc);
// Pause for the Pixel Display Delay time.
for (i = 0; i < ws.nDisplayDelay; i++) {
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
// Pressing Space will interrupt this and all future pixel delays.
if (GetAsyncKeyState(VK_SPACE) != 0) {
ws.nDisplayDelay = 0;
break;
}
}
}
// Bring up the Windows color picker dialog. Return the color selected.
KV KvDialog()
{
cc.rgbResult = dr.kvDot;
cc.lpCustColors = (ulong *)ws.rgkv;
ChooseColor(&cc);
ws.cDialog++;
return cc.rgbResult;
}
/*
******************************************************************************
** Main Windows Processing
******************************************************************************
*/
// The main program, the starting point of Daedalus, follows. This is like the
// "main" function in standard C. The program initializes here, then spins in
// a tight message processing loop until it terminates.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
WNDCLASS wndclass;
MSG msg;
HDC hdc;
time_t lTime;
int i, j;
char ch;
// This is where it all begins. Welcome! :)
ws.szAppName = szDaedalus;
ws.szFileTemp = szFileTempCore;
// Check for being used as a screen saver.
if (lpszCmdLine[0] == '/') {
ch = lpszCmdLine[1];
if (ch == 's')
ws.fSaverRun = fTrue;
else if (ch == 'c' || ch == 'a')
ws.fSaverConfig = fTrue;
else if (ch == 'p')
return -1L;
else if (ch == 'w')
ws.fNoWindow = fTrue;
}
// Set up the window class shared by all instances of Daedalus.
wi.hinst = hinstance;
if (!hPrevInstance) {
memset(&wndclass, 0, sizeof(WNDCLASS));
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNWINDOW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = wi.hinst;
wndclass.hIcon = LoadIcon(wi.hinst, MAKEINTRESOURCE(icon));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszMenuName = MAKEINTRESOURCE(menu);
wndclass.lpszClassName = ws.szAppName;
if (!RegisterClass(&wndclass)) {
PrintSz_E("The window class could not be registered.");
return -1L;
}
}
// Create the actual window to be used and drawn on by this instance.
wi.hmenu = LoadMenu(wi.hinst, MAKEINTRESOURCE(menu));
wi.hwndMain = CreateWindow(
ws.szAppName,
szDaedalus " " szVersion,
WS_CAPTION |
WS_SYSMENU |
WS_MINIMIZEBOX |
WS_MAXIMIZEBOX |
WS_THICKFRAME |
WS_VSCROLL |
WS_HSCROLL |
WS_CLIPCHILDREN |
WS_OVERLAPPED,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
(HWND)NULL,
wi.hmenu,
wi.hinst,
NULL);
if (wi.hwndMain == NULL) {
PrintSz_E("The window could not be created.");
return -1L;
}
wi.haccel = LoadAccelerators(wi.hinst, MAKEINTRESOURCE(accelerator));
if (!ws.fNoWindow)
ShowWindow(wi.hwndMain, nCmdShow);
// Set up some globals that can't be initialized at compile time.
CoInitialize(NULL);
ofn.hwndOwner = wi.hwndMain;
ofn.lpstrFile = ws.szFileName;
ofn.lpstrFileTitle = ws.szFileTitle;
cc.hwndOwner = wi.hwndMain;
for (i = 0; i < cColorMain; i++) {
j = i*16 + 15;
ws.rgkv[i] = GrayN(j);
}
wi.bi.biSize = sizeof(BITMAPINFOHEADER);
wi.bi.biPlanes = 1;
wi.bi.biBitCount = 1;
wi.bi.biCompression = BI_RGB;
wi.bi.biSizeImage = 0;
wi.bi.biXPelsPerMeter = wi.bi.biYPelsPerMeter = 1000;
wi.bi.biClrUsed = 0;
wi.bi.biClrImportant = 0;
wi.rgbq[0].rgbBlue = wi.rgbq[0].rgbGreen = wi.rgbq[0].rgbRed = 0;
wi.rgbq[1].rgbBlue = wi.rgbq[1].rgbGreen = wi.rgbq[1].rgbRed = 255;
wi.rgbq[0].rgbReserved = wi.rgbq[1].rgbReserved = 0;
wi.biC = wi.bi;
wi.biC.biBitCount = cbPixelC << 3;
wi.biI = wi.biC;
hdc = GetDC(wi.hwnd);
wi.hdcBmp = CreateCompatibleDC(hdc);
ReleaseDC(wi.hwnd, hdc);
wi.hbmp = CreateCompatibleBitmap(wi.hdcBmp, xStart, yStart);
wi.hbmpPrev = (HBITMAP)SelectObject(wi.hdcBmp, wi.hbmp);
GetAsyncKeyState(VK_SPACE); // Clear state for commands that check this.
ws.timeReset = GetTickCount();
// Create the default Maze seen when the program starts.
if (!bm.b.FAllocate(xStart, yStart, NULL))
return -1L;
InitRndL(109);
bm.b.CreateMazePerfect();
us.fRndOld = fFalse;
ms.nHuntType = 2;
CopyBitmapToDeviceBitmap();
time(&lTime);
InitRndL((long)lTime ^ ws.timeReset);
#ifdef ASSERT
// Ensure there's nothing wrong with the various action tables.
i = ccmd;
if (i != _APS_NEXT_COMMAND_VALUE - icmdBase)
PrintSzNN_E("Command table sizes %d and %d differ.",
ccmd, _APS_NEXT_COMMAND_VALUE - icmdBase);
for (i = 0; i < ccmd; i++) {
if ((int)rgcmd[i].wCmd != i + 1001) {
PrintSzNN_E("Command table entry %d contains command %d.",
i, rgcmd[i].wCmd);
break;
}
}
for (i = 0; i < copr; i++) {
if (rgopr[i].iopr != i) {
PrintSzNN_E("Operation table entry %d contains operation %d.",
i, rgopr[i].iopr);
break;
}
}
for (i = 0; i < cvar; i++) {
if (rgvar[i].ivar != i) {
PrintSzNN_E("Variable table entry %d contains variable %d.",
i, rgvar[i].ivar);
break;
}
}
for (i = 0; i < cfun; i++) {
if (rgfun[i].ifun != i) {
PrintSzNN_E("Function table entry %d contains function %d.",
i, rgfun[i].ifun);
break;
}
}
#endif
// Handle actions on the Windows command line that started the program.
if (rgszStartup[0] != NULL)
RunCommandLines(rgszStartup);
if (!ws.fSaverRun && !ws.fSaverConfig && !ws.fNoWindow) {
if (lpszCmdLine[0] != chNull)
RunCommandLine(lpszCmdLine, NULL);
FBootExternal("daedalus.ds", fTrue);
} else {
if (ws.fSaverRun) {
ShowCursor(fFalse);
DoCommand(cmdWindowFull);
}
if (ws.fNoWindow) {
FBootExternal("daedalus.ds", fTrue);
ws.fQuitting = fTrue;
} else if (!FBootExternal("daedalus.ds", fTrue)) {
dr.nFrameXY = dr.nFrameD = dr.nFrameZ = 90;
DoCommand(cmdInsideView);
DoCommand(cmdGoMiddle);
DoCommand(cmdMoveRandom);
DoCommand(cmdSpree);
}
}
// Process window messages until the program is told to terminate.
ws.fStarting = fFalse;
while (!ws.fQuitting && GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_KEYDOWN)
ws.nKey = (int)msg.wParam;
if (!TranslateAccelerator(wi.hwndMain, wi.haccel, &msg))
TranslateMessage(&msg);
DispatchMessage(&msg);
}
wi.hwndMain = NULL;
// The program is terminating. Free all memory that's been allocated.
bm.b.Free();
bm.b2.Free();
bm.b3.Free();
bm.k.Free();
bm.k2.Free();
bm.k3.Free();
bm.kI.Free();
bm.kR.Free();
bm.kS.Free();
if (bm.coor != NULL)
DeallocateP(bm.coor);
if (bm.patch != NULL)
DeallocateP(bm.patch);
if (ds.rgstar != NULL)
DeallocateP(ds.rgstar);
if (dr.rgcalc != NULL)
DeallocateP(dr.rgcalc);
if (dr.rgstar != NULL)
DeallocateP(dr.rgstar);
if (dr.rgmtn != NULL)
DeallocateP(dr.rgmtn);
if (dr.rgcld != NULL)
DeallocateP(dr.rgcld);
if (dr.rgtrans != NULL)
DeallocateP(dr.rgtrans);
if (ms.rgnInfEller != NULL)
DeallocateP(ms.rgnInfEller);
if (ws.rgbCmdMacro != NULL)
DeallocateP(ws.rgbCmdMacro);
if (ws.rgszMacro != NULL) {
for (i = 0; i < ws.cszMacro; i++)
if (ws.rgszMacro[i] != NULL)
DeallocateP(ws.rgszMacro[i]);
DeallocateP(ws.rgszMacro);
}
if (ws.rglVar != NULL)
DeallocateP(ws.rglVar);
if (ws.rgszVar != NULL) {
for (i = 0; i < ws.cszVar; i++)
if (ws.rgszVar[i] != NULL)
DeallocateP(ws.rgszVar[i]);
DeallocateP(ws.rgszVar);
}
DeallocateTextures();
if (ws.rgsTrieAlloc != NULL)
DeallocateP(ws.rgsTrieAlloc);
if (ws.rgsTrieConst != NULL)
DeallocateP(ws.rgsTrieConst);
if (ws.rgnTrieConst != NULL)
DeallocateP(ws.rgnTrieConst);
if (ms.fileInf != NULL)
fclose(ms.fileInf);
// Check for memory leaks.
if (gs.fErrorCheck && us.cAlloc != 0)
PrintSzL_E("Number of allocations not freed before exiting: %ld",
us.cAlloc);
Assert(ws.nMacroDepth == 0);
// Free Windows resources and unregister the window class.
CoUninitialize();
if (wi.hMutex != NULL)
CloseHandle(wi.hMutex);
SelectObject(wi.hdcBmp, wi.hbmpPrev);
DeleteObject(wi.hbmp);
if (wi.hbmpC != NULL)
DeleteObject(wi.hbmpC);
if (wi.hbmpI != NULL)
DeleteObject(wi.hbmpI);
DeleteDC(wi.hdcBmp);
ClearPb(&wndclass, sizeof(WNDCLASS));
UnregisterClass(ws.szAppName, wi.hinst);
// Daedalus goes away for real here. Bye! :)
return (int)msg.wParam;
}
// Execute a Windows command. This gets run when a menu option is manually
// selected, and when a command is automatically invoked via scripting.
flag DoCommandW(int wCmd)
{
RECT rc;
POINT pt;
char sz[cchSzDef], *pch;
int i, j, k, d;
CMaz bT;
CMap3 *pbT;
long l;
flag fDidRedraw = fFalse;
switch (wCmd) {
// File menu
case cmdPrint:
if (!DlgPrint())
PrintSz_W("Printing did not complete successfully.");
break;
case cmdPrintSetup:
l = prd.Flags;
prd.Flags |= PD_PRINTSETUP;
PrintDlg((LPPRINTDLG)&prd);
prd.Flags = l;
break;
case cmdDlgFile:
DoDialog(DlgFile, dlgFile);
break;
case cmdShellHelp:
FBootExternal("daedalus.htm", fFalse);
break;
case cmdShellScript:
FBootExternal("script.htm", fFalse);
break;
case cmdShellChange:
FBootExternal("changes.htm", fFalse);
break;
case cmdShellLicense:
FBootExternal("license.htm", fFalse);
break;
case cmdShellWeb:
FBootExternal("daedalus.url", fFalse);
break;
case cmdShellWeb2:
FBootExternal("daedlus2.url", fFalse);
break;
case cmdSetupAll:
case cmdSetupUser:
if (!FCreateProgramGroup(wCmd == cmdSetupAll))
PrintSz_E("Failed to create program group.\nDaedalus "
"can still be run from other icons or directly from its folder.");
break;
case cmdSetupDesktop:
if (!FCreateDesktopIcon())
PrintSz_E("Failed to create desktop icon.");
break;
case cmdSetupExtension:
if (!FRegisterExtensions())
PrintSz_E("Failed to register Daedalus file extensions.\nThis "
"error is ignorable and all features of Daedalus will still work.");
break;
case cmdUnsetup:
if (!FUnregisterExtensions())
PrintSz_E("Failed to unregister Daedalus file extensions.");
break;
case cmdAbout:
DoDialog(DlgAbout, dlgAbout);
break;
// Edit menu
case cmdSpree:
inv(ws.fSpree);
EnsureTimer(ws.nTimerDelay);
CheckMenu(cmdSpree, ws.fSpree);
break;
case cmdCommand:
DoDialog(DlgCommand, dlgCommand);
break;
case cmdDlgEvent:
DoDialog(DlgEvent, dlgEvent);
break;
case cmdCopyBitmap:
case cmdCopyText:
case cmdCopyPicture:
FFileCopy(wCmd);
break;
case cmdPaste:
FFilePaste();
break;
case cmdDlgDisplay:
DoDialog(DlgDisplay, dlgDisplay);
break;
case cmdWindowFull:
if (!ws.fWindowFull) {
GetWindowRect(wi.hwnd, &wi.rcFull);
wi.rcFull.right -= wi.rcFull.left; wi.rcFull.bottom -= wi.rcFull.top;
pt.x = pt.y = 0;
ClientToScreen(wi.hwnd, &pt);
ws.fWindowTop = fTrue;
GetWindowRect(GetDesktopWindow(), &rc);
if (!SetWindowPosition(rc.left + (wi.rcFull.left - pt.x),
rc.top + (wi.rcFull.top - pt.y),
rc.right + (wi.rcFull.right - ws.xpClient),
rc.bottom + (wi.rcFull.bottom - ws.ypClient))) {
PrintSz_E("Failed to enter full screen mode.\n"
"You may need to run Daedalus with greater permissions for full "
"screen mode to succeed.");
break;
}
} else {
ws.fWindowTop = fFalse;
if (wi.rcFull.left < 0)
wi.rcFull.left = 0;
if (wi.rcFull.top < 0)
wi.rcFull.top = 0;
SetWindowPosition(wi.rcFull.left, wi.rcFull.top,
wi.rcFull.right, wi.rcFull.bottom);
}
inv(ws.fWindowFull);
CheckMenu(cmdWindowFull, ws.fWindowFull);
break;
case cmdWindowBitmap:
if (bm.nHow < 3) {
GetWindowRect(wi.hwnd, &rc);
pbT = PbFocus();
MoveWindow(wi.hwnd, rc.left, rc.top,
pbT->m_x * bm.xpPoint + (rc.right - rc.left - ws.xpClient),
pbT->m_y * bm.ypPoint + (rc.bottom - rc.top - ws.ypClient), fTrue);
}
break;
case cmdRedrawNow:
CopyBitmapToDeviceBitmap();
Redraw(wi.hwnd);
fDidRedraw = fTrue;
break;
case cmdRepaintNow:
Redraw(wi.hwnd);
GetAsyncKeyState(VK_SPACE); // Clear state as other commands check this.
fDidRedraw = fTrue;
break;
case cmdScrollUp:
PostMessage(wi.hwnd, WM_VSCROLL, SB_PAGEUP, 0);
break;
case cmdScrollDown:
PostMessage(wi.hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
break;
case cmdScrollHome:
PostMessage(wi.hwnd, WM_HSCROLL, SB_THUMBPOSITION, 0);
PostMessage(wi.hwnd, WM_VSCROLL, SB_THUMBPOSITION, 0);
break;
case cmdScrollEnd:
PostMessage(wi.hwnd, WM_HSCROLL, SB_THUMBPOSITION, nScrollDiv);
PostMessage(wi.hwnd, WM_VSCROLL, SB_THUMBPOSITION, nScrollDiv);
break;
case cmdDlgColor:
DoDialog(DlgColor, dlgColor);
break;
case cmdTimeGet:
l = (GetTimer() + 500) / 1000;
SetMacroReturn(l);
i = (l % 60L); l /= 60L;
j = (l % 60L); l /= 60L;
k = (l % 24L); l /= 24L;
d = l;
sprintf(S(sz), "Time elapsed: ");
pch = sz + CchSz(sz);
if (d > 0) {
sprintf(SO(pch, sz), "%d day%s, ", d, d == 1 ? "" : "s");
while (*pch)
pch++;
}
if (d > 0 || k > 0) {
sprintf(SO(pch, sz), "%d hour%s, ", k, k == 1 ? "" : "s");
while (*pch)
pch++;
}
if (d > 0 || k > 0 || j > 0) {
sprintf(SO(pch, sz), "%d minute%s, ", j, j == 1 ? "" : "s");
while (*pch)
pch++;
}
sprintf(SO(pch, sz), "%d second%s", i, i == 1 ? "" : "s");
PrintSz_N(sz);
break;
case cmdTimeReset:
ws.timeReset = GetTickCount();
ws.timePause = ws.timeReset;
break;
case cmdTimePause:
PauseTimer(!ws.fTimePause);
break;
case cmdDlgRandom:
DoDialog(DlgRandom, dlgRandom);
break;
// Dot menu
case cmdDlgDot:
DoDialog(DlgDot, dlgDot);
break;
case cmdDlgInside:
DoDialog(DlgInside, dlgInside);
break;
// Bitmap menu
case cmdDlgSize:
DoDialog(DlgSize, dlgSize);
break;
case cmdDlgZoom:
DoDialog(DlgZoom, dlgZoom);
break;
case cmdCubemapFlip:
DoDialog(DlgCubeFlip, dlgCubeFlip);
break;
// Color menu
case cmdColmapBrightness:
DoDialog(DlgBright, dlgBright);
break;
case cmdColmapReplace:
DoDialog(DlgColorReplace, dlgColorReplace);
break;
// Maze menu
case cmdDlgSizeMaze:
DoDialog(DlgSizeMaze, dlgSizeMaze);
break;
case cmdBias:
DoDialog(DlgBias, dlgBias);
break;
case cmdDlgMaze:
DoDialog(DlgMaze, dlgMaze);
break;
// Create menu
case cmdDlgLabyrinth:
DoDialog(DlgLabyrinth, dlgLabyrinth);
break;
case cmdDlgCreate:
DoDialog(DlgCreate, dlgCreate);
break;
// Solve menu
// Draw menu
case cmdDlgDraw:
DoDialog(DlgDraw, dlgDraw);
break;
case cmdDlgDraw2:
DoDialog(DlgDraw2, dlgDraw2);
break;
}
return fDidRedraw;
}
#define FKey(ch) (rgbKey[ch] >= 128)
// This is the main message processor for the Daedalus window. Given a user
// input or other message, do the appropriate action and updates.
LRESULT WINAPI WndProc(HWND hwnd, uint wMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
POINT pt;
MSG msg;
int iParam, icmd, x, y, xmax, ymax, i;
byte rgbKey[256];
flag fKey8, fKey4, fKey6, fKey2, fKeyU, fKeyD, fKeyShift, fArrow, fRedraw;
wi.hwnd = hwnd;
switch (wMsg) {
// A command, most likely a menu option, was given.
case WM_COMMAND:
if (ws.fSaverRun)
goto LClose;
iParam = (int)wParam & 0xFFFF;
icmd = iParam - icmdBase;
if (icmd < 0 || icmd >= ccmd)
return DefWindowProc(hwnd, wMsg, wParam, lParam);
if (ws.iEventCommand > 0 && iParam != cmdAbout &&
FCheckEvent(ws.iEventCommand, ws.nKey, icmd, 0))
break;
if (ws.rgbCmdMacro != NULL && iParam != cmdAbout) {
if (ws.rgbCmdMacro[icmd] == 255) {
if (!ws.fIgnorePrint) {
PrintSz_W("This command has been disabled.\n");
ws.fSkipSystem = (iParam == cmdMacro40/*Alt+F4*/);
}
break;
}
if (ws.rgbCmdMacro[icmd] != 0) {
if (FCheckEvent(ws.rgbCmdMacro[icmd], ws.nKey, icmd, 0)) {
ws.fSkipSystem = (iParam == cmdMacro40/*Alt+F4*/);
break;
}
}
}
ws.nKey = 0;
DoCommand(iParam);
ws.fSkipSystem =
(iParam == cmdMacro10/*F10*/ || iParam == cmdMacro22/*Shift+F10*/ ||
iParam == cmdMacro34/*Ctrl+F10*/ || iParam == cmdMacro40/*Alt+F4*/);
if (ws.fQuitting)
goto LClose;
break;
// A special system command was given. Suppress its action if it was invoked
// by a hotkey which ran a macro, to avoid both effects happening.
case WM_SYSCOMMAND:
iParam = (int)wParam & 0xFFF0;
if (ws.fSaverRun) {
if (iParam == SC_SCREENSAVE || iParam == SC_MONITORPOWER)
return fTrue;
goto LClose;
}
if (ws.fSkipSystem) {
if (iParam == SC_KEYMENU || iParam == SC_CLOSE) {
ws.fSkipSystem = fFalse;
break;
}
}
return DefWindowProc(hwnd, wMsg, wParam, lParam);
// Part of the window was uncovered. Usually Windows quickly blanks it out
// with white, before having the app redraw that section. Daedalus however
// can quickly update the screen, so doesn't want anything to happen here.
case WM_ERASEBKGND:
return fTrue;
// The window was just created. Setup the scrollbars.
case WM_CREATE:
SetScrollRange(hwnd, SB_HORZ, 0, nScrollDiv, fFalse);
SetScrollRange(hwnd, SB_VERT, 0, nScrollDiv, fFalse);
ws.xScroll = ws.yScroll = 0;
break;
// The window has been resized. Update our window size variables.
case WM_SIZE:
ws.xpClient = LOWORD(lParam);
ws.ypClient = HIWORD(lParam);
break;
// All or part of the window needs to be redrawn. Go do so.
case WM_PAINT:
ClearPb(&ps, sizeof(PAINTSTRUCT));
hdc = BeginPaint(hwnd, &ps);
SetBkMode(hdc, TRANSPARENT);
IntersectClipRect(hdc, ps.rcPaint.left, ps.rcPaint.top,
ps.rcPaint.right, ps.rcPaint.bottom);
Redraw(hwnd);
EndPaint(hwnd, &ps);
break;
// The left button of the mouse has been clicked over the window.
case WM_LBUTTONDOWN:
if (ws.fSaverRun)
goto LClose;
if (FCheckEventClick(ws.iEventLeft, (long)lParam))
break;
goto LMouse;
// The mouse is being dragged over the window.
case WM_MOUSEMOVE:
if (ws.fSaverRun) {
if (!ws.fSaverMouse) {