forked from sdlpal/sdlpal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
551 lines (452 loc) · 11.8 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
//
// Copyright (c) 2009-2011, Wei Mingzhi <[email protected]>.
// Copyright (c) 2011-2020, SDLPAL development team.
// All rights reserved.
//
// This file is part of SDLPAL.
//
// SDLPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "main.h"
#include <setjmp.h>
#if defined(PAL_HAS_GIT_REVISION)
# undef PAL_GIT_REVISION
# include "generated.h"
#endif
static jmp_buf g_exit_jmp_buf;
static int g_exit_code = 0;
char gExecutablePath[PAL_MAX_PATH];
#define BITMAPNUM_SPLASH_UP (gConfig.fIsWIN95 ? 0x03 : 0x26)
#define BITMAPNUM_SPLASH_DOWN (gConfig.fIsWIN95 ? 0x04 : 0x27)
#define SPRITENUM_SPLASH_TITLE 0x47
#define SPRITENUM_SPLASH_CRANE 0x49
#define NUM_RIX_TITLE 0x05
static VOID
PAL_Init(
VOID
)
/*++
Purpose:
Initialize everything needed by the game.
Parameters:
None.
Return value:
None.
--*/
{
int e;
#if PAL_HAS_GIT_REVISION
UTIL_LogOutput(LOGLEVEL_DEBUG, "SDLPal build revision: %s\n", PAL_GIT_REVISION);
#endif
//
// Initialize subsystems.
//
e = PAL_InitGlobals();
if (e != 0)
{
TerminateOnError("Could not initialize global data: %d.\n", e);
}
e = VIDEO_Startup();
if (e != 0)
{
TerminateOnError("Could not initialize Video: %d.\n", e);
}
VIDEO_SetWindowTitle("Loading...");
e = PAL_InitUI();
if (e != 0)
{
TerminateOnError("Could not initialize UI subsystem: %d.\n", e);
}
e = PAL_InitText();
if (e != 0)
{
TerminateOnError("Could not initialize text subsystem: %d.\n", e);
}
e = PAL_InitFont(&gConfig);
if (e != 0)
{
TerminateOnError("Could not load fonts: %d.\n", e);
}
PAL_InitInput();
PAL_InitResources();
AUDIO_OpenDevice();
PAL_AVIInit();
VIDEO_SetWindowTitle(UTIL_va(UTIL_GlobalBuffer(0), PAL_GLOBAL_BUFFER_SIZE,
"Pal %s%s%s%s",
gConfig.fIsWIN95 ? "Win95" : "DOS",
#if defined(_DEBUG) || defined(DEBUG)
" (Debug) ",
#else
"",
#endif
#if defined(PAL_HAS_GIT_REVISION) && defined(PAL_GIT_REVISION)
" [" PAL_GIT_REVISION "] "
#else
""
#endif
,(gConfig.fEnableGLSL && gConfig.pszShader ? gConfig.pszShader : "")
));
}
VOID
PAL_Shutdown(
int exit_code
)
/*++
Purpose:
Free everything needed by the game.
Parameters:
exit_code - The exit code return to OS.
Return value:
None.
--*/
{
AUDIO_CloseDevice();
PAL_AVIShutdown();
PAL_FreeFont();
PAL_FreeResources();
PAL_FreeUI();
PAL_FreeText();
PAL_ShutdownInput();
VIDEO_Shutdown();
//
// global needs be free in last
// since subsystems may needs config content during destroy
// which also cleared here
//
PAL_FreeGlobals();
g_exit_code = exit_code;
#if !__EMSCRIPTEN__
longjmp(g_exit_jmp_buf, 1);
#else
SDL_Quit();
UTIL_Platform_Quit();
return;
#endif
}
VOID
PAL_TrademarkScreen(
VOID
)
/*++
Purpose:
Show the trademark screen.
Parameters:
None.
Return value:
None.
--*/
{
if (PAL_PlayAVI("1.avi")) return;
PAL_SetPalette(3, FALSE);
PAL_RNGPlay(6, 0, -1, 25);
UTIL_Delay(1000);
PAL_FadeOut(1);
}
VOID
PAL_SplashScreen(
VOID
)
/*++
Purpose:
Show the splash screen.
Parameters:
None.
Return value:
None.
--*/
{
SDL_Color *palette = PAL_GetPalette(1, FALSE);
SDL_Color rgCurrentPalette[256];
SDL_Surface *lpBitmapDown, *lpBitmapUp;
SDL_Rect srcrect, dstrect;
LPSPRITE lpSpriteCrane;
LPBITMAPRLE lpBitmapTitle;
LPBYTE buf, buf2;
int cranepos[9][3], i, iImgPos = 200, iCraneFrame = 0, iTitleHeight;
DWORD dwTime, dwBeginTime;
BOOL fUseCD = TRUE;
if (PAL_PlayAVI("2.avi")) return;
if (palette == NULL)
{
fprintf(stderr, "ERROR: PAL_SplashScreen(): palette == NULL\n");
return;
}
//
// Allocate all the needed memory at once for simplification
//
buf = (LPBYTE)UTIL_calloc(1, 320 * 200 * 2);
buf2 = (LPBYTE)(buf + 320 * 200);
lpSpriteCrane = (LPSPRITE)buf2 + 32000;
//
// Create the surfaces
//
lpBitmapDown = VIDEO_CreateCompatibleSurface(gpScreen);
lpBitmapUp = VIDEO_CreateCompatibleSurface(gpScreen);
//
// Read the bitmaps
//
PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_UP, gpGlobals->f.fpFBP);
Decompress(buf, buf2, 320 * 200);
PAL_FBPBlitToSurface(buf2, lpBitmapUp);
PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_DOWN, gpGlobals->f.fpFBP);
Decompress(buf, buf2, 320 * 200);
PAL_FBPBlitToSurface(buf2, lpBitmapDown);
PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_TITLE, gpGlobals->f.fpMGO);
Decompress(buf, buf2, 32000);
lpBitmapTitle = (LPBITMAPRLE)PAL_SpriteGetFrame(buf2, 0);
PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_CRANE, gpGlobals->f.fpMGO);
Decompress(buf, lpSpriteCrane, 32000);
iTitleHeight = PAL_RLEGetHeight(lpBitmapTitle);
lpBitmapTitle[2] = 0;
lpBitmapTitle[3] = 0; // HACKHACK
//
// Generate the positions of the cranes
//
for (i = 0; i < 9; i++)
{
cranepos[i][0] = RandomLong(300, 600);
cranepos[i][1] = RandomLong(0, 80);
cranepos[i][2] = RandomLong(0, 8);
}
//
// Play the title music
//
if (!AUDIO_PlayCDTrack(7))
{
fUseCD = FALSE;
AUDIO_PlayMusic(NUM_RIX_TITLE, TRUE, 2);
}
//
// Clear all of the events and key states
//
PAL_ProcessEvent();
PAL_ClearKeyState();
dwBeginTime = SDL_GetTicks();
srcrect.x = 0;
srcrect.w = 320;
dstrect.x = 0;
dstrect.w = 320;
while (TRUE)
{
PAL_ProcessEvent();
dwTime = SDL_GetTicks() - dwBeginTime;
//
// Set the palette
//
if (dwTime < 15000)
{
for (i = 0; i < 256; i++)
{
rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
}
}
VIDEO_SetPalette(rgCurrentPalette);
VIDEO_UpdateSurfacePalette(lpBitmapDown);
VIDEO_UpdateSurfacePalette(lpBitmapUp);
//
// Draw the screen
//
if (iImgPos > 1)
{
iImgPos--;
}
//
// The upper part...
//
srcrect.y = iImgPos;
srcrect.h = 200 - iImgPos;
dstrect.y = 0;
dstrect.h = srcrect.h;
VIDEO_CopySurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
//
// The lower part...
//
srcrect.y = 0;
srcrect.h = iImgPos;
dstrect.y = 200 - iImgPos;
dstrect.h = srcrect.h;
VIDEO_CopySurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
//
// Draw the cranes...
//
for (i = 0; i < 9; i++)
{
LPCBITMAPRLE lpFrame = PAL_SpriteGetFrame(lpSpriteCrane,
cranepos[i][2] = (cranepos[i][2] + (iCraneFrame & 1)) % 8);
cranepos[i][1] += ((iImgPos > 1) && (iImgPos & 1)) ? 1 : 0;
PAL_RLEBlitToSurface(lpFrame, gpScreen,
PAL_XY(cranepos[i][0], cranepos[i][1]));
cranepos[i][0]--;
}
iCraneFrame++;
//
// Draw the title...
//
if (PAL_RLEGetHeight(lpBitmapTitle) < iTitleHeight)
{
//
// HACKHACK
//
WORD w = lpBitmapTitle[2] | (lpBitmapTitle[3] << 8);
w++;
lpBitmapTitle[2] = (w & 0xFF);
lpBitmapTitle[3] = (w >> 8);
}
PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
VIDEO_UpdateScreen(NULL);
//
// Check for keypress...
//
if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
{
//
// User has pressed a key...
//
lpBitmapTitle[2] = iTitleHeight & 0xFF;
lpBitmapTitle[3] = iTitleHeight >> 8; // HACKHACK
PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
VIDEO_UpdateScreen(NULL);
if (dwTime < 15000)
{
//
// If the picture has not completed fading in, complete the rest
//
while (dwTime < 15000)
{
for (i = 0; i < 256; i++)
{
rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
}
VIDEO_SetPalette(rgCurrentPalette);
VIDEO_UpdateSurfacePalette(lpBitmapDown);
VIDEO_UpdateSurfacePalette(lpBitmapUp);
UTIL_Delay(8);
dwTime += 250;
}
UTIL_Delay(500);
}
//
// Quit the splash screen
//
break;
}
//
// Delay a while...
//
PAL_ProcessEvent();
while (SDL_GetTicks() - dwBeginTime < dwTime + 85)
{
SDL_Delay(1);
PAL_ProcessEvent();
}
}
VIDEO_FreeSurface(lpBitmapDown);
VIDEO_FreeSurface(lpBitmapUp);
free(buf);
if (!fUseCD)
{
AUDIO_PlayMusic(0, FALSE, 1);
}
PAL_FadeOut(1);
}
int
main(
int argc,
char *argv[]
)
/*++
Purpose:
Program entry.
Parameters:
argc - Number of arguments.
argv - Array of arguments.
Return value:
Integer value.
--*/
{
#if !defined( __EMSCRIPTEN__ ) && !defined(__WINRT__)
memset(gExecutablePath,0,PAL_MAX_PATH);
strncpy(gExecutablePath, argv[0], PAL_MAX_PATH);
#endif
#if PAL_HAS_PLATFORM_STARTUP
UTIL_Platform_Startup(argc,argv);
#endif
#if !__EMSCRIPTEN__
if (setjmp(g_exit_jmp_buf) != 0)
{
// A longjmp is made, should exit here
SDL_Quit();
UTIL_Platform_Quit();
return g_exit_code;
}
#endif
#if !defined(UNIT_TEST) || defined(UNIT_TEST_GAME_INIT)
//
// Initialize SDL
//
if (SDL_Init(PAL_SDL_INIT_FLAGS) == -1)
{
TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
}
PAL_LoadConfig(TRUE);
//
// Platform-specific initialization
//
if (UTIL_Platform_Init(argc, argv) != 0)
return -1;
//
// Should launch setting?
// Generally, the condition should never be TRUE as the UTIL_Platform_Init is assumed
// to handle gConfig.fLaunchSetting correctly. However, it may actually be true due to
// the activatation event on WinRT platform, so close the current process to make new
// process go to setting.
// For platforms without configuration page available, this condition will NEVER be true.
//
if (PAL_HAS_CONFIG_PAGE && gConfig.fLaunchSetting)
return 0;
//
// If user requests a file-based log, then add it after the system-specific one.
//
if (gConfig.pszLogFile)
UTIL_LogAddOutputCallback(UTIL_LogToFile, gConfig.iLogLevel);
//
// Initialize everything
//
PAL_Init();
#endif
#if !defined(UNIT_TEST)
//
// Show the trademark screen and splash screen
//
PAL_TrademarkScreen();
PAL_SplashScreen();
//
// Run the main game routine
//
PAL_GameMain();
//
// Should not really reach here...
//
assert(FALSE);
return 255;
#else
extern int testmain(int argc, char *argv[]);
return testmain(argc, argv);
#endif
}