You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

531 lines
13 KiB

9 months ago
10 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
9 months ago
10 months ago
9 months ago
11 months ago
8 months ago
2 months ago
10 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
7 months ago
7 months ago
9 months ago
10 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
10 months ago
9 months ago
9 months ago
10 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
8 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
11 months ago
10 months ago
10 months ago
9 months ago
  1. /**
  2. @file main_sdl.c
  3. This is an SDL2 implementation of the game front end. It can be used to
  4. compile a native executable or a transpiled JS browser version with
  5. emscripten.
  6. This frontend is not strictly minimal, it could be reduced a lot. If you want
  7. a learning example of frontend, look at another, simpler one, e.g. terminal.
  8. To compile with emscripten run:
  9. emcc ./main_sdl.c -s USE_SDL=2 -O3 --shell-file HTMLshell.html -o game.html
  10. by Miloslav Ciz (drummyfish), 2019
  11. Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
  12. plus a waiver of all other intellectual property. The goal of this work is to
  13. be and remain completely in the public domain forever, available for any use
  14. whatsoever.
  15. */
  16. #if defined(_WIN32) || defined(WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__APPLE__)
  17. #define SFG_OS_IS_MALWARE 1
  18. #endif
  19. // #define SFG_START_LEVEL 1
  20. // #define SFG_QUICK_WIN 1
  21. // #define SFG_IMMORTAL 1
  22. // #define SFG_ALL_LEVELS 1
  23. // #define SFG_UNLOCK_DOOR 1
  24. // #define SFG_REVEAL_MAP 1
  25. // #define SFG_INFINITE_AMMO 1
  26. // #define SFG_TIME_MULTIPLIER 512
  27. // #define SFG_CPU_LOAD(percent) printf("CPU load: %d%\n",percent);
  28. // #define GAME_LQ
  29. #ifndef __EMSCRIPTEN__
  30. #ifndef GAME_LQ
  31. // higher quality
  32. #define SFG_FPS 60
  33. #define SFG_LOG(str) puts(str);
  34. #define SFG_SCREEN_RESOLUTION_X 700
  35. #define SFG_SCREEN_RESOLUTION_Y 512
  36. #define SFG_DITHERED_SHADOW 1
  37. #define SFG_DIMINISH_SPRITES 1
  38. #define SFG_HEADBOB_SHEAR (-1 * SFG_SCREEN_RESOLUTION_Y / 80)
  39. #define SFG_BACKGROUND_BLUR 1
  40. #else
  41. // lower quality
  42. #define SFG_FPS 35
  43. #define SFG_SCREEN_RESOLUTION_X 640
  44. #define SFG_SCREEN_RESOLUTION_Y 480
  45. #define SFG_RAYCASTING_SUBSAMPLE 2
  46. #define SFG_RESOLUTION_SCALEDOWN 2
  47. #define SFG_LOG(str) puts(str);
  48. #define SFG_DIMINISH_SPRITES 0
  49. #define SFG_DITHERED_SHADOW 0
  50. #define SFG_BACKGROUND_BLUR 0
  51. #define SFG_RAYCASTING_MAX_STEPS 18
  52. #define SFG_RAYCASTING_MAX_HITS 8
  53. #endif
  54. #else
  55. // emscripten
  56. #define SFG_FPS 35
  57. #define SFG_SCREEN_RESOLUTION_X 512
  58. #define SFG_SCREEN_RESOLUTION_Y 320
  59. #define SFG_CAN_EXIT 0
  60. #define SFG_RESOLUTION_SCALEDOWN 2
  61. #define SFG_DITHERED_SHADOW 1
  62. #define SFG_BACKGROUND_BLUR 0
  63. #define SFG_RAYCASTING_MAX_STEPS 18
  64. #define SFG_RAYCASTING_MAX_HITS 8
  65. #include <emscripten.h>
  66. #endif
  67. /*
  68. SDL is easier to play thanks to nice controls so make the player take full
  69. damage to make it a bit harder.
  70. */
  71. #define SFG_PLAYER_DAMAGE_MULTIPLIER 1024
  72. #define SDL_MUSIC_VOLUME 16
  73. #define SDL_ANALOG_DIVIDER 1024
  74. #if !SFG_OS_IS_MALWARE
  75. #include <signal.h>
  76. #endif
  77. #define SDL_MAIN_HANDLED 1
  78. #define SDL_DISABLE_IMMINTRIN_H 1
  79. #include <stdio.h>
  80. #include <unistd.h>
  81. #include <SDL2/SDL.h>
  82. #include "game.h"
  83. #include "sounds.h"
  84. const uint8_t *sdlKeyboardState;
  85. uint8_t webKeyboardState[SFG_KEY_COUNT];
  86. uint8_t sdlMouseButtonState = 0;
  87. int8_t sdlMouseWheelState = 0;
  88. SDL_GameController *sdlController;
  89. uint16_t sdlScreen[SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y]; // RGB565
  90. SDL_Window *window;
  91. SDL_Renderer *renderer;
  92. SDL_Texture *texture;
  93. SDL_Surface *screenSurface;
  94. // now implement the Anarch API functions (SFG_*)
  95. void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
  96. {
  97. sdlScreen[y * SFG_SCREEN_RESOLUTION_X + x] = paletteRGB565[colorIndex];
  98. }
  99. uint32_t SFG_getTimeMs()
  100. {
  101. return SDL_GetTicks();
  102. }
  103. void SFG_save(uint8_t data[SFG_SAVE_SIZE])
  104. {
  105. FILE *f = fopen("anarch.sav","wb");
  106. puts("SDL: opening and writing save file");
  107. if (f == NULL)
  108. {
  109. puts("SDL: could not open the file!");
  110. return;
  111. }
  112. fwrite(data,1,SFG_SAVE_SIZE,f);
  113. fclose(f);
  114. }
  115. uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
  116. {
  117. #ifndef __EMSCRIPTEN__
  118. FILE *f = fopen("anarch.sav","rb");
  119. puts("SDL: opening and reading save file");
  120. if (f == NULL)
  121. {
  122. puts("SDL: no save file to open");
  123. }
  124. else
  125. {
  126. fread(data,1,SFG_SAVE_SIZE,f);
  127. fclose(f);
  128. }
  129. return 1;
  130. #else
  131. // no saving for web version
  132. return 0;
  133. #endif
  134. }
  135. void SFG_sleepMs(uint16_t timeMs)
  136. {
  137. #ifndef __EMSCRIPTEN__
  138. usleep(timeMs * 1000);
  139. #endif
  140. }
  141. #ifdef __EMSCRIPTEN__
  142. void webButton(uint8_t key, uint8_t down) // HTML button pressed
  143. {
  144. webKeyboardState[key] = down;
  145. }
  146. #endif
  147. int8_t mouseMoved = 0; /* Whether the mouse has moved since program started,
  148. this is needed to fix an SDL limitation. */
  149. void SFG_getMouseOffset(int16_t *x, int16_t *y)
  150. {
  151. #ifndef __EMSCRIPTEN__
  152. if (mouseMoved)
  153. {
  154. int mX, mY;
  155. SDL_GetMouseState(&mX,&mY);
  156. *x = mX - SFG_SCREEN_RESOLUTION_X / 2;
  157. *y = mY - SFG_SCREEN_RESOLUTION_Y / 2;
  158. SDL_WarpMouseInWindow(window,
  159. SFG_SCREEN_RESOLUTION_X / 2, SFG_SCREEN_RESOLUTION_Y / 2);
  160. }
  161. if (sdlController != NULL)
  162. {
  163. *x +=
  164. (SDL_GameControllerGetAxis(sdlController,SDL_CONTROLLER_AXIS_RIGHTX) +
  165. SDL_GameControllerGetAxis(sdlController,SDL_CONTROLLER_AXIS_LEFTX)) /
  166. SDL_ANALOG_DIVIDER;
  167. *y +=
  168. (SDL_GameControllerGetAxis(sdlController,SDL_CONTROLLER_AXIS_RIGHTY) +
  169. SDL_GameControllerGetAxis(sdlController,SDL_CONTROLLER_AXIS_LEFTY)) /
  170. SDL_ANALOG_DIVIDER;
  171. }
  172. #endif
  173. }
  174. void SFG_processEvent(uint8_t event, uint8_t data)
  175. {
  176. }
  177. int8_t SFG_keyPressed(uint8_t key)
  178. {
  179. if (webKeyboardState[key]) // this only takes effect in the web version
  180. return 1;
  181. #define k(x) sdlKeyboardState[SDL_SCANCODE_ ## x]
  182. #define b(x) ((sdlController != NULL) && \
  183. SDL_GameControllerGetButton(sdlController,SDL_CONTROLLER_BUTTON_ ## x))
  184. switch (key)
  185. {
  186. case SFG_KEY_UP: return k(UP) || k(W) || k(KP_8) || b(DPAD_UP); break;
  187. case SFG_KEY_RIGHT:
  188. return k(RIGHT) || k(E) || k(KP_6) || b(DPAD_RIGHT); break;
  189. case SFG_KEY_DOWN:
  190. return k(DOWN) || k(S) || k(KP_5) || k(KP_2) || b(DPAD_DOWN); break;
  191. case SFG_KEY_LEFT: return k(LEFT) || k(Q) || k(KP_4) || b(DPAD_LEFT); break;
  192. case SFG_KEY_A: return k(J) || k(RETURN) || k(LCTRL) || k(RCTRL) || b(X) ||
  193. b(RIGHTSTICK) || (sdlMouseButtonState & SDL_BUTTON_LMASK); break;
  194. case SFG_KEY_B: return k(K) || k(LSHIFT) || b(B); break;
  195. case SFG_KEY_C: return k(L) || b(Y); break;
  196. case SFG_KEY_JUMP: return k(SPACE) || b(A); break;
  197. case SFG_KEY_STRAFE_LEFT: return k(A) || k(KP_7); break;
  198. case SFG_KEY_STRAFE_RIGHT: return k(D) || k(KP_9); break;
  199. case SFG_KEY_MAP: return k(TAB) || b(BACK); break;
  200. case SFG_KEY_CYCLE_WEAPON: return k(F) ||
  201. (sdlMouseButtonState & SDL_BUTTON_MMASK); break;
  202. case SFG_KEY_TOGGLE_FREELOOK: return b(LEFTSTICK) ||
  203. (sdlMouseButtonState & SDL_BUTTON_RMASK); break;
  204. case SFG_KEY_MENU: return k(ESCAPE) || b(START); break;
  205. case SFG_KEY_NEXT_WEAPON:
  206. if (k(P) || k(X) || b(RIGHTSHOULDER))
  207. return 1;
  208. #define checkMouse(cmp)\
  209. if (sdlMouseWheelState cmp 0) { sdlMouseWheelState = 0; return 1; }
  210. checkMouse(>)
  211. return 0;
  212. break;
  213. case SFG_KEY_PREVIOUS_WEAPON:
  214. if (k(O) || k(Y) || k(Z) || b(LEFTSHOULDER))
  215. return 1;
  216. checkMouse(<)
  217. #undef checkMouse
  218. return 0;
  219. break;
  220. default: return 0; break;
  221. }
  222. #undef k
  223. #undef b
  224. }
  225. int running;
  226. void mainLoopIteration()
  227. {
  228. SDL_Event event;
  229. #ifdef __EMSCRIPTEN__
  230. // hack, without it sound won't work because of shitty browser audio policies
  231. if (SFG_game.frame % 512 == 0)
  232. SDL_PauseAudio(0);
  233. #endif
  234. while (SDL_PollEvent(&event)) // also automatically updates sdlKeyboardState
  235. {
  236. if (event.type == SDL_MOUSEWHEEL)
  237. {
  238. if (event.wheel.y > 0) // scroll up
  239. sdlMouseWheelState = 1;
  240. else if (event.wheel.y < 0) // scroll down
  241. sdlMouseWheelState = -1;
  242. }
  243. else if (event.type == SDL_QUIT)
  244. running = 0;
  245. else if (event.type == SDL_MOUSEMOTION)
  246. mouseMoved = 1;
  247. }
  248. sdlMouseButtonState = SDL_GetMouseState(NULL,NULL);
  249. if (!SFG_mainLoopBody())
  250. running = 0;
  251. SDL_UpdateTexture(texture,NULL,sdlScreen,
  252. SFG_SCREEN_RESOLUTION_X * sizeof(uint16_t));
  253. SDL_RenderClear(renderer);
  254. SDL_RenderCopy(renderer,texture,NULL,NULL);
  255. SDL_RenderPresent(renderer);
  256. }
  257. #ifdef __EMSCRIPTEN__
  258. typedef void (*em_callback_func)(void);
  259. void emscripten_set_main_loop(
  260. em_callback_func func, int fps, int simulate_infinite_loop);
  261. #endif
  262. uint16_t audioBuff[SFG_SFX_SAMPLE_COUNT];
  263. uint16_t audioPos = 0; // audio position for the next audio buffer fill
  264. uint32_t audioUpdateFrame = 0; // game frame at which audio buffer fill happened
  265. static inline int16_t mixSamples(int16_t sample1, int16_t sample2)
  266. {
  267. return sample1 + sample2;
  268. }
  269. uint8_t musicOn = 0;
  270. // ^ this has to be init to 0 (not 1), else a few samples get played at start
  271. void audioFillCallback(void *userdata, uint8_t *s, int l)
  272. {
  273. uint16_t *s16 = (uint16_t *) s;
  274. for (int i = 0; i < l / 2; ++i)
  275. {
  276. s16[i] = musicOn ?
  277. mixSamples(audioBuff[audioPos], SDL_MUSIC_VOLUME *
  278. (SFG_getNextMusicSample() - SFG_musicTrackAverages[SFG_MusicState.track]))
  279. : audioBuff[audioPos];
  280. audioBuff[audioPos] = 0;
  281. audioPos = (audioPos < SFG_SFX_SAMPLE_COUNT - 1) ? (audioPos + 1) : 0;
  282. }
  283. audioUpdateFrame = SFG_game.frame;
  284. }
  285. void SFG_setMusic(uint8_t value)
  286. {
  287. switch (value)
  288. {
  289. case SFG_MUSIC_TURN_ON: musicOn = 1; break;
  290. case SFG_MUSIC_TURN_OFF: musicOn = 0; break;
  291. case SFG_MUSIC_NEXT: SFG_nextMusicTrack(); break;
  292. default: break;
  293. }
  294. }
  295. void SFG_playSound(uint8_t soundIndex, uint8_t volume)
  296. {
  297. uint16_t pos = (audioPos +
  298. ((SFG_game.frame - audioUpdateFrame) * SFG_MS_PER_FRAME * 8)) %
  299. SFG_SFX_SAMPLE_COUNT;
  300. uint16_t volumeScale = 1 << (volume / 37);
  301. for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i)
  302. {
  303. audioBuff[pos] = mixSamples(audioBuff[pos],
  304. (128 - SFG_GET_SFX_SAMPLE(soundIndex,i)) * volumeScale);
  305. pos = (pos < SFG_SFX_SAMPLE_COUNT - 1) ? (pos + 1) : 0;
  306. }
  307. }
  308. void handleSignal(int signal)
  309. {
  310. running = 0;
  311. }
  312. int main(int argc, char *argv[])
  313. {
  314. uint8_t argHelp = 0;
  315. uint8_t argForceWindow = 0;
  316. uint8_t argForceFullscreen = 0;
  317. #ifndef __EMSCRIPTEN__
  318. argForceFullscreen = 1;
  319. #endif
  320. for (uint8_t i = 0; i < SFG_KEY_COUNT; ++i)
  321. webKeyboardState[i] = 0;
  322. for (uint8_t i = 1; i < argc; ++i)
  323. {
  324. if (argv[i][0] == '-' && argv[i][1] == 'h' && argv[i][2] == 0)
  325. argHelp = 1;
  326. else if (argv[i][0] == '-' && argv[i][1] == 'w' && argv[i][2] == 0)
  327. argForceWindow = 1;
  328. else if (argv[i][0] == '-' && argv[i][1] == 'f' && argv[i][2] == 0)
  329. argForceFullscreen = 1;
  330. else
  331. puts("SDL: unknown argument");
  332. }
  333. if (argHelp)
  334. {
  335. puts("Anarch (SDL), version " SFG_VERSION_STRING "\n");
  336. puts("Anarch is a unique suckless FPS game. Collect weapons and items and destroy");
  337. puts("robot enemies in your way in order to get to the level finish. Some door are");
  338. puts("locked and require access cards. Good luck!\n");
  339. puts("created by Miloslav \"drummyfish\" Ciz, 2020, released under CC0 1.0 (public domain)\n");
  340. puts("CLI flags:\n");
  341. puts("-h print this help and exit");
  342. puts("-w force window");
  343. puts("-f force fullscreen\n");
  344. puts("controls:\n");
  345. puts("- arrows, numpad, [W] [S] [A] [D] [Q] [E]: movement");
  346. puts("- mouse: rotation, [LMB] shoot, [RMB] toggle free look");
  347. puts("- [SPACE]: jump");
  348. puts("- [J] [RETURN] [CTRL] [LMB]: game A button (shoot, confirm)");
  349. puts("- [K] [SHIFT]: game B button (cancel, strafe)");
  350. puts("- [L]: game C button (+ down = menu, + up = jump, ...)");
  351. puts("- [F]: cycle next/previous weapon");
  352. puts("- [O] [P] [X] [Y] [Z] [mouse wheel] [mouse middle]: change weapons");
  353. puts("- [TAB]: map");
  354. puts("- [ESCAPE]: menu");
  355. return 0;
  356. }
  357. SFG_init();
  358. puts("SDL: initializing SDL");
  359. SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
  360. window =
  361. SDL_CreateWindow("Anarch", SDL_WINDOWPOS_UNDEFINED,
  362. SDL_WINDOWPOS_UNDEFINED, SFG_SCREEN_RESOLUTION_X, SFG_SCREEN_RESOLUTION_Y,
  363. SDL_WINDOW_SHOWN);
  364. renderer = SDL_CreateRenderer(window,-1,0);
  365. texture =
  366. SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGB565,SDL_TEXTUREACCESS_STATIC,
  367. SFG_SCREEN_RESOLUTION_X,SFG_SCREEN_RESOLUTION_Y);
  368. screenSurface = SDL_GetWindowSurface(window);
  369. #if SFG_FULLSCREEN
  370. argForceFullscreen = 1;
  371. #endif
  372. if (!argForceWindow && argForceFullscreen)
  373. {
  374. puts("SDL: setting fullscreen");
  375. SDL_SetWindowFullscreen(window,SDL_WINDOW_FULLSCREEN_DESKTOP);
  376. }
  377. sdlKeyboardState = SDL_GetKeyboardState(NULL);
  378. sdlController = SDL_GameControllerOpen(0);
  379. #if !SFG_OS_IS_MALWARE
  380. signal(SIGINT,handleSignal);
  381. signal(SIGQUIT,handleSignal);
  382. signal(SIGTERM,handleSignal);
  383. #endif
  384. SDL_AudioSpec audioSpec;
  385. SDL_memset(&audioSpec, 0, sizeof(audioSpec));
  386. audioSpec.callback = audioFillCallback;
  387. audioSpec.freq = 8000;
  388. audioSpec.format = AUDIO_S16;
  389. audioSpec.channels = 1;
  390. #ifdef __EMSCRIPTEN__
  391. audioSpec.samples = 1024;
  392. #else
  393. audioSpec.samples = 256;
  394. #endif
  395. if (SDL_OpenAudio(&audioSpec,NULL) < 0)
  396. puts("SDL: could not initialize audio");
  397. for (int16_t i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i)
  398. audioBuff[i] = 0;
  399. SDL_PauseAudio(0);
  400. running = 1;
  401. SDL_ShowCursor(0);
  402. SDL_PumpEvents();
  403. SDL_GameControllerUpdate();
  404. SDL_WarpMouseInWindow(window,
  405. SFG_SCREEN_RESOLUTION_X / 2, SFG_SCREEN_RESOLUTION_Y / 2);
  406. #ifdef __EMSCRIPTEN__
  407. emscripten_set_main_loop(mainLoopIteration,0,1);
  408. #else
  409. while (running)
  410. mainLoopIteration();
  411. #endif
  412. puts("SDL: freeing SDL");
  413. SDL_GameControllerClose(sdlController);
  414. SDL_PauseAudio(1);
  415. SDL_CloseAudio();
  416. SDL_DestroyTexture(texture);
  417. SDL_DestroyRenderer(renderer);
  418. SDL_DestroyWindow(window);
  419. puts("SDL: ending");
  420. return 0;
  421. }