main.c 11 KB

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