main.c 13 KB

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