main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. //
  2. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  3. // All rights reserved.
  4. //
  5. // This file is part of SDLPAL.
  6. //
  7. // SDLPAL is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. #include "main.h"
  21. #include "getopt.h"
  22. #ifdef PSP
  23. #include "main_PSP.h"
  24. #endif
  25. #if defined (NDS) && defined (GEKKO)
  26. #include <fat.h>
  27. #endif
  28. #define BITMAPNUM_SPLASH_UP 0x26
  29. #define BITMAPNUM_SPLASH_DOWN 0x27
  30. #define SPRITENUM_SPLASH_TITLE 0x47
  31. #define SPRITENUM_SPLASH_CRANE 0x49
  32. #define NUM_RIX_TITLE 0x5
  33. static VOID
  34. PAL_Init(
  35. WORD wScreenWidth,
  36. WORD wScreenHeight,
  37. BOOL fFullScreen
  38. )
  39. /*++
  40. Purpose:
  41. Initialize everything needed by the game.
  42. Parameters:
  43. [IN] wScreenWidth - width of the screen.
  44. [IN] wScreenHeight - height of the screen.
  45. [IN] fFullScreen - TRUE to use full screen mode, FALSE to use windowed mode.
  46. Return value:
  47. None.
  48. --*/
  49. {
  50. int e;
  51. #if defined (NDS) && defined (GEKKO)
  52. fatInitDefault();
  53. #endif
  54. //
  55. // Initialize defaults, video and audio
  56. //
  57. #if defined(DINGOO)
  58. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
  59. #else
  60. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK) == -1)
  61. #endif
  62. {
  63. #if defined (_WIN32) && SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  64. //
  65. // Try the WINDIB driver if DirectX failed.
  66. //
  67. putenv("SDL_VIDEODRIVER=windib");
  68. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK) == -1)
  69. {
  70. TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
  71. }
  72. #else
  73. TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
  74. #endif
  75. }
  76. //
  77. // Initialize subsystems.
  78. //
  79. #ifdef GEKKO
  80. e = VIDEO_Init_GEKKO(wScreenWidth, wScreenHeight, fFullScreen);
  81. #else
  82. e = VIDEO_Init(wScreenWidth, wScreenHeight, fFullScreen);
  83. #endif
  84. if (e != 0)
  85. {
  86. TerminateOnError("Could not initialize Video: %d.\n", e);
  87. }
  88. SDL_WM_SetCaption("Loading...", NULL);
  89. e = PAL_InitGlobals();
  90. if (e != 0)
  91. {
  92. TerminateOnError("Could not initialize global data: %d.\n", e);
  93. }
  94. e = PAL_InitFont();
  95. if (e != 0)
  96. {
  97. TerminateOnError("Could not load fonts: %d.\n", e);
  98. }
  99. e = PAL_InitUI();
  100. if (e != 0)
  101. {
  102. TerminateOnError("Could not initialize UI subsystem: %d.\n", e);
  103. }
  104. e = PAL_InitText();
  105. if (e != 0)
  106. {
  107. TerminateOnError("Could not initialize text subsystem: %d.\n", e);
  108. }
  109. PAL_InitInput();
  110. PAL_InitResources();
  111. SOUND_OpenAudio();
  112. #ifdef _DEBUG
  113. SDL_WM_SetCaption("Pal (Debug Build)", NULL);
  114. #else
  115. SDL_WM_SetCaption("Pal", NULL);
  116. #endif
  117. }
  118. VOID
  119. PAL_Shutdown(
  120. VOID
  121. )
  122. /*++
  123. Purpose:
  124. Free everything needed by the game.
  125. Parameters:
  126. None.
  127. Return value:
  128. None.
  129. --*/
  130. {
  131. SOUND_CloseAudio();
  132. PAL_FreeFont();
  133. PAL_FreeResources();
  134. PAL_FreeGlobals();
  135. PAL_FreeUI();
  136. PAL_FreeText();
  137. PAL_ShutdownInput();
  138. VIDEO_Shutdown();
  139. UTIL_CloseLog();
  140. SDL_Quit();
  141. #if defined(GPH)
  142. chdir("/usr/gp2x");
  143. execl("./gp2xmenu", "./gp2xmenu", NULL);
  144. #endif
  145. }
  146. VOID
  147. PAL_TrademarkScreen(
  148. VOID
  149. )
  150. /*++
  151. Purpose:
  152. Show the trademark screen.
  153. Parameters:
  154. None.
  155. Return value:
  156. None.
  157. --*/
  158. {
  159. PAL_SetPalette(3, FALSE);
  160. PAL_RNGPlay(6, 0, 1000, 25);
  161. UTIL_Delay(1000);
  162. PAL_FadeOut(1);
  163. }
  164. VOID
  165. PAL_SplashScreen(
  166. VOID
  167. )
  168. /*++
  169. Purpose:
  170. Show the splash screen.
  171. Parameters:
  172. None.
  173. Return value:
  174. None.
  175. --*/
  176. {
  177. SDL_Color *palette = PAL_GetPalette(1, FALSE);
  178. SDL_Color rgCurrentPalette[256];
  179. SDL_Surface *lpBitmapDown, *lpBitmapUp;
  180. SDL_Rect srcrect, dstrect;
  181. LPSPRITE lpSpriteCrane;
  182. LPBITMAPRLE lpBitmapTitle;
  183. LPBYTE buf, buf2;
  184. int cranepos[9][3], i, iImgPos = 200, iCraneFrame = 0, iTitleHeight;
  185. DWORD dwTime, dwBeginTime;
  186. BOOL fUseCD = TRUE;
  187. if (palette == NULL)
  188. {
  189. fprintf(stderr, "ERROR: PAL_SplashScreen(): palette == NULL\n");
  190. return;
  191. }
  192. //
  193. // Allocate all the needed memory at once for simplification
  194. //
  195. buf = (LPBYTE)UTIL_calloc(1, 320 * 200 * 2);
  196. buf2 = (LPBYTE)(buf + 320 * 200);
  197. lpSpriteCrane = (LPSPRITE)buf2 + 32000;
  198. //
  199. // Create the surfaces
  200. //
  201. lpBitmapDown = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  202. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  203. gpScreen->format->Amask);
  204. lpBitmapUp = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  205. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  206. gpScreen->format->Amask);
  207. //
  208. // Read the bitmaps
  209. //
  210. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_UP, gpGlobals->f.fpFBP);
  211. Decompress(buf, buf2, 320 * 200);
  212. PAL_FBPBlitToSurface(buf2, lpBitmapUp);
  213. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_DOWN, gpGlobals->f.fpFBP);
  214. Decompress(buf, buf2, 320 * 200);
  215. PAL_FBPBlitToSurface(buf2, lpBitmapDown);
  216. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_TITLE, gpGlobals->f.fpMGO);
  217. Decompress(buf, buf2, 32000);
  218. lpBitmapTitle = (LPBITMAPRLE)PAL_SpriteGetFrame(buf2, 0);
  219. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_CRANE, gpGlobals->f.fpMGO);
  220. Decompress(buf, lpSpriteCrane, 32000);
  221. iTitleHeight = PAL_RLEGetHeight(lpBitmapTitle);
  222. lpBitmapTitle[2] = 0;
  223. lpBitmapTitle[3] = 0; // HACKHACK
  224. //
  225. // Generate the positions of the cranes
  226. //
  227. for (i = 0; i < 9; i++)
  228. {
  229. cranepos[i][0] = RandomLong(300, 600);
  230. cranepos[i][1] = RandomLong(0, 80);
  231. cranepos[i][2] = RandomLong(0, 8);
  232. }
  233. //
  234. // Play the title music
  235. //
  236. if (!SOUND_PlayCDA(7))
  237. {
  238. fUseCD = FALSE;
  239. PAL_PlayMUS(NUM_RIX_TITLE, TRUE, 2);
  240. }
  241. //
  242. // Clear all of the events and key states
  243. //
  244. PAL_ProcessEvent();
  245. PAL_ClearKeyState();
  246. dwBeginTime = SDL_GetTicks();
  247. srcrect.x = 0;
  248. srcrect.w = 320;
  249. dstrect.x = 0;
  250. dstrect.w = 320;
  251. while (TRUE)
  252. {
  253. PAL_ProcessEvent();
  254. dwTime = SDL_GetTicks() - dwBeginTime;
  255. //
  256. // Set the palette
  257. //
  258. if (dwTime < 15000)
  259. {
  260. for (i = 0; i < 256; i++)
  261. {
  262. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  263. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  264. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  265. }
  266. }
  267. VIDEO_SetPalette(rgCurrentPalette);
  268. //
  269. // Draw the screen
  270. //
  271. if (iImgPos > 1)
  272. {
  273. iImgPos--;
  274. }
  275. //
  276. // The upper part...
  277. //
  278. srcrect.y = iImgPos;
  279. srcrect.h = 200 - iImgPos;
  280. dstrect.y = 0;
  281. dstrect.h = srcrect.h;
  282. SDL_BlitSurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
  283. //
  284. // The lower part...
  285. //
  286. srcrect.y = 0;
  287. srcrect.h = iImgPos;
  288. dstrect.y = 200 - iImgPos;
  289. dstrect.h = srcrect.h;
  290. SDL_BlitSurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
  291. //
  292. // Draw the cranes...
  293. //
  294. for (i = 0; i < 9; i++)
  295. {
  296. LPCBITMAPRLE lpFrame = PAL_SpriteGetFrame(lpSpriteCrane,
  297. cranepos[i][2] = (cranepos[i][2] + (iCraneFrame & 1)) % 8);
  298. cranepos[i][1] += ((iImgPos > 1) && (iImgPos & 1)) ? 1 : 0;
  299. PAL_RLEBlitToSurface(lpFrame, gpScreen,
  300. PAL_XY(cranepos[i][0], cranepos[i][1]));
  301. cranepos[i][0]--;
  302. }
  303. iCraneFrame++;
  304. //
  305. // Draw the title...
  306. //
  307. if (PAL_RLEGetHeight(lpBitmapTitle) < iTitleHeight)
  308. {
  309. //
  310. // HACKHACK
  311. //
  312. WORD w = lpBitmapTitle[2] | (lpBitmapTitle[3] << 8);
  313. w++;
  314. lpBitmapTitle[2] = (w & 0xFF);
  315. lpBitmapTitle[3] = (w >> 8);
  316. }
  317. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  318. VIDEO_UpdateScreen(NULL);
  319. //
  320. // Check for keypress...
  321. //
  322. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  323. {
  324. //
  325. // User has pressed a key...
  326. //
  327. lpBitmapTitle[2] = iTitleHeight & 0xFF;
  328. lpBitmapTitle[3] = iTitleHeight >> 8; // HACKHACK
  329. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  330. VIDEO_UpdateScreen(NULL);
  331. if (dwTime < 15000)
  332. {
  333. //
  334. // If the picture has not completed fading in, complete the rest
  335. //
  336. while (dwTime < 15000)
  337. {
  338. for (i = 0; i < 256; i++)
  339. {
  340. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  341. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  342. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  343. }
  344. VIDEO_SetPalette(rgCurrentPalette);
  345. UTIL_Delay(8);
  346. dwTime += 250;
  347. }
  348. UTIL_Delay(500);
  349. }
  350. //
  351. // Quit the splash screen
  352. //
  353. break;
  354. }
  355. //
  356. // Delay a while...
  357. //
  358. PAL_ProcessEvent();
  359. while (SDL_GetTicks() - dwBeginTime < dwTime + 85)
  360. {
  361. SDL_Delay(1);
  362. PAL_ProcessEvent();
  363. }
  364. }
  365. SDL_FreeSurface(lpBitmapDown);
  366. SDL_FreeSurface(lpBitmapUp);
  367. free(buf);
  368. if (!fUseCD)
  369. {
  370. PAL_PlayMUS(0, FALSE, 1);
  371. }
  372. PAL_FadeOut(1);
  373. }
  374. int
  375. main(
  376. int argc,
  377. char *argv[]
  378. )
  379. /*++
  380. Purpose:
  381. Program entry.
  382. Parameters:
  383. argc - Number of arguments.
  384. argv - Array of arguments.
  385. Return value:
  386. Integer value.
  387. --*/
  388. {
  389. WORD wScreenWidth = 0, wScreenHeight = 0;
  390. int c;
  391. BOOL fFullScreen = FALSE;
  392. UTIL_OpenLog();
  393. #ifdef _WIN32
  394. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  395. putenv("SDL_VIDEODRIVER=directx");
  396. #else
  397. putenv("SDL_VIDEODRIVER=win32");
  398. #endif
  399. #endif
  400. #ifndef __SYMBIAN32__
  401. //
  402. // Parse parameters.
  403. //
  404. while ((c = getopt(argc, argv, "w:h:fjm")) != -1)
  405. {
  406. switch (c)
  407. {
  408. case 'w':
  409. //
  410. // Set the width of the screen
  411. //
  412. wScreenWidth = atoi(optarg);
  413. if (wScreenHeight == 0)
  414. {
  415. wScreenHeight = wScreenWidth * 200 / 320;
  416. }
  417. break;
  418. case 'h':
  419. //
  420. // Set the height of the screen
  421. //
  422. wScreenHeight = atoi(optarg);
  423. if (wScreenWidth == 0)
  424. {
  425. wScreenWidth = wScreenHeight * 320 / 200;
  426. }
  427. break;
  428. case 'f':
  429. //
  430. // Fullscreen Mode
  431. //
  432. fFullScreen = TRUE;
  433. break;
  434. case 'j':
  435. //
  436. // Disable joystick
  437. //
  438. g_fUseJoystick = FALSE;
  439. break;
  440. #ifdef PAL_HAS_NATIVEMIDI
  441. case 'm':
  442. //
  443. // Use MIDI music
  444. //
  445. g_fUseMidi = TRUE;
  446. break;
  447. #endif
  448. }
  449. }
  450. #endif
  451. //
  452. // Default resolution is 640x400 (windowed) or 640x480 (fullscreen).
  453. //
  454. if (wScreenWidth == 0)
  455. {
  456. #ifdef __SYMBIAN32__
  457. #ifdef __S60_5X__
  458. wScreenWidth = 640;
  459. wScreenHeight = 360;
  460. #else
  461. wScreenWidth = 320;
  462. wScreenHeight = 240;
  463. #endif
  464. #else
  465. #if defined(GPH) || defined(DINGOO)
  466. wScreenWidth = 320;
  467. wScreenHeight = 240;
  468. #else
  469. wScreenWidth = 640;
  470. wScreenHeight = fFullScreen ? 480 : 400;
  471. #endif
  472. #endif
  473. }
  474. //
  475. // Initialize everything
  476. //
  477. #ifdef PSP
  478. sdlpal_psp_init();
  479. #endif
  480. PAL_Init(wScreenWidth, wScreenHeight, fFullScreen);
  481. //
  482. // Show the trademark screen and splash screen
  483. //
  484. PAL_TrademarkScreen();
  485. PAL_SplashScreen();
  486. //
  487. // Run the main game routine
  488. //
  489. PAL_GameMain();
  490. //
  491. // Should not really reach here...
  492. //
  493. assert(FALSE);
  494. return 255;
  495. }