main.c 14 KB

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