anarch/main_sdl.c

499 lines
12 KiB
C
Raw Normal View History

2020-09-06 15:00:19 -04:00
/**
@file main_sdl.c
This is an SDL2 implementation of the game front end. It can be used to
compile a native executable or a transpiled JS browser version with
emscripten.
2020-11-07 12:58:40 -05:00
This frontend is not strictly minimal, it could be reduced a lot. If you want
a learning example of frontend, look at another, simpler one, e.g. terminal.
2020-10-18 05:36:38 -04:00
2020-09-06 15:00:19 -04:00
To compile with emscripten run:
emcc ./main_sdl.c -s USE_SDL=2 -O3 --shell-file HTMLshell.html -o game.html
by Miloslav Ciz (drummyfish), 2019
Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
2020-11-07 12:58:40 -05:00
plus a waiver of all other intellectual property. The goal of this work is to
2020-09-06 15:00:19 -04:00
be and remain completely in the public domain forever, available for any use
whatsoever.
*/
#if defined(_WIN32) || defined(WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__APPLE__)
#define SFG_OS_IS_MALWARE 1
#endif
2020-10-01 11:10:52 -04:00
// #define SFG_START_LEVEL 1
2020-11-17 04:52:09 -05:00
// #define SFG_QUICK_WIN 1
2020-11-19 12:07:49 -05:00
// #define SFG_IMMORTAL 1
2020-11-17 04:45:16 -05:00
// #define SFG_ALL_LEVELS 1
2020-11-14 06:01:49 -05:00
// #define SFG_UNLOCK_DOOR 1
2020-10-05 11:11:10 -04:00
// #define SFG_REVEAL_MAP 1
2020-11-19 12:07:49 -05:00
// #define SFG_INFINITE_AMMO 1
2020-10-20 14:26:25 -04:00
// #define SFG_TIME_MULTIPLIER 512
2020-10-26 10:51:35 -04:00
// #define SFG_CPU_LOAD(percent) printf("CPU load: %d%\n",percent);
2020-11-16 09:36:53 -05:00
// #define GAME_LQ
2020-10-10 10:22:05 -04:00
2020-10-24 07:46:25 -04:00
#ifndef __EMSCRIPTEN__
#ifndef GAME_LQ
// higher quality
#define SFG_FPS 60
#define SFG_LOG(str) puts(str);
#define SFG_SCREEN_RESOLUTION_X 700
#define SFG_SCREEN_RESOLUTION_Y 512
#define SFG_DITHERED_SHADOW 1
#define SFG_DIMINISH_SPRITES 1
#define SFG_HEADBOB_SHEAR (-1 * SFG_SCREEN_RESOLUTION_Y / 80)
#define SFG_BACKGROUND_BLUR 1
#else
// lower quality
2020-11-14 06:27:09 -05:00
#define SFG_FPS 35
2020-10-24 07:46:25 -04:00
#define SFG_SCREEN_RESOLUTION_X 640
#define SFG_SCREEN_RESOLUTION_Y 480
#define SFG_RAYCASTING_SUBSAMPLE 2
#define SFG_RESOLUTION_SCALEDOWN 2
2020-11-16 09:36:53 -05:00
#define SFG_LOG(str) puts(str);
2020-10-24 07:46:25 -04:00
#define SFG_DIMINISH_SPRITES 0
#define SFG_DITHERED_SHADOW 0
#define SFG_BACKGROUND_BLUR 0
2020-11-14 06:27:09 -05:00
#define SFG_RAYCASTING_MAX_STEPS 18
2020-11-14 06:01:49 -05:00
#define SFG_RAYCASTING_MAX_HITS 8
2020-10-24 07:46:25 -04:00
#endif
2020-10-09 08:47:35 -04:00
#else
2020-10-24 07:46:25 -04:00
// emscripten
2020-11-14 06:27:09 -05:00
#define SFG_FPS 35
2020-10-24 07:46:25 -04:00
#define SFG_SCREEN_RESOLUTION_X 512
#define SFG_SCREEN_RESOLUTION_Y 320
#define SFG_CAN_EXIT 0
2020-10-09 08:47:35 -04:00
#define SFG_RESOLUTION_SCALEDOWN 2
2020-11-10 14:02:45 -05:00
#define SFG_DITHERED_SHADOW 1
2020-10-09 08:47:35 -04:00
#define SFG_BACKGROUND_BLUR 0
2020-11-14 06:27:09 -05:00
#define SFG_RAYCASTING_MAX_STEPS 18
2020-11-14 06:01:49 -05:00
#define SFG_RAYCASTING_MAX_HITS 8
2020-10-24 07:46:25 -04:00
#include <emscripten.h>
2020-10-09 08:47:35 -04:00
#endif
2020-10-03 17:38:33 -04:00
2020-09-26 09:35:09 -04:00
/*
2020-10-28 11:41:23 -04:00
SDL is easier to play thanks to nice controls so make the player take full
2020-09-26 09:35:09 -04:00
damage to make it a bit harder.
*/
#define SFG_PLAYER_DAMAGE_MULTIPLIER 1024
2020-10-28 11:41:23 -04:00
#define SDL_MUSIC_VOLUME 16
2020-09-10 14:17:15 -04:00
2020-10-09 11:45:23 -04:00
#if !SFG_OS_IS_MALWARE
#include <signal.h>
#endif
2020-09-06 15:00:19 -04:00
#include <stdio.h>
2020-10-02 16:31:16 -04:00
#include <unistd.h>
2020-09-06 15:00:19 -04:00
#include <SDL2/SDL.h>
#include "game.h"
#include "sounds.h"
const uint8_t *sdlKeyboardState;
2020-10-11 08:39:46 -04:00
uint8_t webKeyboardState[SFG_KEY_COUNT];
2020-09-06 15:00:19 -04:00
uint8_t sdlMouseButtonState = 0;
int8_t sdlMouseWheelState = 0;
2020-10-28 11:41:23 -04:00
uint16_t sdlScreen[SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y]; // RGB565
2020-09-06 15:00:19 -04:00
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
SDL_Surface *screenSurface;
2020-10-28 11:41:23 -04:00
// now implement the Anarch API functions (SFG_*)
2020-09-06 15:00:19 -04:00
void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
{
2020-10-28 11:41:23 -04:00
sdlScreen[y * SFG_SCREEN_RESOLUTION_X + x] = paletteRGB565[colorIndex];
2020-09-06 15:00:19 -04:00
}
uint32_t SFG_getTimeMs()
{
return SDL_GetTicks();
}
2020-09-19 13:35:26 -04:00
void SFG_save(uint8_t data[SFG_SAVE_SIZE])
{
FILE *f = fopen("anarch.sav","wb");
puts("SDL: opening and writing save file");
if (f == NULL)
{
puts("SDL: could not open the file!");
return;
}
fwrite(data,1,SFG_SAVE_SIZE,f);
fclose(f);
}
uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
{
2020-09-25 16:03:46 -04:00
#ifndef __EMSCRIPTEN__
2020-09-19 13:35:26 -04:00
FILE *f = fopen("anarch.sav","rb");
puts("SDL: opening and reading save file");
if (f == NULL)
{
puts("SDL: no save file to open");
}
else
{
fread(data,1,SFG_SAVE_SIZE,f);
fclose(f);
}
return 1;
2020-09-25 16:03:46 -04:00
#else
2020-10-28 11:41:23 -04:00
// no saving for web version
2020-09-25 16:03:46 -04:00
return 0;
#endif
2020-09-19 13:35:26 -04:00
}
2020-09-06 15:00:19 -04:00
void SFG_sleepMs(uint16_t timeMs)
{
#ifndef __EMSCRIPTEN__
usleep(timeMs * 1000);
#endif
}
2020-10-11 08:39:46 -04:00
#ifdef __EMSCRIPTEN__
2020-10-28 11:41:23 -04:00
void webButton(uint8_t key, uint8_t down) // HTML button pressed
2020-10-11 08:39:46 -04:00
{
webKeyboardState[key] = down;
}
#endif
2020-10-11 13:10:20 -04:00
int8_t mouseMoved = 0; /* Whether the mouse has moved since program started,
this is needed to fix an SDL limitation. */
2020-09-06 15:00:19 -04:00
void SFG_getMouseOffset(int16_t *x, int16_t *y)
{
2020-09-25 16:03:46 -04:00
#ifndef __EMSCRIPTEN__
2020-10-11 13:10:20 -04:00
if (mouseMoved)
{
2020-09-06 15:00:19 -04:00
int mX, mY;
SDL_GetMouseState(&mX,&mY);
*x = mX - SFG_SCREEN_RESOLUTION_X / 2;
*y = mY - SFG_SCREEN_RESOLUTION_Y / 2;
SDL_WarpMouseInWindow(window,
SFG_SCREEN_RESOLUTION_X / 2, SFG_SCREEN_RESOLUTION_Y / 2);
2020-10-11 13:10:20 -04:00
}
2020-09-25 16:03:46 -04:00
#endif
2020-09-06 15:00:19 -04:00
}
2020-10-01 05:47:28 -04:00
void SFG_processEvent(uint8_t event, uint8_t data)
{
}
2020-09-06 15:00:19 -04:00
int8_t SFG_keyPressed(uint8_t key)
{
2020-10-28 11:41:23 -04:00
if (webKeyboardState[key]) // this only takes effect in the web version
2020-10-11 08:39:46 -04:00
return 1;
2020-10-20 11:58:25 -04:00
#define k(x) sdlKeyboardState[SDL_SCANCODE_ ## x]
2020-09-06 15:00:19 -04:00
switch (key)
{
2020-10-20 11:58:25 -04:00
case SFG_KEY_UP: return k(UP) || k(W) || k(KP_8); break;
case SFG_KEY_RIGHT: return k(RIGHT) || k(E) || k(KP_6); break;
case SFG_KEY_DOWN: return k(DOWN) || k(S) || k(KP_5) || k(KP_2); break;
case SFG_KEY_LEFT: return k(LEFT) || k(Q) || k(KP_4); break;
2020-10-28 11:41:23 -04:00
case SFG_KEY_A: return k(J) || k(RETURN) || k(LCTRL) || k(RCTRL) ||
2020-10-23 07:06:18 -04:00
(sdlMouseButtonState & SDL_BUTTON_LMASK); break;
2020-10-28 11:41:23 -04:00
case SFG_KEY_B: return k(K) || k(LSHIFT); break;
2020-10-22 10:36:48 -04:00
case SFG_KEY_C: return k(L); break;
2020-10-20 11:58:25 -04:00
case SFG_KEY_JUMP: return k(SPACE); break;
case SFG_KEY_STRAFE_LEFT: return k(A) || k(KP_7); break;
case SFG_KEY_STRAFE_RIGHT: return k(D) || k(KP_9); break;
case SFG_KEY_MAP: return k(TAB); break;
2020-10-20 15:00:47 -04:00
case SFG_KEY_CYCLE_WEAPON: return k(F) ||
(sdlMouseButtonState & SDL_BUTTON_MMASK); break;
2020-10-20 11:58:25 -04:00
case SFG_KEY_TOGGLE_FREELOOK: return sdlMouseButtonState & SDL_BUTTON_RMASK;
break;
2020-09-06 15:00:19 -04:00
case SFG_KEY_NEXT_WEAPON:
2020-10-20 11:58:25 -04:00
if (k(P) || k(X))
2020-09-26 19:11:19 -04:00
return 1;
2020-10-28 11:41:23 -04:00
#define checkMouse(cmp)\
if (sdlMouseWheelState cmp 0) { sdlMouseWheelState = 0; return 1; }
checkMouse(>)
2020-09-06 15:00:19 -04:00
return 0;
break;
case SFG_KEY_PREVIOUS_WEAPON:
2020-10-20 11:58:25 -04:00
if (k(O) || k(Y) || k(Z))
2020-09-26 19:11:19 -04:00
return 1;
2020-10-28 11:41:23 -04:00
checkMouse(<)
#undef checkMouse
2020-09-06 15:00:19 -04:00
return 0;
break;
2020-10-20 11:58:25 -04:00
case SFG_KEY_MENU: return k(ESCAPE); break;
2020-09-06 15:00:19 -04:00
default: return 0; break;
}
}
int running;
void mainLoopIteration()
{
SDL_Event event;
2020-09-25 18:59:03 -04:00
#ifdef __EMSCRIPTEN__
2020-10-28 11:41:23 -04:00
// hack, without it sound won't work because of shitty browser audio policies
2020-09-25 18:59:03 -04:00
if (SFG_game.frame % 512 == 0)
SDL_PauseAudio(0);
#endif
2020-09-06 15:00:19 -04:00
while (SDL_PollEvent(&event)) // also automatically updates sdlKeyboardState
{
2020-10-03 15:26:48 -04:00
if (event.type == SDL_MOUSEWHEEL)
2020-09-06 15:00:19 -04:00
{
2020-10-28 11:41:23 -04:00
if (event.wheel.y > 0) // scroll up
2020-10-26 10:51:35 -04:00
sdlMouseWheelState = 1;
2020-09-06 15:00:19 -04:00
else if (event.wheel.y < 0) // scroll down
2020-10-26 10:51:35 -04:00
sdlMouseWheelState = -1;
2020-09-06 15:00:19 -04:00
}
2020-10-03 15:26:48 -04:00
else if (event.type == SDL_QUIT)
running = 0;
2020-10-11 13:10:20 -04:00
else if (event.type == SDL_MOUSEMOTION)
mouseMoved = 1;
2020-09-06 15:00:19 -04:00
}
sdlMouseButtonState = SDL_GetMouseState(NULL,NULL);
2020-09-20 04:04:19 -04:00
if (!SFG_mainLoopBody())
running = 0;
2020-09-06 15:00:19 -04:00
2020-10-28 11:41:23 -04:00
SDL_UpdateTexture(texture,NULL,sdlScreen,
SFG_SCREEN_RESOLUTION_X * sizeof(uint16_t));
2020-09-06 15:00:19 -04:00
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
2020-09-20 04:04:19 -04:00
}
2020-09-06 15:00:19 -04:00
#ifdef __EMSCRIPTEN__
typedef void (*em_callback_func)(void);
2020-10-28 11:41:23 -04:00
void emscripten_set_main_loop(
em_callback_func func, int fps, int simulate_infinite_loop);
2020-09-06 15:00:19 -04:00
#endif
2020-09-19 05:52:24 -04:00
uint16_t audioBuff[SFG_SFX_SAMPLE_COUNT];
2020-10-25 14:20:03 -04:00
uint16_t audioPos = 0; // audio position for the next audio buffer fill
uint32_t audioUpdateFrame = 0; // game frame at which audio buffer fill happened
2020-09-06 15:00:19 -04:00
2020-10-08 11:25:41 -04:00
static inline int16_t mixSamples(int16_t sample1, int16_t sample2)
2020-09-06 15:00:19 -04:00
{
2020-10-10 11:06:57 -04:00
return sample1 + sample2;
2020-09-06 15:00:19 -04:00
}
2020-10-23 13:11:50 -04:00
uint8_t musicOn = 0;
// ^ this has to be init to 0 (not 1), else a few samples get played at start
2020-09-06 15:00:19 -04:00
void audioFillCallback(void *userdata, uint8_t *s, int l)
{
2020-09-19 05:52:24 -04:00
uint16_t *s16 = (uint16_t *) s;
for (int i = 0; i < l / 2; ++i)
2020-09-06 15:00:19 -04:00
{
2020-09-19 05:52:24 -04:00
s16[i] = musicOn ?
2020-10-28 11:41:23 -04:00
mixSamples(audioBuff[audioPos], SDL_MUSIC_VOLUME *
2020-10-25 14:45:57 -04:00
(SFG_getNextMusicSample() - SFG_musicTrackAverages[SFG_MusicState.track]))
2020-10-04 11:09:24 -04:00
: audioBuff[audioPos];
2020-09-06 15:00:19 -04:00
2020-10-01 08:10:43 -04:00
audioBuff[audioPos] = 0;
2020-09-06 15:00:19 -04:00
audioPos = (audioPos < SFG_SFX_SAMPLE_COUNT - 1) ? (audioPos + 1) : 0;
}
2020-10-25 14:20:03 -04:00
audioUpdateFrame = SFG_game.frame;
2020-09-06 15:00:19 -04:00
}
2020-10-23 06:34:26 -04:00
void SFG_setMusic(uint8_t value)
2020-09-06 15:00:19 -04:00
{
2020-10-23 06:34:26 -04:00
switch (value)
{
case SFG_MUSIC_TURN_ON: musicOn = 1; break;
case SFG_MUSIC_TURN_OFF: musicOn = 0; break;
case SFG_MUSIC_NEXT: SFG_nextMusicTrack(); break;
2020-10-23 07:06:18 -04:00
default: break;
2020-10-23 06:34:26 -04:00
}
2020-09-06 15:00:19 -04:00
}
void SFG_playSound(uint8_t soundIndex, uint8_t volume)
{
2020-10-26 10:51:35 -04:00
uint16_t pos = (audioPos +
((SFG_game.frame - audioUpdateFrame) * SFG_MS_PER_FRAME * 8)) %
SFG_SFX_SAMPLE_COUNT;
2020-10-25 14:20:03 -04:00
2020-10-10 11:06:57 -04:00
uint16_t volumeScale = 1 << (volume / 37);
2020-09-19 07:31:50 -04:00
for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i)
{
2020-10-03 18:40:03 -04:00
audioBuff[pos] = mixSamples(audioBuff[pos],
(128 - SFG_GET_SFX_SAMPLE(soundIndex,i)) * volumeScale);
2020-09-19 07:31:50 -04:00
pos = (pos < SFG_SFX_SAMPLE_COUNT - 1) ? (pos + 1) : 0;
}
2020-09-06 15:00:19 -04:00
}
2020-10-03 15:26:48 -04:00
void handleSignal(int signal)
{
running = 0;
}
2020-09-06 15:00:19 -04:00
int main(int argc, char *argv[])
{
uint8_t argHelp = 0;
uint8_t argForceWindow = 0;
uint8_t argForceFullscreen = 0;
2020-10-11 08:39:46 -04:00
for (uint8_t i = 0; i < SFG_KEY_COUNT; ++i)
webKeyboardState[i] = 0;
2020-09-06 15:00:19 -04:00
for (uint8_t i = 1; i < argc; ++i)
{
if (argv[i][0] == '-' && argv[i][1] == 'h' && argv[i][2] == 0)
argHelp = 1;
else if (argv[i][0] == '-' && argv[i][1] == 'w' && argv[i][2] == 0)
argForceWindow = 1;
else if (argv[i][0] == '-' && argv[i][1] == 'f' && argv[i][2] == 0)
argForceFullscreen = 1;
else
puts("SDL: unknown argument");
}
if (argHelp)
{
2020-10-28 11:41:23 -04:00
puts("Anarch (SDL), version " SFG_VERSION_STRING "\n");
2020-09-26 19:11:19 -04:00
puts("Anarch is a unique suckless FPS game. Collect weapons and items and destroy");
puts("robot enemies in your way in order to get to the level finish. Some door are");
puts("locked and require access cards. Good luck!\n");
2020-10-28 11:41:23 -04:00
puts("created by Miloslav \"drummyfish\" Ciz, 2020, released under CC0 1.0 (public domain)\n");
2020-09-26 19:11:19 -04:00
puts("CLI flags:\n");
puts("-h print this help and exit");
puts("-w force window");
puts("-f force fullscreen\n");
puts("controls:\n");
puts("- arrows, numpad, [W] [S] [A] [D] [Q] [R]: movement");
2020-10-28 11:41:23 -04:00
puts("- mouse: rotation, [LMB] shoot, [RMB] toggle free look");
2020-09-26 19:11:19 -04:00
puts("- [SPACE]: jump");
2020-10-28 11:41:23 -04:00
puts("- [J] [RETURN] [CTRL] [LMB]: game A button (shoot, confirm)");
puts("- [K] [SHIFT]: game B button (cancel, strafe)");
puts("- [L]: game C button (+ down = menu, + up = jump, ...)");
puts("- [F]: cycle next/previous weapon");
puts("- [O] [P] [X] [Y] [Z] [mouse wheel]: change weapons");
2020-09-26 19:11:19 -04:00
puts("- [TAB]: map");
puts("- [ESCAPE]: menu");
2020-09-06 15:00:19 -04:00
return 0;
}
2020-10-25 14:20:03 -04:00
SFG_init();
2020-09-06 15:00:19 -04:00
puts("SDL: initializing SDL");
window =
2020-10-19 11:38:03 -04:00
SDL_CreateWindow("Anarch", SDL_WINDOWPOS_UNDEFINED,
2020-09-06 15:00:19 -04:00
SDL_WINDOWPOS_UNDEFINED, SFG_SCREEN_RESOLUTION_X, SFG_SCREEN_RESOLUTION_Y,
SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window,-1,0);
texture =
SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGB565,SDL_TEXTUREACCESS_STATIC,
SFG_SCREEN_RESOLUTION_X,SFG_SCREEN_RESOLUTION_Y);
screenSurface = SDL_GetWindowSurface(window);
#if SFG_FULLSCREEN
argForceFullscreen = 1;
#endif
if (!argForceWindow && argForceFullscreen)
{
puts("SDL: setting fullscreen");
SDL_SetWindowFullscreen(window,SDL_WINDOW_FULLSCREEN_DESKTOP);
}
sdlKeyboardState = SDL_GetKeyboardState(NULL);
2020-09-25 16:03:46 -04:00
SDL_Init(SDL_INIT_AUDIO);
2020-09-06 15:00:19 -04:00
2020-10-09 11:45:23 -04:00
#if !SFG_OS_IS_MALWARE
2020-10-03 15:26:48 -04:00
signal(SIGINT,handleSignal);
signal(SIGQUIT,handleSignal);
signal(SIGTERM,handleSignal);
2020-10-09 11:45:23 -04:00
#endif
2020-10-03 15:26:48 -04:00
2020-09-25 18:59:03 -04:00
SDL_AudioSpec audioSpec;
2020-09-06 15:00:19 -04:00
2020-09-25 17:48:27 -04:00
SDL_memset(&audioSpec, 0, sizeof(audioSpec));
2020-09-06 15:00:19 -04:00
audioSpec.callback = audioFillCallback;
audioSpec.freq = 8000;
2020-10-01 08:10:43 -04:00
audioSpec.format = AUDIO_S16;
2020-09-06 15:00:19 -04:00
audioSpec.channels = 1;
2020-09-27 15:19:08 -04:00
#ifdef __EMSCRIPTEN__
2020-09-25 18:59:03 -04:00
audioSpec.samples = 1024;
2020-09-27 15:19:08 -04:00
#else
audioSpec.samples = 256;
#endif
2020-09-06 15:00:19 -04:00
2020-09-25 18:59:03 -04:00
if (SDL_OpenAudio(&audioSpec,NULL) < 0)
2020-09-06 15:00:19 -04:00
puts("SDL: could not initialize audio");
2020-09-19 05:52:24 -04:00
for (int16_t i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i)
2020-10-01 08:10:43 -04:00
audioBuff[i] = 0;
2020-09-06 15:00:19 -04:00
2020-09-17 14:15:05 -04:00
SDL_PauseAudio(0);
2020-09-06 15:00:19 -04:00
running = 1;
2020-10-11 13:10:20 -04:00
SDL_ShowCursor(0);
SDL_PumpEvents();
SDL_WarpMouseInWindow(window,
SFG_SCREEN_RESOLUTION_X / 2, SFG_SCREEN_RESOLUTION_Y / 2);
2020-09-06 15:00:19 -04:00
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(mainLoopIteration,0,1);
#else
while (running)
mainLoopIteration();
#endif
puts("SDL: freeing SDL");
SDL_PauseAudio(1);
2020-10-28 11:41:23 -04:00
SDL_CloseAudio();
2020-09-06 15:00:19 -04:00
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
puts("SDL: ending");
return 0;
}