main.c 11 KB

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