main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. UTIL_Platform_Quit();
  159. }
  160. VOID
  161. PAL_TrademarkScreen(
  162. VOID
  163. )
  164. /*++
  165. Purpose:
  166. Show the trademark screen.
  167. Parameters:
  168. None.
  169. Return value:
  170. None.
  171. --*/
  172. {
  173. PAL_SetPalette(3, FALSE);
  174. PAL_RNGPlay(6, 0, 1000, 25);
  175. UTIL_Delay(1000);
  176. PAL_FadeOut(1);
  177. }
  178. VOID
  179. PAL_SplashScreen(
  180. VOID
  181. )
  182. /*++
  183. Purpose:
  184. Show the splash screen.
  185. Parameters:
  186. None.
  187. Return value:
  188. None.
  189. --*/
  190. {
  191. SDL_Color *palette = PAL_GetPalette(1, FALSE);
  192. SDL_Color rgCurrentPalette[256];
  193. SDL_Surface *lpBitmapDown, *lpBitmapUp;
  194. SDL_Rect srcrect, dstrect;
  195. LPSPRITE lpSpriteCrane;
  196. LPBITMAPRLE lpBitmapTitle;
  197. LPBYTE buf, buf2;
  198. int cranepos[9][3], i, iImgPos = 200, iCraneFrame = 0, iTitleHeight;
  199. DWORD dwTime, dwBeginTime;
  200. BOOL fUseCD = TRUE;
  201. if (palette == NULL)
  202. {
  203. fprintf(stderr, "ERROR: PAL_SplashScreen(): palette == NULL\n");
  204. return;
  205. }
  206. //
  207. // Allocate all the needed memory at once for simplification
  208. //
  209. buf = (LPBYTE)UTIL_calloc(1, 320 * 200 * 2);
  210. buf2 = (LPBYTE)(buf + 320 * 200);
  211. lpSpriteCrane = (LPSPRITE)buf2 + 32000;
  212. //
  213. // Create the surfaces
  214. //
  215. lpBitmapDown = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  216. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  217. gpScreen->format->Amask);
  218. lpBitmapUp = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
  219. gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
  220. gpScreen->format->Amask);
  221. #if SDL_VERSION_ATLEAST(2, 0, 0)
  222. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  223. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  224. #else
  225. SDL_SetPalette(lpBitmapDown, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  226. SDL_SetPalette(lpBitmapUp, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  227. #endif
  228. //
  229. // Read the bitmaps
  230. //
  231. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_UP, gpGlobals->f.fpFBP);
  232. Decompress(buf, buf2, 320 * 200);
  233. PAL_FBPBlitToSurface(buf2, lpBitmapUp);
  234. PAL_MKFReadChunk(buf, 320 * 200, BITMAPNUM_SPLASH_DOWN, gpGlobals->f.fpFBP);
  235. Decompress(buf, buf2, 320 * 200);
  236. PAL_FBPBlitToSurface(buf2, lpBitmapDown);
  237. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_TITLE, gpGlobals->f.fpMGO);
  238. Decompress(buf, buf2, 32000);
  239. lpBitmapTitle = (LPBITMAPRLE)PAL_SpriteGetFrame(buf2, 0);
  240. PAL_MKFReadChunk(buf, 32000, SPRITENUM_SPLASH_CRANE, gpGlobals->f.fpMGO);
  241. Decompress(buf, lpSpriteCrane, 32000);
  242. iTitleHeight = PAL_RLEGetHeight(lpBitmapTitle);
  243. lpBitmapTitle[2] = 0;
  244. lpBitmapTitle[3] = 0; // HACKHACK
  245. //
  246. // Generate the positions of the cranes
  247. //
  248. for (i = 0; i < 9; i++)
  249. {
  250. cranepos[i][0] = RandomLong(300, 600);
  251. cranepos[i][1] = RandomLong(0, 80);
  252. cranepos[i][2] = RandomLong(0, 8);
  253. }
  254. //
  255. // Play the title music
  256. //
  257. if (!SOUND_PlayCDA(7))
  258. {
  259. fUseCD = FALSE;
  260. SOUND_PlayMUS(NUM_RIX_TITLE, TRUE, 2);
  261. }
  262. //
  263. // Clear all of the events and key states
  264. //
  265. PAL_ProcessEvent();
  266. PAL_ClearKeyState();
  267. dwBeginTime = SDL_GetTicks();
  268. srcrect.x = 0;
  269. srcrect.w = 320;
  270. dstrect.x = 0;
  271. dstrect.w = 320;
  272. while (TRUE)
  273. {
  274. PAL_ProcessEvent();
  275. dwTime = SDL_GetTicks() - dwBeginTime;
  276. //
  277. // Set the palette
  278. //
  279. if (dwTime < 15000)
  280. {
  281. for (i = 0; i < 256; i++)
  282. {
  283. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  284. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  285. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  286. }
  287. }
  288. VIDEO_SetPalette(rgCurrentPalette);
  289. #if SDL_VERSION_ATLEAST(2, 0, 0)
  290. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  291. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  292. #else
  293. SDL_SetPalette(lpBitmapDown, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  294. SDL_SetPalette(lpBitmapUp, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  295. #endif
  296. //
  297. // Draw the screen
  298. //
  299. if (iImgPos > 1)
  300. {
  301. iImgPos--;
  302. }
  303. //
  304. // The upper part...
  305. //
  306. srcrect.y = iImgPos;
  307. srcrect.h = 200 - iImgPos;
  308. dstrect.y = 0;
  309. dstrect.h = srcrect.h;
  310. SDL_BlitSurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
  311. //
  312. // The lower part...
  313. //
  314. srcrect.y = 0;
  315. srcrect.h = iImgPos;
  316. dstrect.y = 200 - iImgPos;
  317. dstrect.h = srcrect.h;
  318. SDL_BlitSurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
  319. //
  320. // Draw the cranes...
  321. //
  322. for (i = 0; i < 9; i++)
  323. {
  324. LPCBITMAPRLE lpFrame = PAL_SpriteGetFrame(lpSpriteCrane,
  325. cranepos[i][2] = (cranepos[i][2] + (iCraneFrame & 1)) % 8);
  326. cranepos[i][1] += ((iImgPos > 1) && (iImgPos & 1)) ? 1 : 0;
  327. PAL_RLEBlitToSurface(lpFrame, gpScreen,
  328. PAL_XY(cranepos[i][0], cranepos[i][1]));
  329. cranepos[i][0]--;
  330. }
  331. iCraneFrame++;
  332. //
  333. // Draw the title...
  334. //
  335. if (PAL_RLEGetHeight(lpBitmapTitle) < iTitleHeight)
  336. {
  337. //
  338. // HACKHACK
  339. //
  340. WORD w = lpBitmapTitle[2] | (lpBitmapTitle[3] << 8);
  341. w++;
  342. lpBitmapTitle[2] = (w & 0xFF);
  343. lpBitmapTitle[3] = (w >> 8);
  344. }
  345. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  346. VIDEO_UpdateScreen(NULL);
  347. //
  348. // Check for keypress...
  349. //
  350. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  351. {
  352. //
  353. // User has pressed a key...
  354. //
  355. lpBitmapTitle[2] = iTitleHeight & 0xFF;
  356. lpBitmapTitle[3] = iTitleHeight >> 8; // HACKHACK
  357. PAL_RLEBlitToSurface(lpBitmapTitle, gpScreen, PAL_XY(255, 10));
  358. VIDEO_UpdateScreen(NULL);
  359. if (dwTime < 15000)
  360. {
  361. //
  362. // If the picture has not completed fading in, complete the rest
  363. //
  364. while (dwTime < 15000)
  365. {
  366. for (i = 0; i < 256; i++)
  367. {
  368. rgCurrentPalette[i].r = (BYTE)(palette[i].r * ((float)dwTime / 15000));
  369. rgCurrentPalette[i].g = (BYTE)(palette[i].g * ((float)dwTime / 15000));
  370. rgCurrentPalette[i].b = (BYTE)(palette[i].b * ((float)dwTime / 15000));
  371. }
  372. VIDEO_SetPalette(rgCurrentPalette);
  373. #if SDL_VERSION_ATLEAST(2, 0, 0)
  374. SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
  375. SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
  376. #else
  377. SDL_SetPalette(lpBitmapDown, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  378. SDL_SetPalette(lpBitmapUp, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  379. #endif
  380. UTIL_Delay(8);
  381. dwTime += 250;
  382. }
  383. UTIL_Delay(500);
  384. }
  385. //
  386. // Quit the splash screen
  387. //
  388. break;
  389. }
  390. //
  391. // Delay a while...
  392. //
  393. PAL_ProcessEvent();
  394. while (SDL_GetTicks() - dwBeginTime < dwTime + 85)
  395. {
  396. SDL_Delay(1);
  397. PAL_ProcessEvent();
  398. }
  399. }
  400. SDL_FreeSurface(lpBitmapDown);
  401. SDL_FreeSurface(lpBitmapUp);
  402. free(buf);
  403. if (!fUseCD)
  404. {
  405. SOUND_PlayMUS(0, FALSE, 1);
  406. }
  407. PAL_FadeOut(1);
  408. }
  409. int
  410. main(
  411. int argc,
  412. char *argv[]
  413. )
  414. /*++
  415. Purpose:
  416. Program entry.
  417. Parameters:
  418. argc - Number of arguments.
  419. argv - Array of arguments.
  420. Return value:
  421. Integer value.
  422. --*/
  423. {
  424. #if defined(__APPLE__) && !defined(__IOS__)
  425. char *p = strstr(argv[0], "/Pal.app/");
  426. if (p != NULL)
  427. {
  428. char buf[4096];
  429. strcpy(buf, argv[0]);
  430. buf[p - argv[0]] = '\0';
  431. chdir(buf);
  432. }
  433. #elif defined(__WINPHONE__)
  434. //
  435. // In windows phone, calling exit(0) directly will cause an abnormal exit.
  436. // By using setjmp/longjmp to avoid this.
  437. //
  438. if (setjmp(g_exit_jmp_env) == LONGJMP_EXIT_CODE) return 0;
  439. // We should first check the SD card before running actual codes
  440. UTIL_BasePath();
  441. SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeRight");
  442. SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1");
  443. #endif
  444. UTIL_OpenLog();
  445. #if defined(_WIN32) && SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  446. putenv("SDL_VIDEODRIVER=directx");
  447. #endif
  448. PAL_LoadConfig(TRUE);
  449. //
  450. // Platform-specific initialization
  451. //
  452. if (UTIL_Platform_Init(argc, argv) != 0)
  453. return -1;
  454. //
  455. // Initialize everything
  456. //
  457. #ifdef PSP
  458. sdlpal_psp_init();
  459. #endif
  460. PAL_Init();
  461. //
  462. // Show the trademark screen and splash screen
  463. //
  464. PAL_TrademarkScreen();
  465. PAL_SplashScreen();
  466. //
  467. // Run the main game routine
  468. //
  469. PAL_GameMain();
  470. //
  471. // Should not really reach here...
  472. //
  473. assert(FALSE);
  474. return 255;
  475. }