main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  22. //
  23. #include "main.h"
  24. #ifdef PSP
  25. #include "main_PSP.h"
  26. #endif
  27. #if defined (NDS) && defined (GEKKO)
  28. #include <fat.h>
  29. #endif
  30. #ifdef __WINPHONE__
  31. #include <setjmp.h>
  32. static jmp_buf g_exit_jmp_env;
  33. # define LONGJMP_EXIT_CODE 0xff
  34. #endif
  35. #define BITMAPNUM_SPLASH_UP (gpGlobals->fIsWIN95 ? 0x03 : 0x26)
  36. #define BITMAPNUM_SPLASH_DOWN (gpGlobals->fIsWIN95 ? 0x04 : 0x27)
  37. #define SPRITENUM_SPLASH_TITLE 0x47
  38. #define SPRITENUM_SPLASH_CRANE 0x49
  39. #define NUM_RIX_TITLE 0x05
  40. static VOID
  41. PAL_Init(
  42. VOID
  43. )
  44. /*++
  45. Purpose:
  46. Initialize everything needed by the game.
  47. Parameters:
  48. None.
  49. Return value:
  50. None.
  51. --*/
  52. {
  53. int e;
  54. #if defined (NDS) && defined (GEKKO)
  55. fatInitDefault();
  56. #endif
  57. //
  58. // Initialize defaults, video and audio
  59. //
  60. #if defined(DINGOO)
  61. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
  62. #elif defined (__WINPHONE__)
  63. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1)
  64. #else
  65. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK) == -1)
  66. #endif
  67. {
  68. #if defined (_WIN32) && SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  69. //
  70. // Try the WINDIB driver if DirectX failed.
  71. //
  72. putenv("SDL_VIDEODRIVER=windib");
  73. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK) == -1)
  74. {
  75. TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
  76. }
  77. #else
  78. TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
  79. #endif
  80. }
  81. //
  82. // Initialize subsystems.
  83. //
  84. e = PAL_InitGlobals();
  85. if (e != 0)
  86. {
  87. TerminateOnError("Could not initialize global data: %d.\n", e);
  88. }
  89. e = VIDEO_Startup();
  90. if (e != 0)
  91. {
  92. TerminateOnError("Could not initialize Video: %d.\n", e);
  93. }
  94. SDL_WM_SetCaption("Loading...", NULL);
  95. e = PAL_InitFont(gpGlobals->fUseEmbeddedFonts);
  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. if (gpGlobals->fIsWIN95)
  114. {
  115. #ifdef _DEBUG
  116. SDL_WM_SetCaption("Pal WIN95 (Debug Build)", NULL);
  117. #else
  118. SDL_WM_SetCaption("Pal WIN95", NULL);
  119. #endif
  120. }
  121. else
  122. {
  123. #ifdef _DEBUG
  124. SDL_WM_SetCaption("Pal (Debug Build)", NULL);
  125. #else
  126. SDL_WM_SetCaption("Pal", NULL);
  127. #endif
  128. }
  129. }
  130. VOID
  131. PAL_Shutdown(
  132. VOID
  133. )
  134. /*++
  135. Purpose:
  136. Free everything needed by the game.
  137. Parameters:
  138. None.
  139. Return value:
  140. None.
  141. --*/
  142. {
  143. SOUND_CloseAudio();
  144. PAL_FreeFont();
  145. PAL_FreeResources();
  146. PAL_FreeGlobals();
  147. PAL_FreeUI();
  148. PAL_FreeText();
  149. PAL_ShutdownInput();
  150. VIDEO_Shutdown();
  151. UTIL_CloseLog();
  152. SDL_Quit();
  153. #if defined(GPH)
  154. chdir("/usr/gp2x");
  155. execl("./gp2xmenu", "./gp2xmenu", NULL);
  156. #endif
  157. #ifdef __WINPHONE__
  158. longjmp(g_exit_jmp_env, LONGJMP_EXIT_CODE);
  159. #endif
  160. }
  161. VOID
  162. PAL_TrademarkScreen(
  163. VOID
  164. )
  165. /*++
  166. Purpose:
  167. Show the trademark screen.
  168. Parameters:
  169. None.
  170. Return value:
  171. None.
  172. --*/
  173. {
  174. PAL_SetPalette(3, FALSE);
  175. PAL_RNGPlay(6, 0, 1000, 25);
  176. UTIL_Delay(1000);
  177. PAL_FadeOut(1);
  178. }
  179. VOID
  180. PAL_SplashScreen(
  181. VOID
  182. )
  183. /*++
  184. Purpose:
  185. Show the splash screen.
  186. Parameters:
  187. None.
  188. Return value:
  189. None.
  190. --*/
  191. {
  192. SDL_Color *palette = PAL_GetPalette(1, FALSE);
  193. SDL_Color rgCurrentPalette[256];
  194. SDL_Surface *lpBitmapDown, *lpBitmapUp;
  195. SDL_Rect srcrect, dstrect;
  196. LPSPRITE lpSpriteCrane;
  197. LPBITMAPRLE lpBitmapTitle;
  198. LPBYTE buf, buf2;
  199. int cranepos[9][3], i, iImgPos = 200, iCraneFrame = 0, iTitleHeight;
  200. DWORD dwTime, dwBeginTime;
  201. BOOL fUseCD = TRUE;
  202. if (palette == NULL)
  203. {
  204. fprintf(stderr, "ERROR: PAL_SplashScreen(): palette == NULL\n");
  205. return;
  206. }
  207. //
  208. // Allocate all the needed memory at once for simplification
  209. //
  210. buf = (LPBYTE)UTIL_calloc(1, 320 * 200 * 2);
  211. buf2 = (LPBYTE)(buf + 320 * 200);
  212. lpSpriteCrane = (LPSPRITE)buf2 + 32000;
  213. //
  214. // Create the surfaces
  215. //
  216. lpBitmapDown = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  217. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  218. gpScreen->format->Amask);
  219. lpBitmapUp = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  220. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  221. gpScreen->format->Amask);
  222. #if SDL_VERSION_ATLEAST(2, 0, 0)
  223. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  224. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  225. #else
  226. SDL_SetPalette(lpBitmapDown, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  227. SDL_SetPalette(lpBitmapUp, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  228. #endif
  229. //
  230. // Read the bitmaps
  231. //
  232. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_UP, gpGlobals->f.fpFBP);
  233. Decompress(buf, buf2, 320 * 200);
  234. PAL_FBPBlitToSurface(buf2, lpBitmapUp);
  235. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_DOWN, gpGlobals->f.fpFBP);
  236. Decompress(buf, buf2, 320 * 200);
  237. PAL_FBPBlitToSurface(buf2, lpBitmapDown);
  238. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_TITLE, gpGlobals->f.fpMGO);
  239. Decompress(buf, buf2, 32000);
  240. lpBitmapTitle = (LPBITMAPRLE)PAL_SpriteGetFrame(buf2, 0);
  241. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_CRANE, gpGlobals->f.fpMGO);
  242. Decompress(buf, lpSpriteCrane, 32000);
  243. iTitleHeight = PAL_RLEGetHeight(lpBitmapTitle);
  244. lpBitmapTitle[2] = 0;
  245. lpBitmapTitle[3] = 0; // HACKHACK
  246. //
  247. // Generate the positions of the cranes
  248. //
  249. for (i = 0; i < 9; i++)
  250. {
  251. cranepos[i][0] = RandomLong(300, 600);
  252. cranepos[i][1] = RandomLong(0, 80);
  253. cranepos[i][2] = RandomLong(0, 8);
  254. }
  255. //
  256. // Play the title music
  257. //
  258. if (!SOUND_PlayCDA(7))
  259. {
  260. fUseCD = FALSE;
  261. SOUND_PlayMUS(NUM_RIX_TITLE, TRUE, 2);
  262. }
  263. //
  264. // Clear all of the events and key states
  265. //
  266. PAL_ProcessEvent();
  267. PAL_ClearKeyState();
  268. dwBeginTime = SDL_GetTicks();
  269. srcrect.x = 0;
  270. srcrect.w = 320;
  271. dstrect.x = 0;
  272. dstrect.w = 320;
  273. while (TRUE)
  274. {
  275. PAL_ProcessEvent();
  276. dwTime = SDL_GetTicks() - dwBeginTime;
  277. //
  278. // Set the palette
  279. //
  280. if (dwTime < 15000)
  281. {
  282. for (i = 0; i < 256; i++)
  283. {
  284. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  285. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  286. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  287. }
  288. }
  289. VIDEO_SetPalette(rgCurrentPalette);
  290. #if SDL_VERSION_ATLEAST(2, 0, 0)
  291. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  292. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  293. #else
  294. SDL_SetPalette(lpBitmapDown, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  295. SDL_SetPalette(lpBitmapUp, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  296. #endif
  297. //
  298. // Draw the screen
  299. //
  300. if (iImgPos > 1)
  301. {
  302. iImgPos--;
  303. }
  304. //
  305. // The upper part...
  306. //
  307. srcrect.y = iImgPos;
  308. srcrect.h = 200 - iImgPos;
  309. dstrect.y = 0;
  310. dstrect.h = srcrect.h;
  311. SDL_BlitSurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
  312. //
  313. // The lower part...
  314. //
  315. srcrect.y = 0;
  316. srcrect.h = iImgPos;
  317. dstrect.y = 200 - iImgPos;
  318. dstrect.h = srcrect.h;
  319. SDL_BlitSurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
  320. //
  321. // Draw the cranes...
  322. //
  323. for (i = 0; i < 9; i++)
  324. {
  325. LPCBITMAPRLE lpFrame = PAL_SpriteGetFrame(lpSpriteCrane,
  326. cranepos[i][2] = (cranepos[i][2] + (iCraneFrame & 1)) % 8);
  327. cranepos[i][1] += ((iImgPos > 1) && (iImgPos & 1)) ? 1 : 0;
  328. PAL_RLEBlitToSurface(lpFrame, gpScreen,
  329. PAL_XY(cranepos[i][0], cranepos[i][1]));
  330. cranepos[i][0]--;
  331. }
  332. iCraneFrame++;
  333. //
  334. // Draw the title...
  335. //
  336. if (PAL_RLEGetHeight(lpBitmapTitle) < iTitleHeight)
  337. {
  338. //
  339. // HACKHACK
  340. //
  341. WORD w = lpBitmapTitle[2] | (lpBitmapTitle[3] << 8);
  342. w++;
  343. lpBitmapTitle[2] = (w & 0xFF);
  344. lpBitmapTitle[3] = (w >> 8);
  345. }
  346. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  347. VIDEO_UpdateScreen(NULL);
  348. //
  349. // Check for keypress...
  350. //
  351. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  352. {
  353. //
  354. // User has pressed a key...
  355. //
  356. lpBitmapTitle[2] = iTitleHeight & 0xFF;
  357. lpBitmapTitle[3] = iTitleHeight >> 8; // HACKHACK
  358. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  359. VIDEO_UpdateScreen(NULL);
  360. if (dwTime < 15000)
  361. {
  362. //
  363. // If the picture has not completed fading in, complete the rest
  364. //
  365. while (dwTime < 15000)
  366. {
  367. for (i = 0; i < 256; i++)
  368. {
  369. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  370. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  371. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  372. }
  373. VIDEO_SetPalette(rgCurrentPalette);
  374. #if SDL_VERSION_ATLEAST(2, 0, 0)
  375. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  376. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  377. #else
  378. SDL_SetPalette(lpBitmapDown, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  379. SDL_SetPalette(lpBitmapUp, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  380. #endif
  381. UTIL_Delay(8);
  382. dwTime += 250;
  383. }
  384. UTIL_Delay(500);
  385. }
  386. //
  387. // Quit the splash screen
  388. //
  389. break;
  390. }
  391. //
  392. // Delay a while...
  393. //
  394. PAL_ProcessEvent();
  395. while (SDL_GetTicks() - dwBeginTime < dwTime + 85)
  396. {
  397. SDL_Delay(1);
  398. PAL_ProcessEvent();
  399. }
  400. }
  401. SDL_FreeSurface(lpBitmapDown);
  402. SDL_FreeSurface(lpBitmapUp);
  403. free(buf);
  404. if (!fUseCD)
  405. {
  406. SOUND_PlayMUS(0, FALSE, 1);
  407. }
  408. PAL_FadeOut(1);
  409. }
  410. int
  411. main(
  412. int argc,
  413. char *argv[]
  414. )
  415. /*++
  416. Purpose:
  417. Program entry.
  418. Parameters:
  419. argc - Number of arguments.
  420. argv - Array of arguments.
  421. Return value:
  422. Integer value.
  423. --*/
  424. {
  425. #if defined(__APPLE__) && !defined(__IOS__)
  426. char *p = strstr(argv[0], "/Pal.app/");
  427. if (p != NULL)
  428. {
  429. char buf[4096];
  430. strcpy(buf, argv[0]);
  431. buf[p - argv[0]] = '\0';
  432. chdir(buf);
  433. }
  434. #endif
  435. #ifdef __WINPHONE__
  436. SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeRight");
  437. SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1");
  438. #endif
  439. UTIL_OpenLog();
  440. #if defined(_WIN32) && SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  441. putenv("SDL_VIDEODRIVER=directx");
  442. #endif
  443. //
  444. // Initialize everything
  445. //
  446. #ifdef PSP
  447. sdlpal_psp_init();
  448. #endif
  449. PAL_Init();
  450. #ifdef __WINPHONE__
  451. //
  452. // In windows phone, calling exit(0) directly will cause an abnormal exit.
  453. // By using setjmp/longjmp to avoid this.
  454. //
  455. if (setjmp(g_exit_jmp_env) == LONGJMP_EXIT_CODE) return 0;
  456. #endif
  457. //
  458. // Show the trademark screen and splash screen
  459. //
  460. PAL_TrademarkScreen();
  461. PAL_SplashScreen();
  462. //
  463. // Run the main game routine
  464. //
  465. PAL_GameMain();
  466. //
  467. // Should not really reach here...
  468. //
  469. assert(FALSE);
  470. return 255;
  471. }