main.c 14 KB

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