Files
DoubleTroubleRecomp/runtime/menu.c
T
linkatplugandClaude Opus 5.5 5ba251b4ce Écran large : bords remplis par l'image floutée (désactivable dans F12)
- gfx_gl.c : quand l'image du jeu ne couvre pas la sortie (16:9, 21:9…), un
  fond est dessiné dessous : l'image du jeu réduite à 80×50 sur le processeur
  (moyenne par blocs, flou en boîte de rayon 2, assombrie à 55 %), étirée pour
  couvrir l'écran et lissée par 9 lectures bilinéaires (FS_BACKDROP). Calcul
  seulement quand l'image change ; coût GPU négligeable.
- Réglage display.wide_fill (1 par défaut), menu F12 « Bords flous (écran
  large) ». Lignes du menu à 30 px pour la place.
- docs/SETTINGS.md ; ROADMAP : écran large coché.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 12:55:24 +01:00

701 lines
30 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Menu de réglages : modèle (Settings), fichier settings.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;
const char *const filter_names[FILTER_COUNT] = {"nearest", "smooth", "xbr", "crt"};
/* ================================================================== réglages */
void settings_defaults(void) {
int i;
for (i = 0; i < VOL_COUNT; i++) settings.vol[i] = 100;
settings.fullscreen = 0;
settings.window_scale = 3;
settings.window_width = settings.window_height = 0;
settings.filter = FILTER_NEAREST;
settings.aspect_43 = 1;
settings.vsync = 0;
settings.wide_fill = 1;
settings.hd_pack = 1;
settings.hd_decors = 1;
settings.hd_sprites = 1;
settings.hd_panel = 1;
settings.mods_enabled = 1;
settings.mods_dir[0] = 0;
settings.mods_disabled[0] = 0;
}
static int clampi(int v, int lo, int hi) { return v < lo ? lo : v > hi ? hi : v; }
/* une entrée par clé du fichier ; names : valeurs textuelles (sinon entier) ; str : clé texte libre */
static const struct {
const char *section, *key;
int *val;
int lo, hi;
const char *const *names;
char *str;
size_t str_n;
} keys[] = {
{"display", "fullscreen", &settings.fullscreen, 0, 1, NULL},
{"display", "window_scale", &settings.window_scale, 1, 16, NULL},
{"display", "window_width", &settings.window_width, 0, 16384, NULL},
{"display", "window_height", &settings.window_height, 0, 16384, NULL},
{"display", "filter", &settings.filter, 0, FILTER_COUNT - 1, filter_names},
{"display", "aspect_43", &settings.aspect_43, 0, 1, NULL},
{"display", "vsync", &settings.vsync, 0, 1, NULL},
{"display", "wide_fill", &settings.wide_fill, 0, 1, NULL},
{"display", "hd_pack", &settings.hd_pack, 0, 1, NULL},
{"display", "hd_decors", &settings.hd_decors, 0, 1, NULL},
{"display", "hd_sprites", &settings.hd_sprites, 0, 1, NULL},
{"display", "hd_panel", &settings.hd_panel, 0, 1, NULL},
{"audio", "master_volume", &settings.vol[VOL_GENERAL], 0, 100, NULL},
{"audio", "music_volume", &settings.vol[VOL_MUSIQUE], 0, 100, NULL},
{"audio", "voice_volume", &settings.vol[VOL_VOIX], 0, 100, NULL},
{"audio", "sfx_volume", &settings.vol[VOL_BRUITAGES], 0, 100, NULL},
{"mods", "enabled", &settings.mods_enabled, 0, 1, NULL},
{"mods", "dir", NULL, 0, 0, NULL, settings.mods_dir, sizeof settings.mods_dir},
{"mods", "disabled", NULL, 0, 0, NULL, settings.mods_disabled, sizeof settings.mods_disabled},
};
#define N_KEYS ((int)(sizeof keys / sizeof keys[0]))
static int file_val[N_KEYS], override_val[N_KEYS], overridden[N_KEYS];
static char file_str[N_KEYS][512], override_str[N_KEYS][512];
int menu_hd_available;
static char *trim(char *s) {
char *e;
while (*s == ' ' || *s == '\t') s++;
e = s + strlen(s);
while (e > s && (e[-1] == ' ' || e[-1] == '\t' || e[-1] == '\r' || e[-1] == '\n')) *--e = 0;
return s;
}
/* clé « nom » (section quelconque) ou « section.nom » ; section != NULL : dans cette section seulement */
static int find_key_in(const char *section, const char *k) {
int i;
const char *dot = section ? NULL : strchr(k, '.');
for (i = 0; i < N_KEYS; i++) {
if (dot) {
if (strlen(keys[i].section) == (size_t)(dot - k) && !strncmp(k, keys[i].section, (size_t)(dot - k)) &&
!strcmp(dot + 1, keys[i].key)) return i;
} else if (!strcmp(k, keys[i].key) && (!section || !strcmp(section, keys[i].section))) return i;
}
return -1;
}
static int find_key(const char *k) { return find_key_in(NULL, k); }
/* valeur textuelle → entier borné ; -1 si invalide */
static int parse_value(int i, const char *v, int *out) {
char *end;
long n;
if (keys[i].str) return 0;
if (keys[i].names) {
int j;
for (j = 0; j <= keys[i].hi; j++) if (!strcmp(v, keys[i].names[j])) { *out = j; return 1; }
return 0;
}
if (!strcmp(v, "true") || !strcmp(v, "on")) { *out = keys[i].hi > 0; return 1; }
if (!strcmp(v, "false") || !strcmp(v, "off")) { *out = 0; return 1; }
n = strtol(v, &end, 10);
if (end == v) return 0;
*out = clampi((int)n, keys[i].lo, keys[i].hi);
return 1;
}
static void format_value(int i, int v, char *out, size_t n) {
if (keys[i].names) snprintf(out, n, "%s", keys[i].names[clampi(v, keys[i].lo, keys[i].hi)]);
else snprintf(out, n, "%d", v);
}
const char *settings_default_path(void) {
static char path[600];
const char *base;
#ifdef _WIN32
base = getenv("APPDATA");
if (base && *base) { snprintf(path, sizeof path, "%s\\DoubleTroubleRecomp\\settings.ini", base); return path; }
#else
base = getenv("XDG_CONFIG_HOME");
if (base && *base) { snprintf(path, sizeof path, "%s/DoubleTroubleRecomp/settings.ini", base); return path; }
base = getenv("HOME");
if (base && *base) { snprintf(path, sizeof path, "%s/.config/DoubleTroubleRecomp/settings.ini", base); return path; }
#endif
return "settings.ini";
}
static void set_str(int i, const char *v) { snprintf(keys[i].str, keys[i].str_n, "%s", v); }
int settings_load(const char *path) {
char line[700], section[64] = "";
int i;
FILE *f = fopen(path, "r");
for (i = 0; i < N_KEYS; i++) {
if (keys[i].str) snprintf(file_str[i], sizeof file_str[i], "%s", keys[i].str);
else file_val[i] = *keys[i].val;
}
if (!f) return 0;
while (fgets(line, sizeof line, f)) {
char *k = trim(line), *eq;
int v;
if (*k == '[') { char *e = strchr(k, ']'); if (e) *e = 0; snprintf(section, sizeof section, "%s", k + 1); continue; }
if (!(eq = strchr(k, '=')) || *k == '#' || *k == ';') continue;
*eq = 0;
i = find_key_in(section, trim(k));
if (i < 0) continue;
if (keys[i].str) { set_str(i, trim(eq + 1)); snprintf(file_str[i], sizeof file_str[i], "%s", keys[i].str); }
else if (parse_value(i, trim(eq + 1), &v)) *keys[i].val = file_val[i] = v;
}
fclose(f);
return 1;
}
int settings_override(const char *key, const char *value) {
int i = find_key(key), v;
if (i < 0) return 0;
if (keys[i].str) {
set_str(i, value);
snprintf(override_str[i], sizeof override_str[i], "%s", keys[i].str);
} else {
if (!parse_value(i, value, &v)) return 0;
*keys[i].val = override_val[i] = v;
}
overridden[i] = 1;
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);
}
/* Réécrit le fichier en conservant commentaires, ordre et clés inconnues (ajoutées par le launcher) :
* seules les valeurs des clés connues changent ; les clés absentes sont ajoutées à leur section. */
#define MAX_LINES 400
int settings_save(const char *path) {
static char lines[MAX_LINES][600];
int n = 0, i, j, written[N_KEYS] = {0};
char cur_section[64] = "";
FILE *f = fopen(path, "r");
if (f) {
while (n < MAX_LINES && fgets(lines[n], sizeof lines[n], f)) {
char *e = lines[n] + strlen(lines[n]);
while (e > lines[n] && (e[-1] == '\n' || e[-1] == '\r')) *--e = 0;
n++;
}
fclose(f);
} else {
snprintf(lines[n++], sizeof lines[0], "# DoubleTroubleRecomp : réglages partagés par le jeu (menu F12) et le launcher.");
snprintf(lines[n++], sizeof lines[0], "# Clés et valeurs : docs/SETTINGS.md");
}
/* valeur à écrire : celle du joueur, sauf réglage imposé en ligne de commande et inchangé depuis */
#define OUT_VAL(i) (overridden[i] && *keys[i].val == override_val[i] ? file_val[i] : *keys[i].val)
#define OUT_STR(i) (overridden[i] && !strcmp(keys[i].str, override_str[i]) ? file_str[i] : keys[i].str)
for (i = 0; i < n; i++) {
char tmp[600], *k, *eq;
snprintf(tmp, sizeof tmp, "%s", lines[i]);
k = trim(tmp);
if (*k == '[') { char *e = strchr(k, ']'); if (e) *e = 0; snprintf(cur_section, sizeof cur_section, "%s", k + 1); continue; }
if (*k == '#' || *k == ';' || !(eq = strchr(k, '='))) continue;
*eq = 0;
j = find_key_in(cur_section, trim(k));
if (j >= 0 && !written[j]) {
char v[512];
if (keys[j].str) snprintf(v, sizeof v, "%s", OUT_STR(j));
else format_value(j, OUT_VAL(j), v, sizeof v);
snprintf(lines[i], sizeof lines[0], "%s=%s", keys[j].key, v);
written[j] = 1;
}
}
/* clés manquantes : à la fin de leur section, ou dans une nouvelle section en fin de fichier */
for (j = 0; j < N_KEYS; j++) {
int at = -1, in = 0;
char v[512];
if (written[j]) continue;
for (i = 0; i < n; i++) {
char tmp[600], *k;
snprintf(tmp, sizeof tmp, "%s", lines[i]);
k = trim(tmp);
if (*k == '[') {
char *e = strchr(k, ']');
if (e) *e = 0;
in = !strcmp(k + 1, keys[j].section);
if (in) at = i + 1;
} else if (in && *k) at = i + 1;
}
if (at < 0) {
if (n + 2 > MAX_LINES) break;
lines[n++][0] = 0;
snprintf(lines[n++], sizeof lines[0], "[%s]", keys[j].section);
at = n;
}
if (n + 1 > MAX_LINES) break;
memmove(lines[at + 1], lines[at], (size_t)(n - at) * sizeof lines[0]);
if (keys[j].str) snprintf(v, sizeof v, "%s", OUT_STR(j));
else format_value(j, OUT_VAL(j), v, sizeof v);
snprintf(lines[at], sizeof lines[0], "%s=%s", keys[j].key, v);
n++;
written[j] = 1;
}
#undef OUT_VAL
#undef OUT_STR
f = fopen(path, "w");
if (!f) { make_parent_dir(path); f = fopen(path, "w"); }
if (!f) return 0;
for (i = 0; i < n; i++) fprintf(f, "%s\n", lines[i]);
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; const char *const *names; } Item;
static const char *const filter_labels[FILTER_COUNT] = {"Net", "Lissé", "xBR", "CRT"};
/* tailles de fenêtre proposées : résolutions courantes qui tiennent à l'écran, celle de l'écran, et la taille
* actuelle si elle n'en fait pas partie (menu_open) */
#define MAX_RES 24
static int res_index, n_res, res_w[MAX_RES], res_h[MAX_RES];
static char res_lab[MAX_RES][32];
static const char *res_names[MAX_RES];
static Item items[] = {
{"Volume général", IT_SLIDER, &settings.vol[VOL_GENERAL], 0, 100, 5, NULL},
{"Musique", IT_SLIDER, &settings.vol[VOL_MUSIQUE], 0, 100, 5, NULL},
{"Voix", IT_SLIDER, &settings.vol[VOL_VOIX], 0, 100, 5, NULL},
{"Bruitages", IT_SLIDER, &settings.vol[VOL_BRUITAGES], 0, 100, 5, NULL},
{"Plein écran", IT_SWITCH, &settings.fullscreen, 0, 1, 1, NULL},
{"Taille de la fenêtre", IT_CHOICE, &res_index, 0, 0, 1, res_names}, /* liste : menu_open */
{"Filtre d'image", IT_CHOICE, &settings.filter, 0, FILTER_COUNT - 1, 1, filter_labels},
{"Format d'origine 4:3", IT_SWITCH, &settings.aspect_43, 0, 1, 1, NULL},
{"Bords flous (écran large)", IT_SWITCH, &settings.wide_fill, 0, 1, 1, NULL},
{"Synchro verticale", IT_SWITCH, &settings.vsync, 0, 1, 1, NULL},
{"Pack HD (mods)", IT_SWITCH, &settings.hd_pack, 0, 1, 1, NULL},
{" Décors HD", IT_SWITCH, &settings.hd_decors, 0, 1, 1, NULL},
{" Personnages et objets HD", IT_SWITCH, &settings.hd_sprites, 0, 1, 1, NULL},
{" Barre d'actions HD", IT_SWITCH, &settings.hd_panel, 0, 1, 1, NULL},
{NULL, IT_BUTTONS, NULL, 0, 1, 1, NULL},
};
#define N_ITEMS ((int)(sizeof items / sizeof items[0]))
#define FIRST_DISPLAY 4
#define ITEM_SCALE 5
#define ITEM_HD 10
#define ITEM_HD_LAST 13 /* Décors, personnages et objets, barre d'actions : sous l'interrupteur du pack */
static int sel, button, dragging = -1, last_mx = -1, last_my = -1;
/* géométrie */
#define PANEL_X0 150
#define PANEL_X1 810
#define PANEL_Y0 40
#define PANEL_Y1 700
#define ROW_H 30
#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 146 + i * ROW_H;
if (i < N_ITEMS - 1) return 304 + (i - FIRST_DISPLAY) * ROW_H;
return 618;
}
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);
}
static void add_res(int w, int h, const char *note) {
int i, j;
for (i = 0; i < n_res; i++) if (res_w[i] == w && res_h[i] == h) return;
if (n_res >= MAX_RES || w <= 0 || h <= 0) return;
for (i = 0; i < n_res && (res_w[i] < w || (res_w[i] == w && res_h[i] < h)); i++) {}
for (j = n_res; j > i; j--) {
res_w[j] = res_w[j - 1]; res_h[j] = res_h[j - 1];
memcpy(res_lab[j], res_lab[j - 1], sizeof res_lab[j]);
}
res_w[i] = w; res_h[i] = h;
snprintf(res_lab[i], sizeof res_lab[i], "%d × %d%s", w, h, note);
n_res++;
}
void menu_open(int screen_w, int screen_h, int usable_w, int usable_h) {
static const int common[][2] = {
{960, 720}, {1280, 960}, {1440, 1080}, {1600, 1200}, {1920, 1440}, {2560, 1920}, {2880, 2160}, /* 4:3 */
{1280, 800}, {1680, 1050}, {1920, 1200}, {2560, 1600}, /* 16:10 */
{1280, 720}, {1600, 900}, {1920, 1080}, {2560, 1440}, {3840, 2160}, /* 16:9 */
};
int i, cur_w = settings.window_width, cur_h = settings.window_height;
if (cur_w <= 0 || cur_h <= 0) { cur_w = 320 * settings.window_scale; cur_h = (settings.aspect_43 ? 240 : 200) * settings.window_scale; }
n_res = 0;
for (i = 0; i < (int)(sizeof common / sizeof common[0]); i++)
if (common[i][0] <= usable_w && common[i][1] <= usable_h) add_res(common[i][0], common[i][1], "");
add_res(screen_w, screen_h, " (écran)");
add_res(cur_w, cur_h, "");
for (i = 0; i < n_res; i++) {
res_names[i] = res_lab[i];
if (res_w[i] == cur_w && res_h[i] == cur_h) res_index = i;
}
items[ITEM_SCALE].hi = n_res - 1;
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; }
if (i == ITEM_SCALE) { settings.window_width = res_w[res_index]; settings.window_height = res_h[res_index]; }
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 - 230) 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[48];
uint32_t col = dim ? C_MUTED : C_TEXT;
int ty = cy - font_normal.height / 2, tw, tx, left;
if (it->names) snprintf(buf, sizeof buf, "%s", it->names[v]);
else snprintf(buf, sizeof buf, "×%d", v);
tw = text_width(&font_normal, buf);
tx = tw <= 100 ? CTRL_X1 - 75 - tw / 2 : CTRL_X1 - 30 - tw; /* texte long : aligné à droite */
left = tx - 22 < CTRL_X1 - 142 ? tx - 22 : CTRL_X1 - 142;
chevron(left, cy, 14, -1, v > it->lo ? (active ? C_ACCENT : C_MUTED) : C_TRACK, 255);
text(&font_normal, tx, ty, buf, col, 255);
chevron(CTRL_X1 - 8, cy, 14, 1, v < it->hi ? (active ? C_ACCENT : C_MUTED) : C_TRACK, 255);
if (!it->names) {
snprintf(buf, sizeof buf, "%d×%d", 320 * v, (settings.aspect_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;
fb = px;
memset(px, 0, (size_t)MENU_W * MENU_H * 4); /* le voile sombre est posé par l'hôte */
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, 124, "SON", C_MUTED, 255);
text(&font_small, PANEL_X0 + 44, 280, "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 == ITEM_SCALE && settings.fullscreen) || (i >= ITEM_HD && i <= ITEM_HD_LAST && !menu_hd_available)
|| (i > ITEM_HD && i <= ITEM_HD_LAST && !settings.hd_pack);
int lw = text(&font_normal, PANEL_X0 + 44, y + (ROW_H - font_normal.height) / 2, it->label,
dim ? C_MUTED : C_TEXT, 255);
if (i == ITEM_HD && !menu_hd_available)
text(&font_small, PANEL_X0 + 44 + lw + 14, y + (ROW_H - font_small.height) / 2, "aucun pack installé", C_MUTED, 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);
}