main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. #include "getopt.h"
  25. #ifdef PSP
  26. #include "main_PSP.h"
  27. #endif
  28. #if defined (NDS) && defined (GEKKO)
  29. #include <fat.h>
  30. #endif
  31. #ifdef __WINPHONE__
  32. #include <setjmp.h>
  33. static jmp_buf g_exit_jmp_env;
  34. # define LONGJMP_EXIT_CODE 0xff
  35. #endif
  36. #define BITMAPNUM_SPLASH_UP (gpGlobals->fIsWIN95 ? 0x03 : 0x26)
  37. #define BITMAPNUM_SPLASH_DOWN (gpGlobals->fIsWIN95 ? 0x04 : 0x27)
  38. #define SPRITENUM_SPLASH_TITLE 0x47
  39. #define SPRITENUM_SPLASH_CRANE 0x49
  40. #define NUM_RIX_TITLE 0x05
  41. BOOL g_fUseMidi = FALSE;
  42. static VOID
  43. PAL_Init(
  44. WORD wScreenWidth,
  45. WORD wScreenHeight,
  46. BOOL fFullScreen
  47. )
  48. /*++
  49. Purpose:
  50. Initialize everything needed by the game.
  51. Parameters:
  52. [IN] wScreenWidth - width of the screen.
  53. [IN] wScreenHeight - height of the screen.
  54. [IN] fFullScreen - TRUE to use full screen mode, FALSE to use windowed mode.
  55. Return value:
  56. None.
  57. --*/
  58. {
  59. int e;
  60. #if defined (NDS) && defined (GEKKO)
  61. fatInitDefault();
  62. #endif
  63. //
  64. // Initialize defaults, video and audio
  65. //
  66. #if defined(DINGOO)
  67. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
  68. #elif defined (__WINPHONE__)
  69. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1)
  70. #else
  71. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK) == -1)
  72. #endif
  73. {
  74. #if defined (_WIN32) && SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  75. //
  76. // Try the WINDIB driver if DirectX failed.
  77. //
  78. putenv("SDL_VIDEODRIVER=windib");
  79. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK) == -1)
  80. {
  81. TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
  82. }
  83. #else
  84. TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
  85. #endif
  86. }
  87. //
  88. // Initialize subsystems.
  89. //
  90. e = PAL_InitGlobals();
  91. if (e != 0)
  92. {
  93. TerminateOnError("Could not initialize global data: %d.\n", e);
  94. }
  95. #ifdef GEKKO
  96. e = VIDEO_Init_GEKKO(wScreenWidth, wScreenHeight, fFullScreen);
  97. #else
  98. e = VIDEO_Init(wScreenWidth, wScreenHeight, fFullScreen);
  99. #endif
  100. if (e != 0)
  101. {
  102. TerminateOnError("Could not initialize Video: %d.\n", e);
  103. }
  104. SDL_WM_SetCaption("Loading...", NULL);
  105. e = PAL_InitFont(gpGlobals->fUseEmbeddedFonts);
  106. if (e != 0)
  107. {
  108. TerminateOnError("Could not load fonts: %d.\n", e);
  109. }
  110. e = PAL_InitUI();
  111. if (e != 0)
  112. {
  113. TerminateOnError("Could not initialize UI subsystem: %d.\n", e);
  114. }
  115. e = PAL_InitText();
  116. if (e != 0)
  117. {
  118. TerminateOnError("Could not initialize text subsystem: %d.\n", e);
  119. }
  120. PAL_InitInput();
  121. PAL_InitResources();
  122. SOUND_OpenAudio();
  123. if (gpGlobals->fIsWIN95)
  124. {
  125. #ifdef _DEBUG
  126. SDL_WM_SetCaption("Pal WIN95 (Debug Build)", NULL);
  127. #else
  128. SDL_WM_SetCaption("Pal WIN95", NULL);
  129. #endif
  130. }
  131. else
  132. {
  133. #ifdef _DEBUG
  134. SDL_WM_SetCaption("Pal (Debug Build)", NULL);
  135. #else
  136. SDL_WM_SetCaption("Pal", NULL);
  137. #endif
  138. }
  139. }
  140. VOID
  141. PAL_Shutdown(
  142. VOID
  143. )
  144. /*++
  145. Purpose:
  146. Free everything needed by the game.
  147. Parameters:
  148. None.
  149. Return value:
  150. None.
  151. --*/
  152. {
  153. SOUND_CloseAudio();
  154. PAL_FreeFont();
  155. PAL_FreeResources();
  156. PAL_FreeGlobals();
  157. PAL_FreeUI();
  158. PAL_FreeText();
  159. PAL_ShutdownInput();
  160. VIDEO_Shutdown();
  161. UTIL_CloseLog();
  162. SDL_Quit();
  163. #if defined(GPH)
  164. chdir("/usr/gp2x");
  165. execl("./gp2xmenu", "./gp2xmenu", NULL);
  166. #endif
  167. #ifdef __WINPHONE__
  168. longjmp(g_exit_jmp_env, LONGJMP_EXIT_CODE);
  169. #endif
  170. }
  171. VOID
  172. PAL_TrademarkScreen(
  173. VOID
  174. )
  175. /*++
  176. Purpose:
  177. Show the trademark screen.
  178. Parameters:
  179. None.
  180. Return value:
  181. None.
  182. --*/
  183. {
  184. PAL_SetPalette(3, FALSE);
  185. PAL_RNGPlay(6, 0, 1000, 25);
  186. UTIL_Delay(1000);
  187. PAL_FadeOut(1);
  188. }
  189. VOID
  190. PAL_SplashScreen(
  191. VOID
  192. )
  193. /*++
  194. Purpose:
  195. Show the splash screen.
  196. Parameters:
  197. None.
  198. Return value:
  199. None.
  200. --*/
  201. {
  202. SDL_Color *palette = PAL_GetPalette(1, FALSE);
  203. SDL_Color rgCurrentPalette[256];
  204. SDL_Surface *lpBitmapDown, *lpBitmapUp;
  205. SDL_Rect srcrect, dstrect;
  206. LPSPRITE lpSpriteCrane;
  207. LPBITMAPRLE lpBitmapTitle;
  208. LPBYTE buf, buf2;
  209. int cranepos[9][3], i, iImgPos = 200, iCraneFrame = 0, iTitleHeight;
  210. DWORD dwTime, dwBeginTime;
  211. BOOL fUseCD = TRUE;
  212. if (palette == NULL)
  213. {
  214. fprintf(stderr, "ERROR: PAL_SplashScreen(): palette == NULL\n");
  215. return;
  216. }
  217. //
  218. // Allocate all the needed memory at once for simplification
  219. //
  220. buf = (LPBYTE)UTIL_calloc(1, 320 * 200 * 2);
  221. buf2 = (LPBYTE)(buf + 320 * 200);
  222. lpSpriteCrane = (LPSPRITE)buf2 + 32000;
  223. //
  224. // Create the surfaces
  225. //
  226. lpBitmapDown = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  227. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  228. gpScreen->format->Amask);
  229. lpBitmapUp = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  230. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  231. gpScreen->format->Amask);
  232. #if SDL_VERSION_ATLEAST(2, 0, 0)
  233. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  234. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  235. #else
  236. SDL_SetPalette(lpBitmapDown, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  237. SDL_SetPalette(lpBitmapUp, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  238. #endif
  239. //
  240. // Read the bitmaps
  241. //
  242. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_UP, gpGlobals->f.fpFBP);
  243. Decompress(buf, buf2, 320 * 200);
  244. PAL_FBPBlitToSurface(buf2, lpBitmapUp);
  245. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_DOWN, gpGlobals->f.fpFBP);
  246. Decompress(buf, buf2, 320 * 200);
  247. PAL_FBPBlitToSurface(buf2, lpBitmapDown);
  248. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_TITLE, gpGlobals->f.fpMGO);
  249. Decompress(buf, buf2, 32000);
  250. lpBitmapTitle = (LPBITMAPRLE)PAL_SpriteGetFrame(buf2, 0);
  251. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_CRANE, gpGlobals->f.fpMGO);
  252. Decompress(buf, lpSpriteCrane, 32000);
  253. iTitleHeight = PAL_RLEGetHeight(lpBitmapTitle);
  254. lpBitmapTitle[2] = 0;
  255. lpBitmapTitle[3] = 0; // HACKHACK
  256. //
  257. // Generate the positions of the cranes
  258. //
  259. for (i = 0; i < 9; i++)
  260. {
  261. cranepos[i][0] = RandomLong(300, 600);
  262. cranepos[i][1] = RandomLong(0, 80);
  263. cranepos[i][2] = RandomLong(0, 8);
  264. }
  265. //
  266. // Play the title music
  267. //
  268. if (!SOUND_PlayCDA(7))
  269. {
  270. fUseCD = FALSE;
  271. SOUND_PlayMUS(NUM_RIX_TITLE, TRUE, 2);
  272. }
  273. //
  274. // Clear all of the events and key states
  275. //
  276. PAL_ProcessEvent();
  277. PAL_ClearKeyState();
  278. dwBeginTime = SDL_GetTicks();
  279. srcrect.x = 0;
  280. srcrect.w = 320;
  281. dstrect.x = 0;
  282. dstrect.w = 320;
  283. while (TRUE)
  284. {
  285. PAL_ProcessEvent();
  286. dwTime = SDL_GetTicks() - dwBeginTime;
  287. //
  288. // Set the palette
  289. //
  290. if (dwTime < 15000)
  291. {
  292. for (i = 0; i < 256; i++)
  293. {
  294. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  295. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  296. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  297. }
  298. }
  299. VIDEO_SetPalette(rgCurrentPalette);
  300. #if SDL_VERSION_ATLEAST(2, 0, 0)
  301. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  302. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  303. #else
  304. SDL_SetPalette(lpBitmapDown, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  305. SDL_SetPalette(lpBitmapUp, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  306. #endif
  307. //
  308. // Draw the screen
  309. //
  310. if (iImgPos > 1)
  311. {
  312. iImgPos--;
  313. }
  314. //
  315. // The upper part...
  316. //
  317. srcrect.y = iImgPos;
  318. srcrect.h = 200 - iImgPos;
  319. dstrect.y = 0;
  320. dstrect.h = srcrect.h;
  321. SDL_BlitSurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
  322. //
  323. // The lower part...
  324. //
  325. srcrect.y = 0;
  326. srcrect.h = iImgPos;
  327. dstrect.y = 200 - iImgPos;
  328. dstrect.h = srcrect.h;
  329. SDL_BlitSurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
  330. //
  331. // Draw the cranes...
  332. //
  333. for (i = 0; i < 9; i++)
  334. {
  335. LPCBITMAPRLE lpFrame = PAL_SpriteGetFrame(lpSpriteCrane,
  336. cranepos[i][2] = (cranepos[i][2] + (iCraneFrame & 1)) % 8);
  337. cranepos[i][1] += ((iImgPos > 1) && (iImgPos & 1)) ? 1 : 0;
  338. PAL_RLEBlitToSurface(lpFrame, gpScreen,
  339. PAL_XY(cranepos[i][0], cranepos[i][1]));
  340. cranepos[i][0]--;
  341. }
  342. iCraneFrame++;
  343. //
  344. // Draw the title...
  345. //
  346. if (PAL_RLEGetHeight(lpBitmapTitle) < iTitleHeight)
  347. {
  348. //
  349. // HACKHACK
  350. //
  351. WORD w = lpBitmapTitle[2] | (lpBitmapTitle[3] << 8);
  352. w++;
  353. lpBitmapTitle[2] = (w & 0xFF);
  354. lpBitmapTitle[3] = (w >> 8);
  355. }
  356. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  357. VIDEO_UpdateScreen(NULL);
  358. //
  359. // Check for keypress...
  360. //
  361. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  362. {
  363. //
  364. // User has pressed a key...
  365. //
  366. lpBitmapTitle[2] = iTitleHeight & 0xFF;
  367. lpBitmapTitle[3] = iTitleHeight >> 8; // HACKHACK
  368. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  369. VIDEO_UpdateScreen(NULL);
  370. if (dwTime < 15000)
  371. {
  372. //
  373. // If the picture has not completed fading in, complete the rest
  374. //
  375. while (dwTime < 15000)
  376. {
  377. for (i = 0; i < 256; i++)
  378. {
  379. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  380. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  381. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  382. }
  383. VIDEO_SetPalette(rgCurrentPalette);
  384. #if SDL_VERSION_ATLEAST(2, 0, 0)
  385. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  386. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  387. #else
  388. SDL_SetPalette(lpBitmapDown, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  389. SDL_SetPalette(lpBitmapUp, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  390. #endif
  391. UTIL_Delay(8);
  392. dwTime += 250;
  393. }
  394. UTIL_Delay(500);
  395. }
  396. //
  397. // Quit the splash screen
  398. //
  399. break;
  400. }
  401. //
  402. // Delay a while...
  403. //
  404. PAL_ProcessEvent();
  405. while (SDL_GetTicks() - dwBeginTime < dwTime + 85)
  406. {
  407. SDL_Delay(1);
  408. PAL_ProcessEvent();
  409. }
  410. }
  411. SDL_FreeSurface(lpBitmapDown);
  412. SDL_FreeSurface(lpBitmapUp);
  413. free(buf);
  414. if (!fUseCD)
  415. {
  416. SOUND_PlayMUS(0, FALSE, 1);
  417. }
  418. PAL_FadeOut(1);
  419. }
  420. int
  421. main(
  422. int argc,
  423. char *argv[]
  424. )
  425. /*++
  426. Purpose:
  427. Program entry.
  428. Parameters:
  429. argc - Number of arguments.
  430. argv - Array of arguments.
  431. Return value:
  432. Integer value.
  433. --*/
  434. {
  435. WORD wScreenWidth = 0, wScreenHeight = 0;
  436. int c;
  437. BOOL fFullScreen = FALSE;
  438. #if defined(__APPLE__) && !defined(__IOS__)
  439. char *p = strstr(argv[0], "/Pal.app/");
  440. if (p != NULL)
  441. {
  442. char buf[4096];
  443. strcpy(buf, argv[0]);
  444. buf[p - argv[0]] = '\0';
  445. chdir(buf);
  446. }
  447. #endif
  448. #ifdef __WINPHONE__
  449. SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeRight");
  450. SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1");
  451. #endif
  452. UTIL_OpenLog();
  453. #ifdef _WIN32
  454. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  455. putenv("SDL_VIDEODRIVER=directx");
  456. #endif
  457. #endif
  458. #ifndef __SYMBIAN32__
  459. //
  460. // Parse parameters.
  461. //
  462. while ((c = getopt(argc, argv, "w:h:fjm")) != -1)
  463. {
  464. switch (c)
  465. {
  466. case 'w':
  467. //
  468. // Set the width of the screen
  469. //
  470. wScreenWidth = atoi(optarg);
  471. if (wScreenHeight == 0)
  472. {
  473. wScreenHeight = wScreenWidth * 200 / 320;
  474. }
  475. break;
  476. case 'h':
  477. //
  478. // Set the height of the screen
  479. //
  480. wScreenHeight = atoi(optarg);
  481. if (wScreenWidth == 0)
  482. {
  483. wScreenWidth = wScreenHeight * 320 / 200;
  484. }
  485. break;
  486. case 'f':
  487. //
  488. // Fullscreen Mode
  489. //
  490. fFullScreen = TRUE;
  491. break;
  492. case 'j':
  493. //
  494. // Disable joystick
  495. //
  496. g_fUseJoystick = FALSE;
  497. break;
  498. #if PAL_HAS_NATIVEMIDI
  499. case 'm':
  500. //
  501. // Use MIDI music
  502. //
  503. g_fUseMidi = TRUE;
  504. break;
  505. #endif
  506. }
  507. }
  508. #endif
  509. //
  510. // Default resolution is 640x400 (windowed) or 640x480 (fullscreen).
  511. //
  512. if (wScreenWidth == 0)
  513. {
  514. #ifdef __SYMBIAN32__
  515. #ifdef __S60_5X__
  516. wScreenWidth = 640;
  517. wScreenHeight = 360;
  518. #else
  519. wScreenWidth = 320;
  520. wScreenHeight = 240;
  521. #endif
  522. #else
  523. #if defined(GPH) || defined(DINGOO)
  524. wScreenWidth = 320;
  525. wScreenHeight = 240;
  526. #elif defined (__IOS__) || defined (__ANDROID__)
  527. wScreenWidth = 320;
  528. wScreenHeight = 200;
  529. #else
  530. wScreenWidth = 640;
  531. wScreenHeight = fFullScreen ? 480 : 400;
  532. #endif
  533. #endif
  534. }
  535. //
  536. // Initialize everything
  537. //
  538. #ifdef PSP
  539. sdlpal_psp_init();
  540. #endif
  541. PAL_Init(wScreenWidth, wScreenHeight, fFullScreen);
  542. #ifdef __WINPHONE__
  543. //
  544. // In windows phone, calling exit(0) directly will cause an abnormal exit.
  545. // By using setjmp/longjmp to avoid this.
  546. //
  547. if (setjmp(g_exit_jmp_env) == LONGJMP_EXIT_CODE) return 0;
  548. #endif
  549. //
  550. // Show the trademark screen and splash screen
  551. //
  552. PAL_TrademarkScreen();
  553. PAL_SplashScreen();
  554. //
  555. // Run the main game routine
  556. //
  557. PAL_GameMain();
  558. //
  559. // Should not really reach here...
  560. //
  561. assert(FALSE);
  562. return 255;
  563. }