main.c 14 KB

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