-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSatview.c
779 lines (664 loc) · 23.2 KB
/
Satview.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
#define PALIDX // *Must* be set the same as in SUNCALC.C
/*
Sunclock for Windows
View from Satellite information panel
*/
#include "Sunclock.h"
#include "Linalg.h"
static HDC hhdc; // HDC hidden argument to drawvec
static int lx, ly; // Last X and Y for optimisation
static int yieldYet; // Time to yield to other processes ?
static RECT screct; // Display panel drawing area
static int ltop, lleft, lwid, lhgt; // Logical drawing area upper corner and size
static int imagedia; // Earth image diameter
static int izclip; // Z clipping plane distance from centre of the Earth
static double sscale; // Projected vector to screen scale factor
static double satla, satlo; // Satellite latitude, longitude in degrees
static double satlacos, satlasin; // Satellite latitude sine and cosine
static double lastslat, lastslon, lastsalt; // Save last parameters for refresh
static double currslat, currslon, currsalt; // Position of satellite being tracked
static long bslat, bslon, bsalt; // View that saved bitmap represents
static DWORD lastDrawTime = 0; // Ticks last update required to draw
static DWORD lastDrawEnd = 0; // Time last update completed
static HBITMAP svbmap = NULL; // Satellite view bitmap
static HGLOBAL svdib = NULL; // Texture mapped DIB
int texturing = TRUE; // Texture mapped image desired ?
int satvterm = TRUE; // Show terminator in texture mapped image ?
int viewfrom = IDM_F_SATVIEW1; // View from where ?
int viewFromFrustrated = FALSE; // View from waiting for bitmap to be generated
/* These precalculated sine and cosine tables are *big* (24.5K) and eat a large
portion of DGROUP. If we get into a tight memory situation, they can be
moved to global memory allocated dynamically when we need them. */
struct qTab {
float qSin, qCos;
};
static struct qTab qLon[2048], qLat[1024];
static double qscale;
/* IQTAB -- Table of precalculated scaled sines and cosines for satellite
view rendering. Saves hundreds of thousands of sine and cosine
calls which would be required for a straightforward projection of
the view from the satellite. */
static void iqtab(void)
{
int i;
for (i = 0; i < 2048; i++) {
double lon = (((i - 1024) * PI) / 1024) + dtr(satlo) - (PI / 2);
qLon[i].qSin = (float) (sscale * sin(lon));
qLon[i].qCos = (float) (sscale * cos(lon));
}
for (i = 0; i < 1024; i++) {
double lat = (((i - 512) * (PI / 2)) / 512);
qLat[i].qSin = (float) (sscale * sin(lat));
qLat[i].qCos = (float) abs(cos(lat));
}
}
/* LLXFORM -- Brute-force inlining of function that transforms the
Mercator-projection vectors to 3D vectors in the satellite's
viewpoint frame. Note also the dirty trick where we don't
bother to calculate X and Y if Z is less than the clipping
plane distance. */
#define llxform(lon, lat, x, y, z) \
acsflat = (double) qLat[lat].qCos; \
fy = ((double) qLon[lon].qSin) * acsflat; \
fz = (double) qLat[lat].qSin; \
if ((z = (int) ((fy * -satlasin + fz * satlacos) * qscale)) >= izclip) { \
x = imagedia + (int) (((double) qLon[lon].qCos) * acsflat); \
y = imagedia + (int) (fy * satlacos + fz * satlasin); \
}
#ifdef POLLY
POINT pLine[POLLY];
int pLinel = 0;
#endif
#pragma check_stack (off)
static void PASCAL lldraw(int la1, int lo1, int la2, int lo2)
{
int x1, y1, z1;
static int x2, y2, z2;
static int lla2 = -9999, llo2;
double acsflat, fy, fz;
if (lla2 == la1 && llo2 == lo1) {
x1 = x2;
y1 = y2;
z1 = z2;
} else {
llxform(la1, lo1, x1, y1, z1);
}
llxform(la2, lo2, x2, y2, z2);
lla2 = la2;
llo2 = lo2;
if (z1 >= izclip && z2 >= izclip) {
#ifdef POLLY
if (pLinel >= POLLY || (x1 != lx || y1 != ly)) {
if (pLinel > 0) {
Polyline(hhdc, pLine, pLinel);
}
pLine[0].x = lleft + x1;
pLine[0].y = ltop + y1;
pLine[1].x = lleft + x2;
pLine[1].y = ltop + y2;
pLinel = 2;
} else {
pLine[pLinel].x = lleft + x2;
pLine[pLinel++].y = ltop + y2;
}
lx = x2;
ly = y2;
#else
if (x1 != lx || y1 != ly) {
lx = x1;
ly = y1;
MoveTo(hhdc, lleft + x1, ltop + y1);
}
LineTo(hhdc, lleft + x2, ltop + y2);
#endif
}
}
#pragma check_stack ()
/* TEXTUREMAPIMAGE -- Create texture mapped image of globe. */
static void textureMapImage(int isize, int tmwidth, int tmheight,
int diwidth, int diheight, int showterm,
double satlat, double satlon, double satalt)
{
Vector satpos, v, n, u, up, ippo, raydir, iv;
double acslat = abs(cos(satlat)), a, l, alpha, vheight, zclip,
vl, ui, vi, b, disc, t, tmwo2pi, tmhopi,
normTemp;
int x, y, tmx, tmy, iso2, tmwo2, tmho2, tmhm1;
LPBITMAPINFOHEADER bh, obmap;
WORD ncol, offbits, rowlen, tmrowlen;
unsigned char *opixel;
unsigned char *tpixel;
unsigned char termmask;
#define FastVecNorm(a) assert(VecLen(a) > 0.0); normTemp = 1.0 / VecLen(a); VecScale(normTemp, a)
termmask = (unsigned char) (showterm ? 0x0 : 0x80);
bh = (LPBITMAPINFOHEADER) LockResource(earthImageDIB);
ncol = (WORD) (bh->biClrUsed == 0 ? (1L << bh->biBitCount) : bh->biClrUsed);
offbits = (WORD) (bh->biSize + ncol * sizeof(RGBQUAD));
tpixel = ((unsigned char *) (bh)) + offbits;
tmrowlen = ((tmwidth + (sizeof(LONG) - 1)) / sizeof(LONG)) * sizeof(LONG);
tmhm1 = tmheight - 1;
if (svdib != NULL) {
GlobalFree(svdib);
svdib = NULL;
}
rowlen = ((diwidth + (sizeof(LONG) - 1)) / sizeof(LONG)) * sizeof(LONG);
svdib = GlobalAlloc(GHND, offbits + ((LONG) diheight) * rowlen);
obmap = (LPBITMAPINFOHEADER) GlobalLock(svdib);
memcpy(obmap, bh, offbits);
obmap->biWidth = diwidth;
obmap->biHeight = diheight;
obmap->biSizeImage = ((DWORD) diheight) * rowlen;
opixel = ((unsigned char *) (obmap)) + offbits;
#define PixA(x, y) ((tpixel + ((x) + (((DWORD) (tmhm1 - (y))) * (tmrowlen)))))
#define Opix(x, y) ((opixel + ((x) + (((DWORD) ((diheight - 1) - (y))) * (rowlen)))))
a = satalt + EarthRad;
assert(a > 0.0);
MakeVector(a * cos(satlon) * acslat, a * sin(satlon) * acslat, -a * sin(satlat), satpos);
l = sqrt(a * a - EarthRad * EarthRad);
alpha = asin(EarthRad / a);
vheight = l * sin(alpha);
zclip = a - (l * cos(alpha));
VecCopy(satpos, v);
assert(VecLen(v) > 0.0);
vl = -1.0 / VecLen(v);
VecScale(vl, v); // Invert and normalise at once
// n = Unit direction vector from eye position to North pole
MakeVector(0, 0, EarthRad, n);
VecSub(n, satpos, n);
FastVecNorm(n);
// u = v X n -- View right vector
VecCross(v, n, u);
FastVecNorm(u);
// up = u x v -- View up vector
VecCross(u, v, up);
VecCopy(satpos, ippo);
VecScale(zclip / a, ippo);
// Precompute some values we'll need repeatedly inside the big loop
iso2 = isize / 2;
tmwo2 = tmwidth / 2;
tmho2 = tmheight / 2;
tmwo2pi = (tmwidth / 2) / PI;
tmhopi = tmheight / PI;
for (y = 0; y < isize; y++) {
unsigned char *opix = Opix(lleft, y);
double viXupX, viXupY, viXupZ;
double visq;
vi = -(((double) y) - iso2) / iso2;
visq = vi * vi;
viXupX = vi * up[X];
viXupY = vi * up[Y];
viXupZ = vi * up[Z];
for (x = 0; x < isize; x++) {
ui = (((double) x) - iso2) / iso2;
if (((ui * ui) + visq) <= 1.0) {
raydir[X] = (ippo[X] - (ui * u[X] + viXupX) * vheight) - satpos[X];
raydir[Y] = (ippo[Y] - (ui * u[Y] + viXupY) * vheight) - satpos[Y];
raydir[Z] = (ippo[Z] - (ui * u[Z] + viXupZ) * vheight) - satpos[Z];
FastVecNorm(raydir);
iv[X] = -satpos[X];
iv[Y] = -satpos[Y];
iv[Z] = -satpos[Z];
b = VecDot(iv, raydir);
disc = (b * b - VecDot(iv, iv)) + EarthRad * EarthRad;
if (disc >= 0) { // If disc < 0, no intersection
disc = sqrt(disc);
t = ((b - disc) < 1e-6) ? b + disc : b - disc;
tmx = (int) (tmwo2 - (tmwo2pi * atan2(t * raydir[Y] + satpos[Y], t * raydir[X] + satpos[X])));
tmy = (int) (tmho2 + (tmhopi * asin((t * raydir[Z] + satpos[Z]) / EarthRad)));
opix[x] = *(PixA(tmx, tmy)) | termmask;
}
}
}
}
GlobalUnlock(svdib);
UnlockResource(earthImageDIB);
}
/* PAINTDIB -- Paint the saved texture mapped DIB into the device surface. */
static void paintDIB(HDC hDC)
{
LPBITMAPINFOHEADER obmap, bp;
WORD ncol, offbits;
int bmode = DIB_RGB_COLORS;
unsigned char *opixel;
HPALETTE opal;
bp = obmap = (LPBITMAPINFOHEADER) GlobalLock(svdib);
ncol = (WORD) (obmap->biClrUsed == 0 ? (1L << obmap->biBitCount) : obmap->biClrUsed);
#ifdef PALIDX
bp = palMapStart(obmap, &bmode);
#endif
offbits = (WORD) (obmap->biSize + ncol * sizeof(RGBQUAD));
opixel = ((unsigned char *) (obmap)) + offbits;
opal = SelectPalette(hDC, imagePal, FALSE);
RealizePalette(hDC);
SetDIBitsToDevice(hDC, 1, 1, screct.right - 2, screct.bottom - 2,
1, 1, 0, screct.bottom - 1,
opixel, (LPBITMAPINFO) bp, bmode);
#ifdef PALIDX
palMapEnd();
#endif
GlobalUnlock(svdib);
SelectPalette(hDC, opal, FALSE);
}
/* UPDSATVIEW -- Update view from satellite. */
static void updsatview(HWND hWnd, double satlat, double satlon, double satalt)
{
HPEN dpen, oldPen;
HBRUSH dbrush, obrush;
HCURSOR oldcur;
HWND panel;
HDC hDC, hMemDC;
HBITMAP obmap;
double a, l, alpha, vheight, zclip;
DWORD drawStart; // Time this draw began
int timeYet; // Time to update ?
static int textured = FALSE; // Is saved bitmap texture mapped ?
#define Bres(x) ((long) ((x) * (1800 / PI))) // Fixed point latitude and longitude for tests
lastslat = satlat; // Save last call arguments for repaint
lastslon = satlon; // and for first display when we're activated
lastsalt = satalt;
/* To avoid hogging the machine, don't update any more frequently than
twice the time required to paint the screen. */
drawStart = GetTickCount(); // Current tick count
timeYet = drawStart >= (lastDrawEnd + (2 * lastDrawTime));
/* One more trick--if this is a pure repaint to repair screen damage
(e.g. the satellite hasn't changed), and we have a backing bitmap,
and it isn't time to recalculate, just repair with the bitmap.
Also, if the satellite position hasn't changed measurably, there's
no need to recalculate. */
panel = GetDlgItem(hWnd, IDC_SV_IMAGE);
if (((svbmap != NULL) || (svdib != NULL)) &&
(!timeYet || ((bslat == Bres(satlat)) &&
(bslon == Bres(satlon)) &&
(bsalt == ((long) satalt))
)
)
) {
hDC = GetDC(panel);
GetClientRect(panel, &screct);
if (svdib != NULL) {
paintDIB(hDC);
} else {
hMemDC = CreateCompatibleDC(hDC);
obmap = SelectObject(hMemDC, svbmap);
BitBlt(hDC, 1, 1, screct.right - 2, screct.bottom - 2,
hMemDC, 1, 1, SRCCOPY);
SelectObject(hMemDC, obmap);
DeleteDC(hMemDC);
}
ReleaseDC(panel, hDC);
return;
}
oldcur = SetCursor(LoadCursor(NULL, IDC_WAIT));
ShowCursor(TRUE);
a = satalt + EarthRad; // Geocentric altitude of satellite
l = sqrt(a * a - EarthRad * EarthRad); // Distance to visible limb of Earth
alpha = asin(EarthRad / a); // Half-angle subtended by Earth from satellite
vheight = l * sin(alpha); // Diameter of visible disc from satellite, km
zclip = a - (l * cos(alpha)); // Z-clipping plane distance from Earth's centre
hDC = GetDC(panel);
GetClientRect(panel, &screct);
lwid = lhgt = (min(screct.right, screct.bottom) & -2) - 2;
ltop = (screct.bottom - lhgt) / 2;
lleft = (screct.right - lwid) / 2;
if (svbmap == NULL) {
svbmap = CreateCompatibleBitmap(hDC, screct.right, screct.bottom);
}
hMemDC = CreateCompatibleDC(hDC);
obmap = SelectObject(hMemDC, svbmap);
hhdc = hMemDC;
sscale = (lwid / 2) / (vheight / EarthRad);
izclip = (int) (zclip * (10000 / EarthRad));
satlacos = cos(satlat + PI / 2);
satlasin = sin(satlat + PI / 2);
satla = rtd(satlat);
satlo = rtd(satlon);
BitBlt(hhdc, 1, 1, screct.right - 1, screct.bottom - 1, NULL, 0, 0, BLACKNESS);
if ((viewfrom != IDM_F_SATVIEW1) || tracking) {
char tbuf[80];
double slo = satlo;
// Disable position displayed to show projection in progress
EnableWindow(GetDlgItem(hWnd, IDC_SV_POSITION), FALSE);
/* If the image was loaded already or we just succeeded in loading it, generate
the texture mapped image. */
if (texturing && abs(displaymode) >= 255) {
if (earthImageDIB != NULL && imagePal != NULL) {
// Texture mapped image generation
// #define tmTiming
textured = TRUE;
#ifdef tmTiming
{
DWORD tix = GetTickCount();
char tstr[80];
#endif
textureMapImage(lwid, ImageWid, ImageHgt, screct.right, screct.bottom, satvterm,
satlat, satlon, satalt);
#ifdef tmTiming
tix = GetTickCount() - tix;
sprintf(tstr, "Time: %ld", tix);
SetWindowText(hWnd, tstr);
}
#endif
viewFromFrustrated = FALSE;
} else {
viewFromFrustrated = TRUE; // Indicate waiting for main window to repaint
}
} else {
// Vector image generation
textured = FALSE;
dpen = CreatePen(PS_SOLID, 1, abs(displaymode) == 1 ? GetSysColor(COLOR_WINDOW) :
RGB(255, 255, 0));
dbrush = CreateSolidBrush(abs(displaymode) == 1 ? GetSysColor(COLOR_WINDOWTEXT) :
RGB(0, 0, 255));
obrush = SelectObject(hhdc, dbrush);
Ellipse(hhdc, lleft, ltop, lleft + lwid, ltop + lhgt);
SelectObject(hhdc, obrush);
DeleteObject(dbrush);
oldPen = SelectObject(hhdc, dpen);
yieldYet = 0;
lx = -32000;
iqtab();
qscale = 10000 / sscale;
imagedia = lwid / 2;
#ifdef POLLY
pLinel = 0;
#endif
mapdraw(2048, 1024, lldraw);
#ifdef POLLY
if (pLinel > 0) {
Polyline(hhdc, pLine, pLinel);
}
#endif
SelectObject(hhdc, oldPen);
DeleteObject(dpen);
viewFromFrustrated = FALSE;
}
lastDrawEnd = GetTickCount();
lastDrawTime = lastDrawEnd - drawStart;
bslat = Bres(satlat);
bslon = Bres(satlon);
bsalt = (long) satalt;
if (svdib != NULL) {
paintDIB(hDC);
} else {
BitBlt(hDC, 1, 1, screct.right - 2, screct.bottom - 2, hMemDC, 1, 1, SRCCOPY);
}
// Display the view-from position below the image
if (slo > 180) {
slo = slo - 360;
}
sprintf(tbuf, Format(viewfrom == IDM_F_VFOBSLOC ? 47 : 25),
abs(satla), satla >= 0 ? 'N' : 'S',
abs(slo), slo > 0 ? 'W' : 'E', satalt);
SetDlgItemText(hWnd, IDC_SV_POSITION, (LPSTR) tbuf);
EnableWindow(GetDlgItem(hWnd, IDC_SV_POSITION), TRUE);
} else {
SetDlgItemText(hWnd, IDC_SV_POSITION, (LPSTR) "");
}
ReleaseDC(panel, hDC);
SelectObject(hMemDC, obmap);
DeleteDC(hMemDC);
ShowCursor(FALSE);
SetCursor(oldcur);
#undef Bres
}
/* RELEASE_SATVIEW_BITMAPS -- Release view from satellite backing bitmap. */
void release_satview_bitmaps(void)
{
if (svbmap != NULL) {
DeleteObject(svbmap);
svbmap = NULL;
}
if (svdib != NULL) {
GlobalFree(svdib);
svdib = NULL;
}
}
/* SATVIEWTITLE -- Set title for view from window. */
static void satviewtitle(HWND hDlg)
{
char svtitle[80];
switch (viewfrom) {
case IDM_F_SATVIEW1:
if (tracking) {
#ifndef tmTiming
sprintf(svtitle, Format(23), sat.satname);
SetWindowText(hDlg, svtitle);
#endif
} else {
SetWindowText(hDlg, Format(24));
}
break;
case IDM_F_DAYVIEW:
SetWindowText(hDlg, Format(26));
break;
case IDM_F_MOONVIEW:
SetWindowText(hDlg, Format(28));
break;
case IDM_F_NIGHTVIEW:
SetWindowText(hDlg, Format(27));
break;
case IDM_F_VFOBSLOC:
sprintf(svtitle, Format(48), siteName);
SetWindowText(hDlg, svtitle);
break;
}
}
/* SATVIEWCHANGED -- Inform satellite view that satellite being tracked just changed. */
void satviewchanged(void)
{
if (satViewPanel != NULL) {
release_satview_bitmaps();
lastDrawTime = lastDrawEnd = 0;
if (tracking) {
satviewtitle(satViewPanel);
} else {
// If we've stopped tracking a satellite, close the view-from panel
if (viewfrom == IDM_F_SATVIEW1) {
SendMessage(satViewPanel, WM_CLOSE, 0, 0L);
}
}
}
skySatChanged(); // Let sky and telescope know satellite changed
}
/* CHANGESATVIEW -- Schedule repaint of satellite window when satellite moves. */
void changesatview(double satlat, double satlon, double satalt)
{
currslat = satlat;
currslon = satlon;
currsalt = satalt;
if ((viewfrom == IDM_F_SATVIEW1) && (satViewPanel != NULL)) {
InvalidateRgn(satViewPanel, NULL, FALSE);
}
}
/* CHANGEMOONVIEW -- Schedule repaint of view from Moon. */
void changemoonview(void)
{
if ((viewfrom == IDM_F_MOONVIEW) && (satViewPanel != NULL)) {
InvalidateRgn(satViewPanel, NULL, FALSE);
}
}
/* CHANGESUNVIEW -- Schedule repaint of view from Sun. */
void changesunview(void)
{
if (((viewfrom == IDM_F_DAYVIEW) || (viewfrom == IDM_F_NIGHTVIEW) ||
(viewfrom == IDM_F_VFOBSLOC) ||
((viewfrom == IDM_F_SATVIEW1) && texturing && satvterm))
&& (satViewPanel != NULL)) {
bsalt = 0;
InvalidateRgn(satViewPanel, NULL, FALSE);
}
}
/* SATVIEWDLG -- View from satellite dialogue procedure. */
DialogueProc(SatViewDlg)
{
RECT dw, cw;
HWND cWnd;
static RECT button1;
static int buttonY, buttonDeltaX, buttonBarycentre, lastx, lasty, imagex, imagey,
minx, miny, dragging = FALSE;
switch (message) {
case WM_INITDIALOG:
SendMessage(GetDlgItem(hDlg, IDC_SV_SHOWTERM), BM_SETCHECK, satvterm, 0L);
GetClientRect(hDlg, &dw);
lastx = dw.right;
lasty = dw.bottom;
GetWindowRect(hDlg, &cw);
minx = cw.right - cw.left;
miny = cw.bottom - cw.top;
GetWindowRect(GetDlgItem(hDlg, IDOK), &button1);
ScreenToClient(hDlg, (POINT FAR *) &(button1.left));
buttonY = dw.bottom - button1.top;
GetWindowRect(GetDlgItem(hDlg, IDC_SV_HELP), &cw);
ScreenToClient(hDlg, (POINT FAR *) &(cw.left));
ScreenToClient(hDlg, (POINT FAR *) &(cw.right));
buttonDeltaX = cw.left - button1.left;
buttonBarycentre = ((dw.right - button1.left) / 2) - button1.left;
GetWindowRect(GetDlgItem(hDlg, IDC_SV_IMAGE), &cw);
ScreenToClient(hDlg, (POINT FAR *) &(cw.left));
ScreenToClient(hDlg, (POINT FAR *) &(cw.right));
imagex = cw.right - cw.left;
imagey = cw.bottom - cw.top;
return TRUE;
case WM_CLOSE:
DestroyWindow(hDlg);
return TRUE;
case WM_DESTROY:
release_satview_bitmaps();
satViewPanel = NULL;
return TRUE;
case WM_PALETTECHANGED:
// Force repaint if somebody changes the system palette
if (hDlg != ((HWND) wParam)) {
UpdateWindow(hDlg);
}
return TRUE;
case WM_GETMINMAXINFO:
{ MINMAXINFO FAR *mmi = (MINMAXINFO FAR *) lParam;
mmi->ptMinTrackSize.x = minx;
mmi->ptMinTrackSize.y = miny;
}
break;
case WM_SIZE:
{
int cwidth, cheight;
cwidth = LOWORD(lParam);
cheight = HIWORD(lParam);
#define floatChild(x) cWnd = GetDlgItem(hDlg, x); \
GetWindowRect(cWnd, &cw); \
ScreenToClient(hDlg, (POINT FAR *) &(cw.left)); \
cw.left += (cwidth - lastx) / 2; \
cw.top += (cheight - lasty); \
SetWindowPos(cWnd, NULL, cw.left, cw.top, 0, 0, \
SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER)
floatChild(IDOK);
floatChild(IDC_SV_HELP);
floatChild(IDC_SV_SHOWTERM);
floatChild(IDC_SV_POSITION);
release_satview_bitmaps();
cWnd = GetDlgItem(hDlg, IDC_SV_IMAGE);
SetWindowPos(cWnd, NULL, 0, 0, imagex + (cwidth - lastx),
imagey + (cheight - lasty),
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
imagex += (cwidth - lastx);
imagey += (cheight - lasty);
lastx = cwidth;
lasty = cheight;
}
return TRUE;
case WM_ENTERSIZEMOVE:
dragging = TRUE;
break;
case WM_EXITSIZEMOVE:
dragging = FALSE;
InvalidateRect(hDlg, NULL, FALSE);
UpdateWindow(hDlg);
break;
case WM_PAINT:
{ PAINTSTRUCT ps;
(void) BeginPaint(hDlg, &ps);
if (dragging) {
HDC hDC;
RECT cr;
HWND hWnd = GetDlgItem(hDlg, IDC_SV_IMAGE);
hDC = GetDC(hWnd);
GetClientRect(hWnd, &cr);
FillRect(hDC, &cr, (HBRUSH) GetStockObject(BLACK_BRUSH));
ReleaseDC(hWnd, hDC);
} else {
satviewtitle(hDlg);
ShowWindow(GetDlgItem(hDlg, IDC_SV_SHOWTERM), (abs(displaymode) >= 255) && texturing ?
SW_SHOWNORMAL : SW_HIDE);
if (viewfrom == IDM_F_SATVIEW1) {
lastslat = currslat;
lastslon = currslon;
lastsalt = currsalt;
} else {
double ijdate, moonclong, moonclat, mooncrv, moonlong, moonra, moondec, igmst,
cphase, aom, cdist, cangdia, csund, csuang,
sunra, sundec, sunrv, sunlong, subslong;
ijdate = faketime;
igmst = gmst(ijdate);
if (viewfrom == IDM_F_DAYVIEW || viewfrom == IDM_F_NIGHTVIEW) {
sunpos(ijdate, TRUE, &sunra, &sundec, &sunrv, &sunlong);
subslong = (igmst * 15) - sunra;
if (viewfrom == IDM_F_NIGHTVIEW) {
subslong = fixangle(subslong + 180);
sundec = -sundec;
}
if (subslong > 180) {
subslong = -(360 - subslong);
} else if (subslong < -180) {
subslong += 360;
}
lastslat = dtr(sundec);
lastslon = dtr(subslong);
lastsalt = sunrv * SunSMAX;
} else if (viewfrom == IDM_F_VFOBSLOC) {
lastslat = dtr(siteLat);
lastslon = dtr(siteLon);
lastsalt = SunSMAX;
} else {
highmoon(ijdate, &moonclong, &moonclat, &mooncrv);
ecliptoeq(ijdate, moonclong, moonclat, &moonra, &moondec);
moonlong = (igmst * 15) - moonra;
(void) phase(ijdate, &cphase, &aom, &cdist, &cangdia, &csund, &csuang);
if (moonlong > 180) {
moonlong = -(360 - moonlong);
} else if (moonlong < -180) {
moonlong += 360;
}
lastslat = dtr(moondec);
lastslon = dtr(moonlong);
lastsalt = mooncrv;
}
}
updsatview(hDlg, lastslat, lastslon, lastsalt);
}
EndPaint(hDlg, &ps);
}
return TRUE;
case WM_COMMAND:
switch (WM_COMMAND_ID(wParam)) {
case IDOK:
SendMessage(hDlg, WM_CLOSE, 0, 0L);
return TRUE;
case IDC_SV_HELP:
WinHelp(hWndMain, rstring(IDS_HELPFILE), HELP_KEY,
((DWORD) ((LPSTR) rstring(IDS_HELP_ON_SATVIEW))));
holped = TRUE;
return TRUE;
case IDC_SV_SHOWTERM:
SendMessage((HWND) LOWORD(lParam), BM_SETCHECK,
satvterm = (WORD) !SendMessage((HWND) LOWORD(lParam), BM_GETCHECK, 0, 0L), 0L);
release_satview_bitmaps();
InvalidateRgn(hDlg, NULL, FALSE);
return TRUE;
default:
break;
}
break;
default:
break;
}
return FALSE;
}