-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGP142.C
More file actions
4109 lines (3490 loc) · 134 KB
/
GP142.C
File metadata and controls
4109 lines (3490 loc) · 134 KB
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
/*
** GP142.c by Neil McKenzie
**
** RCS $Id: gp142.c,v 1.17 2000/07/17 22:33:50 boren Exp $
**
** Copyright (C) 1994 Neil McKenzie. All rights reserved.
**
** Copyright (c) 1994 Corey Anderson.
**
** Copyright (c) 1995 Casey Anderson.
** Revised version 1.5 of October, 1994
** Revised version 1.61 of December, 1994
** Revised version 2.00 of January, 1995
** conversion from callback fns to await_event
** Revised version 2.01 of July, 1995
** fixed hotkeys in windows
** Revised version 2.1 of October, 1995
** transparent text in windows; float pragma; casts;
** animation based on wall time elapsed
** "Fixed" PC window size problem, see FUDGE, below -- brd
** Includes changes made for port to WIN32 by Shuichi Koga
** All additions ifdefed by WIN32 -- brd
**
** Revised version 2.2 on July, 1997
** X11R6 Melissa Johnson mjhnsn@u.washington.edu
** Assembly timer code for Mac replaced with C-based code. (Corey)
** Removed stack of objects. Replaced with offscreen buffer.
** Dramatically improves animation quality. (Josh, Melissa, Corey)
** Set GP142_DATE to indicate when GP142 was last _updated_, not
** merely last compiled. (Corey)
**
** Revised version 2.2.1 of February, 1998
** Fixed autoKey/GP142_PERIODIC conflict for Mac,
** and did some cleanup in Mac event handling code (Daniel)
**
** Revised February 4, 2000, Dan Boren boren@cs.washington.edu
** Fixed Windows version of the text window. Implemented double
** buffering to fix severe redraw bugs.
**
** Revised February 4, 2000, Dan Boren boren@cs.washington.edu
** Minor change to Windows version of GP142_open() -- removed
** extraneous comma
**
** Revised February, 2000, Dan Boren boren@cs.washington.edu
** Changed Windows message handling in YeildToSystem(). Changed
** PeekMessage() to GetMessage() in order to reduce CPU usage
** that was at 100% with PeekMessage().
**
** Revised May, 2000, R. Dunn & L. Ruzzo
** added getDimensions functions to get height/width of text strings.
**
** Revised May, 2000 Dan Boren boren@cs.washington.edu
** Used Shuichi's handling of WinMain/main().
** Fixed memory leak in DrawTextWnd().
**
*/
#include "gp142lib.h" /* Library specific header */
#define GP142_RCSREV "$Revision: 1.17 $"
#define GP142_RCSDATE "$Date"
#define GP142_VERSION "2.2xx"
#define GP142_DATE "April 2000"
#define TIME_PER_STEP 50
#define MAXSTRLEN 200 /* max string length for text fns */
#define UNUSED(parameter) (parameter = parameter)
#if defined(WINDOWS)
#define GP142_FUNC FAR EXPORT
#define FUDGE 40 /* On the PC, the window is off by 40 pixels in the */
/* Y dimension. Until we find a general fix, we're */
/* stuck with this hack. -- brd */
#elif defined(MACINTOSH)
#define GP142_FUNC
#elif defined(X11R6)
#define GP142_FUNC
#endif
/* Don't use assembly instructions if we can avoid it. */
#define USE_ASM_FOR_TIMER 0
/************************************************************* global variables ****/
/* for double buffering. Redraw if true - josh */
static int need_redraw = 1;
#ifdef MACINTOSH
#if USE_ASM_FOR_TIMER
typedef struct {
TMTask atmTask;
long tmWakeUp;
long tmReserved;
long tmRefCon;
} TMInfo, *TMInfoPtr;
TMInfo gMyTMInfo;
#endif
Rect dragRect = { 0, 0, 1024, 1024 };
Rect winrect = { 50, 20, 50+2*GP142_YMAX+1, 20+2*GP142_XMAX+1 };
RGBColor gRGBColors[MAX_COLORS] = {
/* Black used to be 0x1000, 0x1000, 0x1000. Why??? */
{ 0x0000, 0x0000, 0x0000 }, /* black */
{ 0xFFFF, 0xFFFF, 0xFFFF }, /* white */
{ 0xFFFF, 0x0000, 0x0000 }, /* red */
{ 0x0000, 0xFFFF, 0x0000 }, /* green */
{ 0x0000, 0x0000, 0xFFFF }, /* blue */
{ 0xFFFF, 0xFFFF, 0x0000 }, /* yellow */
{ 0xFFFF, 0x0000, 0xFFFF }, /* magenta */
{ 0x0000, 0xFFFF, 0xFFFF }, /* cyan */
{ 0x9999, 0x0000, 0xCCCC }, /* purple */
{ 0x0000, 0x3333, 0x9999 }, /* navy blue */
{ 0x9999, 0x6666, 0x9999 }, /* dusty plum */
{ 0x9999, 0xFFFF, 0xFFFF }, /* ice blue */
{ 0x9999, 0xFFFF, 0xCCCC }, /* turquoise */
{ 0xFFFF, 0x6666, 0x0000 }, /* orange */
{ 0x9999, 0x6666, 0x3333 }, /* brown */
{ 0xFFFF, 0x9999, 0xCCCC }, /* pink */
{ 0xFFFF, 0xFFFF, 0x9999 }, /* chalk */
{ 0xCCCC, 0x9999, 0x3333 }, /* gold */
{ 0xFFFF, 0x9999, 0x6666 }, /* peach */
{ 0x0000, 0x6666, 0x3333 }, /* forest green */
{ 0x3333, 0xCCCC, 0x9999 }, /* sea green */
{ 0x9999, 0x9999, 0x3333 }, /* olive */
{ 0x8000, 0x8000, 0x8000 }, /* 50% gray */
{ 0xCCCC, 0xCCCC, 0xCCCC } /* 20% gray -- 80% white */
};
enum {
mAniRun = 1,
mAniHalt,
mAniSingle,
mLog = 5,
mScriptRecord = 7,
mScriptPlay,
mScriptOne,
mQuit = 11};
char Menu_String[] = "\p"
"Animate/A;"
"Halt animation/H;"
"Advance one frame/F;"
"(-;"
"Logging/L;"
"(-;"
"Record " kScriptName "/R;"
"Playback " kScriptName "/P;"
"Playback one event/O;"
"(-;"
"Quit/Q;"
"(-;"
"(GP142 v" GP142_VERSION " " GP142_DATE ";"
"(mckenzie@cs.washington.edu;"
"(corin@cs.washington.edu;"
"(casey@cs.washington.edu"
;
MenuHandle RunMenu, DeskMenu;
CWindowPtr gpWin = (CWindowPtr) 0;
/* The offscreen GWorld where the GP142 graphics are rendered. */
GWorldPtr gGP142World = NULL;
char gColorFlag = 1; /* true if the target Mac runs color Quickdraw */
PixPatHandle color_pp[MAX_COLORS];
int nrows = 32;
int ncols = 50;
#elif defined(WINDOWS)
/* Structure used to keep around variables we need for initialization,
* but the user shouldn't have to see.
*/
typedef struct {
HINSTANCE hInstance, hPrevInstance;
LPSTR lpCmdLine;
int nShowCmd;
} GP142Globals;
/* Only one item of type Global needs to exist. */
static GP142Globals global;
/* We need to state that there exists a main() apriori. */
void main();
HWND hDrawWnd = (HWND)NULL; /* handles to 2 windows in application */
HWND hTextWnd = (HWND)NULL;
HMENU hRunMenu; /* handle to menu */
HINSTANCE hInst; /* Instance handle to this library */
/* need some stuff for double buffering - josh */
HBITMAP offscreen_bitmap;
HDC offscreen_DC, the_hdc;
/* Display context for text window */
HDC text_hdc;
/* Handle to ring buffer for debug statements. We save the last
MAX_CONOLE_LINES lines from the debuf window, so we can redraw them when
the text window receives a WM_PAINT message */
HANDLE gConsoleLines[MAX_CONSOLE_LINES];
int gRingFirst = -1; /* -1 indicates that the ring buffer is empty */
int gRingLast = 0; /* last is the next available position */
#define RING_INC(i) (i = (i==MAX_CONSOLE_LINES-1)?0:i+1)
COLORREF gRGBColors[MAX_COLORS] = {
RGB(0x00, 0x00, 0x00), /* black */
RGB(0xFF, 0xFF, 0xFF), /* white */
RGB(0xFF, 0x00, 0x00), /* red */
RGB(0x00, 0xFF, 0x00), /* green */
RGB(0x00, 0x00, 0xFF), /* blue */
RGB(0xFF, 0xFF, 0x00), /* yellow */
RGB(0xFF, 0x00, 0xFF), /* magenta */
RGB(0x00, 0xFF, 0xFF), /* cyan */
RGB(0x99, 0x00, 0xCC), /* purple */
RGB(0x00, 0x33, 0x99), /* navy blue */
RGB(0x99, 0x66, 0x99), /* dusty plum */
RGB(0x99, 0xFF, 0xFF), /* ice blue */
RGB(0x99, 0xFF, 0xCC), /* turquoise */
RGB(0xFF, 0x66, 0x00), /* orange */
RGB(0x99, 0x66, 0x33), /* brown */
RGB(0xFF, 0x99, 0xCC), /* pink */
RGB(0xFF, 0xFF, 0x99), /* chalk */
RGB(0xCC, 0x99, 0x33), /* gold */
RGB(0xFF, 0x99, 0x66), /* peach */
RGB(0x00, 0x66, 0x33), /* forest green */
RGB(0x33, 0xCC, 0x99), /* sea green */
RGB(0x99, 0x99, 0x33), /* olive */
RGB(0x80, 0x80, 0x80), /* 50% gray */
RGB(0xCC, 0xCC, 0xCC) /* 20% gray -- 80% white */
};
#elif defined(X11R6)
#define MAX_FONTS 7
Display *display;
int screen;
char *prog_name;
Window window;
Window wincomm;
Window winc[8];
GC gc;
GC gccon;
FontDB* font_info;
FontDB* current_font;
XColor color_ids[MAX_COLORS];
int current_color_id;
/* for double-buffering - josh */
Pixmap offscreen_buffer;
RGBColor gRGBColors[MAX_COLORS] = {
{ 0x1000, 0x1000, 0x1000 }, /* black */
{ 0xFFFF, 0xFFFF, 0xFFFF }, /* white */
{ 0xFFFF, 0x0000, 0x0000 }, /* red */
{ 0x0000, 0xFFFF, 0x0000 }, /* green */
{ 0x0000, 0x0000, 0xFFFF }, /* blue */
{ 0xFFFF, 0xFFFF, 0x0000 }, /* yellow */
{ 0xFFFF, 0x0000, 0xFFFF }, /* magenta */
{ 0x0000, 0xFFFF, 0xFFFF }, /* cyan */
{ 0x9999, 0x0000, 0xCCCC }, /* purple */
{ 0x0000, 0x3333, 0x9999 }, /* navy blue */
{ 0x9999, 0x6666, 0x9999 }, /* dusty plum */
{ 0x9999, 0xFFFF, 0xFFFF }, /* ice blue */
{ 0x9999, 0xFFFF, 0xCCCC }, /* turquoise */
{ 0xFFFF, 0x6666, 0x0000 }, /* orange */
{ 0x9999, 0x6666, 0x3333 }, /* brown */
{ 0xFFFF, 0x9999, 0xCCCC }, /* pink */
{ 0xFFFF, 0xFFFF, 0x9999 }, /* chalk */
{ 0xCCCC, 0x9999, 0x3333 }, /* gold */
{ 0xFFFF, 0x9999, 0x6666 }, /* peach */
{ 0x0000, 0x6666, 0x3333 }, /* forest green */
{ 0x3333, 0xCCCC, 0x9999 }, /* sea green */
{ 0x9999, 0x9999, 0x3333 }, /* olive */
{ 0x8000, 0x8000, 0x8000 }, /* 50% gray */
{ 0xCCCC, 0xCCCC, 0xCCCC } /* 20% gray -- 80% white */
};
#endif
static char *gColorName[MAX_COLORS] = {
"black",
"white",
"red",
"green",
"blue",
"yellow",
"magenta",
"cyan",
"purple",
"navy blue",
"dusty plum",
"ice blue",
"turquoise",
"orange",
"brown",
"pink",
"chalk",
"gold",
"peach",
"forest green",
"sea green",
"olive",
"50% gray",
"20% gray"
};
static int gEventAction;
static GP142_event_t gEventRecord;
/* control flags */
static char gRunFlag; /* Running animation? */
static char gSingleStepFlag; /* Single stepping animation? */
static char gDoneFlag; /* Program concluded? */
static char gLogging = LOG_ON; /* Log actions to console? */
static char gRecordingFlag = FALSE; /* Recording user actions for later playback? */
static char gScriptStepFlag = FALSE; /* Playback one action? */
static char gScriptPlayFlag = FALSE; /* Continuously playback actions? */
static int gAnimExpired = FALSE;
/* scripting */
static FILE *gScriptFP = NULL; /* file pointer to script file (in or out); NULL if not open */
static int gTaskCount = 0; /* number of consecutive calls to GP142_user_task */
static int gXCenter, gYCenter;
/********************************************************* Library Initialization ***/
#ifdef MACINTOSH
#if USE_ASM_FOR_TIMER
#ifndef __MWERKS__
void timerFlagProc(void)
{
long oldA5;
TMInfoPtr recPtr;
asm { /* The time manager puts the address */
move.l a1, recPtr /* of the expired timer in register a1*/
}
oldA5 = SetA5(recPtr->tmRefCon); /* This is so we can touch the global */
/* variables of GP142 app*/
gAnimExpired = TRUE;
oldA5 = SetA5(oldA5); /* Change global variable start address */
/* back to what it was*/
PrimeTime((QElemPtr)recPtr, TIME_PER_STEP); /* Reprime the timer for another */
/* .1 seconds */
}
#else /* __MWERKS__ */
/*
** Metrowerks supports asm only for entire functions.
** This func is equiv to the preceding one:
** Save A5; restore GP142's A5 from timer record;
** set global flag; restore A5; requeue timer record.
** WLR 2/96. Thanks to MAC.
*/
static asm void timerFlagProc(void)
{
move.l A5, A0
move.l struct(TMInfo_s.tmRefCon)(A1), A5
move.w #1, gAnimExpired
move.l A0, A5
move.l A1, A0
move.l #TIME_PER_STEP, D0
_PrimeTime
rts
}
#endif
#endif /* USE_ASM_FOR_TIMER */
extern int GP142_open(void) /* was GP142_main() in GP142 v1.x */
{
int console_flag = /* 0 for either defers opening */
nrows!=0 && ncols!=0 ; /* console, but leaves default size. */
ToolboxInit(); /* Initialize the Mac toolbox */
#ifdef __MWERKS__
if (nrows) SIOUXSettings.rows = nrows;
if (ncols) SIOUXSettings.columns= ncols;
SIOUXSettings.initializeTB = 0; /* Do we initialize the ToolBox ... */
SIOUXSettings.setupmenus = 0; /* Do we draw the SIOUX menus ... */
SIOUXSettings.autocloseonquit = 1; /* Do we close the SIOUX window on program termination ... */
SIOUXSettings.asktosaveonclose = 0; /* Do we offer to save on a close ... */
SIOUXSettings.toppixel = 40; /* The topleft window position (in pixels) */
SIOUXSettings.leftpixel = 2; /* (0,0 centers on main screen) ... */
#else
if (nrows) console_options.nrows = nrows;
if (ncols) console_options.ncols = ncols;
#endif
if ( console_flag ) { /* don't open console unnecessarily */
printf("GP142 graphics package version " GP142_VERSION " (" GP142_DATE ")\n");
printf("This is the console window\n");
if (!gColorFlag) {
printf("This Macintosh does not have Color Quickdraw.\n");
printf("All objects will be drawn using black.\n");
}
}
/*Here we initialize a timer, add it to the queue, and prime it*/
#if USE_ASM_FOR_TIMER
gMyTMInfo.atmTask.tmAddr = (TimerUPP)timerFlagProc;
gMyTMInfo.tmRefCon = SetCurrentA5(); /* This is so we can access */
/* the Global variables later */
gMyTMInfo.tmWakeUp = 0;
gMyTMInfo.tmReserved = 0;
InsTime((QElemPtr)&gMyTMInfo);
PrimeTime((QElemPtr)&gMyTMInfo, TIME_PER_STEP);
#endif
SelectWindow((void *)gpWin); /* Better to do it once here than every time */
/* through the event loop -dia */
return GP142_SUCCESS;
}
/* Initialize Mac stuff */
static void ToolboxInit(void)
{
int i;
long myFeature;
Rect r;
#if USES_NEW_HEADERS
InitGraf(&qd.thePort);
#else
InitGraf(&thePort);
#endif
InitFonts();
InitWindows();
InitCursor();
InitDialogs(0);
TEInit();
InitMenus();
FlushEvents( everyEvent, 0 );
/* Desk Accessory menu */
DeskMenu = NewMenu(Desk_ID,"\p\024");
#if USES_NEW_HEADERS
AppendResMenu(DeskMenu, 'DRVR');
#else
AddResMenu(DeskMenu, 'DRVR');
#endif
InsertMenu(DeskMenu, 0);
/* Run menu */
RunMenu = NewMenu(Run_ID, "\pRun");
AppendMenu(RunMenu, (ConstStr255Param)Menu_String);
InsertMenu(RunMenu, 0);
CheckItem(RunMenu, 4, (int)gLogging);
/* Let's see if we can use COLOR */
Gestalt(gestaltQuickdrawVersion, &myFeature);
if (myFeature < gestalt8BitQD) {
gColorFlag = 0;
}
/* Create the window */
DrawMenuBar();
if (gColorFlag)
gpWin = (CWindowPtr) NewCWindow(0,&winrect,"\pGP142 Graphics Window",
1,0,(void *)(-1),1,0);
else
gpWin = (CWindowPtr) NewWindow(0,&winrect,"\pGP142 Graphics Window",
1,0,(void *)(-1),1,0);
gRunFlag = 0;
gDoneFlag = 0;
gSingleStepFlag = 0;
/* Make some colors */
if (gColorFlag) {
for (i=0; i<MAX_COLORS; i++) {
color_pp[i] = NewPixPat();
MakeRGBPat(color_pp[i], &gRGBColors[i]);
}
}
/* Find the offset to the center of the window */
gXCenter = (winrect.right - winrect.left) >> 1;
gYCenter = (winrect.bottom - winrect.top) >> 1;
/* Create the offscreen GWorld into which we will render out GP142
objects */
SetRect(&r, 0, 0, 2*GP142_XMAX+1, 2*GP142_YMAX+1);
if (noErr != NewGWorld(&gGP142World, /* The returned offscreen world */
8, /* bits per pixel */
&r, /* size of buffer */
nil, /* Color table. Use the default. */
nil, /* I forget why this is nil... */
0)) { /* Don't need any flags */
SysBeep(10);
ExitToShell();
return;
}
}
#elif defined(WINDOWS)
/*
* Hide the four global variables from the user, permitting them to simply
* define a main() function. (idea by Shuichi Koga, implemented by Dan Boren).
*/
PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
global.hInstance = hInstance;
global.hPrevInstance = hPrevInstance;
global.lpCmdLine = lpCmdLine;
global.nShowCmd = nShowCmd;
main();
}
void _GP142_pseudocall(void)
{
int a,x,y;
char c='a';
GP142_point p = {0,0};
GP142_event_t t = {0, 0, 0};
x = y =a = 0;
if (a*a) /* we won't actually call these functions
a trick to get rid of compiler warnings */
{
GP142_clear();
GP142_undo();
GP142_logging(LOG_OFF);
GP142_animate(ANI_HALT);
GP142_gets(NULL,NULL);
GP142_pixelXY(0,0,0);
GP142_lineXY(0,0,0,0,0,0);
GP142_rectangleXY(0,0,0,0,0,0);
GP142_triangleXY(0,0,0,0,0,0,0,0);
GP142_ovalXY(0,0,0,0,0,0);
GP142_circleXY(0,0,0,0);
GP142_printfXY(0,0,0,0,NULL);
GP142_textXY(0,0,0,0,NULL);
GP142_await_eventP((GP142_event_t*)NULL);
GP142_pixelP(0,p);
GP142_lineP(0,p,p,0);
GP142_rectangleP(0,p,p,0);
GP142_triangleP(0,p,p,p,0);
GP142_ovalP(0,p,p,0);
GP142_circleP(0,p,0);
GP142_printfP(0,p,0,NULL);
GP142_textP(0,p,0,NULL);
GP142_close();
GP142_await_event(&x,&y,&c);
PlayFromScript();
CloseScript();
SaveToScript(0,t);
}
}
extern int GP142_FUNC GP142_open()
/*GP142_open(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)*/
{
WNDCLASS wc;
int x, y;
/* BOOL done = FALSE; */
_GP142_pseudocall(); /* let the compiler think all
GP142 functions have been
called */
UNUSED(global.lpCmdLine);
/************ register window classes ****************/
if (!global.hPrevInstance) /* initialize window classes when first run */
{
/* first, register the graphics window class */
wc.hInstance = global.hInstance;
wc.hIcon = LoadIcon((HINSTANCE)NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
wc.lpszMenuName = "generic";
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpfnWndProc = (WNDPROC) DrawWndProc;
wc.hbrBackground = CreateSolidBrush(gRGBColors[BKGND_COLOR]),
wc.lpszClassName = "DrawWndClass";
RegisterClass(&wc);
/* now, register the text window, child of draw */
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)TextWndProc;
wc.lpszClassName = "TextWndClass";
RegisterClass(&wc);
}
/************* Initialize global variables *******************/
x = GetSystemMetrics(SM_CXSCREEN);
y = GetSystemMetrics(SM_CYSCREEN);
gRunFlag = FALSE;
gSingleStepFlag = FALSE;
/************ Create windows ***************/
/* Create our two windows: first graphics, then text */
hDrawWnd = CreateWindow("DrawWndClass",
"GP142 Graphics", /* Title of window */
WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_BORDER, /* window style */
0, /* position in upper left */
0,
2*GP142_XMAX+1, /* pixels horz.*/
2*GP142_YMAX+FUDGE, /* pixels vert.*/
(HWND)NULL, /* draw has no parent */
(HMENU)NULL, /* use class menu */
global.hInstance, /* instance of program */
NULL); /* NULL window data */
hTextWnd = CreateWindow(
"TextWndClass", /* window class name */
"GP142 Text", /* Title of window */
WS_OVERLAPPEDWINDOW,
0, /* all the way to the left */
2*GP142_YMAX+FUDGE+10, /* make it abut draw window */
2*GP142_XMAX+1,/* x,*/ /* same width as draw window */
y/4, /* 1/4 maximum height */
(HWND)NULL, /* Text has no parent */
(HMENU)NULL, /* no menu */
global.hInstance, /* instance of program */
NULL); /* NULL window data */
hInst = global.hInstance;
/* init the double-buffer windows - josh */
the_hdc = GetDC(hDrawWnd);
offscreen_bitmap = CreateCompatibleBitmap(the_hdc, 2*GP142_XMAX+1, 2*GP142_YMAX+FUDGE);
offscreen_DC = CreateCompatibleDC(the_hdc);
SelectObject (offscreen_DC, offscreen_bitmap);
ShowWindow(hTextWnd, global.nShowCmd);
ShowWindow(hDrawWnd, global.nShowCmd);
SetTimer(hDrawWnd, 2, TIME_PER_STEP, NULL);
return GP142_SUCCESS;
}
#elif defined(X11R6)
void create_GC(Window window, GC *gc, int fid) {
XGCValues gc_val;
*gc = XCreateGC(display, window, 0, &gc_val);
XSetFont(display, *gc, fid);
XSetForeground(display, *gc, color_ids[2].pixel);
XSetLineAttributes(display, *gc, 3, LineSolid, CapRound, JoinRound);
return;
}
extern int GP142_open(void) {
char *win_name = "GP142 Graphics Window";
char *win_name_comm = "GP142 Control Window";
char *icon_name = "GP142";
char *icon_name_comm = "GP142 Controls";
char *display_name = NULL;
int i;
unsigned int border_width = 2;
XSizeHints *size_hint;
XWMHints *wm_hint;
XClassHint *class_hint;
XTextProperty WinNameComm, WinName, IconNameComm, IconName;
Window dummy_win;
int dummy_x, dummy_y;
unsigned int dummy_width, dummy_height, dummy_border, depth;
prog_name = "GP142";
if (!(size_hint = XAllocSizeHints()) || !(wm_hint = XAllocWMHints())
|| !(class_hint = XAllocClassHint())
|| (display = XOpenDisplay (display_name)) == NULL) {
fprintf(stderr, "%s: Unable to open X display %s.\n", prog_name,
XDisplayName(display_name));
exit(0);
}
screen = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, screen), 10,
10, GP142_XMAX*2, GP142_YMAX*2, border_width,
BlackPixel(display, screen), WhitePixel(display, screen));
wincomm = XCreateSimpleWindow(display, RootWindow(display, screen), 50,
50, 200, 105, border_width, BlackPixel(display, screen),
WhitePixel(display, screen));
/* double buffering - josh */
XGetGeometry(display, window, &dummy_win, &dummy_x, &dummy_y, &dummy_width, &dummy_height,
&dummy_border, &depth);
offscreen_buffer = XCreatePixmap(display, window, GP142_XMAX*2, GP142_YMAX*2, depth);
gXCenter = GP142_XMAX;
gYCenter = GP142_YMAX;
/***TODO***/
if (XStringListToTextProperty(&win_name, 1, &WinName) == 0) {
fprintf(stderr, "\n%s: Ow.\n\n", prog_name);
exit(0);
}
if (XStringListToTextProperty(&icon_name, 1, &IconName) == 0) {
fprintf(stderr, "\n%s: Ow.\n\n", prog_name);
exit(0);
}
if (XStringListToTextProperty(&win_name_comm, 1, &WinNameComm) == 0) {
fprintf(stderr, "\n%s: Ow.\n\n", prog_name);
exit(0);
}
if (XStringListToTextProperty(&icon_name_comm, 1, &IconNameComm) == 0) {
fprintf(stderr, "\n%s: Ow.\n\n", prog_name);
exit(0);
}
gRunFlag = 0;
gDoneFlag = 0;
gSingleStepFlag = 0;
/* size hints to keep windows size from changing */
size_hint -> flags = PSize | PPosition | PMinSize | PMaxSize;
size_hint -> min_width = 600;
size_hint -> min_height = 500;
size_hint -> max_width = 600;
size_hint -> max_height = 500;
wm_hint -> flags = StateHint | InputHint;
wm_hint -> input = True;
wm_hint -> initial_state = NormalState;
class_hint -> res_name = prog_name;
class_hint -> res_class = prog_name;
XSetWMProperties(display, window, &WinName, &IconName, 0, 0,
size_hint, wm_hint, class_hint);
size_hint -> min_width = 200;
size_hint -> min_height = 105;
size_hint -> max_width = 200;
size_hint -> max_height = 105;
XSetWMProperties(display, wincomm, &WinNameComm, &IconNameComm, 0, 0,
size_hint, wm_hint, class_hint);
XSelectInput(display, window, StructureNotifyMask | ExposureMask |
KeyPressMask | ButtonPressMask | EnterWindowMask | LeaveWindowMask);
XSelectInput(display, wincomm, StructureNotifyMask | ExposureMask |
KeyPressMask | ButtonPressMask | EnterWindowMask | LeaveWindowMask);
font_info = (FontDB*) malloc (sizeof(FontDB));
if ((font_info == NULL) ||
(font_info->thefont = XLoadQueryFont(display,
"-adobe-times-medium-r-normal--0-150-75-75-p-0-iso8859-1"))
== NULL) {
fprintf(stderr, "Cannot open adobe-times scalable font.\n\n");
exit(0);
}
font_info -> point_size = 15;
font_info -> next = NULL;
current_font = font_info;
for (i = 0; i < MAX_COLORS; i++) {
color_ids[i].red = gRGBColors[i].red;
color_ids[i].green = gRGBColors[i].green;
color_ids[i].blue = gRGBColors[i].blue;
color_ids[i].flags = DoRed | DoGreen | DoBlue;
XAllocColor(display, DefaultColormap(display, screen), &color_ids[i]);
}
current_color_id = 2;
create_GC(window, &gc, font_info->thefont->fid);
create_GC(window, &gccon, font_info->thefont->fid);
XSetForeground(display, gccon, BlackPixel(display, screen));
XMapWindow(display, window);
XMapWindow(display, wincomm);
for (i = 0; i < 8; i++) {
winc[i] = XCreateSimpleWindow(display, wincomm, (i / 4) * 100,
((i % 4) * 25) + 5, 95, 20, 2, BlackPixel(display,
screen), WhitePixel(display, screen));
XSelectInput(display, winc[i], StructureNotifyMask | ExposureMask |
KeyPressMask | ButtonPressMask | EnterWindowMask | LeaveWindowMask);
XMapWindow(display,winc[i]);
}
/* We must be open and waiting for drawing commands at this
* point. However, X does not guarantee this until we receive the first
* XExpose event
*/
while(1)
{
XEvent event;
XNextEvent(display, &event);
if(event.type == Expose)
if (event.xany.window == window)
break;
else
Process_event(event);
}
return GP142_SUCCESS;
}
#endif
/************************************************************** Library Closure ***/
#ifdef MACINTOSH
extern int GP142_close(void)
{
#if USE_ASM_FOR_TIMER
/* Remove the timer entry from the queue*/
RmvTime((QElemPtr)&gMyTMInfo);
#endif
/* Is there anything we should do here? */
CloseScript();
return GP142_SUCCESS;
}
#elif defined (WINDOWS)
/* Windows Exit Procedure */
int FAR PASCAL EXPORT WEP (int nParameter)
{
UNUSED(nParameter);
return (1);
}
extern int GP142_FUNC
GP142_close(void)
{
/* Free the memory used by our offscreen graphics world */
ReleaseDC(hDrawWnd, the_hdc);
CloseScript();
return GP142_SUCCESS;
}
#elif defined(X11R6)
extern int GP142_close(void) {
CloseScript();
while (font_info) {
FontDB* temp;
XUnloadFont(display, font_info -> thefont -> fid);
temp = font_info;
font_info = font_info -> next;
free(temp); /* no need to free memory at the end of the program
* but do it anyway */
}
XFreeGC(display, gc);
XCloseDisplay(display);
return GP142_SUCCESS;
}
#endif
/******************************************************* Event Handling ***/
/* Concept: Students will call GP142_await_event() in their own event loop,
and handle the returned value accordingly. Possible return values are
GP142_MOUSE, GP142_KBD, GP142_SK, and GP142_QUIT */
/*********************************************************** GP142_await_event()
*/
extern int GP142_FUNC
GP142_await_event(int *x, int *y, char *c)
{
GP142_event_t e;
int a;
a = GP142_await_eventP(&e);
*x = e.x;
*y = e.y;
*c = e.c;
return a;
}
extern int GP142_FUNC
GP142_await_eventP(GP142_event_t *e)
{
int eventCounter;
#ifdef WINDOWS
BOOL done = FALSE;
#endif
/* GP142_await_event() should return only with a message that student
might want to handle. That is, only with mouse, keyboard,
idle, or quit messages. If we're in animiate mode, we'll pass
an idle event after handleing MAX_EVENTS events (or if some
user interaction occurs). Otherwise, we won't return until the
user does some action. */
gEventAction = GP142_NONE;
gEventRecord.x = gEventRecord.y = gEventRecord.c = 0;
eventCounter = 0;
while ( gEventAction == GP142_NONE ||
(gEventAction == GP142_PERIODIC && gAnimExpired == FALSE))
{
eventCounter = 1;
YieldToSystem();
/* If we're reading from a script file, don't honor user's actions;
obey the script instead */
if (gScriptStepFlag || gScriptPlayFlag)
{
gScriptStepFlag = FALSE;
/* This will fill in global data */
if (PlayFromScript() == GP142_SUCCESS)
break;
else
{
gScriptStepFlag = FALSE;
gScriptPlayFlag = FALSE;
}
}
/* Only pass back a GP142_PERIODIC if we're animating, or
single stepping */
if ((gSingleStepFlag || gRunFlag) && gEventAction == GP142_NONE)
{
gEventAction = GP142_PERIODIC;
gAnimExpired = FALSE;
gSingleStepFlag = FALSE;
gScriptStepFlag = FALSE;
}
}
/* Also, if we're recording to a script, we'll want to save the information */
if (gRecordingFlag)
{
if (gEventAction == GP142_PERIODIC)
gTaskCount++;
else
{
if (gTaskCount > 0)
{
SaveToScript(GP142_PERIODIC, gEventRecord);
gTaskCount = 0;
}
SaveToScript(gEventAction, gEventRecord);
}
}
*e = gEventRecord;
/* UNUSED(done); // there should be a reason we declared
// done and eventCounter, just leave them here. */
UNUSED(eventCounter);
return gDoneFlag?GP142_QUIT:gEventAction;
}
static void YieldToSystem(void)
{
#ifdef MACINTOSH
EventRecord event;
#elif defined(WINDOWS)
MSG msg;
BOOL done = FALSE;
#elif defined(X11R6)
XEvent event;
#endif
/* UNUSED(done); */
#ifdef MACINTOSH
#if !USE_ASM_FOR_TIMER /* This stuff was moved out of HandleMessage to fix bug */
UnsignedWide now; /* concerning autoKey event stopping animation -dia */
static UnsignedWide lastExpired = {0,0};
unsigned long delta_microseconds;
/* Has it been long enough since the last animation step? */
Microseconds(&now);