main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. #else
  61. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK) == -1)
  62. #endif
  63. {
  64. #if defined (_WIN32) && SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  65. //
  66. // Try the WINDIB driver if DirectX failed.
  67. //
  68. putenv("SDL_VIDEODRIVER=windib");
  69. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK) == -1)
  70. {
  71. TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
  72. }
  73. #else
  74. TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
  75. #endif
  76. }
  77. //
  78. // Initialize subsystems.
  79. //
  80. #ifdef GEKKO
  81. e = VIDEO_Init_GEKKO(wScreenWidth, wScreenHeight, fFullScreen);
  82. #else
  83. e = VIDEO_Init(wScreenWidth, wScreenHeight, fFullScreen);
  84. #endif
  85. if (e != 0)
  86. {
  87. TerminateOnError("Could not initialize Video: %d.\n", e);
  88. }
  89. SDL_WM_SetCaption("Loading...", NULL);
  90. e = PAL_InitGlobals();
  91. if (e != 0)
  92. {
  93. TerminateOnError("Could not initialize global data: %d.\n", e);
  94. }
  95. e = PAL_InitFont();
  96. if (e != 0)
  97. {
  98. TerminateOnError("Could not load fonts: %d.\n", e);
  99. }
  100. e = PAL_InitUI();
  101. if (e != 0)
  102. {
  103. TerminateOnError("Could not initialize UI subsystem: %d.\n", e);
  104. }
  105. e = PAL_InitText();
  106. if (e != 0)
  107. {
  108. TerminateOnError("Could not initialize text subsystem: %d.\n", e);
  109. }
  110. PAL_InitInput();
  111. PAL_InitResources();
  112. SOUND_OpenAudio();
  113. #ifdef _DEBUG
  114. SDL_WM_SetCaption("Pal (Debug Build)", NULL);
  115. #else
  116. SDL_WM_SetCaption("Pal", NULL);
  117. #endif
  118. }
  119. VOID
  120. PAL_Shutdown(
  121. VOID
  122. )
  123. /*++
  124. Purpose:
  125. Free everything needed by the game.
  126. Parameters:
  127. None.
  128. Return value:
  129. None.
  130. --*/
  131. {
  132. SOUND_CloseAudio();
  133. PAL_FreeFont();
  134. PAL_FreeResources();
  135. PAL_FreeGlobals();
  136. PAL_FreeUI();
  137. PAL_FreeText();
  138. PAL_ShutdownInput();
  139. VIDEO_Shutdown();
  140. UTIL_CloseLog();
  141. SDL_Quit();
  142. #if defined(GPH)
  143. chdir("/usr/gp2x");
  144. execl("./gp2xmenu", "./gp2xmenu", NULL);
  145. #endif
  146. }
  147. VOID
  148. PAL_TrademarkScreen(
  149. VOID
  150. )
  151. /*++
  152. Purpose:
  153. Show the trademark screen.
  154. Parameters:
  155. None.
  156. Return value:
  157. None.
  158. --*/
  159. {
  160. PAL_SetPalette(3, FALSE);
  161. PAL_RNGPlay(6, 0, 1000, 25);
  162. UTIL_Delay(1000);
  163. PAL_FadeOut(1);
  164. }
  165. VOID
  166. PAL_SplashScreen(
  167. VOID
  168. )
  169. /*++
  170. Purpose:
  171. Show the splash screen.
  172. Parameters:
  173. None.
  174. Return value:
  175. None.
  176. --*/
  177. {
  178. SDL_Color *palette = PAL_GetPalette(1, FALSE);
  179. SDL_Color rgCurrentPalette[256];
  180. SDL_Surface *lpBitmapDown, *lpBitmapUp;
  181. SDL_Rect srcrect, dstrect;
  182. LPSPRITE lpSpriteCrane;
  183. LPBITMAPRLE lpBitmapTitle;
  184. LPBYTE buf, buf2;
  185. int cranepos[9][3], i, iImgPos = 200, iCraneFrame = 0, iTitleHeight;
  186. DWORD dwTime, dwBeginTime;
  187. BOOL fUseCD = TRUE;
  188. if (palette == NULL)
  189. {
  190. fprintf(stderr, "ERROR: PAL_SplashScreen(): palette == NULL\n");
  191. return;
  192. }
  193. //
  194. // Allocate all the needed memory at once for simplification
  195. //
  196. buf = (LPBYTE)UTIL_calloc(1, 320 * 200 * 2);
  197. buf2 = (LPBYTE)(buf + 320 * 200);
  198. lpSpriteCrane = (LPSPRITE)buf2 + 32000;
  199. //
  200. // Create the surfaces
  201. //
  202. lpBitmapDown = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  203. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  204. gpScreen->format->Amask);
  205. lpBitmapUp = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  206. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  207. gpScreen->format->Amask);
  208. #if SDL_VERSION_ATLEAST(2, 0, 0)
  209. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  210. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  211. #endif
  212. //
  213. // Read the bitmaps
  214. //
  215. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_UP, gpGlobals->f.fpFBP);
  216. Decompress(buf, buf2, 320 * 200);
  217. PAL_FBPBlitToSurface(buf2, lpBitmapUp);
  218. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_DOWN, gpGlobals->f.fpFBP);
  219. Decompress(buf, buf2, 320 * 200);
  220. PAL_FBPBlitToSurface(buf2, lpBitmapDown);
  221. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_TITLE, gpGlobals->f.fpMGO);
  222. Decompress(buf, buf2, 32000);
  223. lpBitmapTitle = (LPBITMAPRLE)PAL_SpriteGetFrame(buf2, 0);
  224. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_CRANE, gpGlobals->f.fpMGO);
  225. Decompress(buf, lpSpriteCrane, 32000);
  226. iTitleHeight = PAL_RLEGetHeight(lpBitmapTitle);
  227. lpBitmapTitle[2] = 0;
  228. lpBitmapTitle[3] = 0; // HACKHACK
  229. //
  230. // Generate the positions of the cranes
  231. //
  232. for (i = 0; i < 9; i++)
  233. {
  234. cranepos[i][0] = RandomLong(300, 600);
  235. cranepos[i][1] = RandomLong(0, 80);
  236. cranepos[i][2] = RandomLong(0, 8);
  237. }
  238. //
  239. // Play the title music
  240. //
  241. if (!SOUND_PlayCDA(7))
  242. {
  243. fUseCD = FALSE;
  244. PAL_PlayMUS(NUM_RIX_TITLE, TRUE, 2);
  245. }
  246. //
  247. // Clear all of the events and key states
  248. //
  249. PAL_ProcessEvent();
  250. PAL_ClearKeyState();
  251. dwBeginTime = SDL_GetTicks();
  252. srcrect.x = 0;
  253. srcrect.w = 320;
  254. dstrect.x = 0;
  255. dstrect.w = 320;
  256. while (TRUE)
  257. {
  258. PAL_ProcessEvent();
  259. dwTime = SDL_GetTicks() - dwBeginTime;
  260. //
  261. // Set the palette
  262. //
  263. if (dwTime < 15000)
  264. {
  265. for (i = 0; i < 256; i++)
  266. {
  267. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  268. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  269. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  270. }
  271. }
  272. VIDEO_SetPalette(rgCurrentPalette);
  273. //
  274. // Draw the screen
  275. //
  276. if (iImgPos > 1)
  277. {
  278. iImgPos--;
  279. }
  280. //
  281. // The upper part...
  282. //
  283. srcrect.y = iImgPos;
  284. srcrect.h = 200 - iImgPos;
  285. dstrect.y = 0;
  286. dstrect.h = srcrect.h;
  287. SDL_BlitSurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
  288. //
  289. // The lower part...
  290. //
  291. srcrect.y = 0;
  292. srcrect.h = iImgPos;
  293. dstrect.y = 200 - iImgPos;
  294. dstrect.h = srcrect.h;
  295. SDL_BlitSurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
  296. //
  297. // Draw the cranes...
  298. //
  299. for (i = 0; i < 9; i++)
  300. {
  301. LPCBITMAPRLE lpFrame = PAL_SpriteGetFrame(lpSpriteCrane,
  302. cranepos[i][2] = (cranepos[i][2] + (iCraneFrame & 1)) % 8);
  303. cranepos[i][1] += ((iImgPos > 1) && (iImgPos & 1)) ? 1 : 0;
  304. PAL_RLEBlitToSurface(lpFrame, gpScreen,
  305. PAL_XY(cranepos[i][0], cranepos[i][1]));
  306. cranepos[i][0]--;
  307. }
  308. iCraneFrame++;
  309. //
  310. // Draw the title...
  311. //
  312. if (PAL_RLEGetHeight(lpBitmapTitle) < iTitleHeight)
  313. {
  314. //
  315. // HACKHACK
  316. //
  317. WORD w = lpBitmapTitle[2] | (lpBitmapTitle[3] << 8);
  318. w++;
  319. lpBitmapTitle[2] = (w & 0xFF);
  320. lpBitmapTitle[3] = (w >> 8);
  321. }
  322. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  323. VIDEO_UpdateScreen(NULL);
  324. //
  325. // Check for keypress...
  326. //
  327. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  328. {
  329. //
  330. // User has pressed a key...
  331. //
  332. lpBitmapTitle[2] = iTitleHeight & 0xFF;
  333. lpBitmapTitle[3] = iTitleHeight >> 8; // HACKHACK
  334. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  335. VIDEO_UpdateScreen(NULL);
  336. if (dwTime < 15000)
  337. {
  338. //
  339. // If the picture has not completed fading in, complete the rest
  340. //
  341. while (dwTime < 15000)
  342. {
  343. for (i = 0; i < 256; i++)
  344. {
  345. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  346. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  347. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  348. }
  349. VIDEO_SetPalette(rgCurrentPalette);
  350. UTIL_Delay(8);
  351. dwTime += 250;
  352. }
  353. UTIL_Delay(500);
  354. }
  355. //
  356. // Quit the splash screen
  357. //
  358. break;
  359. }
  360. //
  361. // Delay a while...
  362. //
  363. PAL_ProcessEvent();
  364. while (SDL_GetTicks() - dwBeginTime < dwTime + 85)
  365. {
  366. SDL_Delay(1);
  367. PAL_ProcessEvent();
  368. }
  369. }
  370. SDL_FreeSurface(lpBitmapDown);
  371. SDL_FreeSurface(lpBitmapUp);
  372. free(buf);
  373. if (!fUseCD)
  374. {
  375. PAL_PlayMUS(0, FALSE, 1);
  376. }
  377. PAL_FadeOut(1);
  378. }
  379. int
  380. main(
  381. int argc,
  382. char *argv[]
  383. )
  384. /*++
  385. Purpose:
  386. Program entry.
  387. Parameters:
  388. argc - Number of arguments.
  389. argv - Array of arguments.
  390. Return value:
  391. Integer value.
  392. --*/
  393. {
  394. WORD wScreenWidth = 0, wScreenHeight = 0;
  395. int c;
  396. BOOL fFullScreen = FALSE;
  397. #if defined(__APPLE__) && !defined(__IOS__)
  398. char *p = strstr(argv[0], "/Pal.app/");
  399. if (p != NULL)
  400. {
  401. char buf[4096];
  402. strcpy(buf, argv[0]);
  403. buf[p - argv[0]] = '\0';
  404. chdir(buf);
  405. }
  406. #endif
  407. UTIL_OpenLog();
  408. #ifdef _WIN32
  409. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  410. putenv("SDL_VIDEODRIVER=directx");
  411. #else
  412. putenv("SDL_VIDEODRIVER=windows");
  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. #else
  484. wScreenWidth = 640;
  485. wScreenHeight = fFullScreen ? 480 : 400;
  486. #endif
  487. #endif
  488. }
  489. //
  490. // Initialize everything
  491. //
  492. #ifdef PSP
  493. sdlpal_psp_init();
  494. #endif
  495. PAL_Init(wScreenWidth, wScreenHeight, fFullScreen);
  496. //
  497. // Show the trademark screen and splash screen
  498. //
  499. PAL_TrademarkScreen();
  500. PAL_SplashScreen();
  501. //
  502. // Run the main game routine
  503. //
  504. PAL_GameMain();
  505. //
  506. // Should not really reach here...
  507. //
  508. assert(FALSE);
  509. return 255;
  510. }