main.c 11 KB

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