Files
DoubleTroubleRecomp/runtime/menu.c
T
linkatplugandClaude Opus 5.5 0931a664e9 Passer les dialogues au clic et marche de Bud accélérée (onglet Jeu)
- Dans le jeu d'origine, un clic pendant une réplique ne fait rien : le jeu ne
  lit même pas la souris (vérifié sans fenêtre).
- game_frame, à chaque image : un nouveau clic gauche pendant une réplique
  (ADR_PAROLE_DUREE > 0) coupe la voix ; la bulle disparaît au tick suivant.
  game_mouse_buttons cache ce clic au jeu jusqu'au relâchement, pour qu'il ne
  lance pas d'action. Réglage game.skip_speech, 1 par défaut.
- Marche accélérée : le minuteur du jeu (46 Hz) bat à 150 ou 200 % tant que
  Bud marche (ADR_BUD_ETAT 1..4) hors scène (ADR_BUD_SCRIPT = 0). La logique
  reste identique ; la musique et les voix, mixées en temps réel, ne changent
  pas. Réglage game.walk_speed : normal, fast (par défaut) ou faster.
- rt_cfg.in_game : ces crochets, les zones et le rechargement n'agissent que
  pendant TUCKER.EXE, jamais pendant l'intro.
- host_headless : --set k=v et --suivre A,B (variables suivies image par
  image).
- Vérifié sans fenêtre sur la cuisine. Clic pendant le « Regarder » du singe :
  voix coupée en 2 images, bulle finie en 4, Bud ne bouge pas (sans l'option,
  la réplique dure 337 images). Même trajet à pied : 64 images en vitesse
  normale, 33 à × 2.
- Doublon ADR_SOURIS_X/Y retiré de addr_fr.h.

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

1088 lines
48 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 "zones.h"
#include "report.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.hotspots = 1;
settings.crash_report = 1;
settings.autosave = 1;
settings.skip_speech = 1;
settings.walk_speed = 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; }
static const char *const walk_names[3] = {"normal", "fast", "faster"}; /* valeurs de la clé walk_speed */
/* 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", "hotspots", &settings.hotspots, 0, 1, NULL},
{"game", "autosave", &settings.autosave, 0, 1, NULL},
{"game", "skip_speech", &settings.skip_speech, 0, 1, NULL},
{"game", "walk_speed", &settings.walk_speed, 0, 2, walk_names},
{"debug", "crash_report", &settings.crash_report, 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, IT_ACTION };
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"};
static const char *const walk_labels[3] = {"Normale", "× 1,5", "× 2"};
/* 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},
{"Zones cliquables (Espace)", IT_SWITCH, &settings.hotspots, 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},
{"Passer les dialogues au clic", IT_SWITCH, &settings.skip_speech, 0, 1, 1, NULL},
{"Vitesse de marche de Bud", IT_CHOICE, &settings.walk_speed, 0, 2, 1, walk_labels},
{"Sauvegarde automatique", IT_SWITCH, &settings.autosave, 0, 1, 1, NULL},
{"Charger la sauvegarde auto", IT_ACTION, NULL, 0, 0, 0, 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 11
#define ITEM_HD_LAST 14 /* Décors, personnages et objets, barre d'actions : sous l'interrupteur du pack */
#define FIRST_GAME 15
#define ITEM_LOAD_AUTO 18
/* onglets : chacun montre une suite d'éléments ; la rangée des boutons est commune */
#define N_TABS 3
#define SEL_TABS (-1) /* sel : la rangée des onglets */
static const char *const tab_names[N_TABS] = {"Son", "Affichage", "Jeu"};
static const int tab_first[N_TABS + 1] = {0, FIRST_DISPLAY, FIRST_GAME, N_ITEMS - 1};
static int tab = 1; /* gardé d'une ouverture du menu à l'autre */
static int sel, button, dragging = -1, last_mx = -1, last_my = -1;
enum { PAGE_MAIN, PAGE_REPORT, PAGE_DONE };
static int page;
static void report_open(void);
static int report_key(MenuKey k);
static int report_mouse(int x, int y, int down, int moved);
static void report_draw(void);
/* géométrie */
#define PANEL_X0 150
#define PANEL_X1 810
#define PANEL_Y0 40
#define PANEL_Y1 700
#define ROW_H 32
#define TAB_Y0 126
#define TAB_Y1 162
#define ROWS_Y 184
#define CTRL_X0 (PANEL_X0 + 330)
#define CTRL_X1 (PANEL_X1 - 44)
#define SLIDER_X1 (CTRL_X1 - 84)
#define BTN_W 186
#define BTN_H 46
#define BTN_GAP 20
#define N_BTN 3 /* Reprendre, Signaler un bug, Quitter le jeu */
static int row_y(int i) { return i == N_ITEMS - 1 ? 624 : ROWS_Y + (i - tab_first[tab]) * ROW_H; }
static int visible(int i) { return i == N_ITEMS - 1 || (i >= tab_first[tab] && i < tab_first[tab + 1]); }
static int tab_w(int t) { return text_width(&font_normal, tab_names[t]) + 44; }
static int tab_x(int t) {
int x = PANEL_X0 + 44, i;
for (i = 0; i < t; i++) x += tab_w(i) + 12;
return x;
}
static int tab_hit(int x, int y) {
int t;
if (y < TAB_Y0 || y >= TAB_Y1) return -1;
for (t = 0; t < N_TABS; t++) if (x >= tab_x(t) && x < tab_x(t) + tab_w(t)) return t;
return -1;
}
/* rangée suivante dans l'ordre affiché : onglets, éléments de l'onglet, boutons */
static int next_sel(int dir) {
int order[N_ITEMS + 2], n = 0, i, cur = 0;
order[n++] = SEL_TABS;
for (i = tab_first[tab]; i < tab_first[tab + 1]; i++) order[n++] = i;
order[n++] = N_ITEMS - 1;
for (i = 0; i < n; i++) if (order[i] == sel) cur = i;
return order[(cur + dir + n) % n];
}
/* changement d'onglet : un élément de l'ancien onglet laisse la place au premier du nouveau */
static void set_tab(int t) {
tab = (t + N_TABS) % N_TABS;
if (sel >= 0 && sel != N_ITEMS - 1) sel = tab_first[tab];
}
/* « Charger la sauvegarde auto » : 1 si possible ; why : raison sinon, ou lieu de la sauvegarde */
static int autosave_ready(char *why, size_t n) {
int lieu = 0;
if (!game_in_location()) { snprintf(why, n, "pas de partie en cours"); return 0; }
if (!game_autosave_info(&lieu)) { snprintf(why, n, "aucune pour l'instant"); return 0; }
snprintf(why, n, "lieu %d", lieu);
return 1;
}
static int button_x(int b) {
int total = N_BTN * BTN_W + (N_BTN - 1) * 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 = tab_first[tab]; button = 0; dragging = -1; last_mx = last_my = -1;
page = PAGE_MAIN;
}
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;
char why[64];
if (sel == SEL_TABS) { sel = tab_first[tab]; return MENU_STAY; }
it = &items[sel];
switch (it->type) {
case IT_ACTION: return autosave_ready(why, sizeof why) ? MENU_LOAD_AUTO | MENU_CLOSE : MENU_STAY;
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:
if (button == 1) { report_open(); return MENU_STAY; }
return button ? MENU_QUIT : MENU_CLOSE;
default: return MENU_STAY;
}
}
int menu_key(MenuKey k) {
Item *it = sel >= 0 ? &items[sel] : NULL;
if (page != PAGE_MAIN) return report_key(k);
if (k == MK_PREV_TAB || k == MK_NEXT_TAB) { set_tab(tab + (k == MK_NEXT_TAB ? 1 : -1)); return MENU_STAY; }
if (!it) { /* rangée des onglets */
if (k == MK_LEFT || k == MK_RIGHT) set_tab(tab + (k == MK_RIGHT ? 1 : -1));
else if (k == MK_UP || k == MK_DOWN) sel = next_sel(k == MK_DOWN ? 1 : -1);
else if (k == MK_OK) return activate();
else if (k == MK_BACK) return MENU_CLOSE;
return MENU_STAY;
}
switch (k) {
case MK_UP: sel = next_sel(-1); return MENU_STAY;
case MK_DOWN: sel = next_sel(1); return MENU_STAY;
case MK_LEFT: case MK_RIGHT: {
int dir = k == MK_RIGHT ? 1 : -1;
if (it->type == IT_ACTION) return MENU_STAY;
if (it->type == IT_BUTTONS) { button = clampi(button + dir, 0, N_BTN - 1); 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 && it->type != IT_BUTTONS ? set_value(sel, it->lo) : MENU_STAY;
case MK_END: return it->val && it->type != IT_BUTTONS ? set_value(sel, it->hi) : MENU_STAY;
case MK_OK: return activate();
case MK_BACK: return MENU_CLOSE;
default: break;
}
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 (!visible(i)) continue;
if (items[i].type == IT_BUTTONS) {
int j;
if (y < y0 || y >= y0 + BTN_H) continue;
for (j = 0; j < N_BTN; 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 (page != PAGE_MAIN) return report_mouse(x, y, down, moved);
if (dragging < 0 && (i = tab_hit(x, y)) >= 0) { /* onglets */
if (moved) sel = SEL_TABS;
if (down) { tab = i; sel = SEL_TABS; }
return MENU_STAY;
}
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;
case IT_ACTION: 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);
}
}
static int wrap_text(int x, int y, int w, const char *s, uint32_t rgb);
/* élément d'action : petit bouton « Charger » à droite, précédé de l'état (lieu, ou raison) */
static void draw_action(int y, int ready, int active, const char *info) {
int cy = y + ROW_H / 2, x1 = CTRL_X1, w = text_width(&font_small, "Charger") + 32, x0 = x1 - w;
if (ready) {
if (active) round_rect(x0, cy - 14, x1, cy + 14, 14, C_ACCENT, 255);
else outline_rect(x0, cy - 14, x1, cy + 14, 14, 1.5f, C_BORDER, 255);
text(&font_small, x0 + 16, cy - font_small.height / 2, "Charger", active ? C_DARK : C_TEXT, 255);
x0 -= 14;
} else x0 = x1;
text(&font_small, x0 - text_width(&font_small, info), cy - font_small.height / 2, info, 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);
if (page != PAGE_MAIN) { report_draw(); return; }
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);
for (i = 0; i < N_TABS; i++) {
int x0 = tab_x(i), x1 = x0 + tab_w(i), on = i == tab;
if (on) round_rect(x0, TAB_Y0, x1, TAB_Y1, (TAB_Y1 - TAB_Y0) / 2.f, C_ACCENT, 255);
else outline_rect(x0, TAB_Y0, x1, TAB_Y1, (TAB_Y1 - TAB_Y0) / 2.f, 1.5f, C_BORDER, 255);
if (on && sel == SEL_TABS) outline_rect(x0 - 4, TAB_Y0 - 4, x1 + 4, TAB_Y1 + 4, (TAB_Y1 - TAB_Y0) / 2.f + 4, 2, C_TEXT, 255);
text(&font_normal, x0 + 22, (TAB_Y0 + TAB_Y1 - font_normal.height) / 2, tab_names[i], on ? C_DARK : C_TEXT, 255);
}
if (tab == 2)
wrap_text(PANEL_X0 + 44, row_y(tab_first[3] - 1) + ROW_H + 18, PANEL_X1 - PANEL_X0 - 88,
"La marche accélérée ne joue que sur les déplacements demandés, pas sur les scènes. "
"La sauvegarde automatique est écrite à chaque changement de lieu, dans un emplacement à part : "
"elle ne remplace aucune de vos parties.", C_MUTED);
for (i = 0; i < N_ITEMS; i++) {
const Item *it = &items[i];
int y = row_y(i), active = i == sel;
if (!visible(i)) continue;
if (it->type == IT_BUTTONS) {
static const char *lab[N_BTN] = {"Reprendre", "Signaler un bug", "Quitter le jeu"};
int b;
for (b = 0; b < N_BTN; 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);
char why[64];
int ready = i == ITEM_LOAD_AUTO && autosave_ready(why, sizeof why);
if (i == ITEM_LOAD_AUTO) dim = !ready;
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;
case IT_ACTION: draw_action(y, ready, active, why); break;
}
}
}
{
const char *hint = "Tab : onglet · Flèches : choisir, régler · Entrée : valider · Échap : reprendre";
text(&font_small, (MENU_W - text_width(&font_small, hint)) / 2, PANEL_Y1 - 26, 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);
}
void menu_draw_zones(uint32_t *px, const Zone *z, int n, float gx, float gy, float sx, float sy) {
static const uint32_t col[4] = { RGB(255, 216, 74), RGB(64, 224, 255), RGB(106, 255, 122), RGB(255, 106, 216) };
static const char *const lab[4] = { "décor", "sortie", "objet", "personnage" };
int i, k, seen[4] = {0};
fb = px;
memset(px, 0, (size_t)MENU_W * MENU_H * 4);
for (i = n - 1; i >= 0; i--) { /* personnages et objets par-dessus les zones fixes */
float x0 = gx + z[i].x * sx, y0 = gy + z[i].y * sy, x1 = gx + (z[i].x + z[i].w) * sx, y1 = gy + (z[i].y + z[i].h) * sy;
uint32_t c = col[z[i].kind & 3];
round_rect(x0, y0, x1, y1, 4, c, 40);
outline_rect(x0, y0, x1, y1, 4, 2, c, 230);
seen[z[i].kind & 3] = 1;
}
{ /* légende : seulement les types présents */
int w = 36, h = font_small.height + 18, x, x1 = MENU_W - 24, y1 = MENU_H - 24;
for (k = 0; k < 4; k++) if (seen[k]) w += 22 + text_width(&font_small, lab[k]) + 18;
if (w == 36) return;
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);
x = x1 - w + 18;
for (k = 0; k < 4; k++) {
if (!seen[k]) continue;
round_rect(x, y1 - h / 2.f - 6, x + 12, y1 - h / 2.f + 6, 3, col[k], 255);
x += 22;
x += text(&font_small, x, y1 - h + 9, lab[k], C_TEXT, 255) + 18;
}
}
}
/* ================================================================== page « Signaler un bug » */
#define R_COMMENT BUG_COUNT /* rangées : types, commentaire, interrupteur, boutons */
#define R_CRASH (BUG_COUNT + 1)
#define R_BTN (BUG_COUNT + 2)
#define R_N (BUG_COUNT + 3)
#define R_ROW_H 28
#define R_TYPE_Y 138
#define R_FIELD_Y0 392
#define R_FIELD_Y1 432
#define R_CRASH_Y 518
#define R_BTN_Y 624
#define R_BTN_W 250
#define COMMENT_MAX 200
static int rsel, rtype = BUG_AUTRE, rbutton, done_ok;
static char comment[COMMENT_MAX + 8], done_dir[700];
static void report_open(void) { page = PAGE_REPORT; rsel = 0; rbutton = 0; }
int menu_text_wanted(void) { return page == PAGE_REPORT && rsel == R_COMMENT; }
int menu_report_type(void) { return rtype; }
const char *menu_report_comment(void) { return comment; }
const char *menu_report_dir(void) { return done_dir; }
void menu_report_done(const char *dir) {
page = PAGE_DONE;
done_ok = dir != NULL;
snprintf(done_dir, sizeof done_dir, "%s", dir ? dir : "");
rbutton = 0;
if (dir) comment[0] = 0; /* rapport suivant : commentaire vide */
}
void menu_text(const char *utf8) {
size_t n = strlen(comment), add = strlen(utf8);
if (!menu_text_wanted()) return;
if (n + add > COMMENT_MAX) return;
memcpy(comment + n, utf8, add + 1);
}
static void erase_char(void) {
size_t n = strlen(comment);
while (n > 0 && ((unsigned char)comment[n - 1] & 0xc0) == 0x80) n--; /* octets de suite UTF-8 */
if (n > 0) n--;
comment[n] = 0;
}
static int r_button_x(int b) {
int total = 2 * R_BTN_W + BTN_GAP, x0 = (PANEL_X0 + PANEL_X1 - total) / 2;
return x0 + b * (R_BTN_W + BTN_GAP);
}
/* bouton visé dans la rangée du bas, ou -1 */
static int r_button_hit(int x, int y) {
int b;
if (y < R_BTN_Y || y >= R_BTN_Y + BTN_H) return -1;
for (b = 0; b < 2; b++) if (x >= r_button_x(b) && x < r_button_x(b) + R_BTN_W) return b;
return -1;
}
static int r_press_button(void) {
if (page == PAGE_DONE) {
if (rbutton) return MENU_CLOSE;
if (done_ok) return MENU_OPEN_DIR;
page = PAGE_MAIN; /* échec : retour au menu */
return MENU_STAY;
}
if (rbutton == 0) return MENU_REPORT;
page = PAGE_MAIN;
return MENU_STAY;
}
static int report_key(MenuKey k) {
if (page == PAGE_DONE) {
switch (k) {
case MK_LEFT: case MK_RIGHT: rbutton = k == MK_RIGHT; return MENU_STAY;
case MK_OK: return r_press_button();
case MK_BACK: page = PAGE_MAIN; return MENU_STAY;
default: return MENU_STAY;
}
}
switch (k) {
case MK_UP: rsel = (rsel + R_N - 1) % R_N; return MENU_STAY;
case MK_DOWN: rsel = (rsel + 1) % R_N; return MENU_STAY;
case MK_LEFT: case MK_RIGHT:
if (rsel == R_BTN) rbutton = k == MK_RIGHT;
else if (rsel == R_CRASH) settings.crash_report = k == MK_RIGHT;
return MENU_STAY;
case MK_OK:
if (rsel < BUG_COUNT) { rtype = rsel; rsel = R_COMMENT; } /* type choisi : on passe au commentaire */
else if (rsel == R_COMMENT) rsel = R_BTN;
else if (rsel == R_CRASH) settings.crash_report ^= 1;
else return r_press_button();
return MENU_STAY;
case MK_ERASE: if (rsel == R_COMMENT) erase_char(); return MENU_STAY;
case MK_BACK: page = PAGE_MAIN; return MENU_STAY;
default: return MENU_STAY;
}
}
static int report_mouse(int x, int y, int down, int moved) {
int b = r_button_hit(x, y), row = -1;
if (page == PAGE_DONE) {
if (b >= 0) { if (moved) rbutton = b; if (down) { rbutton = b; return r_press_button(); } }
return MENU_STAY;
}
if (x >= PANEL_X0 + 20 && x < PANEL_X1 - 20) {
if (y >= R_TYPE_Y && y < R_TYPE_Y + BUG_COUNT * R_ROW_H) row = (y - R_TYPE_Y) / R_ROW_H;
else if (y >= R_FIELD_Y0 && y < R_FIELD_Y1) row = R_COMMENT;
else if (y >= R_CRASH_Y && y < R_CRASH_Y + ROW_H) row = R_CRASH;
else if (b >= 0) row = R_BTN;
}
if (row < 0) return MENU_STAY;
if (moved && row != R_COMMENT && rsel != R_COMMENT) rsel = row; /* le survol ne quitte pas le champ en cours de saisie */
if (row == R_BTN && moved) rbutton = b;
if (!down) return MENU_STAY;
rsel = row;
if (row < BUG_COUNT) rtype = row;
else if (row == R_CRASH) settings.crash_report ^= 1;
else if (row == R_BTN) { rbutton = b; return r_press_button(); }
return MENU_STAY;
}
static void r_buttons(const char *a, const char *b) {
const char *lab[2] = {a, b};
int i;
for (i = 0; i < 2; i++) {
int x0 = r_button_x(i);
int on = (page == PAGE_DONE || rsel == R_BTN) && rbutton == i, tw = text_width(&font_normal, lab[i]);
if (on) round_rect(x0, R_BTN_Y, x0 + R_BTN_W, R_BTN_Y + BTN_H, BTN_H / 2, C_ACCENT, 255);
else outline_rect(x0, R_BTN_Y, x0 + R_BTN_W, R_BTN_Y + BTN_H, BTN_H / 2, 2, C_BORDER, 255);
text(&font_normal, x0 + (R_BTN_W - tw) / 2, R_BTN_Y + (BTN_H - font_normal.height) / 2, lab[i], on ? C_DARK : C_TEXT, 255);
}
}
static void r_hint(const char *hint) {
text(&font_small, (MENU_W - text_width(&font_small, hint)) / 2, PANEL_Y1 - 26, hint, C_MUTED, 255);
}
static void r_row_mark(int y0, int y1) {
round_rect(PANEL_X0 + 20, y0, PANEL_X1 - 20, y1, 10, C_ROW, 16);
round_rect(PANEL_X0 + 20, y0 + 9, PANEL_X0 + 24, y1 - 9, 2, C_ACCENT, 255);
}
/* texte sur plusieurs lignes : coupé aux espaces et aux séparateurs de dossier pour tenir dans w pixels */
static int wrap_text(int x, int y, int w, const char *s, uint32_t rgb) {
char line[256];
while (*s) {
int n = 0, cut = 0;
while (s[n] && n < (int)sizeof line - 1) {
line[n] = s[n]; line[n + 1] = 0;
if (text_width(&font_small, line) > w) break;
if (s[n] == ' ' || s[n] == '\\' || s[n] == '/') cut = n + 1;
n++;
}
if (!s[n]) cut = n;
else if (!cut) cut = n > 0 ? n : 1;
memcpy(line, s, (size_t)cut); line[cut] = 0;
text(&font_small, x, y, line, rgb, 255);
s += cut;
y += font_small.height + 2;
}
return y;
}
static void report_draw(void) {
int i, fx0 = PANEL_X0 + 44, fx1 = PANEL_X1 - 44;
if (page == PAGE_DONE) {
int y = 140;
text(&font_title, PANEL_X0 + 44, PANEL_Y0 + 26, done_ok ? "Rapport écrit" : "Rapport non écrit", C_ACCENT, 255);
round_rect(PANEL_X0 + 44, 114, PANEL_X1 - 44, 116, 1, C_BORDER, 255);
if (done_ok) {
text(&font_small, fx0, y, "DOSSIER", C_MUTED, 255);
y = wrap_text(fx0, y + 26, fx1 - fx0, done_dir, C_TEXT) + 22;
text(&font_small, fx0, y, "CONTENU", C_MUTED, 255);
y = wrap_text(fx0, y + 26, fx1 - fx0, "rapport.txt (type, commentaire, lieu, positions, zones, réglages), "
"jeu.bmp et ecran.bmp (captures), SAVEGM.98 (la partie à cet instant), settings.ini, "
"memoire.bin (variables du jeu).", C_TEXT) + 22;
wrap_text(fx0, y, fx1 - fx0, "Pour signaler le bug, joindre ce dossier (ou un zip du dossier) au message. "
"Rien n'a été envoyé.", C_MUTED);
r_buttons("Ouvrir le dossier", "Reprendre");
} else {
wrap_text(fx0, y, fx1 - fx0, "Le dossier des rapports n'a pas pu être créé ou écrit :", C_TEXT);
wrap_text(fx0, y + 30, fx1 - fx0, report_root(), C_MUTED);
r_buttons("Retour", "Reprendre");
}
r_hint("Flèches : choisir · Entrée : valider · Échap : retour");
return;
}
text(&font_title, PANEL_X0 + 44, PANEL_Y0 + 26, "Signaler un bug", C_ACCENT, 255);
round_rect(PANEL_X0 + 44, 114, PANEL_X1 - 44, 116, 1, C_BORDER, 255);
text(&font_small, fx0, 120, "TYPE", C_MUTED, 255);
for (i = 0; i < BUG_COUNT; i++) {
int y = R_TYPE_Y + i * R_ROW_H, cy = y + R_ROW_H / 2, on = i == rtype;
if (rsel == i) r_row_mark(y, y + R_ROW_H);
disc(fx0 + 9, cy, 9, on ? C_ACCENT : C_TRACK, 255);
if (on) disc(fx0 + 9, cy, 4, C_DARK, 255);
text(&font_normal, fx0 + 30, y + (R_ROW_H - font_normal.height) / 2, bug_types[i], on || rsel == i ? C_TEXT : C_MUTED, 255);
}
text(&font_small, fx0, R_FIELD_Y0 - 20, "COMMENTAIRE (FACULTATIF)", C_MUTED, 255);
{
int active = rsel == R_COMMENT, ty = (R_FIELD_Y0 + R_FIELD_Y1 - font_normal.height) / 2, w = fx1 - fx0 - 32;
const char *shown = comment;
round_rect(fx0, R_FIELD_Y0, fx1, R_FIELD_Y1, 10, C_DARK, 255);
outline_rect(fx0, R_FIELD_Y0, fx1, R_FIELD_Y1, 10, active ? 2.f : 1.5f, active ? C_ACCENT : C_BORDER, 255);
while (*shown && text_width(&font_normal, shown) > w) { /* texte long : on montre la fin */
shown++;
while (((unsigned char)*shown & 0xc0) == 0x80) shown++;
}
if (*comment) {
int tw = text(&font_normal, fx0 + 16, ty, shown, C_TEXT, 255);
if (active) round_rect(fx0 + 18 + tw, ty + 4, fx0 + 20 + tw, ty + font_normal.height - 4, 1, C_ACCENT, 255);
} else {
if (active) round_rect(fx0 + 16, ty + 4, fx0 + 18, ty + font_normal.height - 4, 1, C_ACCENT, 255);
text(&font_normal, fx0 + 26, ty, "Ce qui se passe, ce qui était attendu...", C_TRACK, 255);
}
}
wrap_text(fx0, 444, fx1 - fx0, "Ajoutés automatiquement : le lieu, les positions, une capture d'écran et une "
"sauvegarde de la partie à cet instant. Rien n'est envoyé : le rapport reste dans un dossier de ce PC.", C_MUTED);
if (rsel == R_CRASH) r_row_mark(R_CRASH_Y, R_CRASH_Y + ROW_H);
text(&font_normal, fx0, R_CRASH_Y + (ROW_H - font_normal.height) / 2, "Rapport automatique si le jeu plante",
rsel == R_CRASH ? C_TEXT : C_MUTED, 255);
draw_switch(R_CRASH_Y, settings.crash_report, rsel == R_CRASH);
r_buttons("Écrire le rapport", "Annuler");
r_hint(menu_text_wanted() ? "Tapez le commentaire · Entrée : suite · Échap : retour"
: "Flèches : choisir · Entrée : valider · Échap : retour");
}