rnd-20091101-3-src
[rocksndiamonds.git] / src / game_sp / MainForm.c
1 // ----------------------------------------------------------------------------
2 // MainForm.c
3 // ----------------------------------------------------------------------------
4
5 #include "MainForm.h"
6
7 #if 1
8
9 static void DrawFrame(int Delta);
10 static void ReStretch(float NewStretch);
11 static void picPane_Paint();
12 static void picViewPort_Resize();
13 static void menBorder_Click();
14
15 #else
16
17 static void CleanTempDir(char *TmpDir);
18 static void CountDown(long HalfSeconds, int bDisplayBar);
19 static void DrawFrame(int Delta);
20 static void DrawPauseLayer(long Layer);
21 static void FillFileList(char *Path);
22 static void FillLevelList(char *Path, int LevelIndex);
23 static void FocusTim_Timer();
24 static void Form_KeyDown(int KeyCode, int Shift);
25 static void Form_KeyUp(int KeyCode, int Shift);
26 static void Form_Paint();
27 static int GetFileNameToSave();
28 static long GetHwndFromTempFileName(char *TmpFile);
29 static char * GetMyTempFileName();
30 static void GetSettings();
31 static char * GetSpeedDescriptionFPS(currency FrameDelayUS);
32 static int GetSpeedIndex(long DelayUS);
33 static char * GetValidTempPath();
34 static long Get_LastOpenFilter();
35 static long Get_LastSaveFilter();
36 static int InitSpeeds();
37 static boolean InstanceStillRunning(char *TmpFile);
38 static void Let_LastOpenFilter(long NewVal);
39 static void Let_LastSaveFilter(long NewVal);
40 static void LoadKeyIndicators();
41 static void LoadMenus();
42 static void PanelTim_Timer();
43 static void ReStretch(float NewStretch);
44 static void RestoreFrame();
45 static boolean SaveAs();
46 static void SaveSettings();
47 static void SetScrollEdges();
48 static void UpdateDeltaT();
49 static void cmbFile_Click();
50 static void cmbFile_KeyDown(int KeyCode, int Shift);
51 static void cmbFile_KeyUp(int KeyCode, int Shift);
52 static void cmbLevel_Click();
53 static void cmbLevel_KeyUp(int KeyCode, int Shift);
54 static void cmblevel_KeyDown(int KeyCode, int Shift);
55 static void cmdPause_Click();
56 static void cmdPause_MouseUp(int Button, int Shift, float X, float Y);
57 static void cmdPlayAll_Click();
58 static void cmdPlayAll_MouseUp(int Button, int Shift, float X, float Y);
59 static void cmdPlayDemo_Click();
60 static void cmdPlayDemo_MouseUp(int Button, int Shift, float X, float Y);
61 static void cmdPlay_Click();
62 static void cmdPlay_MouseUp(int Button, int Shift, float X, float Y);
63 static void cmdRecordDemo_Click();
64 static void cmdRecordDemo_MouseUp(int Button, int Shift, float X, float Y);
65 static void cmdStop_Click();
66 static void cmdStop_MouseUp(int Button, int Shift, float X, float Y);
67 static void fpsTim_Timer();
68 static void menAbout_Click();
69 static void menAutoScroll_Click();
70 static void menBorder_Click();
71 static void menCopy_Click();
72 static void menEnOff_Click();
73 static void menEnOn_Click();
74 static void menExit_Click();
75 static void menFaster_Click();
76 static void menGravOff_Click();
77 static void menGravOn_Click();
78 static void menNewStd_Click();
79 static void menNew_Click();
80 static void menOpen_Click();
81 static void menOptions_Click();
82 static void menPanel_Click();
83 static void menPaste_Click();
84 static void menPause_Click();
85 static void menPlayAll_Click();
86 static void menPlayDemo_Click();
87 static void menPlay_Click();
88 static void menReRecordDemo_Click();
89 static void menRec_Click();
90 static void menRemSP_Click();
91 static void menRestoreBorder_Click();
92 static void menSaveAs_Click();
93 static void menSave_Click();
94 static void menSelectAll_Click();
95 static void menShowLInfo_Click();
96 static void menSlower_Click();
97 static void menSoundFX_Click();
98 static void menStretch_Click(int Index);
99 static void menToolTips_Click();
100 static void menTrim_Click();
101 static void menZonkOff_Click();
102 static void menZonkOn_Click();
103 static void picKeys_MouseUp(int Button, int Shift, float X, float Y);
104 static void picMenu_Click();
105 static void picPane_KeyDown(int KeyCode, int Shift);
106 static void picPane_KeyUp(int KeyCode, int Shift);
107 static void picPane_MouseDown(int Button, int Shift, float X, float Y);
108 static void picPane_MouseMove(int Button, int Shift, float X, float Y);
109 static void picPane_MouseUp(int Button, int Shift, float X, float Y);
110 static void picPane_Paint();
111 static void picViewPort_MouseUp(int Button, int Shift, float X, float Y);
112 static void picViewPort_Paint();
113 static void picViewPort_Resize();
114
115 #endif
116
117 void DrawField(int X, int Y);
118 void DrawFieldAnimated(int X, int Y);
119 void DrawFieldNoAnimated(int X, int Y);
120
121 // --- VERSION 5.00
122 // --- Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
123 // --- Begin VB.Form MainForm 
124 // ---    AutoRedraw      =   -1  'True  // True
125 // ---    Caption         =   "MegaPlex"
126 // ---    ClientHeight    =   5850
127 // ---    ClientLeft      =   2580
128 // ---    ClientTop       =   2205
129 // ---    ClientWidth     =   9285
130 // ---    Icon            =   "MainForm.frx":0000
131 // ---    LinkTopic       =   "Form1"
132 // ---    ScaleHeight     =   390
133 // ---    ScaleMode       =   3  'Pixel  // Pixel
134 // ---    ScaleWidth      =   619
135 // ---    Begin VB.Timer fpsTim 
136 // ---       Enabled         =   0   'False   // False
137 // ---       Interval        =   200
138 // ---       Left            =   1380
139 // ---       Top             =   2940
140 // ---    End
141 // ---    Begin VB.Timer FocusTim 
142 // ---       Interval        =   50
143 // ---       Left            =   1920
144 // ---       Top             =   2940
145 // ---    End
146 // ---    Begin MSComDlg.CommonDialog cmDlg 
147 // ---       Left            =   240
148 // ---       Top             =   2880
149 // ---       _ExtentX        =   847
150 // ---       _ExtentY        =   847
151 // ---       _Version        =   393216
152 // ---       CancelError     =   -1  'True  // True
153 // ---    End
154 // ---    Begin VB.Timer PanelTim 
155 // ---       Enabled         =   0   'False   // False
156 // ---       Interval        =   5
157 // ---       Left            =   840
158 // ---       Top             =   2940
159 // ---    End
160 // ---    Begin VB.Frame Panel 
161 // ---       Height          =   1140
162 // ---       Left            =   180
163 // ---       TabIndex        =   2
164 // ---       Top             =   3540
165 // ---       Width           =   9015
166 // ---       Begin VB.CommandButton cmdPlay 
167 // ---          DisabledPicture =   "MainForm.frx":0442
168 // ---          DownPicture     =   "MainForm.frx":058C
169 // ---          Height          =   345
170 // ---          Left            =   5820
171 // ---          Picture         =   "MainForm.frx":06D6
172 // ---          Style           =   1  'Graphical  // Graphical
173 // ---          TabIndex        =   16
174 // ---          TabStop         =   0   'False   // False
175 // ---          Top             =   240
176 // ---          Width           =   495
177 // ---       End
178 // ---       Begin VB.CommandButton cmdStop 
179 // ---          DisabledPicture =   "MainForm.frx":0820
180 // ---          DownPicture     =   "MainForm.frx":096A
181 // ---          Enabled         =   0   'False   // False
182 // ---          Height          =   345
183 // ---          Left            =   7020
184 // ---          Picture         =   "MainForm.frx":0AB4
185 // ---          Style           =   1  'Graphical  // Graphical
186 // ---          TabIndex        =   15
187 // ---          TabStop         =   0   'False   // False
188 // ---          Top             =   240
189 // ---          Width           =   495
190 // ---       End
191 // ---       Begin VB.CommandButton cmdPlayDemo 
192 // ---          DisabledPicture =   "MainForm.frx":0BFE
193 // ---          DownPicture     =   "MainForm.frx":0D48
194 // ---          Enabled         =   0   'False   // False
195 // ---          Height          =   345
196 // ---          Left            =   7740
197 // ---          Picture         =   "MainForm.frx":0E92
198 // ---          Style           =   1  'Graphical  // Graphical
199 // ---          TabIndex        =   14
200 // ---          TabStop         =   0   'False   // False
201 // ---          Top             =   240
202 // ---          Width           =   495
203 // ---       End
204 // ---       Begin VB.CommandButton cmdPause 
205 // ---          DisabledPicture =   "MainForm.frx":0FDC
206 // ---          DownPicture     =   "MainForm.frx":1126
207 // ---          Enabled         =   0   'False   // False
208 // ---          Height          =   345
209 // ---          Left            =   6420
210 // ---          Picture         =   "MainForm.frx":1270
211 // ---          Style           =   1  'Graphical  // Graphical
212 // ---          TabIndex        =   13
213 // ---          TabStop         =   0   'False   // False
214 // ---          Top             =   240
215 // ---          Width           =   495
216 // ---       End
217 // ---       Begin VB.CommandButton cmdRecordDemo 
218 // ---          DisabledPicture =   "MainForm.frx":13BA
219 // ---          DownPicture     =   "MainForm.frx":1504
220 // ---          Height          =   345
221 // ---          Left            =   8340
222 // ---          Picture         =   "MainForm.frx":164E
223 // ---          Style           =   1  'Graphical  // Graphical
224 // ---          TabIndex        =   12
225 // ---          TabStop         =   0   'False   // False
226 // ---          Top             =   240
227 // ---          Width           =   495
228 // ---       End
229 // ---       Begin VB.ComboBox cmbFile 
230 // ---          BackColor       =   &H00000000&
231 // ---          BeginProperty Font 
232 // ---             Name            =   "Fixedsys"
233 // ---             Size            =   9
234 // ---             Charset         =   0
235 // ---             Weight          =   400
236 // ---             Underline       =   0   'False   // False
237 // ---             Italic          =   0   'False   // False
238 // ---             Strikethrough   =   0   'False   // False
239 // ---          EndProperty
240 // ---          ForeColor       =   &H00FF8080&
241 // ---          Height          =   345
242 // ---          Left            =   120
243 // ---          Sorted          =   -1  'True  // True
244 // ---          Style           =   2  'Dropdown List  // Dropdown List
245 // ---          TabIndex        =   9
246 // ---          Top             =   240
247 // ---          Width           =   3015
248 // ---       End
249 // ---       Begin VB.ComboBox cmbLevel 
250 // ---          BackColor       =   &H00000000&
251 // ---          BeginProperty Font 
252 // ---             Name            =   "Fixedsys"
253 // ---             Size            =   9
254 // ---             Charset         =   0
255 // ---             Weight          =   400
256 // ---             Underline       =   0   'False   // False
257 // ---             Italic          =   0   'False   // False
258 // ---             Strikethrough   =   0   'False   // False
259 // ---          EndProperty
260 // ---          ForeColor       =   &H00FF8080&
261 // ---          Height          =   345
262 // ---          Left            =   840
263 // ---          Sorted          =   -1  'True  // True
264 // ---          Style           =   2  'Dropdown List  // Dropdown List
265 // ---          TabIndex        =   5
266 // ---          Top             =   660
267 // ---          Width           =   4035
268 // ---       End
269 // ---       Begin VB.PictureBox picKeys 
270 // ---          BackColor       =   &H00008080&
271 // ---          BorderStyle     =   0  'None  // None
272 // ---          Height          =   330
273 // ---          Left            =   5400
274 // ---          ScaleHeight     =   22
275 // ---          ScaleMode       =   3  'Pixel  // Pixel
276 // ---          ScaleWidth      =   22
277 // ---          TabIndex        =   4
278 // ---          TabStop         =   0   'False   // False
279 // ---          Top             =   240
280 // ---          Width           =   330
281 // ---          Begin VB.Shape shpKey 
282 // ---             FillColor       =   &H00008000&
283 // ---             FillStyle       =   0  'Solid  // Solid
284 // ---             Height          =   120
285 // ---             Index           =   1
286 // ---             Left            =   0
287 // ---             Shape           =   5  'Rounded Square  // Rounded Square
288 // ---             Top             =   0
289 // ---             Width           =   120
290 // ---          End
291 // ---       End
292 // ---       Begin VB.CommandButton cmdPlayAll 
293 // ---          Caption         =   "Play All Demos"
294 // ---          Enabled         =   0   'False   // False
295 // ---          Height          =   345
296 // ---          Left            =   7380
297 // ---          TabIndex        =   3
298 // ---          TabStop         =   0   'False   // False
299 // ---          Top             =   960
300 // ---          Visible         =   0   'False   // False
301 // ---          Width           =   1395
302 // ---       End
303 // ---       Begin VB.Shape shpProgress 
304 // ---          BackColor       =   &H00800000&
305 // ---          BackStyle       =   1  'Opaque  // Opaque
306 // ---          Height          =   75
307 // ---          Left            =   120
308 // ---          Top             =   1020
309 // ---          Visible         =   0   'False   // False
310 // ---          Width           =   7515
311 // ---       End
312 // ---       Begin VB.Label lblStatus 
313 // ---          Alignment       =   2  'Center  // Center
314 // ---          BorderStyle     =   1  'Fixed Single  // Fixed Single
315 // ---          Caption         =   "MegaPlex"
316 // ---          BeginProperty Font 
317 // ---             Name            =   "Fixedsys"
318 // ---             Size            =   9
319 // ---             Charset         =   0
320 // ---             Weight          =   400
321 // ---             Underline       =   0   'False   // False
322 // ---             Italic          =   0   'False   // False
323 // ---             Strikethrough   =   0   'False   // False
324 // ---          EndProperty
325 // ---          Height          =   345
326 // ---          Left            =   3240
327 // ---          TabIndex        =   11
328 // ---          Top             =   240
329 // ---          Width           =   2055
330 // ---       End
331 // ---       Begin VB.Image Image2 
332 // ---          Height          =   240
333 // ---          Left            =   6780
334 // ---          Picture         =   "MainForm.frx":1BD8
335 // ---          Top             =   720
336 // ---          Width           =   240
337 // ---       End
338 // ---       Begin VB.Image Image1 
339 // ---          Height          =   240
340 // ---          Left            =   5880
341 // ---          Picture         =   "MainForm.frx":1D22
342 // ---          Top             =   720
343 // ---          Width           =   240
344 // ---       End
345 // ---       Begin VB.Label lblFps 
346 // ---          Alignment       =   2  'Center  // Center
347 // ---          BackColor       =   &H00000000&
348 // ---          BorderStyle     =   1  'Fixed Single  // Fixed Single
349 // ---          BeginProperty Font 
350 // ---             Name            =   "Fixedsys"
351 // ---             Size            =   9
352 // ---             Charset         =   0
353 // ---             Weight          =   400
354 // ---             Underline       =   0   'False   // False
355 // ---             Italic          =   0   'False   // False
356 // ---             Strikethrough   =   0   'False   // False
357 // ---          EndProperty
358 // ---          ForeColor       =   &H0000C0C0&
359 // ---          Height          =   345
360 // ---          Left            =   120
361 // ---          TabIndex        =   10
362 // ---          Top             =   660
363 // ---          Width           =   615
364 // ---       End
365 // ---       Begin VB.Label lblInfoCount 
366 // ---          Alignment       =   2  'Center  // Center
367 // ---          BackColor       =   &H00000000&
368 // ---          BorderStyle     =   1  'Fixed Single  // Fixed Single
369 // ---          BeginProperty Font 
370 // ---             Name            =   "Fixedsys"
371 // ---             Size            =   9
372 // ---             Charset         =   0
373 // ---             Weight          =   400
374 // ---             Underline       =   0   'False   // False
375 // ---             Italic          =   0   'False   // False
376 // ---             Strikethrough   =   0   'False   // False
377 // ---          EndProperty
378 // ---          ForeColor       =   &H00FF8080&
379 // ---          Height          =   345
380 // ---          Left            =   5040
381 // ---          TabIndex        =   8
382 // ---          Top             =   660
383 // ---          Width           =   795
384 // ---       End
385 // ---       Begin VB.Label lblRedDiskCount 
386 // ---          Alignment       =   2  'Center  // Center
387 // ---          BackColor       =   &H00000000&
388 // ---          BorderStyle     =   1  'Fixed Single  // Fixed Single
389 // ---          BeginProperty Font 
390 // ---             Name            =   "Fixedsys"
391 // ---             Size            =   9
392 // ---             Charset         =   0
393 // ---             Weight          =   400
394 // ---             Underline       =   0   'False   // False
395 // ---             Italic          =   0   'False   // False
396 // ---             Strikethrough   =   0   'False   // False
397 // ---          EndProperty
398 // ---          ForeColor       =   &H000000FF&
399 // ---          Height          =   345
400 // ---          Left            =   6240
401 // ---          TabIndex        =   7
402 // ---          Top             =   660
403 // ---          Width           =   495
404 // ---       End
405 // ---       Begin VB.Label lblFrameCount 
406 // ---          Alignment       =   2  'Center  // Center
407 // ---          BorderStyle     =   1  'Fixed Single  // Fixed Single
408 // ---          Caption         =   "0"
409 // ---          BeginProperty Font 
410 // ---             Name            =   "Fixedsys"
411 // ---             Size            =   9
412 // ---             Charset         =   0
413 // ---             Weight          =   400
414 // ---             Underline       =   0   'False   // False
415 // ---             Italic          =   0   'False   // False
416 // ---             Strikethrough   =   0   'False   // False
417 // ---          EndProperty
418 // ---          Height          =   345
419 // ---          Left            =   7200
420 // ---          TabIndex        =   6
421 // ---          Top             =   660
422 // ---          Width           =   1635
423 // ---       End
424 // ---    End
425 // ---    Begin VB.PictureBox picViewPort 
426 // ---       BorderStyle     =   0  'None  // None
427 // ---       Height          =   2535
428 // ---       Left            =   180
429 // ---       ScaleHeight     =   169
430 // ---       ScaleMode       =   3  'Pixel  // Pixel
431 // ---       ScaleWidth      =   265
432 // ---       TabIndex        =   0
433 // ---       TabStop         =   0   'False   // False
434 // ---       Top             =   180
435 // ---       Width           =   3975
436 // ---       Begin VB.PictureBox picPane 
437 // ---          AutoSize        =   -1  'True  // True
438 // ---          BackColor       =   &H00000000&
439 // ---          BorderStyle     =   0  'None  // None
440 // ---          DrawWidth       =   3
441 // ---          Height          =   1635
442 // ---          Left            =   240
443 // ---          ScaleHeight     =   109
444 // ---          ScaleMode       =   3  'Pixel  // Pixel
445 // ---          ScaleWidth      =   129
446 // ---          TabIndex        =   1
447 // ---          Top             =   180
448 // ---          Width           =   1935
449 // ---       End
450 // ---       Begin VB.PictureBox picFrame 
451 // ---          Height          =   915
452 // ---          Left            =   1320
453 // ---          ScaleHeight     =   57
454 // ---          ScaleMode       =   3  'Pixel  // Pixel
455 // ---          ScaleWidth      =   81
456 // ---          TabIndex        =   17
457 // ---          TabStop         =   0   'False   // False
458 // ---          Top             =   1200
459 // ---          Width           =   1275
460 // ---       End
461 // ---    End
462 // ---    Begin VB.Menu menFile 
463 // ---       Caption         =   "&File"
464 // ---       Begin VB.Menu menNewStd 
465 // ---          Caption         =   "New S&tandard Level"
466 // ---       End
467 // ---       Begin VB.Menu menNew 
468 // ---          Caption         =   "&New Level"
469 // ---       End
470 // ---       Begin VB.Menu menDash5 
471 // ---          Caption         =   "-"
472 // ---       End
473 // ---       Begin VB.Menu menOpen 
474 // ---          Caption         =   "&Open"
475 // ---       End
476 // ---       Begin VB.Menu menSave 
477 // ---          Caption         =   "&Save"
478 // ---          Shortcut        =   ^S
479 // ---       End
480 // ---       Begin VB.Menu menSaveAs 
481 // ---          Caption         =   "Save &As ..."
482 // ---       End
483 // ---       Begin VB.Menu menDash4 
484 // ---          Caption         =   "-"
485 // ---       End
486 // ---       Begin VB.Menu menExit 
487 // ---          Caption         =   "E&xit"
488 // ---          Shortcut        =   ^Q
489 // ---       End
490 // ---    End
491 // ---    Begin VB.Menu menEditMain 
492 // ---       Caption         =   "&Edit"
493 // ---       Begin VB.Menu menEdit 
494 // ---          Caption         =   "&Edit Level"
495 // ---       End
496 // ---       Begin VB.Menu menDash3 
497 // ---          Caption         =   "-"
498 // ---       End
499 // ---       Begin VB.Menu menCopy 
500 // ---          Caption         =   "&Copy"
501 // ---          Shortcut        =   ^C
502 // ---       End
503 // ---       Begin VB.Menu menPaste 
504 // ---          Caption         =   "&Paste"
505 // ---          Shortcut        =   ^V
506 // ---       End
507 // ---       Begin VB.Menu menTrim 
508 // ---          Caption         =   "&Trim"
509 // ---          Shortcut        =   ^T
510 // ---       End
511 // ---       Begin VB.Menu menDash8 
512 // ---          Caption         =   "-"
513 // ---          Index           =   1
514 // ---       End
515 // ---       Begin VB.Menu menSelectAll 
516 // ---          Caption         =   "Select &All"
517 // ---          Shortcut        =   ^A
518 // ---       End
519 // ---       Begin VB.Menu menDash6 
520 // ---          Caption         =   "-"
521 // ---       End
522 // ---       Begin VB.Menu menRestoreBorder 
523 // ---          Caption         =   "Restore &border"
524 // ---          Shortcut        =   ^B
525 // ---       End
526 // ---       Begin VB.Menu menSp 
527 // ---          Caption         =   "Special Port"
528 // ---          Begin VB.Menu menGravOn 
529 // ---             Caption         =   "&Gravity On"
530 // ---          End
531 // ---          Begin VB.Menu menZonkOn 
532 // ---             Caption         =   "Freeze &Zonks On"
533 // ---          End
534 // ---          Begin VB.Menu menEnOn 
535 // ---             Caption         =   "Freeze &Enemies On"
536 // ---          End
537 // ---          Begin VB.Menu menDash7 
538 // ---             Caption         =   "-"
539 // ---          End
540 // ---          Begin VB.Menu menGravOff 
541 // ---             Caption         =   "Gravity &Off"
542 // ---          End
543 // ---          Begin VB.Menu menZonkOff 
544 // ---             Caption         =   "Freeze Zon&ks Off"
545 // ---          End
546 // ---          Begin VB.Menu menEnOff 
547 // ---             Caption         =   "Freeze E&nemies Off"
548 // ---          End
549 // ---          Begin VB.Menu menDash10 
550 // ---             Caption         =   "-"
551 // ---          End
552 // ---          Begin VB.Menu menRemSP 
553 // ---             Caption         =   "&Remove (Make Normal Port)"
554 // ---          End
555 // ---       End
556 // ---    End
557 // ---    Begin VB.Menu menView 
558 // ---       Caption         =   "&View"
559 // ---       Begin VB.Menu menZoom 
560 // ---          Caption         =   "&Zoom"
561 // ---          Begin VB.Menu menStretch 
562 // ---             Caption         =   "0.25 : 1"
563 // ---             Index           =   1
564 // ---          End
565 // ---       End
566 // ---       Begin VB.Menu menBorder 
567 // ---          Caption         =   "Show &Border"
568 // ---          Checked         =   -1  'True  // True
569 // ---       End
570 // ---       Begin VB.Menu menPanel 
571 // ---          Caption         =   "Show &Panel"
572 // ---          Checked         =   -1  'True  // True
573 // ---       End
574 // ---       Begin VB.Menu menAutoScroll 
575 // ---          Caption         =   "&Autoscroll"
576 // ---          Checked         =   -1  'True  // True
577 // ---       End
578 // ---       Begin VB.Menu menDash9 
579 // ---          Caption         =   "-"
580 // ---       End
581 // ---       Begin VB.Menu menShowLInfo 
582 // ---          Caption         =   "Show Level &Info"
583 // ---       End
584 // ---    End
585 // ---    Begin VB.Menu menSound 
586 // ---       Caption         =   "&Sound"
587 // ---       Begin VB.Menu menSoundFX 
588 // ---          Caption         =   "&FX"
589 // ---       End
590 // ---       Begin VB.Menu menMusic 
591 // ---          Caption         =   "&Music"
592 // ---          Enabled         =   0   'False   // False
593 // ---       End
594 // ---    End
595 // ---    Begin VB.Menu menSpeed 
596 // ---       Caption         =   "Speed"
597 // ---       Begin VB.Menu menSpeedPlay 
598 // ---          Caption         =   "Game play"
599 // ---          Index           =   11
600 // ---          Begin VB.Menu menPlaySpeed 
601 // ---             Caption         =   "1"
602 // ---             Index           =   1
603 // ---          End
604 // ---       End
605 // ---       Begin VB.Menu menSpeedDemo 
606 // ---          Caption         =   "Demo playback"
607 // ---          Begin VB.Menu menDemoSpeed 
608 // ---             Caption         =   "1"
609 // ---             Index           =   1
610 // ---          End
611 // ---       End
612 // ---       Begin VB.Menu menSpeedDash 
613 // ---          Caption         =   "-"
614 // ---       End
615 // ---       Begin VB.Menu menFaster 
616 // ---          Caption         =   "Faster (Pg Up)"
617 // ---       End
618 // ---       Begin VB.Menu menSlower 
619 // ---          Caption         =   "Slower (Pg Down)"
620 // ---       End
621 // ---    End
622 // ---    Begin VB.Menu menRun 
623 // ---       Caption         =   "&Play"
624 // ---       Begin VB.Menu menPlay 
625 // ---          Caption         =   "P&lay Game    (Space)"
626 // ---       End
627 // ---       Begin VB.Menu menPause 
628 // ---          Caption         =   "&Pause    (P)"
629 // ---          Enabled         =   0   'False   // False
630 // ---       End
631 // ---       Begin VB.Menu menStop 
632 // ---          Caption         =   "&Stop    (Q)"
633 // ---          Enabled         =   0   'False   // False
634 // ---       End
635 // ---       Begin VB.Menu menDash0 
636 // ---          Caption         =   "-"
637 // ---       End
638 // ---       Begin VB.Menu menRec 
639 // ---          Caption         =   "&Record Demo    (Ctrl+R)"
640 // ---       End
641 // ---       Begin VB.Menu menPlayDemo 
642 // ---          Caption         =   "Play &Demo    (Ctrl+Space)"
643 // ---       End
644 // ---       Begin VB.Menu menPlayAll 
645 // ---          Caption         =   "Play &All Demos"
646 // ---       End
647 // ---    End
648 // ---    Begin VB.Menu menOptionsMain 
649 // ---       Caption         =   "&Options"
650 // ---       Begin VB.Menu menOptions 
651 // ---          Caption         =   "&Options ..."
652 // ---       End
653 // ---    End
654 // ---    Begin VB.Menu menHelp 
655 // ---       Caption         =   "Help"
656 // ---       Begin VB.Menu menToolTips 
657 // ---          Caption         =   "Show &ToolTips"
658 // ---       End
659 // ---       Begin VB.Menu menDash1 
660 // ---          Caption         =   "-"
661 // ---       End
662 // ---       Begin VB.Menu menAbout 
663 // ---          Caption         =   "&About MegaPlex"
664 // ---       End
665 // ---    End
666 // --- End
667
668 // static char *VB_Name = "MainForm";
669 // static boolean VB_GlobalNameSpace = False;
670 // static boolean VB_Creatable = False;
671 // static boolean VB_PredeclaredId = True;
672 // static boolean VB_Exposed = False;
673
674 // --- Option Explicit
675 // --- Option Compare Text
676
677 long SetParent(long hWndChild, long hWndNewParent);
678 long SetWindowLong(long hWnd, long nIndex, long dwNewLong);
679 long GetWindowLong(long hWnd, long nIndex);
680 long GetTempPath(long nBufferLength, char *lpBuffer);
681 long GetWindowText(long hWnd, char *lpString, long cch);
682
683 #define GWL_STYLE                       ((-16))
684 #define WS_CHILD                        (0x40000000)
685 #define WS_POPUP                        (0x80000000)
686
687 // [UNCOMMENTED] Dim StretchWidth2&
688
689 char *MpxBmp;
690 int OldPointer;
691 long OSX, OSY, MDX, MDY;
692 int MouseButton;
693
694 int PanelSeq;
695
696 boolean Loaded, DemosAvailable;
697
698 #define nSpeedCount                     (17)
699 int SpeedsByDelayUS[nSpeedCount + 1];
700 #define DefaultSpeedIndex                       (12)
701
702 currency LastTick, LastFrame;
703 TickCountObject T;
704
705 const char *AppTitle = "MegaPlex";
706
707 #if 0
708
709 static char *GetSpeedDescriptionFPS(currency FrameDelayUS)
710 {
711   static char *GetSpeedDescriptionFPS;
712
713   long FPS;
714
715   if (FrameDelayUS == 0)
716   {
717     GetSpeedDescriptionFPS = "Fastest possible";
718     return GetSpeedDescriptionFPS;
719   }
720
721   if (FrameDelayUS < 0)
722   {
723     GetSpeedDescriptionFPS = "! display (even faster)";
724     return GetSpeedDescriptionFPS;
725   }
726
727   FPS = 1000000 / FrameDelayUS;
728   GetSpeedDescriptionFPS = CAT(INT_TO_STR(FPS), " fps");
729   if (35 == FPS)
730     GetSpeedDescriptionFPS = CAT(GetSpeedDescriptionFPS, "(Default)");
731
732   return GetSpeedDescriptionFPS;
733 }
734
735 static int InitSpeeds()
736 {
737   static int InitSpeeds;
738
739   SpeedsByDelayUS[1] = -1;  // no display
740   SpeedsByDelayUS[2] = 0; // fastest possible
741   SpeedsByDelayUS[3] = 1000000 / 700; // 700 fps
742   SpeedsByDelayUS[4] = 1000000 / 500; // 500 fps
743   SpeedsByDelayUS[5] = 1000000 / 350; // 350 fps
744   SpeedsByDelayUS[6] = 1000000 / 250; // 250 fps
745   SpeedsByDelayUS[7] = 1000000 / 200; // etc.
746   SpeedsByDelayUS[8] = 1000000 / 150;
747   SpeedsByDelayUS[9] = 1000000 / 100;
748   SpeedsByDelayUS[10] = 1000000 / 70;
749   SpeedsByDelayUS[11] = 1000000 / 50;
750   SpeedsByDelayUS[12] = 1000000 / 35; // default
751   SpeedsByDelayUS[13] = 1000000 / 25;
752   SpeedsByDelayUS[14] = 1000000 / 20;
753   SpeedsByDelayUS[15] = 1000000 / 10;
754   SpeedsByDelayUS[16] = 1000000 / 5;
755   SpeedsByDelayUS[17] = 1000000 / 1;
756
757   return InitSpeeds;
758 }
759
760 static int GetSpeedIndex(long DelayUS)
761 {
762   static int GetSpeedIndex;
763
764   int i;
765   long Diff, MinDiff;
766   int MinIdx;
767
768   MinIdx = DefaultSpeedIndex;
769   MinDiff = 1000000;
770   for (i = 1; i <= nSpeedCount; i++)
771   {
772     Diff = Abs(DelayUS - SpeedsByDelayUS[i]);
773     if (Diff < MinDiff)
774     {
775       MinDiff = Diff;
776       MinIdx = i;
777     }
778   }
779
780   GetSpeedIndex = MinIdx;
781
782   return GetSpeedIndex;
783 }
784
785 static long Get_LastOpenFilter()
786 {
787   static long LastOpenFilter;
788
789   SettingsObject s;
790
791   LastOpenFilter = s.Read("LastOpenFilter", 1);
792
793   return LastOpenFilter;
794 }
795
796 static void Let_LastOpenFilter(long NewVal)
797 {
798   SettingsObject s;
799
800   SettingsObject_Save("LastOpenFilter", NewVal);
801 }
802
803 static long Get_LastSaveFilter()
804 {
805   static long LastSaveFilter;
806
807   SettingsObject s;
808
809   LastSaveFilter = s.Read("LastSaveFilter", 1);
810
811   return LastSaveFilter;
812 }
813
814 static void Let_LastSaveFilter(long NewVal)
815 {
816   SettingsObject s;
817
818   SettingsObject_Save("LastSaveFilter", NewVal);
819 }
820
821 #endif
822
823 static void CountDown(long HalfSeconds, int bDisplayBar)
824 {
825
826 #if 0
827
828   long i;
829   int k;
830   long dT, Delta;
831   long LeftPos, TopPos, RightPos, MaxWidth; // , MaxHeight&
832
833   dT = HalfSeconds * 500;
834   Delta = dT / 200;
835   LeftPos = cmbFile.left;
836   TopPos = cmbFile.top - shpProgress.Height - cmbFile.top / 8;
837   RightPos = lblFrameCount.left + lblFrameCount.Width;
838   MaxWidth = RightPos - LeftPos;
839   // MaxHeight = lblFrameCount.Top - TopPos + lblFrameCount.Height + 4
840   if (bDisplayBar)
841   {
842     shpProgress.Move(LeftPos, TopPos, 0); // , MaxHeight
843     shpProgress.Visible = True;
844   }
845
846   for (i = 1; i <= 200; i++)
847   {
848     // lblFrameCount = i
849     if (bDisplayBar)
850     {
851       if (i < 101)
852       {
853         shpProgress.Width = MaxWidth * i / 100;
854       }
855       else
856       {
857         k = MaxWidth * (i - 100) / 100;
858         shpProgress.Move(k, TopPos, RightPos - k);
859       }
860     }
861
862     T.DelayMS(Delta, False);
863   }
864
865   shpProgress.Visible = False;
866   // lblFrameCount = 0
867
868 #endif
869
870 }
871
872 #if 0
873
874 static char *GetValidTempPath()
875 {
876   static char *GetValidTempPath;
877
878   char *TP;
879   long nSize, L;
880
881   GetValidTempPath = "";
882   nSize = 255;
883   TP = String(nSize, Chr(32));
884   L = GetTempPath(nSize, TP);
885   if (nSize < L)
886   {
887     // buffer was too small, retry with a properly sized buffer:
888     nSize = L;
889     TP = String(nSize, Chr(32));
890     L = GetTempPath(nSize, TP);
891   }
892
893   TP = SlashLess(left(TP, L));
894   if ((0 == L) || (! IsDir(TP)))
895   {
896     // no valid temp path can be retrieved from the system --> create our own
897     TP = CAT(WithSlash(App.Path), "Temp");
898     if (! IsDir(TP))
899     {
900
901       // --- On Error Resume Next
902       MkDir(TP);
903       if (! IS_NOTHING(&Err, sizeof(Err)))
904       {
905         MESSAGE_BOX("an error occured"); // MsgBox "cannot create directory for temporary files " & TP, vbCritical, "MegaPlex - Error";
906         return GetValidTempPath;
907       }
908
909       // --- On Error GoTo 0
910
911     }
912   }
913
914   if (IsDir(TP))
915     GetValidTempPath = TP;
916
917   return GetValidTempPath;
918 }
919
920 static char *GetMyTempFileName()
921 {
922   static char *GetMyTempFileName;
923
924   char *T;
925
926   (char *)T = Hex(hWnd);
927   while (Len(T) < 8)
928   {
929     T = CAT("0", T);
930   }
931
932   GetMyTempFileName = CAT("tmp0x", CAT((char *)T, ".mpx"));
933
934   return GetMyTempFileName;
935 }
936
937 static long GetHwndFromTempFileName(char *TmpFile)
938 {
939   static long GetHwndFromTempFileName;
940
941   char *FN;
942   long LP;
943   int L;
944   char *NumString;
945
946   GetHwndFromTempFileName = 0;
947   FN = StripFileName(TmpFile);
948   LP = Len("tmp0x");
949   L = Len(TmpFile);
950   if (LP < L)
951     return GetHwndFromTempFileName;
952
953   L = L - LP;
954   NumString = right(FN, L - LP);
955   if (8 < L)
956     NumString = left(NumString, 8);
957
958   NumString = CAT("&H", NumString);
959   GetHwndFromTempFileName = Val(NumString);
960
961   return GetHwndFromTempFileName;
962 }
963
964 static void CleanTempDir(char *TmpDir)
965 {
966   char *FN, *MFN;
967
968   MFN = GetMyTempFileName();
969   MayKill(CAT(WithSlash(TmpDir), MFN));
970   FN = Dir(CAT(WithSlash(TmpDir), "tmp0x*.mpx"));
971   while (FN != "")
972   {
973     if (STRING_IS_LIKE(FN, "tmp0x*.mpx"))
974     {
975       if (! InstanceStillRunning(FN))
976         MayKill(CAT(WithSlash(TmpDir), FN));
977     }
978
979     FN = Dir_Without_Args();
980   }
981 }
982
983 static boolean InstanceStillRunning(char *TmpFile)
984 {
985   static boolean InstanceStillRunning;
986
987   long OtherHwnd, nSize, L;
988   char *Cap;
989
990   InstanceStillRunning = False;
991   OtherHwnd = GetHwndFromTempFileName(TmpFile);
992   nSize = 255;
993   Cap = String(nSize, Chr(32));
994   L = GetWindowText(OtherHwnd, Cap, nSize);
995   if ((L == 0) || (nSize < L))
996     return InstanceStillRunning;
997
998   if (STRING_IS_LIKE(Cap, CAT(AppTitle, "*")))
999     InstanceStillRunning = True;
1000
1001   return InstanceStillRunning;
1002 }
1003
1004 #endif
1005
1006 void DisplayLevel()
1007 {
1008   int X, Y;
1009
1010 #if 0
1011   printf("::: MainForm.c: DisplayLevel(): %d, %d, %d\n",
1012          Loaded, LevelLoaded, bPlaying);
1013 #endif
1014
1015   if (! Loaded)
1016     return;
1017
1018   if (! LevelLoaded)
1019     return;
1020
1021 #if 1
1022   SetDisplayRegion();
1023 #endif
1024
1025   DrawFrame(0);
1026
1027   /* !!! CHECK THIS !!! */
1028 #if 1
1029   if (! menBorder.Checked)
1030     DrawFrame(1);
1031 #endif
1032
1033   if (bPlaying)
1034   {
1035 #if 0
1036     printf("::: MainForm.c: DisplayLevel(): [%ld, %ld, %ld, %ld] [%d, %d]...\n",
1037            DisplayMinX, DisplayMinY, DisplayMaxX, DisplayMaxY,
1038            FieldWidth, FieldHeight);
1039 #endif
1040
1041     for (Y = DisplayMinY; Y <= DisplayMaxY; Y++)
1042     {
1043       for (X = DisplayMinX; X <= DisplayMaxX; X++)
1044       {
1045         DrawFieldNoAnimated(X, Y);
1046       }
1047     }
1048
1049     for (Y = DisplayMinY; Y <= DisplayMaxY; Y++)
1050     {
1051       for (X = DisplayMinX; X <= DisplayMaxX; X++)
1052       {
1053         DrawFieldAnimated(X, Y);
1054       }
1055     }
1056
1057   }
1058   else
1059   {
1060     for (Y = DisplayMinY; Y <= DisplayMaxY; Y++)
1061     {
1062       for (X = DisplayMinX; X <= DisplayMaxX; X++)
1063       {
1064         DrawField(X, Y);
1065       }
1066     }
1067   }
1068 }
1069
1070 #if 0
1071
1072 static void cmbFile_Click()
1073 {
1074   CurPath = CAT(StripDir(OrigPath), CAT("/", cmbFile.List(cmbFile.ListIndex)));
1075   OrigPath = CurPath;
1076   FillLevelList(CurPath, LevelNumber);
1077   if (STRING_IS_LIKE(OrigPath, "*.mpx") || STRING_IS_LIKE(OrigPath, "*.sp"))
1078   {
1079     menSave.Enabled = True;
1080   }
1081   else
1082   {
1083     menSave.Enabled = False;
1084   }
1085 }
1086
1087 static void cmbFile_KeyDown(int KeyCode, int Shift)
1088 {
1089   picPane_KeyDown(KeyCode, Shift);
1090 }
1091
1092 static void cmbFile_KeyUp(int KeyCode, int Shift)
1093 {
1094   picPane_KeyUp(KeyCode, Shift);
1095 }
1096
1097 static int GetFileNameToSave()
1098 {
1099   static int GetFileNameToSave;
1100
1101   char *T;
1102
1103   (char *)T = StripFileName(OrigPath);
1104   if (STRING_IS_LIKE(T, "*.dat") || STRING_IS_LIKE(T, "*.d##"))
1105   {
1106     T = "Level" & Format(LevelNumber, "#000");
1107   }
1108
1109   GetFileNameToSave = T;
1110
1111   return GetFileNameToSave;
1112 }
1113
1114 static void cmbLevel_Click()
1115 {
1116   long InfoCount;
1117   boolean Oldflag;
1118   int Msg;
1119   static long LastIndex = 0;
1120   static boolean bBlock = False;
1121
1122   if (bBlock)
1123     return;
1124
1125   // //////////////////////////////////////////////////////////////////
1126   if (ModifiedFlag)
1127   {
1128     VbMsgBoxResult Res;
1129
1130     Msg = "Save changes to " & GetFileNameToSave() & " ?";
1131     Res = MsgBox(Msg, vbYesNoCancel, AppTitle & " - close level");
1132     if (Res == vbCancel) // fallback
1133     {
1134       bBlock = True;
1135
1136       // --- On Error Resume Next
1137       cmbLevel.ListIndex = LastIndex;
1138       // --- On Error GoTo 0
1139
1140       bBlock = False;
1141       Debug.Assert cmbLevel.ListIndex = LastIndex;
1142       return;
1143     }
1144
1145     if (Res == vbYes)
1146     {
1147       // Dim oldCurPath$
1148       // oldCurPath = CurPath
1149       if (FileExists(OrigPath) && menSave.Enabled)
1150       {
1151         menSave_Click();
1152       }
1153       else
1154       {
1155         if (! SaveAs()) // fallback
1156         {
1157           bBlock = True;
1158
1159           // --- On Error Resume Next
1160           cmbLevel.ListIndex = LastIndex;
1161           // --- On Error GoTo 0
1162
1163           bBlock = False;
1164           Debug.Assert cmbLevel.ListIndex = LastIndex;
1165           return;
1166         }
1167       }
1168
1169       // CurPath = oldCurPath
1170     }
1171
1172     Let_ModifiedFlag(False);
1173     CurPath = OrigPath;
1174   }
1175
1176   // //////////////////////////////////////////////////////////////////
1177   {
1178     LevelNumber = cmbLevel.ListIndex + 1;
1179     DemoFlag = False;
1180     if (Loaded)
1181     {
1182       Oldflag = NoDisplayFlag;
1183       NoDisplayFlag = False;
1184       subFetchAndInitLevel();
1185       if ((0 < SignatureDelay) && ! (WindowState == vbMinimized))
1186       {
1187         Msg = LInfo.LevelTitle & vbNewLine & "(" & FieldWidth & " x " & FieldHeight & ")";
1188         if (DemoAvailable && bSignatureAvailable)
1189           Msg = Msg & vbNewLine & vbNewLine & gSignature;
1190
1191         SignatureForm.Signature = Msg;
1192         SignatureForm.DelayMS = SignatureDelay;
1193         int X, Y;
1194
1195         X = left + (Width - SignatureForm.Width) / 2;
1196         Y = top + (Height - SignatureForm.Height) / 2;
1197         SignatureForm.Move X, Y;
1198         SignatureForm.Show vbModeless, Me;
1199         Me.SetFocus;
1200       }
1201
1202       lblStatus = "MegaPlex";
1203       lblFrameCount = GetTotalFramesOfDemo();
1204       if (EditFlag)
1205         EdAll();
1206
1207       ReStretch(Stretch);
1208       // picPane_Paint
1209       Stage.Blt();
1210       NoDisplayFlag = Oldflag;
1211       LastIndex = cmbLevel.ListIndex;
1212     }
1213
1214   }
1215   menPlayDemo.Enabled = DemoAvailable;
1216   cmdPlayDemo.Enabled = DemoAvailable;
1217 }
1218
1219 static void cmblevel_KeyDown(int KeyCode, int Shift)
1220 {
1221   if (GameLoopRunning != 0)
1222     return;
1223
1224   switch (KeyCode)
1225   {
1226     case vbKeySpace:
1227       if (menPlay.Enabled == True)
1228         menPlay_Click();
1229
1230       break;
1231
1232     default:
1233       picPane_KeyDown(KeyCode, Shift);
1234       break;
1235   }
1236 }
1237
1238 static void cmbLevel_KeyUp(int KeyCode, int Shift)
1239 {
1240   picPane_KeyUp(KeyCode, Shift);
1241 }
1242
1243 static void cmdPause_Click()
1244 {
1245   menPause_Click();
1246 }
1247
1248 static void cmdPause_MouseUp(int Button, int Shift, float X, float Y)
1249 {
1250   FocusTim.Enabled = True;
1251 }
1252
1253 static void cmdPlay_Click()
1254 {
1255   if (menPlay.Enabled)
1256     menPlay_Click();
1257 }
1258
1259 static void cmdPlay_MouseUp(int Button, int Shift, float X, float Y)
1260 {
1261   FocusTim.Enabled = True;
1262 }
1263
1264 static void cmdPlayAll_Click()
1265 {
1266   menPlayAll_Click();
1267 }
1268
1269 static void cmdPlayAll_MouseUp(int Button, int Shift, float X, float Y)
1270 {
1271   FocusTim.Enabled = True;
1272 }
1273
1274 static void cmdPlayDemo_Click()
1275 {
1276   menPlayDemo_Click();
1277 }
1278
1279 static void cmdPlayDemo_MouseUp(int Button, int Shift, float X, float Y)
1280 {
1281   FocusTim.Enabled = True;
1282 }
1283
1284 static void cmdRecordDemo_Click()
1285 {
1286   if (menRec.Enabled)
1287     menRec_Click();
1288 }
1289
1290 static void cmdRecordDemo_MouseUp(int Button, int Shift, float X, float Y)
1291 {
1292   FocusTim.Enabled = True;
1293 }
1294
1295 static void cmdStop_Click()
1296 {
1297   if (menStop.Enabled)
1298     menStop_Click();
1299 }
1300
1301 static void cmdStop_MouseUp(int Button, int Shift, float X, float Y)
1302 {
1303   FocusTim.Enabled = True;
1304 }
1305
1306 static void FocusTim_Timer()
1307 {
1308   FocusTim.Enabled = False;
1309
1310   // --- On Error Resume Next
1311   picPane.SetFocus;
1312 }
1313
1314 static void Form_KeyDown(int KeyCode, int Shift)
1315 {
1316   picPane_KeyDown(KeyCode, Shift);
1317 }
1318
1319 static void Form_KeyUp(int KeyCode, int Shift)
1320 {
1321   picPane_KeyUp(KeyCode, Shift);
1322 }
1323
1324 #endif
1325
1326 void Form_Load()
1327 {
1328 #if 0
1329
1330   long i;
1331   SettingsObject s;
1332
1333 #endif
1334
1335   Loaded = False;
1336   DemoFlag = 0;
1337   PanelSeq = 0;
1338   EditFlag = False;
1339   EditMode = edMove;
1340   // FMark = New MarkerObject; // (handle this later, if needed)
1341
1342   InitGlobals();
1343
1344 #if 0
1345
1346   LoadMenus();
1347
1348   TmpPath = GetValidTempPath();
1349   if (TmpPath == "")
1350     exit(-1);
1351
1352   CleanTempDir(TmpPath);
1353   TmpPath = CAT(WithSlash(TmpPath), GetMyTempFileName());
1354   GetSettings();
1355   ShowPanel = 1;
1356
1357   InitDirectX(hWnd, picPane.hWnd);
1358
1359   //  AllowRedDiskCheat = 1
1360   MpxBmp = CAT(App.Path, "/mpx.bmp");
1361   // // Set NormalSprites = New DDSpriteBuffer // (handle this later, if needed)
1362   // NormalSprites.CreateFromFile MPXBMP, 16, 16
1363
1364   Field = REDIM_2D(sizeof(int), 0, 2 + 1 - 1, 0, 2 + 1 - 1);
1365
1366   picViewPort.ScaleMode = vbPixels;
1367
1368 #endif
1369
1370   PauseMode = 0;
1371   //  BaseWidth = 16
1372
1373 #if 0
1374   menBorder_Click();
1375 #endif
1376
1377   Loaded = True;
1378
1379   ReStretch(Stretch);
1380
1381 #if 0
1382
1383   LoadKeyIndicators();
1384
1385   Show();
1386
1387   FillFileList(CurPath);
1388   if (s.Read("ShowSplash", True))
1389   {
1390     // frmSplash.EnableTimer
1391     frmSplash.Show vbModal, Me;
1392   }
1393
1394 #endif
1395 }
1396
1397 #if 0
1398
1399 static void FillLevelList(char *Path, int LevelIndex)
1400 {
1401   long FNum, LevLen, i, iMax;
1402   char *TFile;
1403
1404   cmbLevel.Clear;
1405   if (! FileExists(Path))
1406     return;
1407
1408   if ((STRING_IS_LIKE(Path, "*.D??")) || (STRING_IS_LIKE(Path, "*.sp")))
1409   {
1410     FNum = FreeFile();
1411     LevLen = 1536;
1412     FNum = fopen(Path, "rb");
1413     {
1414       i = 0;
1415       FILE_GET(FNum, (long)1441 + i * LevLen, &LInfo, sizeof(LInfo));
1416       cmbLevel.AddItem Format(i + 1, "#000") & "   " & LInfo.LevelTitle, i;
1417       if (STRING_IS_LIKE(Path, "*.D??"))
1418       {
1419         while (!(EOF(FNum)))
1420         {
1421           i = i + 1;
1422           FILE_GET(FNum, (long)1441 + i * LevLen, &LInfo, sizeof(LInfo));
1423           if (EOF(FNum))
1424             break;
1425
1426           cmbLevel.AddItem Format(i + 1, "#000") & "   " & LInfo.LevelTitle, i;
1427         }
1428       }
1429
1430       if (LevelIndex <= i && 0 < LevelIndex)
1431         i = LevelIndex - 1;
1432       else
1433         i = 0;
1434
1435       cmbLevel.ListIndex = i;
1436     }
1437     fclose(FNum);
1438   }
1439   else if (STRING_IS_LIKE(Path, "*.mpx"))
1440   {
1441     FNum = FreeFile();
1442     LevLen = 1536;
1443     FNum = fopen(Path, "rb");
1444     {
1445       i = 0;
1446       if (MpxOpen(Path))
1447       {
1448         for (i = 1; i <= LevelCount; i++)
1449         {
1450           if (! MpxLoadLInfo(CInt(i)))
1451             break;
1452
1453           cmbLevel.AddItem Format(i, "#000") & "   " & LInfo.LevelTitle, i - 1;
1454         }
1455
1456         MpxClose();
1457       }
1458
1459       if (LevelIndex < i && 0 < LevelIndex)
1460         i = LevelIndex - 1;
1461       else
1462         i = 0;
1463
1464       if (i < cmbLevel.ListCount)
1465         cmbLevel.ListIndex = i;
1466
1467     }
1468     fclose(FNum);
1469   }
1470 }
1471
1472 static void FillFileList(char *Path)
1473 {
1474   long FNum, LevLen, i;
1475   int nDemoCount;
1476   char *TFile, *TPath, *OFile;
1477
1478   cmbFile.Clear;
1479   i = 0;
1480   nDemoCount = 0;
1481   TPath = WithSlash(StripDir(Path));
1482   OFile = StripFileName(Path);
1483   TFile = Dir(TPath & "*.D??");
1484   while (TFile != "")
1485   {
1486     if (FileLen(TPath & TFile) == 170496)
1487     {
1488       cmbFile.AddItem TFile;
1489     }
1490
1491     TFile = Dir;
1492   }
1493
1494   TFile = Dir(TPath & "*.SP");
1495   while (TFile != "")
1496   {
1497     nDemoCount = nDemoCount + 1;
1498     cmbFile.AddItem TFile;
1499     TFile = Dir;
1500   }
1501
1502   TFile = Dir(TPath & "*.mpx");
1503   while (TFile != "")
1504   {
1505     nDemoCount = nDemoCount + 1;
1506     cmbFile.AddItem TFile;
1507     TFile = Dir;
1508   }
1509
1510   i = cmbFile.ListCount - 1;
1511   if (0 < cmbFile.ListCount)
1512   {
1513     do
1514     {
1515       if ((cmbFile.List(i) Like OFile) || i == 0)
1516         break;
1517
1518       i = i - 1;
1519     }
1520     while (!(i < 1));
1521
1522     cmbFile.ListIndex = i;
1523   }
1524
1525   DemosAvailable = (1 < nDemoCount);
1526   menPlayAll.Enabled = DemosAvailable;
1527   cmdPlayAll.Enabled = DemosAvailable;
1528 }
1529
1530 static void LoadMenus()
1531 {
1532   long i;
1533
1534   // speeds
1535   menFaster.Enabled = False;
1536   menSlower.Enabled = False;
1537   InitSpeeds();
1538   for (i = 1; i <= nSpeedCount; i++)
1539   {
1540     if (1 < i)
1541     {
1542       Load menPlaySpeed(i);
1543       Load menDemoSpeed(i);
1544     }
1545
1546     menPlaySpeed(i).Caption = GetSpeedDescriptionFPS(SpeedsByDelayUS[i]);
1547     menDemoSpeed(i).Caption = GetSpeedDescriptionFPS(SpeedsByDelayUS[i]);
1548   }
1549
1550   // zoom
1551   menStretch(1).Caption = Format(0.25, "#0.00") & ";
1552   1";
1553   if (Stretch == 0.25)
1554     menStretch(1).Checked = True;
1555
1556   for (i = 2; i <= 20; i++)
1557   {
1558     Load menStretch(i);
1559     menStretch(i).Caption = Format(0.25 * i, "#0.00") & ";
1560     1";
1561     menStretch(i).Checked = ((i * 0.25) == Stretch);
1562   }
1563
1564   menSp.Enabled = False;
1565 }
1566
1567 //
1568 // Function DisplayShift&()
1569 // Dim X&, Y&, A&, B&, iX&, iiX&, LD&
1570 // Dim Sprite&
1571 //  DisplayShift = 0
1572 //    For A = 0 To picPane.ScaleWidth
1573 //      For Y = DisplayMinY To DisplayMaxY
1574 //        For X = DisplayMinX To DisplayMaxX
1575 //          iX = StretchWidth * X + A
1576 //          Sprite = Field(X, Y).SpritePos
1577 //          If picPane.ScaleWidth < (iX + 1) Then
1578 //            iX = iX - picPane.ScaleWidth
1579 //          Else
1580 //            If picPane.ScaleWidth < (iX + 1 + BaseWidth) Then
1581 //              iiX = iX - picPane.ScaleWidth
1582 //              Sprites.BitBltSprite picPane.hdc, iiX, StretchWidth * Y, Sprite
1583 //            End If
1584 //          End If
1585 //          Sprites.BitBltSprite picPane.hdc, iX, StretchWidth * Y, Sprite
1586 //        Next X
1587 //      Next Y
1588 //      DisplayShift = DisplayShift + 1
1589 //      'DoEvents
1590 //    Next A
1591 // End Function
1592
1593 static void Form_Paint()
1594 {
1595   // Debug.Print "Form_Paint()"
1596 }
1597
1598 static void Form_Resize()
1599 {
1600   long Space, NW, NH;
1601   int Tmp;
1602
1603   if (WindowState == vbMinimized || ! Loaded)
1604     return;
1605
1606   Space = Panel.left;
1607   //  NW = ScaleWidth - 2 * Space: If NW < 0 Then NW = 0
1608   //  NH = ScaleHeight - 3 * Space - cmbLevel.Height: If NH < 0 Then NH = 0
1609   //  picViewPort.Move Space, Space, NW, NH
1610   Tmp = (ShowPanel != 0 ? ScaleHeight - Panel.Height : ScaleHeight);
1611   if (Tmp < 0)
1612     Tmp = 0;
1613
1614   picViewPort.Move 0, 0, ScaleWidth, Tmp;
1615   //  Tmp = 2 * Space + picViewPort.Height
1616   Panel.top = Tmp;
1617   Panel.left = (ScaleWidth - Panel.Width) / 2;
1618   //  'cmdNormal.Top = 2 * Space + picViewPort.Height
1619   //  cmbLevel.Top = Tmp
1620   //  lblRedDiskCount.Top = Tmp
1621   //  lblInfoCount.Top = Tmp
1622   //  picKeys.Top = Tmp
1623   //  lblFrameCount.Top = Tmp
1624   //  cmdPlayAll.Top = Tmp
1625 }
1626
1627 #endif
1628
1629 static void DrawFrame(int Delta)
1630 {
1631   int i, LX, tY, RX, BY;
1632
1633   LX = -1 + Delta;
1634   tY = -1 + Delta;
1635   RX = FieldWidth - Delta;
1636   BY = FieldHeight - Delta;
1637   DrawSprite(LX, tY, posFrameCorner);
1638   DrawSprite(LX, BY, posFrameCorner);
1639   DrawSprite(RX, tY, posFrameCorner);
1640   DrawSprite(RX, BY, posFrameCorner);
1641   for (i = LX + 1; i <= RX - 1; i++)
1642   {
1643     DrawSprite(i, tY, posFrameHorizontal);
1644     DrawSprite(i, BY, posFrameHorizontal);
1645   }
1646
1647   for (i = tY + 1; i <= BY - 1; i++)
1648   {
1649     DrawSprite(LX, i, posFrameVertical);
1650     DrawSprite(RX, i, posFrameVertical);
1651   }
1652 }
1653
1654 static void RestoreFrame()
1655 {
1656   int i, LX, tY, RX, BY;
1657
1658   LX = 0;
1659   tY = 0;
1660   RX = FieldWidth - 1;
1661   BY = FieldHeight - 1;
1662   for (i = LX; i <= RX; i++)
1663   {
1664     DrawField(i, tY);
1665     DrawField(i, BY);
1666   }
1667
1668   for (i = tY + 1; i <= BY - 1; i++)
1669   {
1670     DrawField(LX, i);
1671     DrawField(RX, i);
1672   }
1673 }
1674
1675 #if 0
1676
1677 static void Form_Unload(int Cancel)
1678 {
1679   EndFlag = True;
1680   ExitToMenuFlag = 1;
1681   if (cmdPlayAll.STRING_IS_LIKE(Caption, "Quit*"))
1682   {
1683     cmdPlayAll_Click();
1684   }
1685
1686   if (menEdit.Checked)
1687     menEdit_Click();
1688
1689   if (ModifiedFlag)
1690   {
1691     char *Msg;
1692     VbMsgBoxResult Res;
1693
1694     Msg = "Save changes to " & GetFileNameToSave() & " ?";
1695     Res = MsgBox(Msg, vbYesNoCancel, AppTitle & " - closing");
1696     if (Res == vbCancel)
1697     {
1698       Cancel = -1;
1699       return;
1700     }
1701
1702     if (Res == vbYes)
1703     {
1704       if (FileExists(OrigPath) && menSave.Enabled)
1705       {
1706         menSave_Click();
1707       }
1708       else
1709       {
1710         menSaveAs_Click();
1711       }
1712     }
1713   }
1714
1715   if (FileExists(TmpPath) || ModifiedFlag)
1716   {
1717     MayKill(TmpPath);
1718     CurPath = OrigPath;
1719   }
1720
1721   ReleaseDirectDraw();
1722   SaveSettings();
1723   End;
1724 }
1725
1726 static void fpsTim_Timer()
1727 {
1728   currency TickDiff;
1729   static int count5 = 0;
1730
1731   count5 = count5 + 1;
1732   if (4 < count5)
1733   {
1734     TickDiff = T.TickDiffUS(LastTick);
1735     lblFps.Caption = CLng(Round((1000000 * (TimerVar - LastFrame)) / (TickDiff), 0));
1736     LastFrame = TimerVar;
1737     LastTick = T.TickNow();
1738     count5 = 0;
1739   }
1740
1741   // If NoDisplayFlag Then lblFrameCount = TimerVar
1742   lblFrameCount = TimerVar;
1743 }
1744
1745 static void menAbout_Click()
1746 {
1747   frmSplash.Show vbModal, Me;
1748 }
1749
1750 static void menAutoScroll_Click()
1751 {
1752   {
1753     menAutoScroll.Checked = ! menAutoScroll.Checked;
1754     AutoScrollFlag = menAutoScroll.Checked;
1755   }
1756 }
1757
1758 #endif
1759
1760 static void menBorder_Click()
1761 {
1762   if (menBorder.Checked)
1763   {
1764     menBorder.Checked = False;
1765     DisplayMinX = 1;
1766     DisplayMaxX = FieldWidth - 2;
1767     DisplayWidth = FieldWidth;
1768     DisplayMinY = 1;
1769     DisplayMaxY = FieldHeight - 2;
1770     DisplayHeight = FieldHeight;
1771
1772     if (Loaded && LevelLoaded)
1773       DrawFrame(1);
1774
1775   }
1776   else
1777   {
1778     menBorder.Checked = True;
1779     DisplayMinX = 0;
1780     DisplayMaxX = FieldWidth - 1;
1781     DisplayWidth = FieldWidth + 2;
1782     DisplayMinY = 0;
1783     DisplayMaxY = FieldHeight - 1;
1784     DisplayHeight = FieldHeight + 2;
1785
1786     if (Loaded && LevelLoaded)
1787       RestoreFrame();
1788   }
1789
1790   ReStretch(Stretch);
1791   // DisplayLevel True
1792 }
1793
1794 void SetDisplayRegion()
1795 {
1796   if (! menBorder.Checked)
1797   {
1798     DisplayMinX = 1;
1799     DisplayMaxX = FieldWidth - 2;
1800     DisplayWidth = FieldWidth;
1801     DisplayMinY = 1;
1802     DisplayMaxY = FieldHeight - 2;
1803     DisplayHeight = FieldHeight;
1804
1805     if (LevelLoaded)
1806       DrawFrame(1);
1807
1808   }
1809   else
1810   {
1811     DisplayMinX = 0;
1812     DisplayMaxX = FieldWidth - 1;
1813     DisplayWidth = FieldWidth + 2;
1814     DisplayMinY = 0;
1815     DisplayMaxY = FieldHeight - 1;
1816     DisplayHeight = FieldHeight + 2;
1817
1818     if (LevelLoaded)
1819       RestoreFrame();
1820   }
1821 }
1822
1823 #if 0
1824
1825 static void menCopy_Click()
1826 {
1827   FMark.Copy;
1828 }
1829
1830 void menEdit_Click()
1831 {
1832   long L;
1833
1834   if (menEdit.Checked)
1835   {
1836     menEdit.Checked = False;
1837     // leave edit mode
1838     if (EditFlag)
1839       Unload ToolBox;
1840
1841     EditFlag = False;
1842     UnEdAll();
1843     FMark.ShowMarker False;
1844     picViewPort.MousePointer = 0;
1845     if (ModifiedFlag)
1846     {
1847       if (! STRING_IS_LIKE(CurPath, TmpPath))
1848       {
1849         OrigPath = CurPath;
1850         CurPath = TmpPath;
1851       }
1852
1853       SaveMPX(TmpPath);
1854     }
1855
1856     DisplayLevel();
1857     Stage.Blt();
1858   }
1859   else
1860   {
1861     if (! LevelLoaded)
1862     {
1863       Beep();
1864       return;
1865     }
1866
1867     if (ModifiedFlag)
1868     {
1869       if (! STRING_IS_LIKE(CurPath, TmpPath))
1870       {
1871         OrigPath = CurPath;
1872         CurPath = TmpPath;
1873       }
1874
1875       SaveMPX(TmpPath);
1876     }
1877
1878     subFetchAndInitLevel();
1879     menEdit.Checked = True;
1880     // enter edit mode
1881     EditFlag = True;
1882     // ScaleMode = vbTwips
1883     ToolBox.Move (Width - ToolBox.Width) / 2, Height - ToolBox.Height;
1884     // ScaleMode = vbPixels
1885     //    L = GetWindowLong(ToolBox.hWnd, GWL_STYLE)
1886     //    L = L And (Not WS_POPUP)
1887     //    L = L Or WS_CHILD
1888     //    SetWindowLong ToolBox.hWnd, GWL_STYLE, L
1889     //    SetParent ToolBox.hWnd, hWnd
1890     ToolBox.Show vbModeless, Me;
1891     EdAll();
1892     DisplayLevel();
1893     Stage.Blt();
1894     FMark.ShowMarker True;
1895   }
1896 }
1897
1898 static void menEnOff_Click()
1899 {
1900   menEnOn.Checked = False;
1901   menEnOff.Checked = True;
1902   SpSaveMenu();
1903   SpLoadMenu();
1904 }
1905
1906 static void menEnOn_Click()
1907 {
1908   menEnOn.Checked = True;
1909   menEnOff.Checked = False;
1910   SpSaveMenu();
1911   SpLoadMenu();
1912 }
1913
1914 static void menExit_Click()
1915 {
1916   Unload Me;
1917 }
1918
1919 static void menFaster_Click()
1920 {
1921   int i;
1922
1923   if (! bPlaying)
1924   {
1925     Debug.Assert(False);
1926     return;
1927   }
1928
1929   if (DemoFlag != 0) // demoplayback
1930   {
1931     i = GetSpeedIndex(DeltaTDemo);
1932     i = i - 1;
1933     If i < 2 Then i = 2;
1934     menDemoSpeed_Click (i);
1935   }
1936   else
1937   {
1938     i = GetSpeedIndex(DeltaTPlay);
1939     i = i - 1;
1940     If i < 2 Then i = 2;
1941     menPlaySpeed_Click (i);
1942   }
1943 }
1944
1945 static void menSlower_Click()
1946 {
1947   int i;
1948
1949   if (! bPlaying)
1950   {
1951     Debug.Assert(False);
1952     return;
1953   }
1954
1955   if (DemoFlag != 0) // demoplayback
1956   {
1957     i = GetSpeedIndex(DeltaTDemo);
1958     i = i + 1;
1959     If nSpeedCount < i Then i = nSpeedCount;
1960     menDemoSpeed_Click (i);
1961   }
1962   else
1963   {
1964     i = GetSpeedIndex(DeltaTPlay);
1965     i = i + 1;
1966     If nSpeedCount < i Then i = nSpeedCount;
1967     menPlaySpeed_Click (i);
1968   }
1969 }
1970
1971 void menPlaySpeed_Click(int Index)
1972 {
1973   int i;
1974
1975   //  If NoDisplayFlag And (GameLoopRunning <> 0) Then
1976   //    NoDisplayFlag = False
1977   //    DisplayLevel
1978   //  End If
1979   //  NoDisplayFlag = False
1980   for (i = menPlaySpeed.LBound; i <= menPlaySpeed.UBound; i++)
1981   {
1982     {
1983       menPlaySpeed(i).Checked = (Index == i);
1984     }
1985   }
1986
1987   BlockingSpeed = False;
1988   DeltaTPlay = SpeedsByDelayUS[Index];
1989   UpdateDeltaT();
1990   //  If DeltaTPlay < 0 Then
1991   //    Stage.Blt
1992   //    DeltaT = 0
1993   //    NoDisplayFlag = True
1994   //  End If
1995 }
1996
1997 void menDemoSpeed_Click(int Index)
1998 {
1999   int i;
2000
2001   //  If NoDisplayFlag And (GameLoopRunning <> 0) Then
2002   //    NoDisplayFlag = False
2003   //    DisplayLevel
2004   //  End If
2005   NoDisplayFlag = False;
2006   for (i = menDemoSpeed.LBound; i <= menDemoSpeed.UBound; i++)
2007   {
2008     {
2009       menDemoSpeed(i).Checked = (Index == i);
2010     }
2011   }
2012
2013   BlockingSpeed = False;
2014   DeltaTDemo = SpeedsByDelayUS[Index];
2015   UpdateDeltaT();
2016   //  If DeltaTPlay < 0 Then
2017   //    Stage.Blt
2018   //    DeltaT = 0
2019   //    NoDisplayFlag = True
2020   //  End If
2021 }
2022
2023 static void UpdateDeltaT()
2024 {
2025   if (! bPlaying)
2026     return;
2027
2028   DeltaT = (DemoFlag != 0 ? DeltaTDemo : DeltaTPlay);
2029   if (DeltaT < 0)
2030   {
2031     Stage.Blt();
2032     DeltaT = 0;
2033     NoDisplayFlag = True;
2034   }
2035   else
2036   {
2037     if (NoDisplayFlag && GameLoopRunning != 0)
2038     {
2039       NoDisplayFlag = False;
2040       DisplayLevel();
2041     }
2042     else
2043     {
2044       NoDisplayFlag = False;
2045     }
2046   }
2047 }
2048
2049 static void menGravOff_Click()
2050 {
2051   menGravOn.Checked = False;
2052   menGravOff.Checked = True;
2053   SpSaveMenu();
2054   SpLoadMenu();
2055 }
2056
2057 static void menGravOn_Click()
2058 {
2059   menGravOn.Checked = True;
2060   menGravOff.Checked = False;
2061   SpSaveMenu();
2062   SpLoadMenu();
2063 }
2064
2065 static void menNew_Click()
2066 {
2067   NewForm.Show vbModal, Me;
2068   CreateLevel(FieldWidth, FieldHeight);
2069   ReStretch(Stretch);
2070 }
2071
2072 static void menNewStd_Click()
2073 {
2074   CreateLevel(60, 24);
2075   ReStretch(Stretch);
2076 }
2077
2078 static void menOpen_Click()
2079 {
2080   long LFilt;
2081
2082   {
2083     // cmDlg.DefaultExt = "sp"
2084
2085     /*
2086
2087     (prevent compiler warning here due to suspected use of trigraph)
2088
2089     cmDlg.Filter = "All Levels (*.DAT;*.D??;*.sp;*.mpx)|*.DAT;*.D??;*.sp;*.mpx|MegaPlex Levels (*.mpx)|*.mpx" & "|Supaplex Level Sets (*.DAT;*.D??)|*.DAT;*.D??|SpeedFix Demos (*.sp)|*.sp";
2090     */
2091
2092     LFilt = LastOpenFilter;
2093     cmDlg.FilterIndex = (0 < LFilt & LFilt < 5 ? LFilt : 1);
2094     if (FileExists(CurPath))
2095       cmDlg.InitDir = WithSlash(StripDir(CurPath));
2096
2097     cmDlg.flags = cdlOFNHideReadOnly | cdlOFNLongNames;
2098   }
2099
2100   // --- On Error GoTo menOpenEH
2101   cmDlg.ShowOpen;
2102   // --- On Error GoTo 0
2103
2104   LFilt = cmDlg.FilterIndex;
2105   LastOpenFilter = LFilt;
2106   CurPath = cmDlg.FileName;
2107   OrigPath = CurPath;
2108   FillFileList (CurPath);
2109
2110 menOpenEH:
2111 }
2112
2113 static void menOptions_Click()
2114 {
2115   OptionsForm oFrm;
2116
2117   oFrm.Show vbModal, Me;
2118   SaveSettings();
2119   picViewPort_Resize();
2120 }
2121
2122 static void menPanel_Click()
2123 {
2124   {
2125     PanelVisible = ! menPanel.Checked;
2126   }
2127 }
2128
2129 void Let_PanelVisible(boolean NewVal)
2130 {
2131   static boolean HidePanel = False;
2132
2133   if (HidePanel != NewVal)
2134     return;
2135
2136   HidePanel = ! NewVal;
2137   PanelTim.Enabled = True;
2138 }
2139
2140 static void DrawPauseLayer(long Layer)
2141 {
2142   DirectDrawPalette Pal;
2143   PALETTEENTRY *Val;
2144   long i;
2145
2146   // Dim X&, Y&
2147   //  For Y = 0 To bmpStage.Height Step BaseWidth
2148   //    For X = 0 To bmpStage.Width Step BaseWidth
2149   //      Pause.TransparentDraw bmpStageHDC, X, Y, Layer
2150   //    Next X
2151   //  Next Y
2152   //  With Stage.Surface
2153   //    Set Pal = .GetPalette
2154   //    for i=1 to pal.GetEntries(
2155   //  Stage.Surface.SetPalette
2156 }
2157
2158 static void menPaste_Click()
2159 {
2160   FMark.Paste;
2161   DisplayLevel();
2162   Stage.Blt();
2163 }
2164
2165 static void menPause_Click()
2166 {
2167   static StdPicture OldPic;
2168   static char *OldText = 0;
2169
2170   PauseMode = (PauseMode != 0 ? 0 : 1);
2171   if (PauseMode != 0)
2172   {
2173     if (IS_NOTHING(&OldPic, sizeof(OldPic)))
2174       OldPic = cmdPause.Picture;
2175
2176     cmdPause.Picture = cmdPause.DownPicture;
2177     OldText = lblStatus.Caption;
2178     lblStatus = "Paused ...";
2179   }
2180   else
2181   {
2182     cmdPause.Picture = OldPic;
2183     lblStatus.Caption = OldText;
2184   }
2185 }
2186
2187 static void menPlayAll_Click()
2188 {
2189   long iMin, iMax, i;
2190   int FNum;
2191   char *LogPath, *OutStr, *ReRecPath, *SPPath;
2192   boolean bEqual;
2193   static boolean QuitFlag = False;
2194
2195   if (cmdPlayAll.STRING_IS_LIKE(Caption, "Play) All Demos")
2196   {
2197     cmdPlayAll.Caption = "Quit All";
2198     menPlayAll.Caption = "Quit All";
2199     FocusTim.Enabled = True;
2200     QuitFlag = False;
2201     iMin = 0;
2202     iMax = cmbFile.ListCount - 1;
2203     LogPath = StripDir(CurPath) & "/Error.Log";
2204     if (Dir(LogPath) != "")
2205       Kill(LogPath);
2206
2207     for (i = iMin; i <= iMax; i++)
2208     {
2209       cmbFile.ListIndex = i;
2210       //      If CurPath Like "*.sp" Or CurPath Like "*.mpx" Then
2211       if (DemoAvailable)
2212       {
2213         SPPath = CurPath;
2214         ReRecPath = SPPath & ".ReRec";
2215         menPlayDemo_Click();
2216         // SaveSP ReRecPath
2217         // bEqual = FilesEqual(ReRecPath, SPPath)
2218         // If bEqual Then MayKill ReRecPath
2219         bEqual = True;
2220         OutStr = cmbFile.List(i) & " -> ";
2221         //
2222         if (Val(lblFrameCount) != GetTotalFramesOfDemo())
2223         {
2224           OutStr = OutStr & "Error in GetTotalFramesOfDemo()! ";
2225           Debug.Assert(False);
2226         }
2227
2228         //
2229         if ((LevelStatus == 1) && bEqual)
2230         {
2231           OutStr = OutStr & "Success";
2232         }
2233         else if (QuitFlag)
2234         {
2235           OutStr = OutStr & "All Demos (float)Canceled";
2236         }
2237         else
2238         {
2239           if (LevelStatus != 1)
2240           {
2241             OutStr = OutStr & "### Level Failed ### (TimerVar == " & TimerVar & ")";
2242           }
2243
2244           if (! bEqual)
2245           {
2246             OutStr = OutStr & "### ReRecording is binary different ###";
2247           }
2248         }
2249
2250         FNum = FreeFile();
2251         FNum = fopen(LogPath, "ab");
2252         Print #FNum, OutStr;
2253         fclose(FNum);
2254         if (QuitFlag)
2255           break;
2256       }
2257     }
2258
2259     cmdPlayAll.Caption = "Play All Demos";
2260     menPlayAll.Caption = "Play All Demos";
2261     SettingsObject Settings;
2262
2263     MySignature = Settings.Read("MySignature", "");
2264     FirstDemoByte = 0x81;
2265   }
2266   else
2267   {
2268     QuitFlag = True;
2269     if (menStop.Enabled)
2270       menStop_Click();
2271   }
2272 }
2273
2274 static void menRemSP_Click()
2275 {
2276   RemoveSP();
2277   SpLoadMenu();
2278 }
2279
2280 static void menRestoreBorder_Click()
2281 {
2282   RestoreBorder();
2283 }
2284
2285 static void menSave_Click()
2286 {
2287   if (! ModifiedFlag)
2288     return;
2289
2290   if (STRING_IS_LIKE(CurPath, TmpPath))
2291     CurPath = OrigPath;
2292
2293   if (STRING_IS_LIKE(CurPath, "") || STRING_IS_LIKE(CurPath, "*.dat"))
2294   {
2295     menSaveAs_Click();
2296     return;
2297   }
2298
2299   if (STRING_IS_LIKE(CurPath, "*.sp"))
2300   {
2301     menSaveAs_Click();
2302     return;
2303   }
2304
2305   if (! FileExists(CurPath) || STRING_IS_LIKE(CurPath, "Untitled"))
2306   {
2307     menSaveAs_Click();
2308     return;
2309   }
2310
2311   SaveMPX(CurPath);
2312   Let_ModifiedFlag(False);
2313 }
2314
2315 static void menSaveAs_Click()
2316 {
2317   if (SaveAs())
2318     FillFileList (CurPath);
2319 }
2320
2321 static boolean SaveAs()
2322 {
2323   static boolean SaveAs;
2324
2325   char *FN;
2326   SettingsObject s;
2327
2328   SaveAs = False;
2329   {
2330     // cmDlg.DefaultExt = "sp"
2331     cmDlg.Filter = "MegaPlex Level (*.mpx)|*.mpx|SpeedFix Demo (*.sp)|*.sp";
2332     cmDlg.FilterIndex = LastSaveFilter;
2333     // If OrigPath Like "*.sp" Then cmDlg.FilterIndex = 1 Else cmDlg.FilterIndex = 0
2334     if (FileExists(OrigPath))
2335     {
2336       cmDlg.InitDir = WithSlash(StripDir(OrigPath));
2337       cmDlg.InitDir = s.Read("LastSaveDir", cmDlg.InitDir);
2338       cmDlg.FileName = StripExtensionlessFileName(GetFileNameToSave());
2339     }
2340
2341     cmDlg.flags = cdlOFNHideReadOnly | cdlOFNLongNames;
2342   }
2343
2344   // --- On Error GoTo SaveAsEH
2345   cmDlg.ShowSave;
2346   // --- On Error GoTo 0
2347
2348   LastSaveFilter = cmDlg.FilterIndex;
2349   FN = cmDlg.FileName;
2350   SettingsObject_Save("LastSaveDir", WithSlash(StripDir(FN)));
2351   if (STRING_IS_LIKE(FN, "*.sp"))
2352   {
2353     SaveSP(FN);
2354   }
2355   else if (STRING_IS_LIKE(FN, "*.mpx"))
2356   {
2357     SaveMPX(FN);
2358   }
2359
2360   Let_ModifiedFlag(False);
2361   SaveAs = True;
2362
2363 SaveAsEH:
2364
2365   return SaveAs;
2366 }
2367
2368 static void menSelectAll_Click()
2369 {
2370   FMark.SetPoint1 0, 0;
2371   FMark.SetPoint2 FieldWidth - 1, FieldHeight - 1;
2372 }
2373
2374 static void menShowLInfo_Click()
2375 {
2376   char *Msg;
2377
2378   Msg = LInfo.LevelTitle & vbNewLine & "(" & FieldWidth & " x " & FieldHeight & ")";
2379   if (DemoAvailable && bSignatureAvailable)
2380     Msg = Msg & vbNewLine & vbNewLine & gSignature;
2381
2382   SignatureForm.Signature = Msg;
2383   SignatureForm.DelayMS = 5000;
2384   int X, Y;
2385
2386   X = left + (Width - SignatureForm.Width) / 2;
2387   Y = top + (Height - SignatureForm.Height) / 2;
2388   SignatureForm.Move X, Y;
2389   SignatureForm.Show vbModeless, Me;
2390   Me.SetFocus;
2391 }
2392
2393 static void menSoundFX_Click()
2394 {
2395   {
2396     menSoundFX.Checked = ! menSoundFX.Checked;
2397     FXOnFlag = (menSoundFX.Checked ? -1 : 0);
2398   }
2399 }
2400
2401 #endif
2402
2403 // static void menPlay_Click()
2404 void menPlay_Click()
2405 {
2406 #if 0
2407
2408   boolean OldEditFlag;
2409
2410   // Trace "MainForm", "--> menPlay_Click()"
2411   if (! LevelLoaded)
2412   {
2413     Beep();
2414     return;
2415   }
2416
2417   SignatureForm.DelayMS = 1;
2418   menPlay.Enabled = False;
2419   menPause.Enabled = True;
2420   menStop.Enabled = True;
2421   cmdPlay.Enabled = False;
2422   cmdPause.Enabled = True;
2423   cmdStop.Enabled = True;
2424   cmdPlayDemo.Enabled = False;
2425   menPlayDemo.Enabled = False;
2426   cmdRecordDemo.Enabled = False;
2427   menRec.Enabled = False;
2428   menFaster.Enabled = True;
2429   menSlower.Enabled = True;
2430   if (cmdPlayAll.STRING_IS_LIKE(Caption, "Play*"))
2431   {
2432     cmdPlayAll.Enabled = False;
2433     menPlayAll.Enabled = False;
2434   }
2435
2436   cmbFile.Enabled = False;
2437   cmbLevel.Enabled = False;
2438   OldEditFlag = EditFlag;
2439   if (EditFlag)
2440     menEdit_Click();
2441
2442   menEditMain.Enabled = False;
2443   if (DemoFlag == 0 && RecordDemoFlag == 0)
2444     lblStatus = "Playing";
2445
2446   lblFrameCount = 0;
2447
2448 #endif
2449
2450   LastFrame = 0;
2451
2452 #if 0
2453   LastTick = T.TickNow();
2454 #endif
2455
2456 #if 0
2457   fpsTim.Enabled = True;
2458 #endif
2459
2460   // DimPrimary 100
2461   bPlaying = True;
2462
2463 #if 0
2464   UpdateDeltaT();
2465 #endif
2466
2467 #if 0
2468   printf("::: MainForm.c: menPlay_Click(): subFetchAndInitLevelB(): ...\n");
2469 #endif
2470
2471   subFetchAndInitLevelB();
2472
2473 #if 0
2474   printf("::: MainForm.c: menPlay_Click(): subFetchAndInitLevelB(): done\n");
2475 #endif
2476
2477   ReStretch(Stretch);
2478
2479   // Trace "MainForm", "CountDown 1"
2480   CountDown(2, (0 == DemoFlag));
2481   // Trace "MainForm", "Call GoPlay"
2482
2483   GoPlay();
2484
2485   // Trace "MainForm", "GoPlay returned"
2486
2487 #if 0
2488
2489   if (LevelStatus == 1)
2490   {
2491     lblStatus = "(float)Success";
2492   }
2493   else
2494   {
2495     lblStatus = "Try (float)again";
2496   }
2497
2498 #endif
2499
2500   // Trace "MainForm", "CountDown 1"
2501   CountDown(1, False);
2502   RecordDemoFlag = 0;
2503
2504 #if 0
2505   ShowKey(0);
2506 #endif
2507
2508   bPlaying = False;
2509   // Trace "MainForm", "Call subFetchAndInitLevel"
2510
2511   subFetchAndInitLevel();
2512
2513   // Trace "MainForm", "subFetchAndInitLevel returned"
2514
2515 #if 0
2516   Stage.Blt();
2517 #endif
2518
2519 #if 0
2520
2521   menEditMain.Enabled = True;
2522   if (OldEditFlag)
2523     menEdit_Click();
2524
2525   // Trace "MainForm", "<-- menPlay_Click()"
2526
2527 #endif
2528 }
2529
2530 #if 1
2531
2532 // static void menPlayDemo_Click()
2533 void menPlayDemo_Click()
2534 {
2535   DemoFlag = 1;
2536   RecordDemoFlag = 0;
2537
2538 #if 0
2539   lblStatus = "Demo Playback";
2540 #endif
2541
2542   menPlay_Click();
2543
2544 #if 1
2545   return;       /* return after initialization phase */
2546 #endif
2547
2548 #if 0
2549   if (LevelStatus != 1)
2550     lblStatus = "Demo Failed";
2551 #endif
2552
2553   DemoFlag = 0;
2554 }
2555
2556 #else
2557
2558 // static void menPlayDemo_Click()
2559 void menPlayDemo_Click()
2560 {
2561   DemoFlag = 1;
2562   RecordDemoFlag = 0;
2563
2564 #if 0
2565   lblStatus = "Demo Playback";
2566 #endif
2567
2568   menPlay_Click();
2569
2570 #if 0
2571   if (LevelStatus != 1)
2572     lblStatus = "Demo Failed";
2573 #endif
2574
2575   DemoFlag = 0;
2576 }
2577
2578 #endif
2579
2580 #if 0
2581
2582 static void menRec_Click()
2583 {
2584   Trace("MainForm", "--> menRec_Click()");
2585   if (! LevelLoaded)
2586   {
2587     Beep();
2588     return;
2589   }
2590
2591   RecordDemoFlag = 1;
2592   DemoFlag = 0;
2593   lblStatus.ForeColor = vbRed;
2594   lblStatus = "Recording Demo";
2595   // DemoBuffer = New DemoBufferObject; // (handle this later, if needed)
2596   Debug.Assert(! IS_NOTHING(&DemoBuffer, sizeof(DemoBuffer)));
2597   Trace("MainForm", "Call menPlayClick");
2598   menPlay_Click();
2599   Trace("MainForm", "menPlayClick returned");
2600
2601   lblStatus.ForeColor = vbButtonText;
2602   RecordDemoFlag = 0;
2603   Let_ModifiedFlag(True);
2604   if (! STRING_IS_LIKE(CurPath, TmpPath))
2605   {
2606     OrigPath = CurPath;
2607     CurPath = TmpPath;
2608   }
2609
2610   LInfo.DemoRandomSeed = RecDemoRandomSeed;
2611   Trace("MainForm", "Call SaveMPX(TmpPath)");
2612   SaveMPX(TmpPath);
2613   Trace("MainForm", "Set DemoBuffer == Nothing");
2614   SET_TO_NOTHING(&DemoBuffer, sizeof(DemoBuffer));
2615   Trace("MainForm", "Call subFetchAndInitLevel");
2616   subFetchAndInitLevel();
2617   cmdPlayDemo.Enabled = DemoAvailable;
2618   Trace("MainForm", "<-- menRec_Click()");
2619 }
2620
2621 static void menReRecordDemo_Click()
2622 {
2623   if (! LevelLoaded)
2624   {
2625     Beep();
2626     return;
2627   }
2628
2629   RecordDemoFlag = 1;
2630   DemoFlag = 1;
2631   lblStatus.ForeColor = vbRed;
2632   lblStatus = "ReRecording Demo";
2633   // DemoBuffer = New DemoBufferObject; // (handle this later, if needed)
2634   Debug.Assert(! IS_NOTHING(&DemoBuffer, sizeof(DemoBuffer)));
2635   menPlay_Click();
2636   lblStatus.ForeColor = vbButtonText;
2637   RecordDemoFlag = 0;
2638   Let_ModifiedFlag(True);
2639   if (! STRING_IS_LIKE(CurPath, TmpPath))
2640   {
2641     OrigPath = CurPath;
2642     CurPath = TmpPath;
2643   }
2644
2645   LInfo.DemoRandomSeed = RecDemoRandomSeed;
2646   SaveMPX(TmpPath);
2647   SET_TO_NOTHING(&DemoBuffer, sizeof(DemoBuffer));
2648   subFetchAndInitLevel();
2649 }
2650
2651 void menStop_Click()
2652 {
2653   EndFlag = True;
2654   LeadOutCounter = 1;
2655   if (PauseMode != 0)
2656     menPause_Click();
2657
2658   fpsTim.Enabled = False;
2659   fpsTim_Timer();
2660   lblFps.Caption = "";
2661   menRec.Enabled = True;
2662   menPlay.Enabled = True;
2663   menPause.Enabled = False;
2664   menStop.Enabled = False;
2665   cmdPlay.Enabled = True;
2666   cmdPause.Enabled = False;
2667   cmdStop.Enabled = False;
2668   cmdRecordDemo.Enabled = True;
2669   cmdPlayDemo.Enabled = DemoAvailable;
2670   cmdPlayAll.Enabled = DemosAvailable;
2671   menPlayDemo.Enabled = DemoAvailable;
2672   menPlayAll.Enabled = DemosAvailable;
2673   menFaster.Enabled = False;
2674   menSlower.Enabled = False;
2675   cmbFile.Enabled = True;
2676   cmbLevel.Enabled = True;
2677 }
2678
2679 static void menStretch_Click(int Index)
2680 {
2681   ReStretch(0.25 * Index);
2682   // DisplayLevel True
2683 }
2684
2685 static void menToolTips_Click()
2686 {
2687   {
2688     menToolTips.Checked = ! menToolTips.Checked;
2689     if (menToolTips.Checked)
2690     {
2691       cmdPlay.ToolTipText = "Play Game (Space)";
2692       cmdPause.ToolTipText = "Pause (P)";
2693       cmdStop.ToolTipText = "Stop (Q)";
2694       cmdPlayDemo.ToolTipText = "Play demo (Ctrl+Space)";
2695       cmdRecordDemo.ToolTipText = "Record demo (Ctrl+R)";
2696       lblInfoCount.ToolTipText = "Number of infotrons needed";
2697       lblRedDiskCount.ToolTipText = "Number of red disks";
2698       cmbLevel.ToolTipText = "List of all levels in the file";
2699       cmbFile.ToolTipText = "List of files in current directory";
2700       lblFps.ToolTipText = "Animation speed in fps";
2701       lblFrameCount.ToolTipText = "Game time in frames";
2702     }
2703     else
2704     {
2705       cmdPlay.ToolTipText = "";
2706       cmdPause.ToolTipText = "";
2707       cmdStop.ToolTipText = "";
2708       cmdPlayDemo.ToolTipText = "";
2709       cmdRecordDemo.ToolTipText = "";
2710       lblInfoCount.ToolTipText = "";
2711       lblRedDiskCount.ToolTipText = "";
2712       cmbLevel.ToolTipText = "";
2713       cmbFile.ToolTipText = "";
2714       lblFps.ToolTipText = "";
2715       lblFrameCount.ToolTipText = "";
2716     }
2717
2718   }
2719 }
2720
2721 static void menTrim_Click()
2722 {
2723   LevelInfoType Tmp;
2724   char *OldOPath;
2725
2726   OldOPath = OrigPath;
2727   Tmp = LInfo;
2728   Tmp.SpecialPortCount = 0; // hack: all special ports are deleted
2729   menCopy_Click();
2730   CreateLevel(FMark.Width, FMark.Height);
2731   LInfo = Tmp;
2732   OrigPath = OldOPath;
2733   CurPath = OldOPath;
2734   menSelectAll_Click();
2735   menPaste_Click();
2736   picViewPort_Resize();
2737 }
2738
2739 static void menZonkOff_Click()
2740 {
2741   menZonkOn.Checked = False;
2742   menZonkOff.Checked = True;
2743   SpSaveMenu();
2744   SpLoadMenu();
2745 }
2746
2747 static void menZonkOn_Click()
2748 {
2749   menZonkOn.Checked = True;
2750   menZonkOff.Checked = False;
2751   SpSaveMenu();
2752   SpLoadMenu();
2753 }
2754
2755 static void PanelTim_Timer()
2756 {
2757   int Tmp;
2758
2759   Tmp = Panel.Height;
2760   if (PanelSeq < Panel.Height && -1 < PanelSeq)
2761   {
2762     PanelSeq = (ShowPanel != 0 ? PanelSeq + 2 : PanelSeq - 2);
2763     Tmp = ScaleHeight - Tmp + PanelSeq;
2764     Panel.top = Tmp;
2765     picViewPort.Height = Tmp;
2766   }
2767   else
2768   {
2769     PanelTim.Enabled = False;
2770     PanelSeq = (PanelSeq < 0 ? 0 : PanelSeq - );
2771     ShowPanel = (ShowPanel == 0 ? 1 : 0);
2772     menPanel.Checked = (ShowPanel != 0);
2773   }
2774 }
2775
2776 static void picKeys_MouseUp(int Button, int Shift, float X, float Y)
2777 {
2778   FocusTim.Enabled = True;
2779 }
2780
2781 static void picMenu_Click()
2782 {
2783 }
2784
2785 static void picPane_KeyDown(int KeyCode, int Shift)
2786 {
2787   if (KeyCode < 0 || 255 < KeyCode)
2788     return;
2789
2790   KeyState[KeyCode] = True;
2791   switch (KeyCode)
2792   {
2793     case vbKeyControl:
2794       if (MouseButton == 0 && EditFlag)
2795         picPane.MousePointer = 15;
2796
2797       break;
2798
2799     case vbKeyUp:
2800     case vbKeyLeft:
2801     case vbKeyDown:
2802     case vbKeyRight:
2803     case vbKeySpace:
2804       if (DemoFlag != 0)
2805       {
2806         DemoFlag = 0;
2807         UpdateDeltaT();
2808       }
2809
2810       break;
2811
2812     case vbKeyF12:
2813       if (DemoFlag != 0 && 2 == Shift)
2814       {
2815         DemoFlag = 0;
2816         UpdateDeltaT();
2817       }
2818
2819       break;
2820
2821     case vbKeyF11:
2822       bCapturePane = ! bCapturePane;
2823       break;
2824   }
2825 }
2826
2827 static void picPane_KeyUp(int KeyCode, int Shift)
2828 {
2829   if (KeyCode < 0 || 255 < KeyCode)
2830     return;
2831
2832   KeyState[KeyCode] = False;
2833   switch (KeyCode)
2834   {
2835     case vbKeyPageUp:
2836       if (menFaster.Enabled)
2837         menFaster_Click();
2838
2839       break;
2840
2841     case vbKeyPageDown:
2842       if (menSlower.Enabled)
2843         menSlower_Click();
2844
2845       break;
2846
2847     case vbKeySpace:
2848       if (2 == Shift) // ctrl+space
2849       {
2850         if (menPlayDemo.Enabled)
2851           menPlayDemo_Click();
2852
2853       }
2854       else
2855       {
2856         if (menPlay.Enabled)
2857           menPlay_Click();
2858       }
2859
2860       break;
2861
2862     case vbKeyQ:
2863       if (menStop.Enabled)
2864         menStop_Click();
2865
2866       break;
2867
2868     case vbKeyR:
2869       if (menRec.Enabled)
2870         menRec_Click();
2871
2872       break;
2873
2874     case vbKeyP:
2875     case vbKeyPause:
2876       if (menPause.Enabled && (Shift == 0))
2877         menPause_Click();
2878
2879       break;
2880
2881     case vbKeyControl:
2882       if (MouseButton == 0 && EditFlag)
2883         picPane.MousePointer = 0;
2884       break;
2885   }
2886 }
2887
2888 static void picPane_MouseDown(int Button, int Shift, float X, float Y)
2889 {
2890   int OldEdMode;
2891
2892   MouseButton = Button;
2893   OldEdMode = EditMode;
2894   if (Button != 1)
2895   {
2896     if (Button == 2 && Shift != 2)
2897     {
2898       EditMode = edSelect;
2899     }
2900     else
2901     {
2902       return;
2903     }
2904   }
2905
2906   if (Shift == 2)
2907     EditMode = edMove;
2908
2909   OSX = ScrollX;
2910   OSY = ScrollY;
2911   MDX = X;
2912   MDY = Y;
2913   switch (EditMode)
2914   {
2915     case edMove:
2916       picPane.MousePointer = 5; // size
2917       UserDragFlag = True;
2918       break;
2919
2920     case edDraw:
2921       SetField(X, Y, ToolBox.ASpriteIndex);
2922       break;
2923
2924     case edSelect:
2925       FMark.SetPoint1 EdGetX(X), EdGetY(Y);
2926       FMark.MoveMarker;
2927       break;
2928   }
2929
2930   EditMode = OldEdMode;
2931 }
2932
2933 static void picPane_MouseMove(int Button, int Shift, float X, float Y)
2934 {
2935   boolean Oldflag;
2936   int OldEdMode;
2937
2938   OldEdMode = EditMode;
2939   if (Button != 1)
2940   {
2941     if (EditFlag && Button == 2 && Shift != 2)
2942     {
2943       EditMode = edSelect;
2944     }
2945     else
2946     {
2947       return;
2948     }
2949   }
2950
2951   if (Shift == 2)
2952     EditMode = edMove;
2953
2954   switch (EditMode)
2955   {
2956     case edMove:
2957       Oldflag = NoDisplayFlag;
2958       NoDisplayFlag = False;
2959       ScrollTo(OSX - X + MDX, OSY - Y + MDY);
2960       Stage.Blt();
2961       NoDisplayFlag = Oldflag;
2962       break;
2963
2964     case edDraw:
2965       SetField(X, Y, ToolBox.ASpriteIndex);
2966       break;
2967
2968     case edSelect:
2969       FMark.SetPoint2 EdGetX(X), EdGetY(Y);
2970       FMark.MoveMarker;
2971       break;
2972   }
2973
2974   EditMode = OldEdMode;
2975 }
2976
2977 static void picPane_MouseUp(int Button, int Shift, float X, float Y)
2978 {
2979   MouseButton = MouseButton ^ Button;
2980   picPane.MousePointer = 0;
2981   UserDragFlag = False;
2982   if (menEditMain.Enabled && Button == 2)
2983   {
2984     if (MDX == X && MDY == Y)
2985       PopupMenu menEditMain;
2986   }
2987 }
2988
2989 #endif
2990
2991 static void picPane_Paint()
2992 {
2993   boolean Oldflag;
2994
2995   Oldflag = NoDisplayFlag;
2996   NoDisplayFlag = False;
2997
2998 #if 0
2999   Stage.Blt();
3000 #endif
3001
3002   NoDisplayFlag = Oldflag;
3003 }
3004
3005 static void ReStretch(float NewStretch)
3006 {
3007 #if 1
3008   long BW2, LW, LH;
3009 #else
3010   long BW2, LW, LH, i;
3011 #endif
3012
3013 #if 0
3014   printf("::: MainForm.c: ReStretch(): %d, %d\n", Loaded, LevelLoaded);
3015 #endif
3016
3017   if (! Loaded)
3018   {
3019 #if 0
3020     printf("::: NewStretch == %f\n", NewStretch);
3021 #endif
3022
3023     Stretch = NewStretch;
3024     return;
3025   }
3026
3027 #if 0
3028   MousePointer = 11;
3029   SET_TO_NOTHING(&Stage, sizeof(Stage));
3030   SET_TO_NOTHING(&StretchedSprites, sizeof(StretchedSprites));
3031   menStretch(Stretch / 0.25).Checked = False;
3032 #endif
3033
3034   Stretch = NewStretch;
3035
3036 #if 0
3037   menStretch(Stretch / 0.25).Checked = True;
3038   //  StretchWidth = BaseWidth ' * NewStretch
3039   //  StretchWidth2 = StretchWidth \ 2
3040   //  TwoPixels = 2 '* Stretch
3041   // StretchLoad Sprites, imgMpx.Picture, 16, 16
3042 #endif
3043
3044   BW2 = StretchWidth / 2;
3045   LW = (FieldWidth + 2) * BaseWidth; // StretchWidth
3046   LH = (FieldHeight + 2) * BaseWidth; // StretchWidth
3047
3048 #if 0
3049   // i = bmpStage.CreateAtSize(LW, LH)
3050   // Stage = New DDScrollBuffer; // (handle this later, if needed)
3051   i = Stage.CreateAtSize(LW, LH, picPane.hWnd);
3052   // Set StretchedSprites = NormalSprites.GetStretchCopy(Stretch)
3053   ReLoadStretchSprites();
3054   if (i == 0 || IS_NOTHING(&StretchedSprites, sizeof(StretchedSprites)))
3055   {
3056     // menStretch(Stretch / 0.5).Enabled = False
3057     if (0.5 <= Stretch)
3058       ReStretch(Stretch - 0.25);
3059
3060   }
3061   else
3062 #endif
3063   {
3064     StretchedSprites.DestXOff = 1 * BaseWidth; // StretchWidth
3065     StretchedSprites.DestYOff = 1 * BaseWidth; // StretchWidth
3066     Stage.DestXOff = 1 * StretchWidth;
3067     Stage.DestYOff = 1 * StretchWidth;
3068     StretchedSprites.DestinationSurface = Stage.Surface;
3069
3070 #if 0
3071     Stage.Cls();
3072 #endif
3073
3074     if (Loaded && LevelLoaded)
3075     {
3076 #if 0
3077       printf("::: MainForm.c: ReStretch(): ...\n");
3078 #endif
3079
3080       SetDisplayRegion();
3081       picViewPort_Resize();
3082       DisplayLevel();
3083     }
3084
3085     subCalculateScreenScrollPos();
3086
3087 #if 0
3088   printf("::: MainForm.c: ReStretch(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
3089 #endif
3090
3091     ScrollTo(ScreenScrollXPos, ScreenScrollYPos);
3092
3093 #if 0
3094     Stage.Blt();
3095 #endif
3096
3097     picPane_Paint();
3098   }
3099
3100 #if 0
3101   MousePointer = 0;
3102 #endif
3103 }
3104
3105 static void SetScrollEdges()
3106 {
3107 #if 0
3108   printf("::: MainForm.c: SetScrollEdges() ... [%ld, %ld, %ld, %ld] [%f, %d] [%d, %d, %d, %d]\n",
3109          DisplayMinX, DisplayMinY, DisplayMaxX, DisplayMaxY,
3110          Stretch, BaseWidth,
3111          ScrollMinX, ScrollMinY, ScrollMaxX, ScrollMaxY);
3112 #endif
3113
3114   ScrollMinX = (int)(DisplayMinX - 0.5) * Stretch * BaseWidth;
3115   ScrollMinY = (int)(DisplayMinY - 0.5) * Stretch * BaseWidth;
3116   ScrollMaxX = (int)(DisplayMaxX + 1.5) * Stretch * BaseWidth - picPane.Width;
3117   ScrollMaxY = (int)(DisplayMaxY + 1.5) * Stretch * BaseWidth - picPane.Height;
3118
3119 #if 0
3120   printf("::: MainForm.c: SetScrollEdges() done [%d, %d, %d, %d]\n",
3121          ScrollMinX, ScrollMinY, ScrollMaxX, ScrollMaxY);
3122 #endif
3123 }
3124
3125 void DrawField(int X, int Y)
3126 {
3127   int Tmp, tsi;
3128
3129   tsi = GetSI(X, Y);
3130   Tmp = LowByte(PlayField16[tsi]);
3131   if (40 < Tmp)
3132     Tmp = 0;
3133
3134   if (Tmp == fiRAM || Tmp == fiHardWare)
3135     Tmp = DisPlayField[tsi];
3136
3137   if (Tmp == fiBug || Tmp == 40)
3138     Tmp = DisPlayField[tsi];
3139
3140   if (EditFlag)
3141   {
3142     if (fiOrangeDisk < Tmp && Tmp < fiSnikSnak)
3143       Tmp = DisPlayField[tsi];
3144   }
3145
3146   StretchedSprites.BltEx(StretchWidth * X, StretchWidth * Y, Tmp);
3147 }
3148
3149 void DrawFieldAnimated(int X, int Y)
3150 {
3151   int Tmp, tsi;
3152
3153   tsi = GetSI(X, Y);
3154   Tmp = LowByte(PlayField16[tsi]);
3155   switch (Tmp)
3156   {
3157     case fiSnikSnak:
3158       subDrawAnimatedSnikSnaks(tsi);
3159       break;
3160
3161     case fiElectron:
3162       subDrawAnimatedElectrons(tsi);
3163       break;
3164
3165     default:
3166       //      If 40 < Tmp Then Tmp = 0
3167       //      If Tmp = fiRAM Or Tmp = fiHardWare Then Tmp = DisPlayField(tsi)
3168       //      If Tmp = fiBug Or Tmp = 40 Then Tmp = DisPlayField(tsi)
3169       //      If EditFlag Then
3170       //        If fiOrangeDisk < Tmp And Tmp < fiSnikSnak Then Tmp = DisPlayField(tsi)
3171       //      End If
3172       //      StretchedSprites.BltEx StretchWidth * X, StretchWidth * Y, Tmp
3173       break;
3174   }
3175 }
3176
3177 void DrawFieldNoAnimated(int X, int Y)
3178 {
3179   int Tmp, tsi;
3180
3181   tsi = GetSI(X, Y);
3182   Tmp = LowByte(PlayField16[tsi]);
3183   switch (Tmp)
3184   {
3185     case fiSnikSnak:
3186       StretchedSprites.BltEx(StretchWidth * X, StretchWidth * Y, fiSpace);
3187       break;
3188
3189     case fiElectron:
3190       StretchedSprites.BltEx(StretchWidth * X, StretchWidth * Y, fiSpace);
3191       break;
3192
3193     default:
3194       if (40 < Tmp)
3195         Tmp = 0;
3196
3197       if (Tmp == fiRAM || Tmp == fiHardWare)
3198         Tmp = DisPlayField[tsi];
3199
3200       if (Tmp == fiBug || Tmp == 40)
3201         Tmp = DisPlayField[tsi];
3202
3203       if (EditFlag)
3204       {
3205         if (fiOrangeDisk < Tmp && Tmp < fiSnikSnak)
3206           Tmp = DisPlayField[tsi];
3207       }
3208
3209 #if 0
3210       printf("::: MainForm.c: DrawFieldNoAnimated(): %d, %d [%d]\n",
3211              X, Y, Tmp);
3212 #endif
3213
3214       StretchedSprites.BltEx(StretchWidth * X, StretchWidth * Y, Tmp);
3215       break;
3216   }
3217 }
3218
3219 void DrawSprite(int X, int Y, int SpritePos)
3220 {
3221   StretchedSprites.BltEx(StretchWidth * X, StretchWidth * Y, SpritePos);
3222 }
3223
3224 #if 0
3225
3226 void InvalidateRect(long XMin, long YMin, long XMax, long YMax)
3227 {
3228   long X, Y;
3229
3230   //  For Y = YMin To YMax
3231   //    For X = XMin To XMax
3232   //      Field(X, Y).GraphicsValid = 0
3233   //    Next X
3234   //  Next Y
3235 }
3236
3237 static void picViewPort_MouseUp(int Button, int Shift, float X, float Y)
3238 {
3239   FocusTim.Enabled = True;
3240 }
3241
3242 static void picViewPort_Paint()
3243 {
3244   // Debug.Print "picViewPort_Paint()"
3245 }
3246
3247 #endif
3248
3249 static void picViewPort_Resize()
3250 {
3251 #if 0
3252   long bdX, bdY, PanelVisibility;
3253   long X, Y, dx, dY;
3254   SettingsObject s;
3255   boolean B;
3256
3257   bdX = picFrame.Width - picFrame.ScaleWidth;
3258   bdY = picFrame.Height - picFrame.ScaleHeight;
3259   dx = Min(picViewPort.ScaleWidth - bdX, (DisplayWidth - 1) * Stretch * BaseWidth);
3260   dx = Max(dx, 0);
3261   dY = Min(picViewPort.ScaleHeight - bdX, (DisplayHeight - 1) * Stretch * BaseWidth);
3262   dY = Max(dY, 0);
3263   B = s.Read("LimitToOriginalFieldSize", False);
3264   PanelVisibility = 24 * (Panel.Height - PanelSeq - (float)1) / Panel.Height;
3265   if (B)
3266   {
3267     dx = Min(320 * Stretch, dx);
3268     dY = Min((200 - PanelVisibility) * Stretch, dY);
3269   }
3270
3271   X = Max((picViewPort.ScaleWidth - dx) / 2, 0);
3272   Y = Max((picViewPort.ScaleHeight - dY) / 2, 0);
3273   picPane.Move X, Y, dx, dY;
3274   picFrame.Move X - bdX / 2, Y - bdY / 2, dx + bdX, dY + bdY;
3275 #endif
3276
3277   SetScrollEdges();
3278
3279 #if 0
3280   printf("::: MainForm.c: picViewPort_Resize(): %d, %d\n", ScrollX, ScrollY);
3281 #endif
3282
3283   ScrollTo(ScrollX, ScrollY);
3284
3285   //  SizeTim.Interval = 1
3286 }
3287
3288 #if 0
3289
3290 //
3291 // Private Sub SizeTim_Timer()
3292 // Dim wdX&, wdY&
3293 //  SizeTim.Interval = 0
3294 //  wdX = Max(0, picViewPort.ScaleWidth - picPane.Width) * Screen.TwipsPerPixelX
3295 //  wdY = Max(0, picViewPort.ScaleHeight - picPane.Height) * Screen.TwipsPerPixelY
3296 //  If (0 < wdX) Or (0 < wdY) Then
3297 //    Move Left, Top, Width - wdX, Height - wdY
3298 //  End If
3299 // End Sub
3300
3301 static void LoadKeyIndicators()
3302 {
3303   int i;
3304
3305   picKeys.BackColor = vbButtonFace;
3306   for (i = 2; i <= 5; i++)
3307   {
3308     Load shpKey(i);
3309   }
3310
3311   for (i = 1; i <= 5; i++)
3312   {
3313     shpKey(i).FillColor = vbButtonFace;
3314     shpKey(i).Visible = True;
3315   }
3316
3317   shpKey(1).Move 7, 0;
3318   shpKey(2).Move 0, 7;
3319   shpKey(3).Move 7, 14;
3320   shpKey(4).Move 14, 7;
3321   shpKey(5).Move 7, 7;
3322 }
3323
3324 void ShowKey(int KeyVar)
3325 {
3326   boolean State[5 + 1];
3327   int i;
3328   boolean Tmp;
3329   long Col;
3330   static boolean LastState[5 + 1];
3331
3332   //  For i = 1 To 5
3333   //    State(i) = False
3334   //  Next i
3335   switch (KeyVar)
3336   {
3337     case 0:
3338       // do nothing
3339       break;
3340
3341     case Is < 5:
3342       State[KeyVar] = True;
3343       break;
3344
3345     default:
3346       State[5] = True;
3347       if (KeyVar < 9)
3348         State[KeyVar - 4] = True;
3349       break;
3350   }
3351
3352   for (i = 1; i <= 5; i++)
3353   {
3354     Tmp = State[i];
3355     if (Tmp ^ LastState[i])
3356     {
3357       Col = (i == 5 ? vbRed : vbGreen);
3358       shpKey(i).FillColor = (Tmp ? Col : vbButtonFace);
3359       shpKey(i).Refresh;
3360       LastState[i] = Tmp;
3361     }
3362   }
3363 }
3364
3365 static void GetSettings()
3366 {
3367   SettingsObject s;
3368   char *APath;
3369   long X, Y;
3370   int i;
3371   boolean Flag;
3372
3373   {
3374     // last file info
3375     APath = WithSlash(App.Path);
3376     CurPath = s.Read("LastPath", APath);
3377     OrigPath = CurPath;
3378     // window width and height
3379     X = s.Read("Width", Width);
3380     Y = s.Read("Height", Height);
3381     if (X < 0 Then X == 0: If Y < 0)
3382       Y = 0;
3383
3384     if (Screen.Width < X)
3385       X = Screen.Width;
3386
3387     if (Screen.Height < Y)
3388       Y = Screen.Height;
3389
3390     Width = X;
3391     Height = Y;
3392     // window position and state
3393     X = s.Read("Left", left);
3394     Y = s.Read("Top", top);
3395     if (X < 0 Then X == 0: If Y < 0)
3396       Y = 0;
3397
3398     if (Screen.Width < (X + Width))
3399       X = Screen.Width - Width;
3400
3401     if (Screen.Height < (Y + Height))
3402       Y = Screen.Height - Height;
3403
3404     left = X;
3405     top = Y;
3406     WindowState = s.Read("WinState", vbNormal);
3407     // flags/options
3408     Flag = s.Read("ShowToolTips", True);
3409     if (Flag)
3410       menToolTips_Click();
3411
3412     menBorder.Checked = ! CBool(s.Read("ShowBorder", False));
3413     Flag = CBool(s.Read("AutoScroll", True));
3414     if (! Flag)
3415     {
3416       AutoScrollFlag = False;
3417       menAutoScroll.Checked = False;
3418     }
3419
3420     Flag = CBool(s.Read("SoundFX", True));
3421     FXOnFlag = (Flag ? -1 : 0);
3422     menSoundFX.Checked = Flag;
3423     SignatureDelay = CLng(s.Read("SignatureDelay", 3000));
3424     AllowRedDiskCheat = CInt(s.Read("AllowRedDiskCheat", 1));
3425     AllowEatRightRedDiskBug = CInt(s.Read("AllowEatRightRedDiskBug", 1));
3426     MySignature = s.Read("MySignature", "");
3427     // speed
3428     X = s.Read("FrameDelayPlay", 1000000 / 35);
3429     i = GetSpeedIndex(X);
3430     menPlaySpeed_Click(i);
3431     X = s.Read("FrameDelayDemo", 1000000 / 35);
3432     i = GetSpeedIndex(X);
3433     menDemoSpeed_Click(i);
3434     // Zoom
3435     i = s.Read("Stretch", 4);
3436     if (i < menStretch.LBound || menStretch.UBound < i)
3437       i = 4;
3438
3439     menStretch_Click (i);
3440   }
3441 }
3442
3443 static void SaveSettings()
3444 {
3445   SettingsObject s;
3446   int i;
3447
3448   {
3449     s.Save "LastPath", (ModifiedFlag ? OrigPath : CurPath);
3450     if (WindowState != vbMinimized)
3451     {
3452       s.Save "Width", Width;
3453       s.Save "Height", Height;
3454       s.Save "Left", left;
3455       s.Save "Top", top;
3456       s.Save "WinState", WindowState;
3457     }
3458
3459     s.Save "ShowToolTips", menToolTips.Checked;
3460     s.Save "ShowBorder", menBorder.Checked;
3461     s.Save "AutoScroll", menAutoScroll.Checked;
3462     s.Save "SoundFX", menSoundFX.Checked;
3463     s.Save "SignatureDelay", SignatureDelay;
3464     s.Save "AllowRedDiskCheat", AllowRedDiskCheat;
3465     s.Save "AllowEatRightRedDiskBug", AllowEatRightRedDiskBug;
3466     s.Save "MySignature", MySignature;
3467     // speeds
3468     s.Save "FrameDelayPlay", DeltaTPlay;
3469     s.Save "FrameDelayDemo", DeltaTDemo;
3470     // zoom
3471     for (i = menStretch.LBound; i <= menStretch.UBound; i++)
3472     {
3473       if (menStretch(i).Checked)
3474         break;
3475     }
3476
3477     s.Save "Stretch", i;
3478     //    s.Save "",
3479   }
3480 }
3481
3482 void ReLoadStretchSprites()
3483 {
3484   long Tmp;
3485   BitMapObject NBMP, SBMP;
3486   char *tmpMpxBmp;
3487
3488   //  If Stretch = 1 Then
3489   MpxBmp = App.Path & "/Mpx.bmp";
3490   //  Else
3491   //    MpxBmp = App.Path & "\Mpx" & Stretch & ".bmp"
3492   //    tmpMpxBmp = App.Path & "\Mpx.bmp"
3493   //    If FileExists(MpxBmp) And FileExists(tmpMpxBmp) Then
3494   //      If FileDateTime(MpxBmp) < FileDateTime(tmpMpxBmp) Then
3495   //        MayKill MpxBmp
3496   //      End If
3497   //    End If
3498   //    If Not FileExists(MpxBmp) Then
3499   //      MousePointer = 11
3500   // //      Set NBMP = New BitMapObject // (handle this later, if needed)
3501   //      tmpMpxBmp = App.Path & "\Mpx.bmp"
3502   //      If Not FileExists(tmpMpxBmp) Then
3503   //        ReportError "ReLoadStretchSprites", "File not found: " & tmpMpxBmp
3504   //        MESSAGE_BOX("an error occured"); // MsgBox "File not found: " & tmpMpxBmp, vbCritical, "MegaPlex - Error"
3505   //        End
3506   //      End If
3507   //      NBMP.CreateFromFile tmpMpxBmp
3508   //      Set SBMP = NBMP.GetStretchCopy(Stretch)
3509   //      SBMP.SaveToFile MpxBmp
3510   //      Set NBMP = Nothing
3511   //      Set SBMP = Nothing
3512   //      MousePointer = 0
3513   //    End If
3514   //  End If
3515   // StretchedSprites = New DDSpriteBuffer; // (handle this later, if needed)
3516   if (! StretchedSprites.CreateFromFile(MpxBmp, 16, 16))
3517     SET_TO_NOTHING(&StretchedSprites, sizeof(StretchedSprites));
3518 }
3519
3520 void SaveSnapshot(currency Number)
3521 {
3522   char *Path;
3523
3524   Path = CAT(WithSlash(App.Path), "Capture");
3525   if (! IsDir(Path))
3526     MkDir(Path);
3527
3528   Path = Path & "\" & Format(Number, "00000000") & ".bmp";
3529   SavePicture CaptureWindow(picPane.hWnd, True, 0, 0, picPane.ScaleWidth, picPane.ScaleHeight), Path;
3530 }
3531
3532 #endif