Files
DoubleTroubleRecomp/runtime/hd_sprites.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

357 lines
15 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.
/* Sprites HD, côté hôte : voir hd_sprites.h. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include "hd_sprites.h"
#include "sprites.h"
#include "gfx.h"
#include "mods.h"
#include "png.h"
#include "dxt.h"
#include "rt.h"
#define W0 320
#define H0 200
#define NENT 96 /* images HD suivies (chargées, en cours, absentes) */
#define BUDGET (48u << 20) /* octets de textures de sprites (mipmaps comprises), au-delà on libère :
* le pilote en garde une copie en mémoire du processus (DXT5 : 1,4 Mo
* par planche 1280×800, une quarantaine de planches) */
#define NQ 32 /* chargements en attente au plus */
enum { E_FREE, E_LOADING, E_READY, E_MISSING };
typedef struct {
char key[40]; /* SPRITES\A01_04, SHEETS\BUD_1 */
int state, tex, w, h;
uint32_t last; /* dernière image où le sprite était à l'écran */
size_t bytes;
} Ent;
static Ent ent[NENT];
static size_t used_bytes;
static uint32_t frame_no;
static int pending; /* entrées E_LOADING */
/* ------------------------------------------------------------------ fil de chargement des PNG */
typedef struct { int ent; char path[600]; } Req;
typedef struct { int ent; uint32_t *px; uint8_t *dxt; size_t dxt_size; int levels, w, h; } Res;
static Req req[NQ];
static Res res[NQ];
static int rq_n, rs_n, quit, use_dxt;
static SDL_mutex *mtx;
static SDL_cond *cv;
static SDL_Thread *thr;
static int SDLCALL worker(void *unused) {
(void)unused;
SDL_LockMutex(mtx);
while (!quit) {
Req r;
int w = 0, h = 0;
const char *err = NULL;
uint32_t *px;
uint8_t *dxt = NULL;
size_t dxt_size = 0;
int levels = 0;
if (!rq_n) { SDL_CondWait(cv, mtx); continue; }
r = req[0];
memmove(req, req + 1, (size_t)--rq_n * sizeof *req);
SDL_UnlockMutex(mtx);
px = png_load(r.path, &w, &h, &err);
if (!px) fprintf(stderr, "[sprites HD] %s : %s\n", r.path, err ? err : "illisible");
else if (use_dxt && (dxt = dxt5_mips(px, w, h, &levels, &dxt_size)) != NULL) { free(px); px = NULL; }
SDL_LockMutex(mtx);
res[rs_n].ent = r.ent; res[rs_n].px = px; res[rs_n].w = w; res[rs_n].h = h;
res[rs_n].dxt = dxt; res[rs_n].dxt_size = dxt_size; res[rs_n].levels = levels;
rs_n++; /* place assurée : au plus NQ chargements en cours */
}
SDL_UnlockMutex(mtx);
return 0;
}
void hds_init(void) {
mtx = SDL_CreateMutex();
cv = SDL_CreateCond();
use_dxt = gfx_spr_dxt_supported();
if (mtx && cv) thr = SDL_CreateThread(worker, "sprites HD", NULL);
if (!thr) fprintf(stderr, "[sprites HD] fil de chargement impossible : %s\n", SDL_GetError());
}
void hds_shutdown(void) {
int i;
if (thr) {
SDL_LockMutex(mtx); quit = 1; SDL_CondSignal(cv); SDL_UnlockMutex(mtx);
SDL_WaitThread(thr, NULL);
thr = NULL;
}
for (i = 0; i < rs_n; i++) { free(res[i].px); free(res[i].dxt); }
rs_n = rq_n = 0;
for (i = 0; i < NENT; i++) if (ent[i].tex) gfx_spr_tex_free(ent[i].tex);
memset(ent, 0, sizeof ent);
used_bytes = 0; pending = 0;
if (cv) SDL_DestroyCond(cv);
if (mtx) SDL_DestroyMutex(mtx);
cv = NULL; mtx = NULL;
}
/* ------------------------------------------------------------------ cache */
static int ntex(void) {
int i, n = 0;
for (i = 0; i < NENT; i++) n += ent[i].state == E_READY;
return n;
}
static void release(Ent *e) {
if (e->tex) gfx_spr_tex_free(e->tex);
used_bytes -= e->bytes;
memset(e, 0, sizeof *e);
}
/* entrée la moins récemment vue qu'on peut libérer (ni en cours de chargement, ni à l'écran) */
static Ent *lru(int ready_only) {
Ent *best = NULL;
int i;
for (i = 0; i < NENT; i++) {
Ent *e = &ent[i];
if (e->state == E_LOADING || e->state == E_FREE || e->last == frame_no) continue;
if (ready_only && e->state != E_READY) continue;
if (!best || e->last < best->last) best = e;
}
return best;
}
static Ent *get(const char *key) {
char path[600];
Ent *e = NULL;
int i;
for (i = 0; i < NENT; i++)
if (ent[i].state != E_FREE && !strcmp(ent[i].key, key)) { ent[i].last = frame_no; return &ent[i]; }
if (pending >= NQ || !thr) return NULL; /* on réessaiera à l'image suivante */
for (i = 0; i < NENT && !e; i++) if (ent[i].state == E_FREE) e = &ent[i];
if (!e && (e = lru(0)) != NULL) release(e);
if (!e) return NULL;
snprintf(e->key, sizeof e->key, "%s", key);
e->last = frame_no;
if (!mods_find_hd(key, path, sizeof path)) {
e->state = E_MISSING;
if (rt_cfg.verbose) fprintf(stderr, "[sprites HD] pas d'image HD pour %s\n", key);
return e;
}
e->state = E_LOADING;
pending++;
SDL_LockMutex(mtx);
req[rq_n].ent = (int)(e - ent);
snprintf(req[rq_n].path, sizeof req[rq_n].path, "%s", path);
rq_n++;
SDL_CondSignal(cv);
SDL_UnlockMutex(mtx);
return e;
}
int hds_poll(void) {
Res got[NQ];
int n, i, changed = 0;
if (!mtx) return 0;
SDL_LockMutex(mtx);
n = rs_n;
memcpy(got, res, (size_t)n * sizeof *res);
rs_n = 0;
SDL_UnlockMutex(mtx);
for (i = 0; i < n; i++) {
Ent *e = &ent[got[i].ent];
size_t bytes = got[i].dxt ? got[i].dxt_size : (size_t)got[i].w * (size_t)got[i].h * 4 * 4 / 3;
pending--;
if (e->state != E_LOADING) { free(got[i].px); free(got[i].dxt); continue; }
e->state = E_MISSING;
if (got[i].px || got[i].dxt) {
Ent *v;
while ((used_bytes + bytes > BUDGET || ntex() >= GFX_SPR_TEX - 1) && (v = lru(1)) != NULL) release(v);
e->tex = got[i].dxt ? gfx_spr_tex_load_dxt5(got[i].dxt, got[i].w, got[i].h, got[i].levels)
: gfx_spr_tex_load(got[i].px, got[i].w, got[i].h);
if (e->tex) {
e->state = E_READY; e->w = got[i].w; e->h = got[i].h; e->bytes = bytes;
used_bytes += bytes;
changed = 1;
if (rt_cfg.verbose)
fprintf(stderr, "[sprites HD] %s : %dx%d (%u Mo de textures)\n", e->key, e->w, e->h, (unsigned)(used_bytes >> 20));
}
}
free(got[i].px);
free(got[i].dxt);
}
return changed;
}
/* ------------------------------------------------------------------ image */
/* Correction de couleur par indice : couleur de la palette du jeu à cet instant - couleur de l'image exportée,
* codée (c + 255) / 2. Les planches d'objets servent à toutes les pièces d'une partie, chacune avec sa palette,
* et les fondus changent la palette : l'image HD n'a les bonnes couleurs qu'une fois corrigée. */
static void palette_correction(const uint8_t *dac, const uint8_t *ref, uint8_t corr[256][3]) {
int i, c;
for (i = 0; i < 256; i++)
for (c = 0; c < 3; c++) {
int v = dac[i * 3 + c] & 63;
int e = ((v << 2 | v >> 4) - ref[i * 3 + c] + 255 + 1) / 2;
corr[i][c] = (uint8_t)(e < 1 ? 1 : e); /* 0 est réservé : « pas de correction » */
}
}
/* atlas d'un sprite : A = masque ; RGB = correction de son indice, prolongée dans les pixels transparents
* (l'image HD y a des couleurs voisines, et le shader interpole la correction entre pixels d'origine) */
static void fill_atlas(uint8_t *at, const SprDraw *s, int k, const uint8_t *owner, const uint8_t corr[256][3]) {
int x, y, pass;
for (y = 0; y < s->h; y++) {
int sy = s->y + y;
uint8_t *a = at + (size_t)y * GFX_MASK_W * 4;
const uint8_t *row = s->pix + y * s->w;
for (x = 0; x < s->w; x++, a += 4) {
int sx = s->x + x, idx = row[x];
a[3] = !idx ? 255 : (sx >= 0 && sx < W0 && sy >= 0 && sy < H0 && owner[sy * W0 + sx] == k + 1) ? 255 : 0;
if (idx) { a[0] = corr[idx][0]; a[1] = corr[idx][1]; a[2] = corr[idx][2]; }
else a[0] = a[1] = a[2] = 0; /* 0 : pas encore de correction */
}
}
for (pass = 0; pass < 2; pass++) /* bord transparent : moyenne des voisins opaques */
for (y = 0; y < s->h; y++)
for (x = 0; x < s->w; x++) {
uint8_t *a = at + ((size_t)y * GFX_MASK_W + x) * 4;
int sum[3] = {0}, n = 0, dx, dy;
if (s->pix[y * s->w + x] || a[0] | a[1] | a[2]) continue;
for (dy = -1; dy <= 1; dy++)
for (dx = -1; dx <= 1; dx++) {
const uint8_t *b;
if (x + dx < 0 || x + dx >= s->w || y + dy < 0 || y + dy >= s->h) continue;
b = at + ((size_t)(y + dy) * GFX_MASK_W + x + dx) * 4;
if (!(b[0] | b[1] | b[2])) continue;
sum[0] += b[0]; sum[1] += b[1]; sum[2] += b[2]; n++;
}
if (n) { a[0] = (uint8_t)(sum[0] / n); a[1] = (uint8_t)(sum[1] / n); a[2] = (uint8_t)(sum[2] / n); }
}
for (y = 0; y < s->h; y++) /* reste : pas de correction */
for (x = 0; x < s->w; x++) {
uint8_t *a = at + ((size_t)y * GFX_MASK_W + x) * 4;
if (!(a[0] | a[1] | a[2])) a[0] = a[1] = a[2] = 128;
}
}
/* Objet dessiné avec son propre fond (rectangle opaque peint comme le décor) : la partie de ce fond reliée au
* bord du rectangle, de la couleur du décor d'origine à cet endroit (à peu près : le fond de la planche n'a pas
* toujours les mêmes indices), revient au décor HD, sinon le fond agrandi du sprite se verrait en rectangle. Le remplissage part des pixels opaques du bord et ne
* traverse pas la transparence : le contour d'un personnage n'est pas touché ; un pixel de la même couleur
* au milieu du sprite reste au sprite. */
static int near_color(const uint8_t *dac, int a, int b) {
int c;
if (a == b) return 1;
for (c = 0; c < 3; c++) {
int d = (dac[a * 3 + c] & 63) - (dac[b * 3 + c] & 63);
if (d > 5 || d < -5) return 0; /* 6 bits : écart de 20/255 au plus */
}
return 1;
}
static void release_own_background(const SprDraw *s, int k, uint8_t *owner, uint8_t *alpha, const uint8_t *dac,
const uint8_t *bg, int bg_stride) {
static uint8_t seen[640 * 400];
static int stack[640 * 400];
int n = 0, x, y, i;
#define BG_LIKE(X, Y) (s->pix[(Y) * s->w + (X)] && (s->x + (X) >= 0 && s->x + (X) < W0 && s->y + (Y) >= 0 \
&& s->y + (Y) < H0 && owner[(s->y + (Y)) * W0 + s->x + (X)] == k + 1 \
&& near_color(dac, s->pix[(Y) * s->w + (X)], bg[(size_t)(s->y + (Y)) * bg_stride + s->x + (X)])))
memset(seen, 0, (size_t)s->w * s->h);
for (y = 0; y < s->h; y++)
for (x = 0; x < s->w; x++)
if ((x == 0 || y == 0 || x == s->w - 1 || y == s->h - 1) && BG_LIKE(x, y)) {
seen[y * s->w + x] = 1; stack[n++] = y * s->w + x;
}
while (n > 0) {
int p = stack[--n], px = p % s->w, py = p / s->w;
static const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
owner[(s->y + py) * W0 + s->x + px] = 0;
alpha[(s->y + py) * W0 + s->x + px] = 0; /* décor HD à la place */
for (i = 0; i < 4; i++) {
int qx = px + d[i][0], qy = py + d[i][1];
if (qx < 0 || qy < 0 || qx >= s->w || qy >= s->h || seen[qy * s->w + qx] || !BG_LIKE(qx, qy)) continue;
seen[qy * s->w + qx] = 1; stack[n++] = qy * s->w + qx;
}
}
#undef BG_LIKE
}
int hds_compose(const uint8_t *fb, const uint8_t *dac, uint8_t *alpha, const uint8_t *bg, int bg_stride, int enabled) {
static uint8_t owner[W0 * H0]; /* 1 + sprite visible en chaque pixel, 0 = aucun */
static uint8_t atlas[GFX_MASK_W * GFX_MASK_H * 4];
static uint8_t corr[256][3];
const uint8_t *corr_pal = NULL;
static GfxSprite out[GFX_SPR_MAX];
static uint8_t hd_ok[GFX_SPR_MAX];
const SprDraw *list;
int n = spr_frame(&list, NULL), k, m = 0, shx = 0, shy = 0, shh = 0, rows = 0, i;
frame_no++;
if (!enabled || n <= 0) { gfx_spr_frame(NULL, 0, NULL, 0); return 0; }
if (n > GFX_SPR_MAX) n = GFX_SPR_MAX;
memset(owner, 0, sizeof owner);
for (k = 0; k < n; k++) { /* le dernier sprite dessiné dont le pixel est resté */
const SprDraw *s = &list[k];
int x, y;
for (y = 0; y < s->h; y++) {
int sy = s->y + y;
const uint8_t *row = s->pix + y * s->w;
if (sy < 0 || sy >= H0) continue;
for (x = 0; x < s->w; x++) {
int sx = s->x + x;
if (!row[x] || sx < 0 || sx >= W0) continue;
owner[sy * W0 + sx] = fb[sy * W0 + sx] == row[x] ? (uint8_t)(k + 1) : 0;
}
}
}
for (k = 0; k < n; k++) {
const SprDraw *s = &list[k];
int x, y, visible = 0;
float sc;
Ent *e;
hd_ok[k] = 0;
if (!s->ref_pal || s->img_w <= 0 || s->img_h <= 0) continue;
e = get(s->key);
if (!e || e->state != E_READY) continue;
if (s->w * s->h > 640 * 400) continue;
sc = (float)e->w / (float)s->img_w;
if (sc < 1.f || fabsf((float)e->h - (float)s->img_h * sc) > sc + 0.5f) {
fprintf(stderr, "[sprites HD] %s : %dx%d ne correspond pas à %dx%d d'origine (à réexporter)\n",
e->key, e->w, e->h, s->img_w, s->img_h);
release(e);
e->state = E_MISSING; snprintf(e->key, sizeof e->key, "%s", s->key); e->last = frame_no;
continue;
}
for (y = 0; y < s->h && !visible; y++) {
int sy = s->y + y;
if (sy < 0 || sy >= H0) continue;
for (x = 0; x < s->w; x++) {
int sx = s->x + x;
if (sx >= 0 && sx < W0 && owner[sy * W0 + sx] == k + 1) { visible = 1; break; }
}
}
if (!visible) continue;
if (bg) release_own_background(s, k, owner, alpha, dac, bg, bg_stride);
if (shx + s->w > GFX_MASK_W) { shx = 0; shy += shh; shh = 0; }
if (s->w > GFX_MASK_W || shy + s->h > GFX_MASK_H) continue;
if (s->ref_pal != corr_pal) { palette_correction(dac, s->ref_pal, corr); corr_pal = s->ref_pal; }
fill_atlas(atlas + ((size_t)shy * GFX_MASK_W + shx) * 4, s, k, owner, corr);
out[m].tex = e->tex;
out[m].x = (float)s->x; out[m].y = (float)s->y; out[m].w = (float)s->w; out[m].h = (float)s->h;
out[m].u0 = (float)s->sx / (float)s->img_w; out[m].v0 = (float)s->sy / (float)s->img_h;
out[m].u1 = (float)(s->sx + s->w) / (float)s->img_w; out[m].v1 = (float)(s->sy + s->h) / (float)s->img_h;
out[m].mx = shx; out[m].my = shy;
m++;
shx += s->w;
if (s->h > shh) shh = s->h;
if (shy + shh > rows) rows = shy + shh;
hd_ok[k] = 1;
}
if (m)
for (i = 0; i < W0 * H0; i++)
if (owner[i] && hd_ok[owner[i] - 1]) alpha[i] = 0;
gfx_spr_frame(out, m, atlas, rows);
return m;
}