main.c 13 KB

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