main.c 13 KB

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