From 716a3dda38e30bc03a9c6bb37e9ea0cc65b120f2 Mon Sep 17 00:00:00 2001 From: Miloslav Ciz Date: Sun, 1 Jun 2025 19:59:09 +0200 Subject: [PATCH] Add frontend helper --- TODO.txt | 5 +-- frontend_csfml.c | 50 ++------------------------- frontend_helper.h | 87 +++++++++++++++++++++++++++++++++++++++++++++++ frontend_sdl.c | 67 ++---------------------------------- frontend_x11.c | 57 ++----------------------------- game.h | 6 ++-- settings.h | 6 ++-- 7 files changed, 105 insertions(+), 173 deletions(-) create mode 100644 frontend_helper.h diff --git a/TODO.txt b/TODO.txt index e52a64e..7560afc 100644 --- a/TODO.txt +++ b/TODO.txt @@ -2,8 +2,6 @@ fuck issue trackers :D =========== GENERAL ============== -- make helper header for PC frontends that use stdio for data file, parse - arguments etc. - frontends: - auto test frontend, with no I/O, that will just internally run a series of inputs and check if the output is as expected @@ -31,6 +29,7 @@ fuck issue trackers :D - empty and large data file - FPS on each platform - try to use the racing module by itself + - spellcheck comments =========== BUGS ================= @@ -40,6 +39,8 @@ fuck issue trackers :D =========== HANDLED ============== - should drifting make a sound? NO NEED +- make helper header for PC frontends that use stdio for data file, parse + arguments etc. - make car turned on its back behave nicer? but how? PROLLY NOT - Try doing the bouncy car body? Just keep a point and its velocity, change its velocity by a proportion of car's velocity change (this minus prev. diff --git a/frontend_csfml.c b/frontend_csfml.c index 9db8aaf..5454c98 100644 --- a/frontend_csfml.c +++ b/frontend_csfml.c @@ -9,21 +9,16 @@ #include #include -#define LCR_SETTING_LOG_LEVEL 2 #define DATA_FILE_NAME "data" -//#define LCR_LOADING_COMMAND SDL_PumpEvents(); - -// #define LCR_FPS_GET_MS SDL_GetTicks() // uncomment for FPS measuring - #include "game.h" +#include "frontend_helper.h" #define WINDOW_SIZE (LCR_SETTING_RESOLUTION_X * LCR_SETTING_RESOLUTION_Y) sfUint32 windowPixels[WINDOW_SIZE * 2]; uint8_t fullscreen = 1; FILE *musicFile = 0; -FILE *dataFile = 0; sfClock *clock; sfRenderWindow* window; sfSoundStream *sound; @@ -33,36 +28,6 @@ sfSoundStream *sound; int16_t audioBuffer[AUDIO_BUFFER_SIZE]; uint8_t musicBuffer[AUDIO_BUFFER_SIZE]; -char LCR_getNextDataFileChar(void) -{ - if (!dataFile) - return 0; - - int c = fgetc(dataFile); - - if (c == EOF) - { - rewind(dataFile); - return 0; - } - - return c; -} - -void LCR_appendDataStr(const char *str) -{ - fclose(dataFile); - - dataFile = fopen(DATA_FILE_NAME,"a"); - - if (dataFile) - { - fprintf(dataFile,"%s",str); - fclose(dataFile); - dataFile = fopen(DATA_FILE_NAME,"r"); - } -} - uint8_t LCR_keyPressed(uint8_t key) { #define k(x) sfKeyboard_isKeyPressed(sfKey ## x) @@ -94,11 +59,6 @@ void LCR_drawPixel(unsigned long index, uint16_t color) (((color << 19) | (color >> 8)) & 0x00f800f8) ; } -void LCR_log(const char *str) -{ - printf("LOG: %s\n",str); -} - void printHelp(void) { printf( @@ -147,10 +107,7 @@ int main(int argc, char *argv[]) default: break; } - dataFile = fopen(DATA_FILE_NAME,"r"); - - if (!dataFile) - LCR_log("couldn't open data file"); + openDataFile(); musicFile = fopen("assets/music","rb"); @@ -211,8 +168,7 @@ int main(int argc, char *argv[]) if (musicFile) fclose(musicFile); - if (dataFile) - fclose(dataFile); + closeDataFile(); sfSoundStream_stop(sound); sfSoundStream_destroy(sound); diff --git a/frontend_helper.h b/frontend_helper.h new file mode 100644 index 0000000..a0f249b --- /dev/null +++ b/frontend_helper.h @@ -0,0 +1,87 @@ +/** @file frontend_helper.h + + Helper file for generic PC frontends that can use the stdio library, to avoid + duplication of code. +*/ + +// TODO: quality presets? + +#include +#include + +#define DATA_FILE_NAME "data" + +FILE *dataFile = 0; + +char LCR_getNextDataFileChar(void) +{ +#ifdef __EMSCRIPTEN__ + return 0; +#else + if (!dataFile) + return 0; + + int c = fgetc(dataFile); + + if (c == EOF) + { + rewind(dataFile); + return 0; + } + + return c; +#endif +} + +void LCR_appendDataStr(const char *str) +{ +#ifndef __EMSCRIPTEN__ + if (!dataFile) + return; + + if (str == 0 || *str == 0) + rewind(dataFile); + else + { +#if LCR_SETTING_LOG_LEVEL > 1 + printf("SDL: appending data \"%s\"\n",str); +#endif + + fclose(dataFile); + + dataFile = fopen(DATA_FILE_NAME,"a"); + + if (dataFile) + { + fprintf(dataFile,"%s",str); + fclose(dataFile); + dataFile = fopen(DATA_FILE_NAME,"r"); + } + } +#else + printf("%s",str); +#endif +} + +void LCR_log(const char *str) +{ + printf("%s\n",str); +} + +void openDataFile(void) +{ +#ifndef __EMSCRIPTEN__ + dataFile = fopen(DATA_FILE_NAME,"r"); + + if (!dataFile) + puts("WARNING: couldn't open data file"); +#endif +} + +void closeDataFile(void) +{ +#ifndef __EMSCRIPTEN__ + if (dataFile) + fclose(dataFile); +#endif +} diff --git a/frontend_sdl.c b/frontend_sdl.c index b3e2321..ee2cf3a 100644 --- a/frontend_sdl.c +++ b/frontend_sdl.c @@ -6,7 +6,6 @@ #include #define LCR_SETTING_LOG_LEVEL 2 -#define DATA_FILE_NAME "data" #define LCR_LOADING_COMMAND SDL_PumpEvents(); #define LCR_FPS_GET_MS SDL_GetTicks() // uncomment for FPS measuring @@ -21,6 +20,7 @@ #endif #include "game.h" +#include "frontend_helper.h" SDL_Window *window; SDL_Renderer *renderer; @@ -37,57 +37,6 @@ uint16_t screen[LCR_SETTING_RESOLUTION_X * LCR_SETTING_RESOLUTION_Y]; FILE *musicFile = 0; -FILE *dataFile = 0; - -char LCR_getNextDataFileChar(void) -{ -#ifdef __EMSCRIPTEN__ - return 0; -#else - if (!dataFile) - return 0; - - int c = fgetc(dataFile); - - if (c == EOF) - { - rewind(dataFile); - return 0; - } - - return c; -#endif -} - -void LCR_appendDataStr(const char *str) -{ -#ifndef __EMSCRIPTEN__ - if (!dataFile) - return; - - if (str == 0 || *str == 0) - rewind(dataFile); - else - { -#if LCR_SETTING_LOG_LEVEL > 1 - printf("SDL: appending data \"%s\"\n",str); -#endif - - fclose(dataFile); - - dataFile = fopen(DATA_FILE_NAME,"a"); - - if (dataFile) - { - fprintf(dataFile,"%s",str); - fclose(dataFile); - dataFile = fopen(DATA_FILE_NAME,"r"); - } - } -#else - printf("%s",str); -#endif -} void audioFillCallback(void *userdata, uint8_t *s, int l) { @@ -153,11 +102,6 @@ void LCR_drawPixel(unsigned long index, uint16_t color) screen[index] = color; } -void LCR_log(const char *str) -{ - printf("LOG: %s\n",str); -} - void printHelp(void) { printf( @@ -216,10 +160,8 @@ int main(int argc, char *argv[]) #ifdef __EMSCRIPTEN__ fullscreen = 0; #else - dataFile = fopen(DATA_FILE_NAME,"r"); - if (!dataFile) - LCR_log("couldn't open data file"); + openDataFile(); musicFile = fopen("assets/music","rb"); @@ -311,10 +253,7 @@ int main(int argc, char *argv[]) if (musicFile) fclose(musicFile); -#ifndef __EMSCRIPTEN__ - if (dataFile) - fclose(dataFile); -#endif + closeDataFile(); SDL_PauseAudio(1); SDL_CloseAudio(); diff --git a/frontend_x11.c b/frontend_x11.c index 7164911..a48386f 100644 --- a/frontend_x11.c +++ b/frontend_x11.c @@ -15,12 +15,10 @@ #define LCR_SETTING_RESOLUTION_Y 240 #define LCR_SETTING_MUSIC 0 -#define DATA_FILE_NAME "data" - #include "game.h" +#include "frontend_helper.h" char framebuffer[LCR_SETTING_RESOLUTION_X * LCR_SETTING_RESOLUTION_Y * 4]; -FILE *dataFile = 0; uint8_t buttonStates[8]; void LCR_drawPixel(unsigned long index, uint16_t color) @@ -34,44 +32,6 @@ void LCR_drawPixel(unsigned long index, uint16_t color) *p = (color >> 8) & 0xf8; } -char LCR_getNextDataFileChar(void) -{ - if (!dataFile) - return 0; - - int c = fgetc(dataFile); - - if (c == EOF) - { - rewind(dataFile); - return 0; - } - - return c; -} - -void LCR_appendDataStr(const char *str) -{ - if (!dataFile) - return; - - if (str == 0 || *str == 0) - rewind(dataFile); - else - { - fclose(dataFile); - - dataFile = fopen(DATA_FILE_NAME,"a"); - - if (dataFile) - { - fprintf(dataFile,"%s",str); - fclose(dataFile); - dataFile = fopen(DATA_FILE_NAME,"r"); - } - } -} - uint8_t LCR_keyPressed(uint8_t key) { return buttonStates[key % 8]; @@ -82,11 +42,6 @@ void LCR_sleep(uint16_t timeMs) usleep(timeMs * 1000); } -void LCR_log(const char *str) -{ - printf("LOG: %s\n",str); -} - void printHelp(void) { printf( @@ -113,10 +68,7 @@ int main(int argc, char **argv) Display *display = XOpenDisplay(0); int screen = DefaultScreen(display); - dataFile = fopen(DATA_FILE_NAME,"r"); - - if (!dataFile) - LCR_log("couldn't open data file"); + openDataFile(); Window window = XCreateSimpleWindow(display,RootWindow(display,screen),10,10, LCR_SETTING_RESOLUTION_X,LCR_SETTING_RESOLUTION_Y,1, @@ -189,10 +141,7 @@ int main(int argc, char **argv) XDestroyImage(image); XCloseDisplay(display); - - if (dataFile) - fclose(dataFile); - + closeDataFile(); LCR_gameEnd(); return 0; diff --git a/game.h b/game.h index 51ef82e..ee6f66f 100644 --- a/game.h +++ b/game.h @@ -1390,14 +1390,14 @@ void LCR_gameHandleInput(void) else if (LCR_game.state == LCR_GAME_STATE_RUN_STARTING) { if (LCR_game.time - LCR_game.stateStartTime - >= 1000 * LCR_SETTING_COUNTDOWN_SECONDS) + >= LCR_SETTING_COUNTDOWN_MS) { LCR_gameSetState(LCR_GAME_STATE_RUN); LCR_gamePopupMessage(""); } else - LCR_gamePopupNumber(LCR_SETTING_COUNTDOWN_SECONDS - - (LCR_game.time - LCR_game.stateStartTime) / 1000); + LCR_gamePopupNumber(1 + (LCR_SETTING_COUNTDOWN_MS - + (LCR_game.time - LCR_game.stateStartTime)) / 1000); } if (LCR_game.keyStates[LCR_KEY_B] == 1) diff --git a/settings.h b/settings.h index 3188724..3d564b6 100644 --- a/settings.h +++ b/settings.h @@ -195,9 +195,9 @@ #define LCR_SETTING_ENABLE_DATA_FILE 1 #endif -#ifndef LCR_SETTING_COUNTDOWN_SECONDS - /** Run start countdown length in seconds. */ - #define LCR_SETTING_COUNTDOWN_SECONDS 3 //1 // for release make 3 +#ifndef LCR_SETTING_COUNTDOWN_MS + /** Run start countdown length in milliseconds. */ + #define LCR_SETTING_COUNTDOWN_MS 2850 #endif #ifndef LCR_SETTING_MAP_CHUNK_RELOAD_INTERVAL