main.c 13 KB

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