main.c 12 KB

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