anarch/platform_sdl.h

301 lines
6.9 KiB
C
Raw Normal View History

2019-10-03 18:04:14 -04:00
/**
@file platform_sdl.h
2020-01-05 06:47:25 -05:00
This is an SDL2 implementation of the game front end. It can be used to
compile a native executable or a transpired JS browser version with
emscripten.
To compile with emscripten run:
emcc ./main.c -s USE_SDL=2 -O3 --shell-file HTMLshell.html -o game.html
2019-10-03 18:04:14 -04:00
by Miloslav Ciz (drummyfish), 2019
Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
plus a waiver of all other intellectual property. The goal of this work is
be and remain completely in the public domain forever, available for any use
whatsoever.
*/
2019-09-25 09:51:19 -04:00
#ifndef _SFG_PLATFORM_H
#define _SFG_PLATFORM_H
2019-09-27 06:46:44 -04:00
#include "settings.h"
2019-09-25 09:51:19 -04:00
#include <stdio.h>
#include <SDL2/SDL.h>
#include "palette.h"
2020-02-04 11:17:00 -05:00
#include "sounds.h"
2019-09-29 07:50:40 -04:00
#undef SFG_LOG
#define SFG_LOG(str) printf("game: %s\n",str);
2020-01-02 11:22:00 -05:00
#undef SFG_BACKGROUND_BLUR
#define SFG_BACKGROUND_BLUR 1
2019-09-25 09:51:19 -04:00
const uint8_t *sdlKeyboardState;
2019-10-04 10:32:24 -04:00
uint16_t screen[SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y]; // RGB565 format
2019-09-25 09:51:19 -04:00
2020-01-18 05:10:05 -05:00
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
SDL_Surface *screenSurface;
2019-09-25 09:51:19 -04:00
void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
{
2019-10-04 10:32:24 -04:00
screen[y * SFG_SCREEN_RESOLUTION_X + x] = paletteRGB565[colorIndex];
2019-09-25 09:51:19 -04:00
}
uint32_t SFG_getTimeMs()
{
return SDL_GetTicks();
}
void SFG_sleepMs(uint16_t timeMs)
{
2020-01-05 06:47:25 -05:00
#ifndef __EMSCRIPTEN__
2019-09-25 09:51:19 -04:00
usleep(timeMs * 1000);
2020-01-05 06:47:25 -05:00
#endif
2019-09-25 09:51:19 -04:00
}
2020-01-18 05:10:05 -05:00
void SFG_getMouseOffset(int16_t *x, int16_t *y)
{
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);
}
2019-09-25 09:51:19 -04:00
int8_t SFG_keyPressed(uint8_t key)
{
switch (key)
{
2019-10-03 14:24:34 -04:00
case SFG_KEY_UP:
return sdlKeyboardState[SDL_SCANCODE_UP] ||
sdlKeyboardState[SDL_SCANCODE_W] ||
sdlKeyboardState[SDL_SCANCODE_KP_8];
break;
case SFG_KEY_RIGHT:
return sdlKeyboardState[SDL_SCANCODE_RIGHT] ||
sdlKeyboardState[SDL_SCANCODE_E] ||
sdlKeyboardState[SDL_SCANCODE_KP_6];
break;
case SFG_KEY_DOWN:
return sdlKeyboardState[SDL_SCANCODE_DOWN] ||
sdlKeyboardState[SDL_SCANCODE_S] ||
2019-10-04 15:09:10 -04:00
sdlKeyboardState[SDL_SCANCODE_KP_5] ||
sdlKeyboardState[SDL_SCANCODE_KP_2];
2019-10-03 14:24:34 -04:00
break;
case SFG_KEY_LEFT:
return sdlKeyboardState[SDL_SCANCODE_LEFT] ||
sdlKeyboardState[SDL_SCANCODE_Q] ||
sdlKeyboardState[SDL_SCANCODE_KP_4];
break;
case SFG_KEY_A:
return sdlKeyboardState[SDL_SCANCODE_G];
break;
case SFG_KEY_B:
2020-01-18 06:27:20 -05:00
return sdlKeyboardState[SDL_SCANCODE_H] || SDL_GetMouseState(0,0);
2019-10-03 14:24:34 -04:00
break;
case SFG_KEY_C:
return sdlKeyboardState[SDL_SCANCODE_J];
break;
case SFG_KEY_JUMP:
return sdlKeyboardState[SDL_SCANCODE_SPACE];
break;
case SFG_KEY_STRAFE_LEFT:
return sdlKeyboardState[SDL_SCANCODE_A] ||
sdlKeyboardState[SDL_SCANCODE_KP_7];
break;
case SFG_KEY_STRAFE_RIGHT:
return sdlKeyboardState[SDL_SCANCODE_D] ||
sdlKeyboardState[SDL_SCANCODE_KP_9];
break;
2019-10-02 08:42:30 -04:00
2019-10-08 07:46:12 -04:00
case SFG_KEY_MAP:
return sdlKeyboardState[SDL_SCANCODE_TAB];
break;
2020-01-18 06:27:20 -05:00
case SFG_KEY_TOGGLE_FREELOOK:
return sdlKeyboardState[SDL_SCANCODE_T];
break;
case SFG_KEY_NEXT_WEAPON:
2020-01-18 07:06:52 -05:00
return sdlKeyboardState[SDL_SCANCODE_M];
2020-01-18 06:27:20 -05:00
break;
case SFG_KEY_PREVIOUS_WEAPON:
2020-01-18 07:06:52 -05:00
return sdlKeyboardState[SDL_SCANCODE_N];
2020-01-18 06:27:20 -05:00
break;
2019-09-25 09:51:19 -04:00
default: return 0; break;
}
}
2020-01-18 06:27:20 -05:00
int running;
2019-09-25 09:51:19 -04:00
2020-01-05 06:47:25 -05:00
void mainLoopIteration()
{
SDL_PumpEvents(); // updates the keyboard state
if (sdlKeyboardState[SDL_SCANCODE_ESCAPE])
2020-01-18 06:27:20 -05:00
running = 0;
2020-01-05 06:47:25 -05:00
SFG_mainLoopBody();
SDL_UpdateTexture(texture,NULL,screen,SFG_SCREEN_RESOLUTION_X * sizeof(uint16_t));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
}
2020-01-18 06:27:20 -05:00
#ifdef __EMSCRIPTEN__
2020-01-05 06:47:25 -05:00
typedef void (*em_callback_func)(void);
void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop);
#endif
2020-02-04 11:17:00 -05:00
uint8_t audioBuff[SFG_SFX_SAMPLE_COUNT];
uint32_t audioPos = 0;
void audioFillCallback(void *userdata, uint8_t *s, int l)
{
if (audioPos >= SFG_SFX_SAMPLE_COUNT)
SDL_PauseAudio(1);
for (int i = 0; i < l; ++i)
if (audioPos < SFG_SFX_SAMPLE_COUNT)
{
s[i] = audioBuff[audioPos];
audioPos++;
}
else
s[i] = 127;
}
void SFG_playSound(uint8_t soundIndex, uint8_t volume)
{
for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i)
audioBuff[i] = SFG_GET_SFX_SAMPLE(soundIndex,i) * 16;
audioPos = 0;
SDL_PauseAudio(0);
}
2019-10-04 10:10:43 -04:00
int main(int argc, char *argv[])
2019-09-25 09:51:19 -04:00
{
2019-10-04 10:10:43 -04:00
uint8_t argHelp = 0;
uint8_t argForceWindow = 0;
uint8_t argForceFullscreen = 0;
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
printf("SDL: unknown argument: %s\n",argv[i]);
}
if (argHelp)
{
printf("TODOGAME, a suckless first person shooter game (SDL2 frontend)\n\n");
printf("version TODO, by Miloslav Ciz, released under CC0 1.0 + waiver of all IP\n");
printf("possible arguments:\n\n");
printf("-h print this help and end\n");
printf("-w force run in window\n");
2019-10-04 10:32:24 -04:00
printf("-f force run fullscreen\n\n");
printf("controls:\n");
printf("TODO\n");
2019-10-04 10:10:43 -04:00
return 0;
}
2019-09-29 07:50:40 -04:00
printf("SDL: starting\n");
2019-09-25 09:51:19 -04:00
2019-09-29 07:50:40 -04:00
printf("SDL: initializing SDL\n");
2019-09-25 09:51:19 -04:00
2020-01-05 06:47:25 -05:00
window =
2019-09-25 09:51:19 -04:00
SDL_CreateWindow("raycasting", SDL_WINDOWPOS_UNDEFINED,
2019-10-04 10:32:24 -04:00
SDL_WINDOWPOS_UNDEFINED, SFG_SCREEN_RESOLUTION_X, SFG_SCREEN_RESOLUTION_Y,
2019-09-25 09:51:19 -04:00
SDL_WINDOW_SHOWN);
2020-01-05 06:47:25 -05:00
renderer = SDL_CreateRenderer(window,-1,0);
2019-09-25 09:51:19 -04:00
2020-01-05 06:47:25 -05:00
texture =
2019-09-25 09:51:19 -04:00
SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGB565,SDL_TEXTUREACCESS_STATIC,
2019-10-04 10:32:24 -04:00
SFG_SCREEN_RESOLUTION_X,SFG_SCREEN_RESOLUTION_Y);
2019-09-25 09:51:19 -04:00
2020-01-05 06:47:25 -05:00
screenSurface = SDL_GetWindowSurface(window);
2019-09-25 09:51:19 -04:00
2019-09-29 08:54:40 -04:00
#if SFG_FULLSCREEN
2019-10-04 10:10:43 -04:00
argForceFullscreen = 1;
2019-09-29 08:54:40 -04:00
#endif
2019-10-04 10:10:43 -04:00
if (!argForceWindow && argForceFullscreen)
{
printf("SDL: setting fullscreen\n");
SDL_SetWindowFullscreen(window,SDL_WINDOW_FULLSCREEN_DESKTOP);
}
2019-09-25 09:51:19 -04:00
sdlKeyboardState = SDL_GetKeyboardState(NULL);
2020-01-18 05:10:05 -05:00
SDL_ShowCursor(0);
2020-02-04 11:17:00 -05:00
SFG_init(SDL_INIT_AUDIO);
SDL_AudioSpec audioSpec;
audioSpec.callback = audioFillCallback;
audioSpec.userdata = 0;
audioSpec.freq = 8000;
audioSpec.format = AUDIO_U8;
audioSpec.channels = 1;
if (SDL_OpenAudio(&audioSpec, NULL) < 0)
printf("SDL: could not initialize audio\n");
2019-09-29 07:50:40 -04:00
2020-01-18 06:27:20 -05:00
running = 1;
2019-09-25 09:51:19 -04:00
2020-01-05 06:47:25 -05:00
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(mainLoopIteration,0,1);
#else
2019-09-25 09:51:19 -04:00
while (running)
2020-01-18 06:27:20 -05:00
mainLoopIteration();
2020-01-05 06:47:25 -05:00
#endif
2019-09-25 09:51:19 -04:00
2019-09-29 07:50:40 -04:00
printf("SDL: freeing SDL\n");
2019-09-25 09:51:19 -04:00
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
2020-02-04 11:17:00 -05:00
SDL_CloseAudio();
2019-09-25 09:51:19 -04:00
2019-09-29 07:50:40 -04:00
printf("SDL: ending\n");
2019-09-25 09:51:19 -04:00
return 0;
}
#endif // guard