main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. VIDEO_SetWindowTitle(va("Pal %s%s%s",
  85. gConfig.fIsWIN95 ? "Win95" : "DOS",
  86. #if defined(_DEBUG) || defined(DEBUG)
  87. " (Debug) ",
  88. #else
  89. "",
  90. #endif
  91. #if defined(PAL_HAS_GIT_REVISION) && defined(PAL_GIT_REVISION)
  92. " [" PAL_GIT_REVISION "] "
  93. #else
  94. ""
  95. #endif
  96. ));
  97. }
  98. VOID
  99. PAL_Shutdown(
  100. int exit_code
  101. )
  102. /*++
  103. Purpose:
  104. Free everything needed by the game.
  105. Parameters:
  106. exit_code - The exit code return to OS.
  107. Return value:
  108. None.
  109. --*/
  110. {
  111. AUDIO_CloseDevice();
  112. PAL_FreeFont();
  113. PAL_FreeResources();
  114. PAL_FreeGlobals();
  115. PAL_FreeUI();
  116. PAL_FreeText();
  117. PAL_ShutdownInput();
  118. VIDEO_Shutdown();
  119. SDL_Quit();
  120. g_exit_code = exit_code;
  121. longjmp(g_exit_jmp_buf, 1);
  122. }
  123. VOID
  124. PAL_TrademarkScreen(
  125. VOID
  126. )
  127. /*++
  128. Purpose:
  129. Show the trademark screen.
  130. Parameters:
  131. None.
  132. Return value:
  133. None.
  134. --*/
  135. {
  136. PAL_SetPalette(3, FALSE);
  137. PAL_RNGPlay(6, 0, 1000, 25);
  138. UTIL_Delay(1000);
  139. PAL_FadeOut(1);
  140. }
  141. VOID
  142. PAL_SplashScreen(
  143. VOID
  144. )
  145. /*++
  146. Purpose:
  147. Show the splash screen.
  148. Parameters:
  149. None.
  150. Return value:
  151. None.
  152. --*/
  153. {
  154. SDL_Color *palette = PAL_GetPalette(1, FALSE);
  155. SDL_Color rgCurrentPalette[256];
  156. SDL_Surface *lpBitmapDown, *lpBitmapUp;
  157. SDL_Rect srcrect, dstrect;
  158. LPSPRITE lpSpriteCrane;
  159. LPBITMAPRLE lpBitmapTitle;
  160. LPBYTE buf, buf2;
  161. int cranepos[9][3], i, iImgPos = 200, iCraneFrame = 0, iTitleHeight;
  162. DWORD dwTime, dwBeginTime;
  163. BOOL fUseCD = TRUE;
  164. if (palette == NULL)
  165. {
  166. fprintf(stderr, "ERROR: PAL_SplashScreen(): palette == NULL\n");
  167. return;
  168. }
  169. //
  170. // Allocate all the needed memory at once for simplification
  171. //
  172. buf = (LPBYTE)UTIL_calloc(1, 320 * 200 * 2);
  173. buf2 = (LPBYTE)(buf + 320 * 200);
  174. lpSpriteCrane = (LPSPRITE)buf2 + 32000;
  175. //
  176. // Create the surfaces
  177. //
  178. lpBitmapDown = VIDEO_CreateCompatibleSurface(gpScreen);
  179. lpBitmapUp = VIDEO_CreateCompatibleSurface(gpScreen);
  180. //
  181. // Read the bitmaps
  182. //
  183. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_UP, gpGlobals->f.fpFBP);
  184. Decompress(buf, buf2, 320 * 200);
  185. PAL_FBPBlitToSurface(buf2, lpBitmapUp);
  186. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_DOWN, gpGlobals->f.fpFBP);
  187. Decompress(buf, buf2, 320 * 200);
  188. PAL_FBPBlitToSurface(buf2, lpBitmapDown);
  189. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_TITLE, gpGlobals->f.fpMGO);
  190. Decompress(buf, buf2, 32000);
  191. lpBitmapTitle = (LPBITMAPRLE)PAL_SpriteGetFrame(buf2, 0);
  192. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_CRANE, gpGlobals->f.fpMGO);
  193. Decompress(buf, lpSpriteCrane, 32000);
  194. iTitleHeight = PAL_RLEGetHeight(lpBitmapTitle);
  195. lpBitmapTitle[2] = 0;
  196. lpBitmapTitle[3] = 0; // HACKHACK
  197. //
  198. // Generate the positions of the cranes
  199. //
  200. for (i = 0; i < 9; i++)
  201. {
  202. cranepos[i][0] = RandomLong(300, 600);
  203. cranepos[i][1] = RandomLong(0, 80);
  204. cranepos[i][2] = RandomLong(0, 8);
  205. }
  206. //
  207. // Play the title music
  208. //
  209. if (!AUDIO_PlayCDTrack(7))
  210. {
  211. fUseCD = FALSE;
  212. AUDIO_PlayMusic(NUM_RIX_TITLE, TRUE, 2);
  213. }
  214. //
  215. // Clear all of the events and key states
  216. //
  217. PAL_ProcessEvent();
  218. PAL_ClearKeyState();
  219. dwBeginTime = SDL_GetTicks();
  220. srcrect.x = 0;
  221. srcrect.w = 320;
  222. dstrect.x = 0;
  223. dstrect.w = 320;
  224. while (TRUE)
  225. {
  226. PAL_ProcessEvent();
  227. dwTime = SDL_GetTicks() - dwBeginTime;
  228. //
  229. // Set the palette
  230. //
  231. if (dwTime < 15000)
  232. {
  233. for (i = 0; i < 256; i++)
  234. {
  235. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  236. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  237. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  238. }
  239. }
  240. VIDEO_SetPalette(rgCurrentPalette);
  241. VIDEO_UpdateSurfacePalette(lpBitmapDown);
  242. VIDEO_UpdateSurfacePalette(lpBitmapUp);
  243. //
  244. // Draw the screen
  245. //
  246. if (iImgPos > 1)
  247. {
  248. iImgPos--;
  249. }
  250. //
  251. // The upper part...
  252. //
  253. srcrect.y = iImgPos;
  254. srcrect.h = 200 - iImgPos;
  255. dstrect.y = 0;
  256. dstrect.h = srcrect.h;
  257. VIDEO_CopySurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
  258. //
  259. // The lower part...
  260. //
  261. srcrect.y = 0;
  262. srcrect.h = iImgPos;
  263. dstrect.y = 200 - iImgPos;
  264. dstrect.h = srcrect.h;
  265. VIDEO_CopySurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
  266. //
  267. // Draw the cranes...
  268. //
  269. for (i = 0; i < 9; i++)
  270. {
  271. LPCBITMAPRLE lpFrame = PAL_SpriteGetFrame(lpSpriteCrane,
  272. cranepos[i][2] = (cranepos[i][2] + (iCraneFrame & 1)) % 8);
  273. cranepos[i][1] += ((iImgPos > 1) && (iImgPos & 1)) ? 1 : 0;
  274. PAL_RLEBlitToSurface(lpFrame, gpScreen,
  275. PAL_XY(cranepos[i][0], cranepos[i][1]));
  276. cranepos[i][0]--;
  277. }
  278. iCraneFrame++;
  279. //
  280. // Draw the title...
  281. //
  282. if (PAL_RLEGetHeight(lpBitmapTitle) < iTitleHeight)
  283. {
  284. //
  285. // HACKHACK
  286. //
  287. WORD w = lpBitmapTitle[2] | (lpBitmapTitle[3] << 8);
  288. w++;
  289. lpBitmapTitle[2] = (w & 0xFF);
  290. lpBitmapTitle[3] = (w >> 8);
  291. }
  292. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  293. VIDEO_UpdateScreen(NULL);
  294. //
  295. // Check for keypress...
  296. //
  297. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  298. {
  299. //
  300. // User has pressed a key...
  301. //
  302. lpBitmapTitle[2] = iTitleHeight & 0xFF;
  303. lpBitmapTitle[3] = iTitleHeight >> 8; // HACKHACK
  304. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  305. VIDEO_UpdateScreen(NULL);
  306. if (dwTime < 15000)
  307. {
  308. //
  309. // If the picture has not completed fading in, complete the rest
  310. //
  311. while (dwTime < 15000)
  312. {
  313. for (i = 0; i < 256; i++)
  314. {
  315. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  316. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  317. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  318. }
  319. VIDEO_SetPalette(rgCurrentPalette);
  320. VIDEO_UpdateSurfacePalette(lpBitmapDown);
  321. VIDEO_UpdateSurfacePalette(lpBitmapUp);
  322. UTIL_Delay(8);
  323. dwTime += 250;
  324. }
  325. UTIL_Delay(500);
  326. }
  327. //
  328. // Quit the splash screen
  329. //
  330. break;
  331. }
  332. //
  333. // Delay a while...
  334. //
  335. PAL_ProcessEvent();
  336. while (SDL_GetTicks() - dwBeginTime < dwTime + 85)
  337. {
  338. SDL_Delay(1);
  339. PAL_ProcessEvent();
  340. }
  341. }
  342. VIDEO_FreeSurface(lpBitmapDown);
  343. VIDEO_FreeSurface(lpBitmapUp);
  344. free(buf);
  345. if (!fUseCD)
  346. {
  347. AUDIO_PlayMusic(0, FALSE, 1);
  348. }
  349. PAL_FadeOut(1);
  350. }
  351. int
  352. main(
  353. int argc,
  354. char *argv[]
  355. )
  356. /*++
  357. Purpose:
  358. Program entry.
  359. Parameters:
  360. argc - Number of arguments.
  361. argv - Array of arguments.
  362. Return value:
  363. Integer value.
  364. --*/
  365. {
  366. #if defined(__APPLE__) && !defined(__IOS__) && !defined(DEBUG) //for ease of debugging(specify resource dir in xcode scheme)
  367. char *p = strstr(argv[0], "/Pal.app/");
  368. if (p != NULL)
  369. {
  370. char buf[4096];
  371. strcpy(buf, argv[0]);
  372. buf[p - argv[0]] = '\0';
  373. chdir(buf);
  374. }
  375. #endif
  376. if (setjmp(g_exit_jmp_buf) != 0)
  377. {
  378. // A longjmp is made, should exit here
  379. UTIL_Platform_Quit();
  380. return g_exit_code;
  381. }
  382. #if !defined(UNIT_TEST) || defined(UNIT_TEST_GAME_INIT)
  383. PAL_LoadConfig(TRUE);
  384. //
  385. // Platform-specific initialization
  386. //
  387. if (UTIL_Platform_Init(argc, argv) != 0)
  388. return -1;
  389. //
  390. // Should launch setting?
  391. // Generally, the condition should never be TRUE as the UTIL_Platform_Init is assumed
  392. // to handle gConfig.fLaunchSetting correctly. However, it may actually be true due to
  393. // the activatation event on WinRT platform, so close the current process to make new
  394. // process go to setting.
  395. // For platforms without configuration page available, this condition will NEVER be true.
  396. //
  397. if (PAL_HAS_CONFIG_PAGE && gConfig.fLaunchSetting)
  398. return 0;
  399. //
  400. // If user requests a file-based log, then add it after the system-specific one.
  401. //
  402. if (gConfig.pszLogFile)
  403. UTIL_LogAddOutputCallback(UTIL_LogToFile, gConfig.iLogLevel);
  404. //
  405. // Initialize everything
  406. //
  407. PAL_Init();
  408. #endif
  409. #if !defined(UNIT_TEST)
  410. //
  411. // Show the trademark screen and splash screen
  412. //
  413. PAL_TrademarkScreen();
  414. PAL_SplashScreen();
  415. //
  416. // Run the main game routine
  417. //
  418. PAL_GameMain();
  419. //
  420. // Should not really reach here...
  421. //
  422. assert(FALSE);
  423. return 255;
  424. #else
  425. extern int testmain(int argc, char *argv[]);
  426. return testmain(argc, argv);
  427. #endif
  428. }