main.c 13 KB

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