main.c 14 KB

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