Add frontend helper

This commit is contained in:
Miloslav Ciz 2025-06-01 19:59:09 +02:00
parent 765c742f08
commit 716a3dda38
7 changed files with 105 additions and 173 deletions

View file

@ -2,8 +2,6 @@ fuck issue trackers :D
=========== GENERAL ============== =========== GENERAL ==============
- make helper header for PC frontends that use stdio for data file, parse
arguments etc.
- frontends: - frontends:
- auto test frontend, with no I/O, that will just internally run a series of - auto test frontend, with no I/O, that will just internally run a series of
inputs and check if the output is as expected inputs and check if the output is as expected
@ -31,6 +29,7 @@ fuck issue trackers :D
- empty and large data file - empty and large data file
- FPS on each platform - FPS on each platform
- try to use the racing module by itself - try to use the racing module by itself
- spellcheck comments
=========== BUGS ================= =========== BUGS =================
@ -40,6 +39,8 @@ fuck issue trackers :D
=========== HANDLED ============== =========== HANDLED ==============
- should drifting make a sound? NO NEED - 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 - 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 - 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. its velocity by a proportion of car's velocity change (this minus prev.

View file

@ -9,21 +9,16 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#define LCR_SETTING_LOG_LEVEL 2
#define DATA_FILE_NAME "data" #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 "game.h"
#include "frontend_helper.h"
#define WINDOW_SIZE (LCR_SETTING_RESOLUTION_X * LCR_SETTING_RESOLUTION_Y) #define WINDOW_SIZE (LCR_SETTING_RESOLUTION_X * LCR_SETTING_RESOLUTION_Y)
sfUint32 windowPixels[WINDOW_SIZE * 2]; sfUint32 windowPixels[WINDOW_SIZE * 2];
uint8_t fullscreen = 1; uint8_t fullscreen = 1;
FILE *musicFile = 0; FILE *musicFile = 0;
FILE *dataFile = 0;
sfClock *clock; sfClock *clock;
sfRenderWindow* window; sfRenderWindow* window;
sfSoundStream *sound; sfSoundStream *sound;
@ -33,36 +28,6 @@ sfSoundStream *sound;
int16_t audioBuffer[AUDIO_BUFFER_SIZE]; int16_t audioBuffer[AUDIO_BUFFER_SIZE];
uint8_t musicBuffer[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) uint8_t LCR_keyPressed(uint8_t key)
{ {
#define k(x) sfKeyboard_isKeyPressed(sfKey ## x) #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) ; (((color << 19) | (color >> 8)) & 0x00f800f8) ;
} }
void LCR_log(const char *str)
{
printf("LOG: %s\n",str);
}
void printHelp(void) void printHelp(void)
{ {
printf( printf(
@ -147,10 +107,7 @@ int main(int argc, char *argv[])
default: break; default: break;
} }
dataFile = fopen(DATA_FILE_NAME,"r"); openDataFile();
if (!dataFile)
LCR_log("couldn't open data file");
musicFile = fopen("assets/music","rb"); musicFile = fopen("assets/music","rb");
@ -211,8 +168,7 @@ int main(int argc, char *argv[])
if (musicFile) if (musicFile)
fclose(musicFile); fclose(musicFile);
if (dataFile) closeDataFile();
fclose(dataFile);
sfSoundStream_stop(sound); sfSoundStream_stop(sound);
sfSoundStream_destroy(sound); sfSoundStream_destroy(sound);

87
frontend_helper.h Normal file
View file

@ -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 <stdint.h>
#include <stdio.h>
#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
}

View file

@ -6,7 +6,6 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#define LCR_SETTING_LOG_LEVEL 2 #define LCR_SETTING_LOG_LEVEL 2
#define DATA_FILE_NAME "data"
#define LCR_LOADING_COMMAND SDL_PumpEvents(); #define LCR_LOADING_COMMAND SDL_PumpEvents();
#define LCR_FPS_GET_MS SDL_GetTicks() // uncomment for FPS measuring #define LCR_FPS_GET_MS SDL_GetTicks() // uncomment for FPS measuring
@ -21,6 +20,7 @@
#endif #endif
#include "game.h" #include "game.h"
#include "frontend_helper.h"
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
@ -37,57 +37,6 @@ uint16_t
screen[LCR_SETTING_RESOLUTION_X * LCR_SETTING_RESOLUTION_Y]; screen[LCR_SETTING_RESOLUTION_X * LCR_SETTING_RESOLUTION_Y];
FILE *musicFile = 0; 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) 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; screen[index] = color;
} }
void LCR_log(const char *str)
{
printf("LOG: %s\n",str);
}
void printHelp(void) void printHelp(void)
{ {
printf( printf(
@ -216,10 +160,8 @@ int main(int argc, char *argv[])
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
fullscreen = 0; fullscreen = 0;
#else #else
dataFile = fopen(DATA_FILE_NAME,"r");
if (!dataFile) openDataFile();
LCR_log("couldn't open data file");
musicFile = fopen("assets/music","rb"); musicFile = fopen("assets/music","rb");
@ -311,10 +253,7 @@ int main(int argc, char *argv[])
if (musicFile) if (musicFile)
fclose(musicFile); fclose(musicFile);
#ifndef __EMSCRIPTEN__ closeDataFile();
if (dataFile)
fclose(dataFile);
#endif
SDL_PauseAudio(1); SDL_PauseAudio(1);
SDL_CloseAudio(); SDL_CloseAudio();

View file

@ -15,12 +15,10 @@
#define LCR_SETTING_RESOLUTION_Y 240 #define LCR_SETTING_RESOLUTION_Y 240
#define LCR_SETTING_MUSIC 0 #define LCR_SETTING_MUSIC 0
#define DATA_FILE_NAME "data"
#include "game.h" #include "game.h"
#include "frontend_helper.h"
char framebuffer[LCR_SETTING_RESOLUTION_X * LCR_SETTING_RESOLUTION_Y * 4]; char framebuffer[LCR_SETTING_RESOLUTION_X * LCR_SETTING_RESOLUTION_Y * 4];
FILE *dataFile = 0;
uint8_t buttonStates[8]; uint8_t buttonStates[8];
void LCR_drawPixel(unsigned long index, uint16_t color) 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; *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) uint8_t LCR_keyPressed(uint8_t key)
{ {
return buttonStates[key % 8]; return buttonStates[key % 8];
@ -82,11 +42,6 @@ void LCR_sleep(uint16_t timeMs)
usleep(timeMs * 1000); usleep(timeMs * 1000);
} }
void LCR_log(const char *str)
{
printf("LOG: %s\n",str);
}
void printHelp(void) void printHelp(void)
{ {
printf( printf(
@ -113,10 +68,7 @@ int main(int argc, char **argv)
Display *display = XOpenDisplay(0); Display *display = XOpenDisplay(0);
int screen = DefaultScreen(display); int screen = DefaultScreen(display);
dataFile = fopen(DATA_FILE_NAME,"r"); openDataFile();
if (!dataFile)
LCR_log("couldn't open data file");
Window window = XCreateSimpleWindow(display,RootWindow(display,screen),10,10, Window window = XCreateSimpleWindow(display,RootWindow(display,screen),10,10,
LCR_SETTING_RESOLUTION_X,LCR_SETTING_RESOLUTION_Y,1, LCR_SETTING_RESOLUTION_X,LCR_SETTING_RESOLUTION_Y,1,
@ -189,10 +141,7 @@ int main(int argc, char **argv)
XDestroyImage(image); XDestroyImage(image);
XCloseDisplay(display); XCloseDisplay(display);
closeDataFile();
if (dataFile)
fclose(dataFile);
LCR_gameEnd(); LCR_gameEnd();
return 0; return 0;

6
game.h
View file

@ -1390,14 +1390,14 @@ void LCR_gameHandleInput(void)
else if (LCR_game.state == LCR_GAME_STATE_RUN_STARTING) else if (LCR_game.state == LCR_GAME_STATE_RUN_STARTING)
{ {
if (LCR_game.time - LCR_game.stateStartTime if (LCR_game.time - LCR_game.stateStartTime
>= 1000 * LCR_SETTING_COUNTDOWN_SECONDS) >= LCR_SETTING_COUNTDOWN_MS)
{ {
LCR_gameSetState(LCR_GAME_STATE_RUN); LCR_gameSetState(LCR_GAME_STATE_RUN);
LCR_gamePopupMessage(""); LCR_gamePopupMessage("");
} }
else else
LCR_gamePopupNumber(LCR_SETTING_COUNTDOWN_SECONDS - LCR_gamePopupNumber(1 + (LCR_SETTING_COUNTDOWN_MS -
(LCR_game.time - LCR_game.stateStartTime) / 1000); (LCR_game.time - LCR_game.stateStartTime)) / 1000);
} }
if (LCR_game.keyStates[LCR_KEY_B] == 1) if (LCR_game.keyStates[LCR_KEY_B] == 1)

View file

@ -195,9 +195,9 @@
#define LCR_SETTING_ENABLE_DATA_FILE 1 #define LCR_SETTING_ENABLE_DATA_FILE 1
#endif #endif
#ifndef LCR_SETTING_COUNTDOWN_SECONDS #ifndef LCR_SETTING_COUNTDOWN_MS
/** Run start countdown length in seconds. */ /** Run start countdown length in milliseconds. */
#define LCR_SETTING_COUNTDOWN_SECONDS 3 //1 // for release make 3 #define LCR_SETTING_COUNTDOWN_MS 2850
#endif #endif
#ifndef LCR_SETTING_MAP_CHUNK_RELOAD_INTERVAL #ifndef LCR_SETTING_MAP_CHUNK_RELOAD_INTERVAL