main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. #endif
  214. //
  215. // Read the bitmaps
  216. //
  217. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_UP, gpGlobals->f.fpFBP);
  218. Decompress(buf, buf2, 320 * 200);
  219. PAL_FBPBlitToSurface(buf2, lpBitmapUp);
  220. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_DOWN, gpGlobals->f.fpFBP);
  221. Decompress(buf, buf2, 320 * 200);
  222. PAL_FBPBlitToSurface(buf2, lpBitmapDown);
  223. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_TITLE, gpGlobals->f.fpMGO);
  224. Decompress(buf, buf2, 32000);
  225. lpBitmapTitle = (LPBITMAPRLE)PAL_SpriteGetFrame(buf2, 0);
  226. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_CRANE, gpGlobals->f.fpMGO);
  227. Decompress(buf, lpSpriteCrane, 32000);
  228. iTitleHeight = PAL_RLEGetHeight(lpBitmapTitle);
  229. lpBitmapTitle[2] = 0;
  230. lpBitmapTitle[3] = 0; // HACKHACK
  231. //
  232. // Generate the positions of the cranes
  233. //
  234. for (i = 0; i < 9; i++)
  235. {
  236. cranepos[i][0] = RandomLong(300, 600);
  237. cranepos[i][1] = RandomLong(0, 80);
  238. cranepos[i][2] = RandomLong(0, 8);
  239. }
  240. //
  241. // Play the title music
  242. //
  243. if (!SOUND_PlayCDA(7))
  244. {
  245. fUseCD = FALSE;
  246. PAL_PlayMUS(NUM_RIX_TITLE, TRUE, 2);
  247. }
  248. //
  249. // Clear all of the events and key states
  250. //
  251. PAL_ProcessEvent();
  252. PAL_ClearKeyState();
  253. dwBeginTime = SDL_GetTicks();
  254. srcrect.x = 0;
  255. srcrect.w = 320;
  256. dstrect.x = 0;
  257. dstrect.w = 320;
  258. while (TRUE)
  259. {
  260. PAL_ProcessEvent();
  261. dwTime = SDL_GetTicks() - dwBeginTime;
  262. //
  263. // Set the palette
  264. //
  265. if (dwTime < 15000)
  266. {
  267. for (i = 0; i < 256; i++)
  268. {
  269. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  270. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  271. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  272. }
  273. }
  274. VIDEO_SetPalette(rgCurrentPalette);
  275. #if SDL_VERSION_ATLEAST(2, 0, 0)
  276. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  277. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  278. #endif
  279. //
  280. // Draw the screen
  281. //
  282. if (iImgPos > 1)
  283. {
  284. iImgPos--;
  285. }
  286. //
  287. // The upper part...
  288. //
  289. srcrect.y = iImgPos;
  290. srcrect.h = 200 - iImgPos;
  291. dstrect.y = 0;
  292. dstrect.h = srcrect.h;
  293. SDL_BlitSurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
  294. //
  295. // The lower part...
  296. //
  297. srcrect.y = 0;
  298. srcrect.h = iImgPos;
  299. dstrect.y = 200 - iImgPos;
  300. dstrect.h = srcrect.h;
  301. SDL_BlitSurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
  302. //
  303. // Draw the cranes...
  304. //
  305. for (i = 0; i < 9; i++)
  306. {
  307. LPCBITMAPRLE lpFrame = PAL_SpriteGetFrame(lpSpriteCrane,
  308. cranepos[i][2] = (cranepos[i][2] + (iCraneFrame & 1)) % 8);
  309. cranepos[i][1] += ((iImgPos > 1) && (iImgPos & 1)) ? 1 : 0;
  310. PAL_RLEBlitToSurface(lpFrame, gpScreen,
  311. PAL_XY(cranepos[i][0], cranepos[i][1]));
  312. cranepos[i][0]--;
  313. }
  314. iCraneFrame++;
  315. //
  316. // Draw the title...
  317. //
  318. if (PAL_RLEGetHeight(lpBitmapTitle) < iTitleHeight)
  319. {
  320. //
  321. // HACKHACK
  322. //
  323. WORD w = lpBitmapTitle[2] | (lpBitmapTitle[3] << 8);
  324. w++;
  325. lpBitmapTitle[2] = (w & 0xFF);
  326. lpBitmapTitle[3] = (w >> 8);
  327. }
  328. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  329. VIDEO_UpdateScreen(NULL);
  330. //
  331. // Check for keypress...
  332. //
  333. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  334. {
  335. //
  336. // User has pressed a key...
  337. //
  338. lpBitmapTitle[2] = iTitleHeight & 0xFF;
  339. lpBitmapTitle[3] = iTitleHeight >> 8; // HACKHACK
  340. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  341. VIDEO_UpdateScreen(NULL);
  342. if (dwTime < 15000)
  343. {
  344. //
  345. // If the picture has not completed fading in, complete the rest
  346. //
  347. while (dwTime < 15000)
  348. {
  349. for (i = 0; i < 256; i++)
  350. {
  351. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  352. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  353. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  354. }
  355. VIDEO_SetPalette(rgCurrentPalette);
  356. #if SDL_VERSION_ATLEAST(2, 0, 0)
  357. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  358. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  359. #endif
  360. UTIL_Delay(8);
  361. dwTime += 250;
  362. }
  363. UTIL_Delay(500);
  364. }
  365. //
  366. // Quit the splash screen
  367. //
  368. break;
  369. }
  370. //
  371. // Delay a while...
  372. //
  373. PAL_ProcessEvent();
  374. while (SDL_GetTicks() - dwBeginTime < dwTime + 85)
  375. {
  376. SDL_Delay(1);
  377. PAL_ProcessEvent();
  378. }
  379. }
  380. SDL_FreeSurface(lpBitmapDown);
  381. SDL_FreeSurface(lpBitmapUp);
  382. free(buf);
  383. if (!fUseCD)
  384. {
  385. PAL_PlayMUS(0, FALSE, 1);
  386. }
  387. PAL_FadeOut(1);
  388. }
  389. int
  390. main(
  391. int argc,
  392. char *argv[]
  393. )
  394. /*++
  395. Purpose:
  396. Program entry.
  397. Parameters:
  398. argc - Number of arguments.
  399. argv - Array of arguments.
  400. Return value:
  401. Integer value.
  402. --*/
  403. {
  404. WORD wScreenWidth = 0, wScreenHeight = 0;
  405. int c;
  406. BOOL fFullScreen = FALSE;
  407. #if defined(__APPLE__) && !defined(__IOS__)
  408. char *p = strstr(argv[0], "/Pal.app/");
  409. if (p != NULL)
  410. {
  411. char buf[4096];
  412. strcpy(buf, argv[0]);
  413. buf[p - argv[0]] = '\0';
  414. chdir(buf);
  415. }
  416. #endif
  417. UTIL_OpenLog();
  418. #ifdef _WIN32
  419. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  420. putenv("SDL_VIDEODRIVER=directx");
  421. #endif
  422. #endif
  423. #ifndef __SYMBIAN32__
  424. //
  425. // Parse parameters.
  426. //
  427. while ((c = getopt(argc, argv, "w:h:fjm")) != -1)
  428. {
  429. switch (c)
  430. {
  431. case 'w':
  432. //
  433. // Set the width of the screen
  434. //
  435. wScreenWidth = atoi(optarg);
  436. if (wScreenHeight == 0)
  437. {
  438. wScreenHeight = wScreenWidth * 200 / 320;
  439. }
  440. break;
  441. case 'h':
  442. //
  443. // Set the height of the screen
  444. //
  445. wScreenHeight = atoi(optarg);
  446. if (wScreenWidth == 0)
  447. {
  448. wScreenWidth = wScreenHeight * 320 / 200;
  449. }
  450. break;
  451. case 'f':
  452. //
  453. // Fullscreen Mode
  454. //
  455. fFullScreen = TRUE;
  456. break;
  457. case 'j':
  458. //
  459. // Disable joystick
  460. //
  461. g_fUseJoystick = FALSE;
  462. break;
  463. #ifdef PAL_HAS_NATIVEMIDI
  464. case 'm':
  465. //
  466. // Use MIDI music
  467. //
  468. g_fUseMidi = TRUE;
  469. break;
  470. #endif
  471. }
  472. }
  473. #endif
  474. //
  475. // Default resolution is 640x400 (windowed) or 640x480 (fullscreen).
  476. //
  477. if (wScreenWidth == 0)
  478. {
  479. #ifdef __SYMBIAN32__
  480. #ifdef __S60_5X__
  481. wScreenWidth = 640;
  482. wScreenHeight = 360;
  483. #else
  484. wScreenWidth = 320;
  485. wScreenHeight = 240;
  486. #endif
  487. #else
  488. #if defined(GPH) || defined(DINGOO)
  489. wScreenWidth = 320;
  490. wScreenHeight = 240;
  491. #elif defined (__IOS__) || defined (__ANDROID__)
  492. wScreenWidth = 320;
  493. wScreenHeight = 200;
  494. #else
  495. wScreenWidth = 640;
  496. wScreenHeight = fFullScreen ? 480 : 400;
  497. #endif
  498. #endif
  499. }
  500. //
  501. // Initialize everything
  502. //
  503. #ifdef PSP
  504. sdlpal_psp_init();
  505. #endif
  506. PAL_Init(wScreenWidth, wScreenHeight, fFullScreen);
  507. //
  508. // Show the trademark screen and splash screen
  509. //
  510. PAL_TrademarkScreen();
  511. PAL_SplashScreen();
  512. //
  513. // Run the main game routine
  514. //
  515. PAL_GameMain();
  516. //
  517. // Should not really reach here...
  518. //
  519. assert(FALSE);
  520. return 255;
  521. }