main.c 12 KB

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