main.c 14 KB

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