Files
DoubleTroubleRecomp/runtime/hd.c
T
linkatplugandClaude Opus 5.5 629c139e2b Sprites HD : personnages, objets et curseur en haute définition
- runtime/hd_sprites.c : choisit les sprites à dessiner en HD à partir de ce que le jeu a dessiné
  (dernier sprite encore visible par pixel), charge les images HD sur un fil à part, cache LRU.
- Couleurs : correction par indice (palette du jeu - palette de l'export), interpolée dans le
  shader ; les planches d'objets partagées entre pièces et les fondus ont les bonnes couleurs.
- Objets dessinés avec leur propre fond : la partie du fond de la couleur du décor revient au
  décor HD.
- runtime/dxt.c : compression DXT5 et mipmaps sur le fil de chargement (4 fois moins de mémoire,
  sans à-coup) ; mémoire privée 243 Mo avec décors et sprites HD (objectif < 256 Mo).
- Menu F12 : « Pack HD (mods) » avec deux sous-réglages, « Décors HD » (hd_decors) et
  « Personnages et objets HD » (hd_sprites).
- tools/hd_export.py : --planches, --sprites, --sans-decors (planches RGBA, couleurs prolongées
  dans la transparence).
- png.c sans état global (décodage sur le fil de chargement).
- docs : MODS.md (sprites HD), SETTINGS.md (nouvelles clés), ROADMAP cochée.

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

216 lines
8.0 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.
/* Pack HD : voir hd.h. */
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hd.h"
#include "mods.h"
#include "rt.h"
#define W0 320
#define H0 200
#define NSTRIPS 4
#define NSAMPLES 512
typedef struct {
int id, parts;
char stem[16]; /* nom sans la lettre finale (LOC03 pour LOC03A, LOC03B…) */
char last; /* lettre du dernier morceau, 0 si nom sans lettre */
char png[HD_MAX_PARTS][600];
uint8_t *pix; /* parts × 320 × 200, ligne par ligne */
uint8_t pal[768]; /* palette 8 bits du premier morceau */
uint8_t used[256]; /* indices présents dans le décor */
int last_off, last_score;
} Strip;
static Strip strips[NSTRIPS]; /* strips[0] = le plus récent */
static int enabled = 1, next_id = 1;
static int sx[NSAMPLES], sy[NSAMPLES];
static const char *png_ptr[HD_MAX_PARTS];
void hd_set_enabled(int on) { enabled = on; }
int hd_enabled(void) { return enabled; }
void hd_reset(void) {
int i;
for (i = 0; i < NSTRIPS; i++) { free(strips[i].pix); memset(&strips[i], 0, sizeof strips[i]); }
}
/* PCX 320×200 8 bits → indices + palette ; 0 si format inattendu */
int hd_read_pcx(const char *path, uint8_t *pix, uint8_t *pal) {
FILE *f = fopen(path, "rb");
uint8_t *d;
long len;
int ok = 0;
if (!f) return 0;
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
d = (len > 128 + 769 && len < (1 << 22)) ? (uint8_t *)malloc((size_t)len) : NULL;
if (d && fread(d, 1, (size_t)len, f) == (size_t)len && d[0] == 10 && d[3] == 8 && d[65] == 1 && d[len - 769] == 12) {
int w = (d[8] | d[9] << 8) - (d[4] | d[5] << 8) + 1, h = (d[10] | d[11] << 8) - (d[6] | d[7] << 8) + 1;
int bpl = d[66] | d[67] << 8, y = 0, x = 0;
long i = 128, end = len - 769;
if (w == W0 && h == H0 && bpl >= W0) {
while (y < H0 && i < end) {
int b = d[i++], run = 1;
if (b >= 0xc0) { run = b & 0x3f; b = i < end ? d[i++] : 0; }
while (run-- > 0 && y < H0) {
if (x < W0) pix[y * W0 + x] = (uint8_t)b;
if (++x == bpl) { x = 0; y++; }
}
}
memcpy(pal, d + len - 768, 768);
ok = y == H0;
}
}
free(d);
fclose(f);
return ok;
}
/* « LOC03A.PCX » → racine « LOC03 », lettre 'A' ; nom sans lettre finale → lettre 0 */
static void split_name(const char *base, char *stem, size_t n, char *letter) {
size_t len = 0;
while (base[len] && base[len] != '.') len++;
if (len >= n) len = n - 1;
memcpy(stem, base, len);
stem[len] = 0;
*letter = 0;
if (len >= 2 && stem[len - 1] >= 'A' && stem[len - 1] <= 'D' && isdigit((unsigned char)stem[len - 2])) {
*letter = stem[len - 1];
stem[len - 1] = 0;
}
}
void hd_file_opened(const char *rel, const char *host_path) {
const char *base = strrchr(rel, '\\');
char stem[16], letter, name[20], png[600];
uint8_t *part;
Strip *s = &strips[0];
size_t len;
base = base ? base + 1 : rel;
len = strlen(base);
if (len < 5 || strcmp(base + len - 4, ".PCX")) return; /* noté même désactivé : réactivable en jeu */
snprintf(name, sizeof name, "%.*s", (int)(len - 4), base);
if (!mods_find_hd(name, png, sizeof png)) return;
split_name(base, stem, sizeof stem, &letter);
/* morceau suivant d'une pièce en plusieurs parties (LOCxxB juste après LOCxxA…) */
if (!(letter && s->pix && s->last && letter == s->last + 1 && s->parts < HD_MAX_PARTS && !strcmp(s->stem, stem))) {
free(strips[NSTRIPS - 1].pix);
memmove(&strips[1], &strips[0], (NSTRIPS - 1) * sizeof strips[0]);
memset(s, 0, sizeof *s);
snprintf(s->stem, sizeof s->stem, "%s", stem);
}
part = (uint8_t *)malloc(W0 * H0);
if (!part) return;
{
static uint8_t other_pal[768]; /* les morceaux suivants partagent la palette du premier */
if (!hd_read_pcx(host_path, part, s->parts ? other_pal : s->pal)) { free(part); return; }
}
{
int n = s->parts + 1, y;
uint8_t *pix = (uint8_t *)malloc((size_t)n * W0 * H0);
if (!pix) { free(part); return; }
for (y = 0; y < H0; y++) {
if (s->parts) memcpy(pix + (size_t)y * n * W0, s->pix + (size_t)y * s->parts * W0, (size_t)s->parts * W0);
memcpy(pix + (size_t)y * n * W0 + (size_t)s->parts * W0, part + y * W0, W0);
}
for (y = 0; y < W0 * H0; y++) s->used[part[y]] = 1;
free(s->pix);
free(part);
s->pix = pix;
snprintf(s->png[s->parts], sizeof s->png[0], "%s", png);
s->parts = n;
s->last = letter;
s->id = next_id++;
s->last_score = 0;
if (rt_cfg.verbose) fprintf(stderr, "[hd] %s : morceau %d (%s)\n", stem, n, png);
}
}
static void init_samples(void) {
uint32_t r = 12345;
int i;
for (i = 0; i < NSAMPLES; i++) {
r = r * 1103515245u + 12345u; sx[i] = (int)((r >> 8) % W0);
r = r * 1103515245u + 12345u; sy[i] = (int)((r >> 8) % H0);
}
}
static int score_at(const Strip *s, const uint8_t *fb, int off) {
int i, n = 0, w = s->parts * W0;
for (i = 0; i < NSAMPLES; i++) n += fb[sy[i] * W0 + sx[i]] == s->pix[sy[i] * w + sx[i] + off];
return n;
}
/* fondu : rapport entre palette courante et palette du décor, s'il s'agit bien d'un simple facteur */
static int palette_tint(const Strip *s, const uint8_t *dac, float tint[3]) {
double cur[3] = {0}, org[3] = {0}, err = 0, tot = 0;
int i, c;
for (i = 0; i < 256; i++)
if (s->used[i])
for (c = 0; c < 3; c++) {
int v = dac[i * 3 + c] & 63;
cur[c] += (v << 2 | v >> 4);
org[c] += s->pal[i * 3 + c];
}
for (c = 0; c < 3; c++) tint[c] = (float)((cur[c] + 1.0) / (org[c] + 1.0));
for (i = 0; i < 256; i++)
if (s->used[i])
for (c = 0; c < 3; c++) {
int v = dac[i * 3 + c] & 63;
err += fabs((v << 2 | v >> 4) - tint[c] * s->pal[i * 3 + c]);
tot += s->pal[i * 3 + c];
}
return tot <= 0 || err / tot < 0.12;
}
int hd_compose(const uint8_t *fb, const uint8_t *dac, uint8_t *mask, HdFrame *out) {
int k, best_k = -1, best_off = 0, best = 0, x, y, i;
memset(out, 0, sizeof *out);
if (!enabled || !strips[0].pix) return 0;
if (!sx[0] && !sy[0]) init_samples();
for (k = 0; k < NSTRIPS; k++) {
Strip *s = &strips[k];
int off, maxoff, sc;
if (!s->pix) continue;
maxoff = (s->parts - 1) * W0;
/* d'abord le décalage précédent : en général rien n'a bougé */
sc = score_at(s, fb, s->last_off);
if (s->last_score && sc * 10 >= s->last_score * 9) off = s->last_off;
else {
int o;
off = s->last_off;
for (o = 0; o <= maxoff; o++) {
int v = o == s->last_off ? sc : score_at(s, fb, o);
if (v > sc) { sc = v; off = o; }
}
}
if (sc > best) { best = sc; best_k = k; best_off = off; }
s->last_off = off;
s->last_score = sc;
}
if (best_k < 0 || best * 100 < NSAMPLES * 35) return 0; /* le décor n'est pas à l'écran */
{
Strip *s = &strips[best_k];
int w = s->parts * W0;
if (!palette_tint(s, dac, out->tint)) return 0;
for (y = 0; y < H0; y++) {
const uint8_t *bg = s->pix + (size_t)y * w + best_off, *row = fb + y * W0;
uint8_t *m = mask + y * W0;
for (x = 0; x < W0; x++) m[x] = row[x] == bg[x] ? 0 : 255;
}
out->active = 1;
out->set_id = s->id;
out->parts = s->parts;
for (i = 0; i < s->parts; i++) png_ptr[i] = s->png[i];
for (i = 0; i < s->parts; i++) out->png[i] = png_ptr[i];
out->offset = best_off;
out->bg = s->pix + best_off;
out->bg_stride = w;
}
return 1;
}