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 ==============
- 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.

View file

@ -9,21 +9,16 @@
#include <stdint.h>
#include <stdio.h>
#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);

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>
#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();

View file

@ -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;

6
game.h
View file

@ -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)

View file

@ -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