main.c 13 KB

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