Son de l'intro (voix 16 bits) et menu de réglages F12

- audio.c : flux 16 bits non signés (voix de l'intro, qui saturaient),
  pause du mixeur pendant le menu sauf les sons en boucle.
- ail.c : retrait de la trace de débogage.
- menu.c/menu.h : menu en surimpression indépendant de SDL (volumes,
  plein écran, taille de fenêtre, lissage, format 4:3) et fichier de réglages.
- menu_font.h : police Roboto (Apache 2.0) générée par tools/gen_font.py.
- host_sdl.c : F12 ouvre le menu ; le temps du jeu est figé pendant le menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
linkatplugandClaude Opus 5.5 committed 2026-12-05 02:55:52 +01:00
1 parent e91fa9e8ba
commit 90c398d531
9 files changed
+4525 -28

No files matched your search

+2 -2
View File
@@ -15,11 +15,11 @@ if /i not "%1"=="nogen" (
if not exist build\obj mkdir build\obj
set CFLAGS=/nologo /O2 /GS- /W3 /wd4146 /wd4244 /wd4018 /wd4102 /wd4101 /wd4189 /MP /utf-8 /Iruntime /Ibuild\gen_tucker /Ibuild\gen_intro /I%SDL%\include /D_CRT_SECURE_NO_WARNINGS /Fobuild\obj\
cl %CFLAGS% /c runtime\cpu.c runtime\fpu.c runtime\loader.c runtime\dos.c runtime\machine.c runtime\audio.c runtime\ail.c runtime\overrides.c runtime\main.c build\gen_tucker\*.c build\gen_intro\*.c || exit /b 1
cl %CFLAGS% /c runtime\cpu.c runtime\fpu.c runtime\loader.c runtime\dos.c runtime\machine.c runtime\audio.c runtime\ail.c runtime\menu.c runtime\overrides.c runtime\main.c build\gen_tucker\*.c build\gen_intro\*.c || exit /b 1
cl %CFLAGS% /c runtime\host_headless.c runtime\host_sdl.c || exit /b 1
if not exist build\lib mkdir build\lib
lib /nologo /OUT:build\lib\jeu.lib build\obj\cpu.obj build\obj\fpu.obj build\obj\loader.obj build\obj\dos.obj build\obj\machine.obj build\obj\audio.obj build\obj\ail.obj build\obj\overrides.obj build\obj\main.obj build\obj\tucker_*.obj build\obj\intro_*.obj || exit /b 1
lib /nologo /OUT:build\lib\jeu.lib build\obj\cpu.obj build\obj\fpu.obj build\obj\loader.obj build\obj\dos.obj build\obj\machine.obj build\obj\audio.obj build\obj\ail.obj build\obj\menu.obj build\obj\overrides.obj build\obj\main.obj build\obj\tucker_*.obj build\obj\intro_*.obj || exit /b 1
link /nologo /STACK:67108864 /OUT:build\tucker_headless.exe build\obj\host_headless.obj build\lib\jeu.lib || exit /b 1
link /nologo /STACK:67108864 /SUBSYSTEM:CONSOLE /OUT:build\tucker.exe build\obj\host_sdl.obj build\lib\jeu.lib %SDL%\lib\x64\SDL2.lib || exit /b 1
-5
View File
@@ -168,10 +168,5 @@ void ail_load_sample_buffer(void) { /* (HSAMPLE, U32 n°, void *donné
audio_v_group(v, group_of(h, AUDIO_SPEECH));
audio_v_queue(v, (int)n, M + (p & MEM_MASK), len > (16u << 20) ? 0 : len);
}
if (rt_cfg.verbose) {
static int shown;
if (shown++ < 6) fprintf(stderr, "[ail] load_sample_buffer(%08x, %u, %08x, %u) %02x %02x %02x %02x %02x %02x %02x %02x\n",
h, n, p, len, RD8(p), RD8(p + 1), RD8(p + 2), RD8(p + 3), RD8(p + 4), RD8(p + 5), RD8(p + 6), RD8(p + 7));
}
RET(0);
}
+8 -4
View File
@@ -35,19 +35,22 @@ typedef struct {
static Voice voices[AUDIO_VOICES];
int audio_volume[AUDIO_GROUPS] = {100, 100, 100}; /* réglages utilisateur, en % */
int audio_master = 100;
int audio_hold; /* menu ouvert : seules les boucles (ambiances, musique) continuent */
static int fixed_voice(int group, int ch) {
return group == AUDIO_FX ? ch : group == AUDIO_MUSIC ? AUDIO_FX_CH + ch : AUDIO_FX_CH + AUDIO_MUS_CH;
}
/* ------------------------------------------------------------------ WAV → PCM 16 bits */
static int16_t *to_pcm16(const uint8_t *d, uint32_t len, int bits16, int sign8, uint32_t *samples) {
/* signed = 0 : données non signées (8 ou 16 bits), comme AIL sans DIG_PCM_SIGN */
static int16_t *to_pcm16(const uint8_t *d, uint32_t len, int bits16, int is_signed, uint32_t *samples) {
uint32_t i, n = bits16 ? len / 2 : len;
uint16_t flip = is_signed ? 0 : 0x8000;
int16_t *p = (int16_t *)malloc((n ? n : 1) * sizeof(int16_t));
if (!p) return NULL;
for (i = 0; i < n; i++)
p[i] = bits16 ? (int16_t)(d[2 * i] | d[2 * i + 1] << 8)
: (int16_t)(sign8 ? (int8_t)d[i] << 8 : (d[i] - 128) << 8);
p[i] = bits16 ? (int16_t)((uint16_t)(d[2 * i] | d[2 * i + 1] << 8) ^ flip)
: (int16_t)(is_signed ? (int8_t)d[i] << 8 : (d[i] - 128) << 8);
*samples = n;
return p;
}
@@ -73,7 +76,7 @@ static int wav_decode(const uint8_t *d, uint32_t len, Chunk *c, int *chans, uint
p += 8 + sz + (sz & 1);
}
if (tag != 1 || !data_off || !*rate || (bits != 8 && bits != 16) || fmt_ch < 1 || fmt_ch > 2) return 0;
c->pcm = to_pcm16(d + data_off, data_len, bits == 16, 0, &n);
c->pcm = to_pcm16(d + data_off, data_len, bits == 16, bits == 16, &n); /* WAV : 8 bits non signé, 16 bits signé */
if (!c->pcm) return 0;
*chans = (int)fmt_ch;
c->frames = n / fmt_ch;
@@ -104,6 +107,7 @@ void audio_mix(int16_t *out, int frames) {
Voice *v = &voices[k];
int32_t gain;
if (!v->active || v->paused) continue;
if (audio_hold && (v->streaming || v->loops != 0)) continue; /* reprend exactement au même endroit */
gain = v->vol * audio_volume[v->group] * audio_master / 100; /* 0..12700 */
for (i = 0; i < n; i++) {
const Chunk *c = cur_chunk(v);
+174 -17
View File
@@ -1,8 +1,9 @@
/* Hôte SDL2 : fenêtre, temps réel, souris, clavier.
/* Hôte SDL2 : fenêtre, temps réel, souris, clavier, menu de réglages.
* --echelle N taille de la fenêtre (défaut 3 → 960×720)
* --plein démarre en plein écran
* F11 ou Alt+Entrée : plein écran ; fermer la fenêtre : quitter.
* L'image 320×200 est affichée au format 4:3 (pixels plus hauts que larges, comme sur un écran VGA). */
* F12 : menu de réglages (le jeu est en pause) ; F11 ou Alt+Entrée : plein écran ; fermer la fenêtre : quitter.
* Réglages enregistrés dans <saves>/reglages.ini ; les options de la ligne de commande priment.
* Par défaut l'image 320×200 est affichée au format 4:3 (pixels plus hauts que larges, comme sur un écran VGA). */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -10,14 +11,19 @@
#include <SDL.h>
#include "cpu.h"
#include "rt.h"
#include "menu.h"
int host_quit;
static SDL_Window *win;
static SDL_Renderer *ren;
static SDL_Texture *tex;
static SDL_Texture *tex, *ovl; /* image du jeu ; surimpression (menu, bulle d'aide) */
static uint32_t *ovl_px;
static SDL_AudioDeviceID adev;
static uint64_t t0, freq;
static uint64_t t0, freq, toast_t0;
static int mx = 160, my = 100, mb;
static int game_h = 240; /* hauteur logique : 240 (4:3) ou 200 (pixels carrés) */
static int swallow_esc_up; /* Échap a fermé le menu : son relâchement ne va pas au jeu */
static char cfg_path[600];
static uint64_t press_until[3]; /* un appui reste visible au moins 80 ms : le jeu lit l'état des boutons à chaque image */
/* ------------------------------------------------------------------ clavier : SDL → scancodes PC (jeu 1) */
@@ -75,26 +81,75 @@ static void SDLCALL audio_cb(void *user, Uint8 *stream, int len) {
void host_audio_lock(void) { if (adev) SDL_LockAudioDevice(adev); }
void host_audio_unlock(void) { if (adev) SDL_UnlockAudioDevice(adev); }
/* ------------------------------------------------------------------ affichage */
/* plus grande échelle de fenêtre qui tient dans l'espace utilisable de l'écran */
static int max_scale(void) {
SDL_Rect r;
int d = win ? SDL_GetWindowDisplayIndex(win) : 0, m;
if (d < 0 || SDL_GetDisplayUsableBounds(d, &r) != 0) return 4;
m = r.w / 320;
if ((r.h - 40) / 240 < m) m = (r.h - 40) / 240; /* place de la barre de titre */
return m < 1 ? 1 : m;
}
/* applique settings à la fenêtre ; ne redimensionne que si l'échelle ou le format ont changé */
static void apply_display(void) {
static int done_scale = -1, done_fmt = -1, done_full = -1;
int full = (SDL_GetWindowFlags(win) & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
game_h = settings.format_43 ? 240 : 200;
if (full != settings.plein_ecran) SDL_SetWindowFullscreen(win, settings.plein_ecran ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
if (!settings.plein_ecran && (settings.echelle != done_scale || settings.format_43 != done_fmt || done_full == 1)) {
SDL_SetWindowSize(win, 320 * settings.echelle, game_h * settings.echelle);
SDL_SetWindowPosition(win, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
}
done_scale = settings.echelle; done_fmt = settings.format_43; done_full = settings.plein_ecran;
SDL_SetTextureScaleMode(tex, settings.lissage ? SDL_ScaleModeLinear : SDL_ScaleModeNearest);
SDL_RenderSetLogicalSize(ren, 320, game_h);
}
/* surimpression : repère 4:3 de MENU_W × MENU_H, quel que soit le format du jeu */
static void draw_overlay(int alpha) {
SDL_RenderSetLogicalSize(ren, MENU_W, MENU_H);
SDL_SetTextureAlphaMod(ovl, (Uint8)alpha);
SDL_RenderCopy(ren, ovl, NULL, NULL);
SDL_RenderSetLogicalSize(ren, 320, game_h);
}
/* ------------------------------------------------------------------ cycle de vie */
int host_init(int argc, char **argv) {
int i, scale = 3, full = 0;
int i, scale = 0, full = 0;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--echelle") && i + 1 < argc) scale = atoi(argv[i + 1]);
else if (!strcmp(argv[i], "--plein")) full = 1;
}
if (scale < 1) scale = 1;
settings_defaults();
snprintf(cfg_path, sizeof cfg_path, "%s/reglages.ini", rt_cfg.save_dir);
settings_load(cfg_path);
if (scale >= 1) settings.echelle = scale;
if (full) settings.plein_ecran = 1;
settings_apply_audio();
SDL_SetMainReady();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) != 0) {
fprintf(stderr, "SDL : %s\n", SDL_GetError());
return 0;
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
win = SDL_CreateWindow("Bud Tucker in Double Trouble (recomp)", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
320 * scale, 240 * scale, SDL_WINDOW_RESIZABLE | (full ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
if (settings.echelle > max_scale()) settings.echelle = max_scale();
game_h = settings.format_43 ? 240 : 200;
win = SDL_CreateWindow("Bud Tucker in Double Trouble (recomp) — F12 : réglages", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 320 * settings.echelle, game_h * settings.echelle,
SDL_WINDOW_RESIZABLE | (settings.plein_ecran ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
ren = win ? SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED) : NULL;
if (!ren) { fprintf(stderr, "SDL : %s\n", SDL_GetError()); return 0; }
SDL_RenderSetLogicalSize(ren, 320, 240); /* 4:3 ; souris convertie par SDL */
tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 200);
ovl = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MENU_W, MENU_H);
ovl_px = (uint32_t *)malloc((size_t)MENU_W * MENU_H * 4);
if (!tex || !ovl || !ovl_px) { fprintf(stderr, "SDL : %s\n", SDL_GetError()); return 0; }
SDL_SetTextureBlendMode(ovl, SDL_BLENDMODE_BLEND);
SDL_SetTextureScaleMode(ovl, SDL_ScaleModeLinear);
apply_display();
menu_draw_toast(ovl_px, "F12 : réglages");
SDL_UpdateTexture(ovl, NULL, ovl_px, MENU_W * 4);
SDL_ShowCursor(SDL_DISABLE); /* le jeu dessine son propre curseur */
{
SDL_AudioSpec want, got;
@@ -106,12 +161,14 @@ int host_init(int argc, char **argv) {
else fprintf(stderr, "SDL : pas de sortie audio (%s)\n", SDL_GetError());
}
freq = SDL_GetPerformanceFrequency();
t0 = SDL_GetPerformanceCounter();
t0 = toast_t0 = SDL_GetPerformanceCounter();
return 1;
}
void host_shutdown(void) {
if (adev) SDL_CloseAudioDevice(adev);
if (ovl) SDL_DestroyTexture(ovl);
free(ovl_px);
if (tex) SDL_DestroyTexture(tex);
if (ren) SDL_DestroyRenderer(ren);
if (win) SDL_DestroyWindow(win);
@@ -142,9 +199,101 @@ void host_frame(void) {
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
SDL_RenderClear(ren);
SDL_RenderCopy(ren, tex, NULL, NULL);
if (toast_t0) { /* bulle « F12 : réglages » : 4 s, puis fondu d'1 s */
double t = (double)(SDL_GetPerformanceCounter() - toast_t0) / (double)freq;
if (t >= 5.0) toast_t0 = 0;
else draw_overlay(t < 4.0 ? 255 : (int)(255 * (5.0 - t)));
}
SDL_RenderPresent(ren);
}
/* ------------------------------------------------------------------ menu de réglages (modal : le jeu est en pause) */
static int is_fullscreen_key(const SDL_KeyboardEvent *k) {
return k->keysym.scancode == SDL_SCANCODE_F11 ||
((k->keysym.scancode == SDL_SCANCODE_RETURN || k->keysym.scancode == SDL_SCANCODE_KP_ENTER) && (k->keysym.mod & KMOD_ALT));
}
static void menu_render(void) {
SDL_Rect game;
menu_draw(ovl_px);
SDL_UpdateTexture(ovl, NULL, ovl_px, MENU_W * 4);
SDL_RenderSetLogicalSize(ren, MENU_W, MENU_H); /* souris du menu dans ce repère */
game.x = 0; game.w = MENU_W; game.h = MENU_W * game_h / 320; game.y = (MENU_H - game.h) / 2;
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
SDL_RenderClear(ren);
SDL_RenderCopy(ren, tex, NULL, &game); /* dernière image du jeu, figée */
SDL_SetTextureAlphaMod(ovl, 255);
SDL_RenderCopy(ren, ovl, NULL, NULL);
SDL_RenderPresent(ren);
}
static void menu_modal(void) {
uint64_t start = SDL_GetPerformanceCounter();
int open = 1, dirty = 1, pmx = -1, pmy = -1;
SDL_Event e;
menu_open(max_scale());
toast_t0 = 0;
host_audio_lock(); audio_hold = 1; host_audio_unlock();
SDL_ShowCursor(SDL_ENABLE);
SDL_RenderSetLogicalSize(ren, MENU_W, MENU_H);
while (open && !host_quit) {
int r = 0;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT: host_quit = 1; break;
case SDL_KEYDOWN: {
SDL_Scancode s = e.key.keysym.scancode;
dirty = 1;
if (is_fullscreen_key(&e.key)) { settings.plein_ecran ^= 1; r |= MENU_DISPLAY; break; }
switch (s) {
case SDL_SCANCODE_F12: if (!e.key.repeat) r |= MENU_CLOSE; break;
case SDL_SCANCODE_ESCAPE: if (!e.key.repeat) { r |= MENU_CLOSE; swallow_esc_up = 1; } break;
case SDL_SCANCODE_UP: case SDL_SCANCODE_KP_8: r |= menu_key(MK_UP); break;
case SDL_SCANCODE_DOWN: case SDL_SCANCODE_KP_2: case SDL_SCANCODE_TAB: r |= menu_key(MK_DOWN); break;
case SDL_SCANCODE_LEFT: case SDL_SCANCODE_KP_4: r |= menu_key(MK_LEFT); break;
case SDL_SCANCODE_RIGHT: case SDL_SCANCODE_KP_6: r |= menu_key(MK_RIGHT); break;
case SDL_SCANCODE_HOME: r |= menu_key(MK_HOME); break;
case SDL_SCANCODE_END: r |= menu_key(MK_END); break;
case SDL_SCANCODE_RETURN: case SDL_SCANCODE_KP_ENTER: case SDL_SCANCODE_SPACE:
if (!e.key.repeat) r |= menu_key(MK_OK);
break;
default: break;
}
break;
}
case SDL_MOUSEMOTION:
pmx = e.motion.x; pmy = e.motion.y;
r |= menu_mouse(pmx, pmy, 0, 0, 0); dirty = 1;
break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP:
pmx = e.button.x; pmy = e.button.y;
if (e.button.button == SDL_BUTTON_LEFT)
r |= menu_mouse(pmx, pmy, e.type == SDL_MOUSEBUTTONDOWN, e.type == SDL_MOUSEBUTTONUP, 0);
else if (e.button.button == SDL_BUTTON_RIGHT && e.type == SDL_MOUSEBUTTONDOWN) r |= MENU_CLOSE;
dirty = 1;
break;
case SDL_MOUSEWHEEL: /* agit sur la rangée survolée */
if (e.wheel.y) r |= menu_mouse(pmx, pmy, 0, 0, e.wheel.y > 0 ? 1 : -1);
dirty = 1;
break;
case SDL_WINDOWEVENT: dirty = 1; break;
default: break;
}
}
if (r & MENU_DISPLAY) apply_display();
if (r & MENU_QUIT) host_quit = 1;
if (r & MENU_CLOSE) open = 0;
if (dirty && open) { SDL_RenderSetLogicalSize(ren, MENU_W, MENU_H); menu_render(); dirty = 0; }
SDL_Delay(8);
}
settings_save(cfg_path);
SDL_RenderSetLogicalSize(ren, 320, game_h);
SDL_ShowCursor(SDL_DISABLE);
host_audio_lock(); audio_hold = 0; host_audio_unlock();
mb = 0; memset(press_until, 0, sizeof press_until);
t0 += SDL_GetPerformanceCounter() - start; /* le temps du jeu ne s'est pas écoulé pendant le menu */
}
/* ------------------------------------------------------------------ événements */
void host_pump(void) {
SDL_Event e;
@@ -152,21 +301,29 @@ void host_pump(void) {
switch (e.type) {
case SDL_QUIT: host_quit = 1; break;
case SDL_MOUSEMOTION:
mx = e.motion.x; my = e.motion.y * 200 / 240;
mx = e.motion.x; my = e.motion.y * 200 / game_h;
break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: {
int bit = e.button.button == SDL_BUTTON_LEFT ? 1 : e.button.button == SDL_BUTTON_RIGHT ? 2 : 4;
mx = e.button.x; my = e.button.y * 200 / 240;
mx = e.button.x; my = e.button.y * 200 / game_h;
if (e.type == SDL_MOUSEBUTTONDOWN) { mb |= bit; press_until[bit >> 1] = host_time_us() + 80000; }
else mb &= ~bit;
break;
}
case SDL_KEYDOWN: case SDL_KEYUP: {
uint8_t sc;
if (e.type == SDL_KEYDOWN && (e.key.keysym.scancode == SDL_SCANCODE_F11 ||
(e.key.keysym.scancode == SDL_SCANCODE_RETURN && (e.key.keysym.mod & KMOD_ALT)))) {
uint32_t fl = SDL_GetWindowFlags(win);
SDL_SetWindowFullscreen(win, (fl & SDL_WINDOW_FULLSCREEN_DESKTOP) ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
if (e.type == SDL_KEYDOWN && is_fullscreen_key(&e.key)) {
settings.plein_ecran = !(SDL_GetWindowFlags(win) & SDL_WINDOW_FULLSCREEN_DESKTOP);
apply_display();
settings_save(cfg_path);
break;
}
if (e.key.keysym.scancode == SDL_SCANCODE_F12) {
if (e.type == SDL_KEYDOWN && !e.key.repeat) menu_modal();
break;
}
if (e.type == SDL_KEYUP && e.key.keysym.scancode == SDL_SCANCODE_ESCAPE && swallow_esc_up) {
swallow_esc_up = 0;
break;
}
sc = pc_scancode(e.key.keysym.scancode);
+462
View File
@@ -0,0 +1,462 @@
/* Menu de réglages : modèle (Settings), fichier reglages.ini, navigation et dessin logiciel.
* Aucun appel à SDL ici : ce fichier servira tel quel au portage web. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "menu.h"
#include "menu_font.h"
#include "rt.h"
#ifdef _WIN32
#include <direct.h>
#define MKDIR(d) _mkdir(d)
#else
#include <sys/stat.h>
#define MKDIR(d) mkdir(d, 0755)
#endif
Settings settings;
/* ================================================================== réglages */
void settings_defaults(void) {
int i;
for (i = 0; i < VOL_COUNT; i++) settings.vol[i] = 100;
settings.plein_ecran = 0;
settings.echelle = 3;
settings.lissage = 0;
settings.format_43 = 1;
}
static int clampi(int v, int lo, int hi) { return v < lo ? lo : v > hi ? hi : v; }
static const struct { const char *key; int *val; int lo, hi; } ini_keys[] = {
{"volume_general", &settings.vol[VOL_GENERAL], 0, 100},
{"volume_musique", &settings.vol[VOL_MUSIQUE], 0, 100},
{"volume_voix", &settings.vol[VOL_VOIX], 0, 100},
{"volume_bruitages", &settings.vol[VOL_BRUITAGES], 0, 100},
{"plein_ecran", &settings.plein_ecran, 0, 1},
{"echelle", &settings.echelle, 1, 16},
{"lissage", &settings.lissage, 0, 1},
{"format_43", &settings.format_43, 0, 1},
};
#define N_INI (sizeof ini_keys / sizeof ini_keys[0])
int settings_load(const char *path) {
char line[256];
FILE *f = fopen(path, "r");
if (!f) return 0;
while (fgets(line, sizeof line, f)) {
char *eq = strchr(line, '='), *k = line, *e;
size_t i;
if (!eq || line[0] == '#' || line[0] == ';') continue;
*eq = 0;
while (*k == ' ' || *k == '\t') k++;
for (e = eq; e > k && (e[-1] == ' ' || e[-1] == '\t'); e--) e[-1] = 0;
for (i = 0; i < N_INI; i++)
if (!strcmp(k, ini_keys[i].key)) *ini_keys[i].val = clampi(atoi(eq + 1), ini_keys[i].lo, ini_keys[i].hi);
}
fclose(f);
return 1;
}
static void make_parent_dir(const char *path) {
char dir[600];
char *s1, *s2;
snprintf(dir, sizeof dir, "%s", path);
s1 = strrchr(dir, '/'); s2 = strrchr(dir, '\\');
if (s2 > s1) s1 = s2;
if (!s1) return;
*s1 = 0;
MKDIR(dir);
}
int settings_save(const char *path) {
size_t i;
FILE *f = fopen(path, "w");
if (!f) { make_parent_dir(path); f = fopen(path, "w"); }
if (!f) return 0;
fprintf(f, "# Réglages de tucker-recomp (menu F12). Volumes en %%, booléens 0/1.\n");
for (i = 0; i < N_INI; i++) fprintf(f, "%s=%d\n", ini_keys[i].key, *ini_keys[i].val);
fclose(f);
return 1;
}
/* l'oreille perçoit le volume de façon logarithmique : une courbe quadratique rend le curseur régulier */
static int curve(int pct) { return pct * pct / 100; }
void settings_apply_audio(void) {
audio_master = curve(settings.vol[VOL_GENERAL]);
audio_volume[AUDIO_MUSIC] = curve(settings.vol[VOL_MUSIQUE]);
audio_volume[AUDIO_SPEECH] = curve(settings.vol[VOL_VOIX]);
audio_volume[AUDIO_FX] = curve(settings.vol[VOL_BRUITAGES]);
}
/* ================================================================== dessin logiciel */
static uint32_t *fb; /* MENU_W × MENU_H, ARGB non prémultiplié */
#define RGB(r, g, b) ((uint32_t)(r) << 16 | (uint32_t)(g) << 8 | (uint32_t)(b))
static const uint32_t C_PANEL = RGB(28, 20, 51), C_BORDER = RGB(70, 54, 120), C_ACCENT = RGB(255, 204, 51),
C_TEXT = RGB(242, 238, 252), C_MUTED = RGB(157, 147, 192), C_TRACK = RGB(62, 50, 102),
C_DARK = RGB(30, 20, 40), C_ROW = RGB(255, 255, 255);
/* composition « par-dessus » en alpha non prémultiplié */
static void blend(int x, int y, uint32_t rgb, int a) {
uint32_t *p, d;
int da, oa, r, g, b;
if ((unsigned)x >= MENU_W || (unsigned)y >= MENU_H || a <= 0) return;
if (a > 255) a = 255;
p = &fb[y * MENU_W + x];
d = *p;
da = (int)(d >> 24);
oa = a + da * (255 - a) / 255;
if (!oa) return;
r = ((int)(rgb >> 16 & 255) * a + (int)(d >> 16 & 255) * da * (255 - a) / 255) / oa;
g = ((int)(rgb >> 8 & 255) * a + (int)(d >> 8 & 255) * da * (255 - a) / 255) / oa;
b = ((int)(rgb & 255) * a + (int)(d & 255) * da * (255 - a) / 255) / oa;
*p = (uint32_t)oa << 24 | (uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b;
}
/* rectangle arrondi anticrénelé ; r = rayon des coins */
static void round_rect(float x0, float y0, float x1, float y1, float r, uint32_t rgb, int a) {
int x, y;
for (y = (int)floorf(y0); y < (int)ceilf(y1); y++)
for (x = (int)floorf(x0); x < (int)ceilf(x1); x++) {
float px = x + 0.5f, py = y + 0.5f;
float cx = px < x0 + r ? x0 + r : px > x1 - r ? x1 - r : px;
float cy = py < y0 + r ? y0 + r : py > y1 - r ? y1 - r : py;
float d = sqrtf((px - cx) * (px - cx) + (py - cy) * (py - cy));
float cov = r - d + 0.5f;
/* bords droits : couverture partielle aux limites non entières */
float ex = fminf(px - x0 + 0.5f, x1 - px + 0.5f), ey = fminf(py - y0 + 0.5f, y1 - py + 0.5f);
if (cov > 1) cov = 1;
if (ex < cov) cov = ex;
if (ey < cov) cov = ey;
if (cov > 0) blend(x, y, rgb, (int)(a * cov));
}
}
static void outline_rect(float x0, float y0, float x1, float y1, float r, float w, uint32_t rgb, int a) {
/* anneau = grand rectangle moins petit : dessiné ligne à ligne pour rester simple */
int x, y;
for (y = (int)floorf(y0); y < (int)ceilf(y1); y++)
for (x = (int)floorf(x0); x < (int)ceilf(x1); x++) {
float px = x + 0.5f, py = y + 0.5f, cov_o, cov_i, ri = r - w;
float cx = px < x0 + r ? x0 + r : px > x1 - r ? x1 - r : px;
float cy = py < y0 + r ? y0 + r : py > y1 - r ? y1 - r : py;
float d = sqrtf((px - cx) * (px - cx) + (py - cy) * (py - cy));
float edge = fminf(fminf(px - x0, x1 - px), fminf(py - y0, y1 - py));
cov_o = fminf(fminf(r - d + 0.5f, edge + 0.5f), 1.f);
{
float ix0 = x0 + w, iy0 = y0 + w, ix1 = x1 - w, iy1 = y1 - w;
float icx = px < ix0 + ri ? ix0 + ri : px > ix1 - ri ? ix1 - ri : px;
float icy = py < iy0 + ri ? iy0 + ri : py > iy1 - ri ? iy1 - ri : py;
float id = sqrtf((px - icx) * (px - icx) + (py - icy) * (py - icy));
float iedge = fminf(fminf(px - ix0, ix1 - px), fminf(py - iy0, iy1 - py));
cov_i = fminf(fminf(ri - id + 0.5f, iedge + 0.5f), 1.f);
if (cov_i < 0) cov_i = 0;
}
if (cov_o - cov_i > 0) blend(x, y, rgb, (int)(a * (cov_o - cov_i)));
}
}
/* chevron plein « ‹ » (dir < 0) ou « › » (dir > 0), anticrénelé par suréchantillonnage 4×4 */
static void chevron(float cx, float cy, float s, int dir, uint32_t rgb, int a) {
int x, y, i, j;
for (y = (int)(cy - s) - 1; y <= (int)(cy + s) + 1; y++)
for (x = (int)(cx - s) - 1; x <= (int)(cx + s) + 1; x++) {
int n = 0;
for (j = 0; j < 4; j++)
for (i = 0; i < 4; i++) {
float px = x + (i + 0.5f) / 4 - cx, py = y + (j + 0.5f) / 4 - cy;
float u = dir > 0 ? px : -px; /* pointe vers +u */
if (u <= s * 0.5f && u >= -s * 0.5f && fabsf(py) <= (s * 0.5f - u) * 1.2f) n++;
}
if (n) blend(x, y, rgb, a * n / 16);
}
}
static void disc(float cx, float cy, float r, uint32_t rgb, int a) {
round_rect(cx - r, cy - r, cx + r, cy + r, r, rgb, a);
}
/* ------------------------------------------------------------------ texte */
static uint32_t utf8_next(const char **s) {
const unsigned char *p = (const unsigned char *)*s;
uint32_t c = *p++;
if (c >= 0xf0 && p[0] && p[1] && p[2]) { c = (c & 7) << 18 | (p[0] & 63u) << 12 | (p[1] & 63u) << 6 | (p[2] & 63u); p += 3; }
else if (c >= 0xe0 && p[0] && p[1]) { c = (c & 15) << 12 | (p[0] & 63u) << 6 | (p[1] & 63u); p += 2; }
else if (c >= 0xc0 && p[0]) { c = (c & 31) << 6 | (p[0] & 63u); p += 1; }
*s = (const char *)p;
return c;
}
static const MenuGlyph *glyph(const MenuFont *f, uint32_t code) {
int i;
for (i = 0; i < f->count; i++) if (f->glyphs[i].code == code) return &f->glyphs[i];
for (i = 0; i < f->count; i++) if (f->glyphs[i].code == '?') return &f->glyphs[i];
return &f->glyphs[0];
}
static int text_width(const MenuFont *f, const char *s) {
int w = 0;
while (*s) w += glyph(f, utf8_next(&s))->adv;
return w;
}
static int text(const MenuFont *f, int x, int y, const char *s, uint32_t rgb, int a) {
int x0 = x;
while (*s) {
const MenuGlyph *g = glyph(f, utf8_next(&s));
const uint8_t *d = f->data + g->off;
int gx, gy;
for (gy = 0; gy < f->height; gy++)
for (gx = 0; gx < g->w; gx++) {
int c = d[gy * g->w + gx];
if (c) blend(x + g->xoff + gx, y + gy, rgb, c * a / 255);
}
x += g->adv;
}
return x - x0;
}
/* ================================================================== modèle du menu */
enum { IT_SLIDER, IT_SWITCH, IT_CHOICE, IT_BUTTONS };
typedef struct { const char *label; int type; int *val; int lo, hi, step; } Item;
static Item items[] = {
{"Volume général", IT_SLIDER, &settings.vol[VOL_GENERAL], 0, 100, 5},
{"Musique", IT_SLIDER, &settings.vol[VOL_MUSIQUE], 0, 100, 5},
{"Voix", IT_SLIDER, &settings.vol[VOL_VOIX], 0, 100, 5},
{"Bruitages", IT_SLIDER, &settings.vol[VOL_BRUITAGES], 0, 100, 5},
{"Plein écran", IT_SWITCH, &settings.plein_ecran, 0, 1, 1},
{"Taille de la fenêtre", IT_CHOICE, &settings.echelle, 1, 16, 1},
{"Image lissée", IT_SWITCH, &settings.lissage, 0, 1, 1},
{"Format d'origine 4:3", IT_SWITCH, &settings.format_43, 0, 1, 1},
{NULL, IT_BUTTONS, NULL, 0, 1, 1},
};
#define N_ITEMS ((int)(sizeof items / sizeof items[0]))
#define FIRST_DISPLAY 4
static int sel, button, dragging = -1, max_scale = 4, last_mx = -1, last_my = -1;
/* géométrie */
#define PANEL_X0 150
#define PANEL_X1 810
#define PANEL_Y0 40
#define PANEL_Y1 680
#define ROW_H 44
#define CTRL_X0 (PANEL_X0 + 330)
#define CTRL_X1 (PANEL_X1 - 44)
#define SLIDER_X1 (CTRL_X1 - 84)
#define BTN_W 210
#define BTN_H 46
#define BTN_GAP 24
static int row_y(int i) {
if (i < FIRST_DISPLAY) return 158 + i * ROW_H;
if (i < N_ITEMS - 1) return 374 + (i - FIRST_DISPLAY) * ROW_H;
return 576;
}
static int button_x(int b) {
int total = 2 * BTN_W + BTN_GAP, x0 = (PANEL_X0 + PANEL_X1 - total) / 2;
return x0 + b * (BTN_W + BTN_GAP);
}
void menu_open(int max_echelle) {
max_scale = max_echelle < 1 ? 1 : max_echelle;
items[5].hi = max_scale;
settings.echelle = clampi(settings.echelle, 1, max_scale);
sel = 0; button = 0; dragging = -1; last_mx = last_my = -1;
}
static int set_value(int i, int v) {
Item *it = &items[i];
int old = *it->val;
*it->val = clampi(v, it->lo, it->hi);
if (*it->val == old) return MENU_STAY;
if (it->type == IT_SLIDER) { settings_apply_audio(); return MENU_STAY; }
return MENU_DISPLAY;
}
static int activate(void) {
Item *it = &items[sel];
switch (it->type) {
case IT_SWITCH: return set_value(sel, !*it->val);
case IT_CHOICE: return set_value(sel, *it->val >= it->hi ? it->lo : *it->val + 1);
case IT_BUTTONS: return button ? MENU_QUIT : MENU_CLOSE;
default: return MENU_STAY;
}
}
int menu_key(MenuKey k) {
Item *it = &items[sel];
switch (k) {
case MK_UP: sel = (sel + N_ITEMS - 1) % N_ITEMS; return MENU_STAY;
case MK_DOWN: sel = (sel + 1) % N_ITEMS; return MENU_STAY;
case MK_LEFT: case MK_RIGHT: {
int dir = k == MK_RIGHT ? 1 : -1;
if (it->type == IT_BUTTONS) { button = dir > 0; return MENU_STAY; }
if (it->type == IT_SWITCH) return set_value(sel, dir > 0);
return set_value(sel, *it->val + dir * it->step);
}
case MK_HOME: return it->val ? set_value(sel, it->lo) : MENU_STAY;
case MK_END: return it->val ? set_value(sel, it->hi) : MENU_STAY;
case MK_OK: return activate();
case MK_BACK: return MENU_CLOSE;
}
return MENU_STAY;
}
static int slider_value(const Item *it, int x) {
float t = (float)(x - CTRL_X0) / (float)(SLIDER_X1 - CTRL_X0);
int v;
if (t < 0) t = 0;
if (t > 1) t = 1;
v = it->lo + (int)(t * (it->hi - it->lo) + 0.5f);
return v;
}
/* rangée sous le pointeur, ou -1 ; pour la rangée des boutons, *b = bouton visé (-1 = aucun) */
static int hit(int x, int y, int *b) {
int i;
*b = -1;
if (x < PANEL_X0 + 20 || x >= PANEL_X1 - 20) return -1;
for (i = 0; i < N_ITEMS; i++) {
int y0 = row_y(i);
if (items[i].type == IT_BUTTONS) {
int j;
if (y < y0 || y >= y0 + BTN_H) continue;
for (j = 0; j < 2; j++) if (x >= button_x(j) && x < button_x(j) + BTN_W) *b = j;
return *b >= 0 ? i : -1;
}
if (y >= y0 && y < y0 + ROW_H) return i;
}
return -1;
}
int menu_mouse(int x, int y, int down, int up, int wheel) {
int b, i, r = MENU_STAY, moved = x != last_mx || y != last_my;
last_mx = x; last_my = y;
if (dragging >= 0) {
r |= set_value(dragging, slider_value(&items[dragging], x));
if (up) dragging = -1;
return r;
}
i = hit(x, y, &b);
if (i >= 0 && moved) { sel = i; if (b >= 0) button = b; }
if (i < 0) return MENU_STAY;
if (wheel) {
Item *it = &items[i];
sel = i;
if (it->type == IT_SLIDER || it->type == IT_CHOICE) r |= set_value(i, *it->val + (wheel > 0 ? 1 : -1) * it->step);
}
if (down) {
Item *it = &items[i];
sel = i;
switch (it->type) {
case IT_SLIDER:
if (x >= CTRL_X0 - 12 && x <= SLIDER_X1 + 12) { dragging = i; r |= set_value(i, slider_value(it, x)); }
break;
case IT_SWITCH: r |= set_value(i, !*it->val); break;
case IT_CHOICE:
if (x >= CTRL_X1 - 150) r |= set_value(i, *it->val + (x >= CTRL_X1 - 75 ? 1 : -1));
break;
case IT_BUTTONS: button = b; r |= activate(); break;
}
}
return r;
}
/* ------------------------------------------------------------------ rendu */
static void draw_slider(int y, const Item *it, int active) {
int cy = y + ROW_H / 2, w = SLIDER_X1 - CTRL_X0, v = *it->val;
float fx = CTRL_X0 + (float)w * (v - it->lo) / (float)(it->hi - it->lo);
char buf[16];
round_rect(CTRL_X0, cy - 3, SLIDER_X1, cy + 3, 3, C_TRACK, 255);
if (v > it->lo) round_rect(CTRL_X0, cy - 3, fx, cy + 3, 3, active ? C_ACCENT : C_MUTED, 255);
disc(fx, cy, active ? 10.f : 8.f, active ? C_ACCENT : C_TEXT, 255);
if (v == 0) snprintf(buf, sizeof buf, "Coupé");
else snprintf(buf, sizeof buf, "%d %%", v);
text(&font_small, CTRL_X1 - text_width(&font_small, buf), cy - font_small.height / 2, buf, v ? C_TEXT : C_MUTED, 255);
}
static void draw_switch(int y, int on, int active) {
int cy = y + ROW_H / 2, x1 = CTRL_X1, x0 = x1 - 54;
const char *lab = on ? "Oui" : "Non";
round_rect(x0, cy - 14, x1, cy + 14, 14, on ? C_ACCENT : C_TRACK, 255);
disc(on ? x1 - 14 : x0 + 14, cy, 10, on ? C_DARK : (active ? C_TEXT : C_MUTED), 255);
text(&font_small, x0 - 14 - text_width(&font_small, lab), cy - font_small.height / 2, lab, active ? C_TEXT : C_MUTED, 255);
}
static void draw_choice(int y, const Item *it, int active, int dim) {
int cy = y + ROW_H / 2, v = *it->val;
char buf[24];
uint32_t col = dim ? C_MUTED : C_TEXT;
int ty = cy - font_normal.height / 2;
snprintf(buf, sizeof buf, "×%d", v);
chevron(CTRL_X1 - 142, cy, 14, -1, v > it->lo ? (active ? C_ACCENT : C_MUTED) : C_TRACK, 255);
text(&font_normal, CTRL_X1 - 75 - text_width(&font_normal, buf) / 2, ty, buf, col, 255);
chevron(CTRL_X1 - 8, cy, 14, 1, v < it->hi ? (active ? C_ACCENT : C_MUTED) : C_TRACK, 255);
snprintf(buf, sizeof buf, "%d×%d", 320 * v, (settings.format_43 ? 240 : 200) * v);
text(&font_small, CTRL_X1 - 150 - 18 - text_width(&font_small, buf), cy - font_small.height / 2, buf, C_MUTED, 255);
}
void menu_draw(uint32_t *px) {
int i, n;
fb = px;
n = MENU_W * MENU_H;
for (i = 0; i < n; i++) px[i] = 0x8c060312u; /* voile sombre sur le jeu */
round_rect(PANEL_X0 + 6, PANEL_Y0 + 10, PANEL_X1 + 6, PANEL_Y1 + 10, 22, 0, 90); /* ombre */
round_rect(PANEL_X0, PANEL_Y0, PANEL_X1, PANEL_Y1, 22, C_PANEL, 245);
outline_rect(PANEL_X0, PANEL_Y0, PANEL_X1, PANEL_Y1, 22, 2, C_BORDER, 255);
text(&font_title, PANEL_X0 + 44, PANEL_Y0 + 26, "Réglages", C_ACCENT, 255);
round_rect(PANEL_X0 + 44, 114, PANEL_X1 - 44, 116, 1, C_BORDER, 255);
text(&font_small, PANEL_X0 + 44, 128, "SON", C_MUTED, 255);
text(&font_small, PANEL_X0 + 44, 344, "AFFICHAGE", C_MUTED, 255);
for (i = 0; i < N_ITEMS; i++) {
const Item *it = &items[i];
int y = row_y(i), active = i == sel;
if (it->type == IT_BUTTONS) {
static const char *lab[2] = {"Reprendre", "Quitter le jeu"};
int b;
for (b = 0; b < 2; b++) {
int x0 = button_x(b), on = active && button == b;
int tw = text_width(&font_normal, lab[b]);
if (on) round_rect(x0, y, x0 + BTN_W, y + BTN_H, BTN_H / 2, C_ACCENT, 255);
else outline_rect(x0, y, x0 + BTN_W, y + BTN_H, BTN_H / 2, 2, C_BORDER, 255);
text(&font_normal, x0 + (BTN_W - tw) / 2, y + (BTN_H - font_normal.height) / 2, lab[b], on ? C_DARK : C_TEXT, 255);
}
continue;
}
if (active) {
round_rect(PANEL_X0 + 20, y, PANEL_X1 - 20, y + ROW_H, 10, C_ROW, 16);
round_rect(PANEL_X0 + 20, y + 10, PANEL_X0 + 24, y + ROW_H - 10, 2, C_ACCENT, 255);
}
{
int dim = i == 5 && settings.plein_ecran;
text(&font_normal, PANEL_X0 + 44, y + (ROW_H - font_normal.height) / 2, it->label,
dim ? C_MUTED : C_TEXT, 255);
switch (it->type) {
case IT_SLIDER: draw_slider(y, it, active); break;
case IT_SWITCH: draw_switch(y, *it->val, active); break;
case IT_CHOICE: draw_choice(y, it, active, dim); break;
}
}
}
{
const char *hint = "Flèches : choisir et régler · Entrée : valider · Échap : reprendre";
text(&font_small, (MENU_W - text_width(&font_small, hint)) / 2, PANEL_Y1 - 38, hint, C_MUTED, 255);
}
}
void menu_draw_toast(uint32_t *px, const char *msg) {
int w = text_width(&font_small, msg) + 36, h = font_small.height + 18;
int x1 = MENU_W - 24, y1 = MENU_H - 24;
fb = px;
memset(px, 0, (size_t)MENU_W * MENU_H * 4);
round_rect(x1 - w, y1 - h, x1, y1, h / 2.f, C_PANEL, 220);
outline_rect(x1 - w, y1 - h, x1, y1, h / 2.f, 1.5f, C_BORDER, 255);
text(&font_small, x1 - w + 18, y1 - h + 9, msg, C_TEXT, 255);
}
+37
View File
@@ -0,0 +1,37 @@
/* Menu de réglages en surimpression (volumes, affichage) et fichier de réglages.
* Indépendant de SDL : l'hôte fournit les touches et la souris, le menu dessine dans un tampon ARGB
* de MENU_W × MENU_H pixels (4:3), que l'hôte affiche par-dessus l'image du jeu. */
#pragma once
#include <stdint.h>
#define MENU_W 960
#define MENU_H 720
enum { VOL_GENERAL, VOL_MUSIQUE, VOL_VOIX, VOL_BRUITAGES, VOL_COUNT };
typedef struct {
int vol[VOL_COUNT]; /* 0..100 % (courbe perceptive appliquée au mixeur) */
int plein_ecran; /* 0/1 */
int echelle; /* taille de la fenêtre : 320 × echelle */
int lissage; /* 0 = pixels nets, 1 = filtrage bilinéaire */
int format_43; /* 1 = 320×200 étiré en 4:3 comme sur un écran VGA, 0 = pixels carrés */
} Settings;
extern Settings settings;
void settings_defaults(void);
int settings_load(const char *path); /* 1 si lu */
int settings_save(const char *path);
void settings_apply_audio(void); /* settings.vol → audio_volume / audio_master */
typedef enum { MK_UP, MK_DOWN, MK_LEFT, MK_RIGHT, MK_OK, MK_BACK, MK_HOME, MK_END } MenuKey;
/* ce que l'hôte doit faire après un événement */
enum { MENU_STAY = 0, MENU_CLOSE = 1, MENU_QUIT = 2, MENU_DISPLAY = 4 /* affichage à réappliquer */ };
void menu_open(int max_echelle);
int menu_key(MenuKey k);
/* souris en pixels du tampon du menu ; down/up : bouton gauche enfoncé / relâché pendant cet événement */
int menu_mouse(int x, int y, int down, int up, int wheel);
void menu_draw(uint32_t *px); /* MENU_W × MENU_H, ARGB non prémultiplié */
void menu_draw_toast(uint32_t *px, const char *text); /* bulle discrète en bas à droite, fond transparent */
+3720
View File
File diff suppressed because it is too large. Load diff
+1
View File
@@ -118,6 +118,7 @@ void ail_load_sample_buffer(void);
extern int (*ail_group_hint)(uint32_t hsample); /* groupe de volume d'un canal, propre au programme */
extern int audio_volume[AUDIO_GROUPS]; /* % par groupe */
extern int audio_master; /* % général */
extern int audio_hold; /* 1 = pause (menu) sauf sons en boucle */
void audio_mix(int16_t *out, int frames); /* stéréo entrelacé, AUDIO_RATE */
void audio_update(void); /* fil principal, à chaque image */
void audio_play_mem(int group, int ch, uint32_t addr, int vol, int loop, uint32_t flag);
+121
View File
@@ -0,0 +1,121 @@
"""Génère runtime/menu_font.h : glyphes anticrénelés (niveaux de gris) pour le menu de réglages.
Rastérise une police installée sous Windows avec GDI (ctypes, sans dépendance) et écrit
un en-tête C portable : le runtime n'a plus besoin de la police ni de GDI.
Police par défaut : Roboto (licence Apache 2.0).
python tools/gen_font.py [sortie] [famille]
"""
import ctypes
import ctypes.wintypes as wt
import sys
OUT = sys.argv[1] if len(sys.argv) > 1 else "runtime/menu_font.h"
FAMILY = sys.argv[2] if len(sys.argv) > 2 else "Roboto"
CHARS = "".join(chr(c) for c in range(32, 127)) + "éèêëàâäçôöîïùûüœÉÈÊÀÂÇÔÎ×←→↑↓·…–°"
SIZES = [("small", 22, 400), ("normal", 30, 400), ("title", 46, 700)] # hauteur de cellule en pixels
gdi = ctypes.WinDLL("gdi32")
user = ctypes.WinDLL("user32")
class BITMAPINFOHEADER(ctypes.Structure):
_fields_ = [("biSize", wt.DWORD), ("biWidth", wt.LONG), ("biHeight", wt.LONG), ("biPlanes", wt.WORD),
("biBitCount", wt.WORD), ("biCompression", wt.DWORD), ("biSizeImage", wt.DWORD),
("biXPelsPerMeter", wt.LONG), ("biYPelsPerMeter", wt.LONG), ("biClrUsed", wt.DWORD),
("biClrImportant", wt.DWORD)]
class TEXTMETRICW(ctypes.Structure):
_fields_ = [("tmHeight", wt.LONG), ("tmAscent", wt.LONG), ("tmDescent", wt.LONG),
("tmInternalLeading", wt.LONG), ("tmExternalLeading", wt.LONG), ("tmAveCharWidth", wt.LONG),
("tmMaxCharWidth", wt.LONG), ("tmWeight", wt.LONG), ("tmOverhang", wt.LONG),
("tmDigitizedAspectX", wt.LONG), ("tmDigitizedAspectY", wt.LONG), ("tmFirstChar", wt.WCHAR),
("tmLastChar", wt.WCHAR), ("tmDefaultChar", wt.WCHAR), ("tmBreakChar", wt.WCHAR),
("tmItalic", ctypes.c_byte), ("tmUnderlined", ctypes.c_byte), ("tmStruckOut", ctypes.c_byte),
("tmPitchAndFamily", ctypes.c_byte), ("tmCharSet", ctypes.c_byte)]
gdi.CreateFontW.restype = wt.HANDLE
gdi.CreateFontW.argtypes = [ctypes.c_int] * 5 + [wt.DWORD] * 8 + [wt.LPCWSTR]
gdi.CreateCompatibleDC.restype = wt.HANDLE
gdi.CreateCompatibleDC.argtypes = [wt.HANDLE]
gdi.CreateDIBSection.restype = wt.HANDLE
gdi.CreateDIBSection.argtypes = [wt.HANDLE, ctypes.c_void_p, wt.UINT, ctypes.POINTER(ctypes.c_void_p), wt.HANDLE, wt.DWORD]
gdi.SelectObject.restype = wt.HANDLE
gdi.SelectObject.argtypes = [wt.HANDLE, wt.HANDLE]
gdi.DeleteObject.argtypes = [wt.HANDLE]
gdi.DeleteDC.argtypes = [wt.HANDLE]
gdi.SetTextColor.argtypes = [wt.HANDLE, wt.DWORD]
gdi.SetBkColor.argtypes = [wt.HANDLE, wt.DWORD]
gdi.TextOutW.argtypes = [wt.HANDLE, ctypes.c_int, ctypes.c_int, wt.LPCWSTR, ctypes.c_int]
gdi.GetTextExtentPoint32W.argtypes = [wt.HANDLE, wt.LPCWSTR, ctypes.c_int, ctypes.POINTER(wt.SIZE)]
gdi.GetTextMetricsW.argtypes = [wt.HANDLE, ctypes.POINTER(TEXTMETRICW)]
gdi.GetTextFaceW.argtypes = [wt.HANDLE, ctypes.c_int, wt.LPWSTR]
gdi.GdiFlush.argtypes = []
def render(cell, weight):
dc = gdi.CreateCompatibleDC(None)
font = gdi.CreateFontW(-cell * 3 // 4, 0, 0, 0, weight, 0, 0, 0, 1, 4, 0, 4, 0, FAMILY)
gdi.SelectObject(dc, font)
face = ctypes.create_unicode_buffer(64)
gdi.GetTextFaceW(dc, 64, face)
if face.value.lower() != FAMILY.lower():
sys.exit("police %r introuvable (GDI propose %r)" % (FAMILY, face.value))
tm = TEXTMETRICW()
gdi.GetTextMetricsW(dc, ctypes.byref(tm))
h = tm.tmHeight
pad = h // 2
W = 4 * h
bmi = BITMAPINFOHEADER(ctypes.sizeof(BITMAPINFOHEADER), W, -h, 1, 32, 0, 0, 0, 0, 0, 0)
bits = ctypes.c_void_p()
dib = gdi.CreateDIBSection(dc, ctypes.byref(bmi), 0, ctypes.byref(bits), None, 0)
gdi.SelectObject(dc, dib)
gdi.SetTextColor(dc, 0xffffff)
gdi.SetBkColor(dc, 0)
buf = (ctypes.c_uint32 * (W * h)).from_address(bits.value)
glyphs = []
for ch in CHARS:
sz = wt.SIZE()
gdi.GetTextExtentPoint32W(dc, ch, 1, ctypes.byref(sz))
ctypes.memset(bits, 0, W * h * 4)
gdi.TextOutW(dc, pad, 0, ch, 1)
gdi.GdiFlush()
cov = [[(buf[y * W + x] >> 8) & 255 for x in range(W)] for y in range(h)]
cols = [x for x in range(W) if any(cov[y][x] for y in range(h))]
if cols:
x0, x1 = cols[0], cols[-1] + 1
else:
x0 = x1 = pad
glyphs.append((ord(ch), sz.cx, x0 - pad, x1 - x0, [row[x0:x1] for row in cov]))
gdi.DeleteObject(dib)
gdi.DeleteObject(font)
gdi.DeleteDC(dc)
return h, tm.tmAscent, glyphs
out = ["/* Généré par tools/gen_font.py : ne pas modifier à la main.",
" * Glyphes de la police %s (licence Apache 2.0), couverture 0..255. */" % FAMILY,
"#pragma once", "#include <stdint.h>", "",
"typedef struct { uint16_t code; int8_t xoff; uint8_t adv, w; uint32_t off; } MenuGlyph;",
"typedef struct { int height, ascent, count; const MenuGlyph *glyphs; const uint8_t *data; } MenuFont;", ""]
for name, cell, weight in SIZES:
h, asc, glyphs = render(cell, weight)
data, table = [], []
for code, adv, xoff, w, rows in glyphs:
table.append("{%d,%d,%d,%d,%d}" % (code, xoff, adv, w, len(data)))
for r in rows:
data.extend(r)
out.append("static const uint8_t font_%s_data[%d] = {" % (name, max(1, len(data))))
for i in range(0, len(data), 40):
out.append(" " + ",".join(str(v) for v in data[i:i + 40]) + ",")
out.append("};")
out.append("static const MenuGlyph font_%s_glyphs[%d] = {" % (name, len(table)))
for i in range(0, len(table), 8):
out.append(" " + ",".join(table[i:i + 8]) + ",")
out.append("};")
out.append("static const MenuFont font_%s = {%d, %d, %d, font_%s_glyphs, font_%s_data};" % (name, h, asc, len(table), name, name))
out.append("")
print("%s : hauteur %d, %d glyphes, %d octets" % (name, h, len(table), len(data)))
open(OUT, "w", encoding="utf-8", newline="\n").write("\n".join(out))