/
ASTEROID.C
1326 lines (1159 loc) · 45.1 KB
/
ASTEROID.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
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
/****************************************************************************
* ASTEROID.C *
* *
* Main program, PM input loop, and all PM specific functions (except *
* those included in SUBS.C for speed). *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **
* *
* -- ASTEROID is an OS/2 v2.0 Presentation Manager implementation of the *
* -- Atari coin-op arcade game Asteroids. *
* *
* ASTEROID is copyright (c) 1991-1993 by Todd B. Crowe. There is no *
* warrantee implied or otherwise. You may copy the program freely but it *
* must be accompanied by this notice. The program may not be sold or *
* distributed commercially or otherwise for profit. I reserve all rights *
* and privileges to this program. *
* *
****************************************************************************/
#define INCL_DOS
#define MAIN /* This is the main program */
#include "asteroid.h" /* Include game data, #def's, etc */
#include "pmdefs.h" /* Include PM specific stuff */
/* Local procedure prototyping */
VOID StartGame(INT);
VOID ProcessChar(CHAR, BOOL, CHAR, BOOL);
VOID SetText(HWND, INT, KEY *);
VOID InitFonts(HPS);
VOID InitMenu(VOID);
VOID DoCommand(HWND, ULONG, MPARAM, MPARAM);
BOOL TogglePause(INT);
VOID HideFrameControls(VOID);
VOID ShowFrameControls(VOID);
VOID ShowMouse(BOOL);
VOID Initialize(VOID);
VOID Shutdown(ULONG);
/* Local PM data items (nothing interesting) */
INT iTickLen = TIMER_TICK_LENGTH;
HAB hab;
HMQ hmq;
HWND hwndFrame,hwndObject,hwndClient,hwndTitleBar,
hwndSysMenu,hwndMinMax,hwndMenu,hwndHelp;
CHAR szClientClass[] = ASTEROID_VER;
HELPINIT hinit = { sizeof(HELPINIT), 0L, NULL,
(PHELPTABLE) MAKELONG(ID_RESOURCE, 0xFFFF),
NULLHANDLE, NULLHANDLE, 0, 0,
ASTEROID_VER, CMIC_HIDE_PANEL_ID, "ASTEROID.HLP"};
/****************************************************************************
* main *
* - Typical PM main skeleton. Creates object window for hiding frame *
* controls. *
* - No I/O *
****************************************************************************/
void main(void)
{
static ULONG flFrameFlags = FCF_SHELLPOSITION | FCF_SIZEBORDER |
FCF_TASKLIST | FCF_MINMAX |
FCF_TITLEBAR | FCF_SYSMENU |
FCF_MENU | FCF_ACCELTABLE;
QMSG qmsg;
hab = WinInitialize(0);
hmq = WinCreateMsgQueue(hab,0);
WinRegisterClass(hab, szClientClass, (PFNWP) ClientWndProc,
CS_SIZEREDRAW | CS_MOVENOTIFY, 0);
hwndFrame = WinCreateStdWindow(
HWND_DESKTOP, 0L, &flFrameFlags,
szClientClass, szClientClass, 0L, NULLHANDLE, ID_RESOURCE, &hwndClient);
/* If WM_CREATE did not fail, initialize and go into input loop */
if (hwndFrame != NULLHANDLE) {
/* Create an object window to pass parenthood of frame controls *
* to in order to hide them */
hwndObject = WinCreateWindow(HWND_OBJECT, WC_FRAME, (PUCHAR)" ",
0L, 0, 0, 0, 0, NULLHANDLE, HWND_TOP, ID_OBJECT, NULL, NULL);
/* Save control window handles for use in Show-/Hide-FrameControls */
hwndTitleBar = WinWindowFromID(hwndFrame, FID_TITLEBAR);
hwndSysMenu = WinWindowFromID(hwndFrame, FID_SYSMENU);
hwndMenu = WinWindowFromID(hwndFrame, FID_MENU);
hwndMinMax = WinWindowFromID(hwndFrame, FID_MINMAX);
Initialize();
while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
WinDispatchMsg(hab, &qmsg);
}
Shutdown(0L);
}
/****************************************************************************
* ClientWndProc *
* - Typical PM client window procedure. (see below) *
* - Standard client window I/O *
****************************************************************************/
MRESULT EXPENTRY ClientWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
RECTL rcl;
SWP swp;
HPS hpsPaint;
static HPS hps; /* Permanent HPS */
static INT cx, cy; /* Client window dimensions */
static BOOL bSlowUpdateNow = FALSE; /* Update toggle for asteroids *
* and enemy which are slow. */
static POINTL ptlCenter; /* Center of client window */
switch (msg) {
/* Recieved from WinCreateStdWindow */
case WM_CREATE:
/* Get permanent PS for entire window */
hps = WinGetPS(hwnd);
/* Load private Asteroid fonts from ASTEROID.DLL */
if (GpiLoadFonts(hab, "ASTEROID") != GPI_OK) {
WinReleasePS(hps);
WinAlarm(HWND_DESKTOP, WA_WARNING);
WinMessageBox(HWND_DESKTOP,NULLHANDLE,
"Please put ASTEROID.DLL in a directory in your LIBPATH.",
"Error reading ASTEROID.DLL",
0,MB_ICONHAND|MB_OK|MB_APPLMODAL);
WinPostMsg(hwnd,WM_QUIT,(MPARAM) 0L,(MPARAM) 0L);
return (MRESULT) TRUE;
}
/* Register/create logical fonts for use */
InitFonts(hps);
/* Display About dialoge box */
WinDlgBox(HWND_DESKTOP, hwnd, (PFNWP)AboutDlgProc, NULLHANDLE,
IDD_ABOUT, NULL);
return 0;
/* Recieved during attract mode when user starts game */
case WM_STARTGAME:
/* Determine the number of players */
cPlayers = (INT)LONGFROMMP(mp1);
/* Initialize each player */
for (Player=0;Player<cPlayers;Player++) {
Level[Player] = 1;
Ships[Player] = prfProfile.iSHIPS;
DeletePhotons();
InitAsteroids();
InitEnemy();
}
/* Start with player 1 */
Player = 0;
iGameMode = GAME_MODE_NEXT;
iGameModeCnt = GAME_PAUSE_TIME-1;
/* Hide the pointer if mouse controls are enabled */
ShowMouse(FALSE);
/* Paint everything */
WinSendMsg(hwnd, WM_PAINT, MPVOID, MPVOID);
return 0;
/* Recieved at startup and at the completion of a game */
case WM_INITGAME:
/* Make mouse visible if we hid it before */
ShowMouse(TRUE);
/* Fix menu to reflect attract mode */
EnableMenuItem(hwndMenu, IDM_START, TRUE);
EnableMenuItem(hwndMenu, IDM_STOP, FALSE);
/* Initialize player and enemy data structures */
cPlayers = 0;
Level[0] = 1;
for (Player=0;Player<2;Player++) {
Score[Player] = 0L;
Ships[Player] = 0;
DeletePhotons();
}
Player = 0;
/* Initialize asteroids (and enemy) for attract mode */
InitAsteroids();
InitEnemy();
/* Depending on whether ASTEROID was just started or a game just *
* completed display the "High Score" or "Press 1 or 2" screen. */
if (SHORT1FROMMP(mp1) == 0) {
iGameMode = GAME_MODE_INIT1;
iGameModeCnt = GAME_INIT_TIME;
}
else {
iGameMode = GAME_MODE_INIT2;
iGameModeCnt = GAME_INIT_TIME;
}
/* Paint everything */
WinSendMsg(hwnd, WM_PAINT, MPVOID, MPVOID);
return 0;
/* Usually recieved from the system, sometime forced by the program to *
* ensure the screen is not corrupt. */
case WM_PAINT:
/* Clear entire window to insure no "droppings" */
WinQueryWindowRect(hwnd,&rcl);
WinFillRect(hps, &rcl, CLR_BLACK);
WinInvalidateRect(hwnd, &rcl, FALSE);
/* Get the update region and paint it black */
hpsPaint = WinBeginPaint(hwnd, (HPS)NULL, &rcl);
WinFillRect(hpsPaint, &rcl, CLR_BLACK);
WinEndPaint(hpsPaint);
/* Only in normal play mode should we draw the ship */
if ((iGameMode == GAME_MODE_PLAY) &&
(iShipMode[Player] != EXPLOSION) &&
(iShipMode[Player] != HYPERSPACE))
DrawShip(hps, cx, cy, DRAW_INIT);
else if ((iGameMode == GAME_MODE_PLAY) &&
(iShipMode[Player] == EXPLOSION))
ExplodeShip(hps, cx, cy);
/* Draw the enemy if it is on the screen */
if (iEnemyMode[Player] != NONE)
if (iEnemyMode[Player] != EXPLOSION)
DrawEnemy(hps, cx, cy,DRAW_INIT);
else
ExplodeEnemy(hps, cx, cy);
/* Draw photons and asteroids in all modes but the "Enter your *
* initials" mode, otherwise draw that screen. */
if (iGameMode != GAME_MODE_HIGH) {
DrawPhotons(hps, cx, cy, DRAW_INIT);
DrawAsteroids(hps, cx, cy, DRAW_INIT);
}
else
DrawHighScore(hps, cx, cy, DRAW_INIT);
/* Always draw the score */
DrawScore(hps, cx, cy, DRAW_INIT);
return 0;
/* Left mouse button down. This simulates the move/track function *
* in the system menu. */
case WM_BUTTON1DOWN:
if (prfProfile.bMOUSECONTROL &&
iGameMode != GAME_MODE_INIT1 &&
iGameMode != GAME_MODE_INIT2 &&
!TogglePause(CHECK)) {
UPDATE_FIRE(iShipMode[Player], TRUE);
return (MRESULT)TRUE;
}
return WinSendMsg(hwndFrame, WM_TRACKFRAME,
(MPARAM) (SHORT1FROMMP(mp2) | TF_MOVE), MPVOID);
case WM_BUTTON1UP:
if (prfProfile.bMOUSECONTROL) {
UPDATE_FIRE(iShipMode[Player], FALSE);
return (MRESULT)TRUE;
}
return 0;
/* Left mouse button double clicked. Toggle frame control display. */
case WM_BUTTON1DBLCLK:
if (!prfProfile.bMOUSECONTROL ||
iGameMode == GAME_MODE_INIT1 ||
iGameMode == GAME_MODE_INIT2 ||
TogglePause(CHECK)) {
if (prfProfile.bCONTROLS = !prfProfile.bCONTROLS)
ShowFrameControls();
else
HideFrameControls();
}
return 0;
case WM_BUTTON2DOWN:
if (prfProfile.bMOUSECONTROL) {
UPDATE_SHIELD(iShipMode[Player], iShipShieldCnt[Player]);
return (MRESULT)TRUE;
}
return 0;
case WM_BUTTON2CLICK:
if (prfProfile.bMOUSECONTROL) {
UPDATE_HYPERSPACE(iShipMode[Player], iShipModeCnt[Player]);
return (MRESULT)TRUE;
}
return 0;
/* Right mouse button double clicked. Display the about dialog box. */
case WM_BUTTON2DBLCLK:
if (!prfProfile.bMOUSECONTROL ||
iGameMode == GAME_MODE_INIT1 ||
iGameMode == GAME_MODE_INIT2 ||
TogglePause(CHECK)) {
WinDlgBox(HWND_DESKTOP, hwndClient, (PFNWP)AboutDlgProc,
NULLHANDLE, IDD_ABOUT, NULL);
}
return 0;
/* User typed a key. Most of this is self explanatory. */
case WM_CHAR:
ProcessChar((CHAR) (CHARMSG(&msg)->vkey-1),
CHARMSG(&msg)->fs & KC_VIRTUALKEY,
(CHAR) (CHARMSG(&msg)->chr),
(BOOL) !(CHARMSG(&msg)->fs & KC_KEYUP));
return 0;
/* User entered a command via the menu bar. */
case WM_COMMAND:
DoCommand(hwnd, msg, mp1, mp2);
return 0;
/* Suspend/un-suspend game depending on focus */
case WM_SETFOCUS:
if ((BOOL) SHORT1FROMMP(mp2))
TogglePause(SUSPEND_OFF);
else if (!prfProfile.bBACKGRND)
TogglePause(SUSPEND_ON);
return 0;
/* Keep track of the client window size. Profile information is not *
* updated here because there are better places (i.e. at exit) */
case WM_SIZE:
cx = (INT)SHORT1FROMMP(mp2);
cy = (INT)SHORT2FROMMP(mp2);
/* Keep track of client window position. Also updates profile info. */
case WM_MOVE:
WinQueryWindowPos(hwndFrame,&swp);
if (!(swp.fl & SWP_MAXIMIZE) && !(swp.fl & SWP_MINIMIZE)) {
prfProfile.x = swp.x;prfProfile.y = swp.y;
prfProfile.cx = swp.cx;prfProfile.cy = swp.cy;
}
if (swp.fl & SWP_MINIMIZE)
if (!prfProfile.bBACKGRND) {
/* Set icon */
WinSendMsg(hwndFrame, WM_SETICON,
(MPARAM) WinLoadPointer(HWND_DESKTOP, NULLHANDLE,
ID_RESOURCE), MPVOID);
TogglePause(SUSPEND_ON);
}
else
WinSendMsg(hwndFrame, WM_SETICON, MPVOID, MPVOID);
else
TogglePause(SUSPEND_OFF);
ptlCenter.x = swp.cx / 2;
ptlCenter.y = swp.cy / 2;
WinMapWindowPoints(hwndClient, HWND_DESKTOP, &ptlCenter, 1L);
return 0;
/* Recieved approximately 31 times a second. This is the longest and *
* ugliest of the messages, partly because there are so many cases *
* to keep track of, partly because it must be highly optimized. */
case WM_TIMER:
if (prfProfile.bMOUSECONTROL &&
iGameMode != GAME_MODE_INIT1 &&
iGameMode != GAME_MODE_INIT2) { POINTL ptl;
static BOOL bUp, bLeft, bRight;
WinQueryPointerPos(HWND_DESKTOP, &ptl);
if (bUp || (ptl.y - ptlCenter.y > 0))
UPDATE_THRUST(iShipMode[Player],
bUp = (ptl.y - ptlCenter.y > 0));
if (bLeft || (ptlCenter.x - ptl.x > 0))
UPDATE_LEFT(iShipMode[Player],
bLeft = (ptlCenter.x - ptl.x > 0));
if (bRight || (ptlCenter.x - ptl.x < 0))
UPDATE_RIGHT(iShipMode[Player],
bRight = (ptlCenter.x - ptl.x < 0));
WinSetPointerPos(HWND_DESKTOP, ptlCenter.x, ptlCenter.y);
}
/* Determine the current game mode */
switch (iGameMode) {
/* Either initialization/attract mode screen. */
case GAME_MODE_INIT1: case GAME_MODE_INIT2:
/* Switch screens when count expires */
if (--iGameModeCnt == 0) {
if (iGameMode == GAME_MODE_INIT1)
iGameMode = GAME_MODE_INIT2;
else
iGameMode = GAME_MODE_INIT1;
iGameModeCnt = GAME_INIT_TIME;
/* Score must be redrawn because the attract mode screens *
* draw the score differently. */
DrawScore(hps, cx, cy, DRAW_REINIT);
}
/* Update photons, asteroids, enemy, and score */
UpdatePhotons(hps, cx, cy);
if (uiSpeed == SPEED_OS2 || (bSlowUpdateNow = !bSlowUpdateNow)) {
UpdateAsteroids(hps, cx, cy);
UpdateEnemy(hps, cx, cy);
DrawScore(hps, cx, cy, DRAW_REFRESH);
}
break;
/* Completion of one player's turn or new game */
case GAME_MODE_NEXT:
/* Initially, erase and redraw everything for new player */
if (iGameModeCnt-- == GAME_PAUSE_TIME) {
if ((cPlayers == MAXPLAYERS) &&
(Ships[(Player+1) % MAXPLAYERS])) {
DrawAsteroids(hps, cx, cy, DRAW_ERASE);
DrawPhotons(hps, cx, cy, DRAW_ERASE);
Player = (Player+1) % MAXPLAYERS;
}
DrawScore(hps, cx, cy, DRAW_REINIT);
DrawAsteroids(hps, cx, cy, DRAW_INIT);
}
/* During countdown update score and asteroids */
else if (iGameModeCnt > 0) {
if (uiSpeed == SPEED_OS2 || (bSlowUpdateNow = !bSlowUpdateNow)) {
DrawScore(hps, cx, cy, DRAW_REFRESH);
UpdateAsteroids(hps, cx, cy);
}
}
/* At end of countdown start the player */
else {
InitShip();
InitEnemy();
iGameMode = GAME_MODE_PLAY;
DrawScore(hps, cx, cy, DRAW_REINIT);
}
break;
/* Normal play mode */
case GAME_MODE_PLAY:
/* Update ship, photons, asteroids, enemy, and score */
UpdateShip(hps, cx, cy);
UpdatePhotons(hps, cx, cy);
if (uiSpeed == SPEED_OS2 || (bSlowUpdateNow = !bSlowUpdateNow)) {
UpdateAsteroids(hps, cx, cy);
UpdateEnemy(hps, cx, cy);
/* Erase old and draw new scores if there is a change*/
if (bChangeScore) {
bChangeScore = FALSE;
DrawScore(hps, cx, cy, DRAW_REINIT);
}
/* Else just refresh the score */
else
DrawScore(hps, cx, cy, DRAW_REFRESH);
}
break;
/* Game over mode. This is the longest and ugliest case because *
* conditions are highly dependent on the number of players, *
* multiplayer game status, and the number and order of high *
* scores. */
case GAME_MODE_OVER:
/* Initially, just update the score and number of ships */
if (iGameModeCnt-- == GAME_PAUSE_TIME)
DrawScore(hps, cx, cy, DRAW_REINIT);
/* During countdown refresh the score and update the asteroids */
else if (iGameModeCnt > 0) {
if (uiSpeed == SPEED_OS2 || (bSlowUpdateNow = !bSlowUpdateNow)) {
DrawScore(hps, cx, cy, DRAW_REFRESH);
UpdateAsteroids(hps, cx, cy);
}
}
/* At the end of the countdown, if there are any other players, *
* continue with them. */
else {
/* Countinue on with any remaining players. */
if ((cPlayers == MAXPLAYERS) &&
(Ships[(Player+1) % MAXPLAYERS])) {
/* Erase all of the old asteroids. */
DrawAsteroids(hps, cx, cy, DRAW_ERASE);
/* Setup everything for the next player */
Player = (Player+1) % MAXPLAYERS;
InitShip();
InitEnemy();
iGameMode = GAME_MODE_PLAY;
DrawAsteroids(hps, cx, cy, DRAW_INIT);
DrawScore(hps, cx, cy, DRAW_REINIT);
}
/* Check for new high scores and update table as necessary. */
else {
/* Erase all of the old asteroids. */
DrawAsteroids(hps, cx, cy, DRAW_ERASE);
/* The following if/else block is admittedly a kludge, it is *
* simple and it does work, however. Ideally it should *
* sort the high scores and update the high score table in *
* descending order. */
/* If player 1 scored higher than player 2 then check *
* player 1 first for a high score. */
if (Score[0] > Score[1])
for (Player=0;Player<cPlayers;Player++)
/* If the player's score is > than the lowest, *
* update the high score table. */
if (Score[Player] > prfProfile.lSCORES[9]) {
UpdateHighScores();
iGameMode = GAME_MODE_HIGH;
}
/* Otherwise, make sure he is not asked for his *
* initials. */
else
Score[Player] = 0;
/* Otherwise, check player 2 first */
else
for (Player=cPlayers;Player>=0;Player--)
/* If the player's score is > than the lowest, *
* update the high score table. */
if (Score[Player] > prfProfile.lSCORES[9]) {
UpdateHighScores();
iGameMode = GAME_MODE_HIGH;
}
/* Otherwise, make sure he is not asked for his *
* initials. */
else
Score[Player] = 0L;
/* If there was no high score, go into attract mode */
if (iGameMode != GAME_MODE_HIGH)
WinSendMsg(hwnd,WM_INITGAME,MPFROMSHORT(1),(MPARAM) 0L);
/* Else, check for player 1's initials first then 2's *
* This is not faithful, in the arcade game the player *
* with the higher score always goes first. */
else {
if (Score[0] > 0L)
Player = 0;
else
Player = 1;
DrawScore(hps, cx, cy, DRAW_REINIT);
DrawHighScore(hps, cx, cy, DRAW_INIT);
}
}
}
break;
/* Mode which prompts players to enter their initials */
case GAME_MODE_HIGH:
/* If the player's position is > 0 then refresh the screen */
if (Score[Player] > 0L)
DrawHighScore(hps, cx, cy, DRAW_REFRESH);
/* Else, the current player is done go to the next */
else if ((cPlayers == MAXPLAYERS) && (Player == 0) &&
(Score[1] > 0L)) {
Player++;
DrawHighScore(hps, cx, cy, DRAW_REINIT);
}
/* If there are no more high scores then go into attract mode */
else
WinSendMsg(hwnd, WM_INITGAME, MPFROMSHORT(1), MPVOID);
break;
}
return 0;
/* Used by help manager */
case HM_QUERY_KEYS_HELP:
return((MRESULT)IDH_CLIENTKEYS);
/* Recieved always from the system or in the case of an initialization*
* error. Both messages will normally save the profile information.*
* Ideally the profile section should be moved to a subroutine and the*
* the following should be broken into two distinct cases. */
case WM_SAVEAPPLICATION:
case WM_DESTROY:
/* If the fonts were not found bApplicationOk will be false, in *
* that case the window profile information should not be updated.*/
if (TRUE) {
/* Copy window position and size info into profile structure */
WinQueryWindowPos(hwndFrame, &swp);
if (swp.fl & SWP_MAXIMIZE)
prfProfile.ulMINMAX = SWP_MAXIMIZE;
else if (swp.fl & SWP_MINIMIZE)
prfProfile.ulMINMAX = SWP_MINIMIZE;
else {
prfProfile.ulMINMAX = 0;
prfProfile.x = swp.x;prfProfile.y = swp.y;
prfProfile.cx = swp.cx;prfProfile.cy = swp.cy;
}
/* Write profile information */
PrfWriteProfileData(HINI_USERPROFILE, szClientClass, "Data",
&prfProfile, sizeof(PROFILEREC));
}
/* If the application is terminating release the fonts and the hps. */
if (msg == WM_DESTROY) {
/* Make sure mouse is visible if we hid it before */
ShowMouse(TRUE);
/* Release font identifiers and DLL resource module */
GpiSetCharSet(hps, LCID_DEFAULT);
GpiDeleteSetId(hps, LCID_LARGE);
GpiDeleteSetId(hps, LCID_SMALL);
GpiUnloadFonts(hab, "ASTEROID");
/* Release the "permanent" presentation space */
WinReleasePS(hps);
}
return 0;
}
return WinDefWindowProc(hwnd, msg, mp1, mp2);
}
/****************************************************************************
* StartGame *
* - Starts a new 1 or 2 player game based on the value in sPlyrs. *
****************************************************************************/
VOID StartGame(INT iPlyrs)
{
/* Fix check marks for related menu items */
EnableMenuItem(hwndMenu, IDM_START, FALSE);
EnableMenuItem(hwndMenu, IDM_STOP, TRUE);
/* Request a new game */
WinPostMsg(hwndClient, WM_STARTGAME, MPFROMLONG((LONG)iPlyrs), MPVOID);
}
/****************************************************************************
* ProcessChar *
* Processes WM_CHAR messages for ClientWndProc *
* Receives the virtual key and character codes *
****************************************************************************/
VOID ProcessChar(CHAR vkey, BOOL vkv, CHAR chr, BOOL keydown)
{
BOOL bKeyPress = TRUE;
static INT iSpeed = 0, iPos = 0;
static CHAR szSpeed[2][8] = { "Windows", "OS/2" };
if (!vkv && !keydown) {
if (iSpeed == 0 && chr == szSpeed[0][iPos] ||
iSpeed == 1 && chr == szSpeed[1][iPos]) {
iPos++;
} else if (iSpeed == 0 && chr == szSpeed[1][iPos]) {
iPos = 1;
iSpeed = 1;
} else if (iSpeed == 1 && chr == szSpeed[0][iPos]) {
iPos = 1;
iSpeed = 0;
} else {
iPos = 0;
}
if (iSpeed == 0 && iPos == 7) {
iPos = 0;
if (uiSpeed == SPEED_WINDOWS) {
uiSpeed = SPEED_NORMAL;
iTickLen = TIMER_TICK_LENGTH;
WinStartTimer(hab, hwndClient, ID_TIMER, iTickLen);
} else {
uiSpeed = SPEED_WINDOWS;
iTickLen = TIMER_TICK_LENGTH*4;
WinStartTimer(hab, hwndClient, ID_TIMER, iTickLen);
}
} else if (iSpeed == 1 && iPos == 4) {
iPos = 0;
if (uiSpeed == SPEED_WINDOWS) {
uiSpeed = SPEED_OS2;
iTickLen = TIMER_TICK_LENGTH;
WinStartTimer(hab, hwndClient, ID_TIMER, iTickLen);
} else if (uiSpeed == SPEED_OS2) {
uiSpeed = SPEED_NORMAL;
} else {
uiSpeed = SPEED_OS2;
}
}
}
if ((iGameMode == GAME_MODE_HIGH) && !keydown)
return;
if ((vkv && (vkey == prfProfile.keyDEFS[4].vk)) ||
(!vkv && (chr == prfProfile.keyDEFS[4].chr))) {
if (iShipMode[Player] & FIRERELEASE) {
if (!keydown) {
iShipMode[Player] &= ~FIRERELEASE;
}
} else {
UPDATE_FIRE(iShipMode[Player], keydown);
}
} else if ((vkv && (vkey == prfProfile.keyDEFS[0].vk)) ||
(!vkv && (chr == prfProfile.keyDEFS[0].chr))) {
UPDATE_LEFT(iShipMode[Player], keydown);
} else if ((vkv && (vkey == prfProfile.keyDEFS[1].vk)) ||
(!vkv && (chr == prfProfile.keyDEFS[1].chr))) {
UPDATE_RIGHT(iShipMode[Player], keydown);
} else if ((vkv && (vkey == prfProfile.keyDEFS[2].vk)) ||
(!vkv && (chr == prfProfile.keyDEFS[2].chr))) {
UPDATE_THRUST(iShipMode[Player], keydown);
} else if ((vkv && (vkey == prfProfile.keyDEFS[3].vk)) ||
(!vkv && (chr == prfProfile.keyDEFS[3].chr))) {
UPDATE_HYPERSPACE(iShipMode[Player], iShipModeCnt[Player]);
} else if ((vkv && (vkey == prfProfile.keyDEFS[5].vk )) ||
(!vkv && (chr == prfProfile.keyDEFS[5].chr))) {
UPDATE_SHIELD(iShipMode[Player], iShipShieldCnt[Player]);
} else {
bKeyPress = FALSE;
}
if (bKeyPress && TogglePause(CHECK))
TogglePause(TOGGLE);
}
/****************************************************************************
* AboutDlgProc *
* - Typical PM dialog window procedure. (Almost skeletal) *
* - Standard dialog window I/O *
****************************************************************************/
MRESULT EXPENTRY AboutDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
switch (msg) {
case WM_COMMAND:
switch (COMMANDMSG(&msg)->cmd) {
case DID_CANCEL:
/* On cancel, tell application to terminate */
WinPostMsg(hwndFrame, WM_QUIT, 0L, 0L);
case DID_OK:
WinDismissDlg(hwnd, 0);
return 0;
}
break;
}
return WinDefDlgProc(hwnd, msg, mp1, mp2);
}
/****************************************************************************
* KeyDlgProc *
* - Redefines keys by processing WM_CHAR's and assigning the key to the *
* selected radiobutton (game key) *
* - Standard dialog window I/O *
****************************************************************************/
MRESULT EXPENTRY KeyDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
int i;
static KEY keyTmp[KEYS];
static SHORT id;
switch (msg) {
case WM_INITDLG:
/* Set the associated text for each of the radio buttons */
for (i=0;i<KEYS;i++) {
keyTmp[i] = prfProfile.keyDEFS[i];
SetText(hwnd, TEXT_LEFT+i, &prfProfile.keyDEFS[i]);
}
/* Check the first radio button and let it set up id */
WinSendDlgItemMsg(hwnd, KEY_LEFT, BM_CLICK, MPFROMSHORT(TRUE), MPVOID);
return (MRESULT) TRUE;
case WM_HELP:
i = IDH_KEYS;
WinSendMsg(hwndHelp, HM_DISPLAY_HELP,
MPFROMP(&i), MPFROMSHORT(HM_RESOURCEID));
return 0;
case WM_COMMAND:
switch (SHORT1FROMMP(mp1)) {
case DID_OK:
for (i=0;i<KEYS;i++)
prfProfile.keyDEFS[i] = keyTmp[i];
case DID_CANCEL:
WinDismissDlg(hwnd, 0);
return 0;
}
return 0;
case WM_CONTROL:
if (SHORT2FROMMP(mp1) == BN_CLICKED) {
switch (SHORT1FROMMP(mp1)) {
case KEY_LEFT:
id = TEXT_LEFT;
break;
case KEY_RIGHT:
id = TEXT_RIGHT;
break;
case KEY_THRUST:
id = TEXT_THRUST;
break;
case KEY_HYPER:
id = TEXT_HYPER;
break;
case KEY_FIRE:
id = TEXT_FIRE;
break;
case KEY_SHIELD:
id = TEXT_SHIELD;
}
WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd,id));
}
return 0;
case WM_CHAR:
if (!(CHARMSG(&msg)->fs & KC_KEYUP)) {
if ((CHARMSG(&msg)->fs & KC_VIRTUALKEY) &&
(CHARMSG(&msg)->vkey != VK_SHIFT) &&
(CHARMSG(&msg)->vkey != VK_CTRL) &&
(CHARMSG(&msg)->vkey != VK_ALT) &&
(CHARMSG(&msg)->vkey != VK_F1) &&
(CHARMSG(&msg)->vkey-1 < MAXVKEY)) {
if (CHARMSG(&msg)->vkey == VK_F3) {
WinSendMsg(hwnd, WM_COMMAND, MPFROM2SHORT(DID_CANCEL, 0),
MPVOID);
return 0;
}
keyTmp[id-TEXT_LEFT].vk = (CHAR) (CHARMSG(&msg)->vkey - 1);
keyTmp[id-TEXT_LEFT].chr = 0;
}
else if ((CHARMSG(&msg)->fs & KC_CHAR) &&
(CHARMSG(&msg)->chr > 32) &&
(CHARMSG(&msg)->chr < 127)) {
keyTmp[id-TEXT_LEFT].vk = 0;
keyTmp[id-TEXT_LEFT].chr = (CHAR) CHARMSG(&msg)->chr;
}
else {
keyTmp[id-TEXT_LEFT].vk = 0;
keyTmp[id-TEXT_LEFT].chr = 0;
}
}
SetText(hwnd, id, &keyTmp[id-TEXT_LEFT]);
return 0;
}
return WinDefDlgProc(hwnd, msg, mp1, mp2);
}
/****************************************************************************
* SetText *
* - Used by KeyDlgProc to set the text in control windows *
* in those dialog boxes. *
****************************************************************************/
VOID SetText(HWND hwnd, INT id, KEY *key)
{
CHAR tmp[2];
if (key->vk)
WinSetWindowText(WinWindowFromID(hwnd, id), VKEY[key->vk].name);
else if (key->chr) {
tmp[0] = key->chr;tmp[1] = 0;
WinSetWindowText(WinWindowFromID(hwnd, id), tmp);
}
else
WinSetWindowText(WinWindowFromID(hwnd, id), "???");
}
/****************************************************************************
* InitMenu *
* - Sets menu item check according to profile information. *
****************************************************************************/
VOID InitMenu(VOID)
{
/* Set options menu to mach profile info */
CheckMenuItem(hwndMenu, IDM_SHIPS+prfProfile.iSHIPS, TRUE);
CheckMenuItem(hwndMenu, IDM_PHOTONS+prfProfile.iPHOTONS, TRUE);
if (prfProfile.bRAPIDFIRE) {
CheckMenuItem(hwndMenu, IDM_RAPIDFIRE, TRUE);
EnableMenuItem(hwndMenu, IDM_FIRERATE, TRUE);
}
CheckMenuItem(hwndMenu, IDM_FIRERATE+prfProfile.iFIRERATE, TRUE);
if (prfProfile.bSHIELD)
CheckMenuItem(hwndMenu, IDM_SHIELD, TRUE);
if (prfProfile.bMOUSECONTROL)
CheckMenuItem(hwndMenu, IDM_MOUSE, TRUE);
/* If help instance creation failed disable the help menu items */
if (hwndHelp == NULLHANDLE) {
EnableMenuItem(hwndMenu, IDM_HELPFORHELP, FALSE);
EnableMenuItem(hwndMenu, IDM_EXTENDEDHELP, FALSE);
EnableMenuItem(hwndMenu, IDM_KEYSHELP, FALSE);
EnableMenuItem(hwndMenu, IDM_HELPINDEX, FALSE);
}
/* Set background execution toggle */
CheckMenuItem(hwndMenu, IDM_BACKGRND, prfProfile.bBACKGRND);
}
/****************************************************************************
* DoCommand *
* - Handles WM_COMMAND messages for client window. *
* - Standard client window I/O *
****************************************************************************/
VOID DoCommand(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
switch (SHORT1FROMMP(mp1)) {
case IDM_START1: case IDM_START2:
/* Start a new 1 or 2 player game. */
if (iGameMode == GAME_MODE_INIT1 || iGameMode == GAME_MODE_INIT2) {
TogglePause(FORCE_UNPAUSE);
StartGame(SHORT1FROMMP(mp1)-IDM_START);
}
break;
case IDM_PAUSE:
/* Toggle pause state. */
TogglePause(TOGGLE);
break;
case IDM_STOP:
/* End game, but don't exit */
WinPostMsg(hwnd, WM_INITGAME, (MPARAM) 0L, MPVOID);
break;
case IDM_3SHIPS: case IDM_4SHIPS: case IDM_5SHIPS:
/* Select 3, 4, or 5 ships */
CheckMenuItem(hwndMenu, IDM_SHIPS+prfProfile.iSHIPS, FALSE);
prfProfile.iSHIPS = SHORT1FROMMP(mp1) - IDM_SHIPS;
CheckMenuItem(hwndMenu, IDM_SHIPS+prfProfile.iSHIPS, TRUE);
break;
case IDM_3PHOTONS: case IDM_4PHOTONS:
case IDM_6PHOTONS: case IDM_8PHOTONS:
/* Select 3, 4, 6, or 8 photons */
CheckMenuItem(hwndMenu, IDM_PHOTONS+prfProfile.iPHOTONS, FALSE);
prfProfile.iPHOTONS = SHORT1FROMMP(mp1) - IDM_PHOTONS;
CheckMenuItem(hwndMenu, IDM_PHOTONS+prfProfile.iPHOTONS, TRUE);
break;
case IDM_RAPIDFIRE:
/* Select rapidfire option */
if (prfProfile.bRAPIDFIRE = !prfProfile.bRAPIDFIRE) {
CheckMenuItem(hwndMenu, IDM_RAPIDFIRE, TRUE);
EnableMenuItem(hwndMenu, IDM_FIRERATE, TRUE);
}
else {
CheckMenuItem(hwndMenu, IDM_RAPIDFIRE, FALSE);
EnableMenuItem(hwndMenu, IDM_FIRERATE, FALSE);
}
break;
case IDM_SLOWRATE: case IDM_MEDRATE: case IDM_FASTRATE:
CheckMenuItem(hwndMenu, IDM_FIRERATE+prfProfile.iFIRERATE, FALSE);
prfProfile.iFIRERATE = SHORT1FROMMP(mp1) - IDM_FIRERATE;
CheckMenuItem(hwndMenu, SHORT1FROMMP(mp1), TRUE);
break;
case IDM_SHIELD:
/* Select shield option */
if (prfProfile.bSHIELD = !prfProfile.bSHIELD)
CheckMenuItem(hwndMenu, IDM_SHIELD, TRUE);
else
CheckMenuItem(hwndMenu, IDM_SHIELD, FALSE);
break;
case IDM_MOUSE:
/* Allow mouse input for ship control */
if (prfProfile.bMOUSECONTROL = !prfProfile.bMOUSECONTROL) {
/* Hide the pointer if playing a game */
ShowMouse(FALSE);
CheckMenuItem(hwndMenu, IDM_MOUSE, TRUE);
} else {
/* Show the pointer if it is currently hidden */
ShowMouse(TRUE);
CheckMenuItem(hwndMenu, IDM_MOUSE, FALSE);
}
break;
case IDM_KEYS:
/* Pop up key definition dialog box */
WinDlgBox(HWND_DESKTOP, hwnd, (PFNWP)KeyDlgProc, NULLHANDLE, IDD_KEY,
NULL);
break;
case IDM_FRAME:
/* This case does not come from the menu, but instead from the *
* accelerator table. */
if (prfProfile.bCONTROLS = !prfProfile.bCONTROLS)
ShowFrameControls();
else
HideFrameControls();
break;
case IDM_BACKGRND:
CheckMenuItem(hwndMenu, IDM_BACKGRND,
prfProfile.bBACKGRND = !prfProfile.bBACKGRND);
break;
case IDM_HELPFORHELP:
WinSendMsg(hwndHelp, HM_DISPLAY_HELP, MPVOID, MPVOID);
break;