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. #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. SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1");
  445. #endif
  446. UTIL_OpenLog();
  447. #ifdef _WIN32
  448. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  449. putenv("SDL_VIDEODRIVER=directx");
  450. #endif
  451. #endif
  452. #ifndef __SYMBIAN32__
  453. //
  454. // Parse parameters.
  455. //
  456. while ((c = getopt(argc, argv, "w:h:fjm")) != -1)
  457. {
  458. switch (c)
  459. {
  460. case 'w':
  461. //
  462. // Set the width of the screen
  463. //
  464. wScreenWidth = atoi(optarg);
  465. if (wScreenHeight == 0)
  466. {
  467. wScreenHeight = wScreenWidth * 200 / 320;
  468. }
  469. break;
  470. case 'h':
  471. //
  472. // Set the height of the screen
  473. //
  474. wScreenHeight = atoi(optarg);
  475. if (wScreenWidth == 0)
  476. {
  477. wScreenWidth = wScreenHeight * 320 / 200;
  478. }
  479. break;
  480. case 'f':
  481. //
  482. // Fullscreen Mode
  483. //
  484. fFullScreen = TRUE;
  485. break;
  486. case 'j':
  487. //
  488. // Disable joystick
  489. //
  490. g_fUseJoystick = FALSE;
  491. break;
  492. #ifdef PAL_HAS_NATIVEMIDI
  493. case 'm':
  494. //
  495. // Use MIDI music
  496. //
  497. g_fUseMidi = TRUE;
  498. break;
  499. #endif
  500. }
  501. }
  502. #endif
  503. //
  504. // Default resolution is 640x400 (windowed) or 640x480 (fullscreen).
  505. //
  506. if (wScreenWidth == 0)
  507. {
  508. #ifdef __SYMBIAN32__
  509. #ifdef __S60_5X__
  510. wScreenWidth = 640;
  511. wScreenHeight = 360;
  512. #else
  513. wScreenWidth = 320;
  514. wScreenHeight = 240;
  515. #endif
  516. #else
  517. #if defined(GPH) || defined(DINGOO)
  518. wScreenWidth = 320;
  519. wScreenHeight = 240;
  520. #elif defined (__IOS__) || defined (__ANDROID__)
  521. wScreenWidth = 320;
  522. wScreenHeight = 200;
  523. #else
  524. wScreenWidth = 640;
  525. wScreenHeight = fFullScreen ? 480 : 400;
  526. #endif
  527. #endif
  528. }
  529. //
  530. // Initialize everything
  531. //
  532. #ifdef PSP
  533. sdlpal_psp_init();
  534. #endif
  535. PAL_Init(wScreenWidth, wScreenHeight, fFullScreen);
  536. //
  537. // Show the trademark screen and splash screen
  538. //
  539. PAL_TrademarkScreen();
  540. PAL_SplashScreen();
  541. //
  542. // Run the main game routine
  543. //
  544. PAL_GameMain();
  545. //
  546. // Should not really reach here...
  547. //
  548. assert(FALSE);
  549. return 255;
  550. }