main.c 11 KB

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