Files
DoubleTroubleRecomp/runtime/sprites.c
T
linkatplugandClaude Opus 5.5 83e60d38ba Adresses de la version française regroupées dans runtime/addr_fr.h
Toutes les adresses propres à TUCKER.EXE et INTRO.EXE (fonctions remplacées, variables du
son, du minuteur, de l'affichage, de la souris, canaux de l'intro) ont un nom et un
commentaire dans addr_fr.h, inclus par addr.h. overrides.c, sprites.c et les commentaires
n'emploient plus que ces noms (FN_xxx pour les fonctions, ADR_xxx pour les variables,
INTRO_xxx pour l'intro) ; ORIG(FN_xxx) désigne la fonction d'origine. Les listes
overrides_*.txt restent en hexadécimal, avec le nom en commentaire sur chaque ligne.

Ajouter une langue : écrire addr_xx.h avec les mêmes noms et le choisir dans addr.h.

Aucun changement de comportement : le code après préprocesseur est identique token pour
token à celui d'avant (overrides.c, sprites.c, audio.c, dos.c), au suffixe u près sur
trois constantes de même valeur.

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

299 lines
12 KiB
C

/* Sprites HD : voir sprites.h. */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpu.h"
#include "rt.h"
#include "addr.h"
#include "sprites.h"
#define ROW 640 /* largeur du tampon de la pièce */
#define NREG 96 /* fichiers SPR suivis */
#define NENC 4096 /* sprites découpés dans des planches PCX */
#define NDRAW 160 /* sprites par image */
#define POOL (1 << 20) /* pixels décodés par image */
/* ------------------------------------------------------------------ palettes de référence */
typedef struct { char name[16]; uint8_t pal[768]; int ok; } Pal;
static Pal pals[24];
static int npals;
static int read_pcx_palette(const char *path, uint8_t *pal) {
FILE *f = fopen(path, "rb");
long len;
int ok = 0;
if (!f) return 0;
fseek(f, 0, SEEK_END);
len = ftell(f);
if (len > 769 && fseek(f, len - 769, SEEK_SET) == 0 && fgetc(f) == 12) ok = fread(pal, 1, 768, f) == 768;
fclose(f);
return ok;
}
/* palette du PCX « name » (sans extension) ; host : chemin réel s'il est connu, sinon recherche dans le jeu */
static const uint8_t *pcx_palette(const char *name, const char *host) {
int i;
char path[600];
for (i = 0; i < npals; i++) if (!strcmp(pals[i].name, name)) return pals[i].ok ? pals[i].pal : NULL;
if (npals == 24) npals = 0;
i = npals++;
snprintf(pals[i].name, sizeof pals[i].name, "%s", name);
pals[i].ok = 0;
if (host) pals[i].ok = read_pcx_palette(host, pals[i].pal);
else {
char dosname[32];
snprintf(dosname, sizeof dosname, "%s.PCX", name);
if (dos_map_path(dosname, path, sizeof path, 0)) pals[i].ok = read_pcx_palette(path, pals[i].pal);
}
return pals[i].ok ? pals[i].pal : NULL;
}
/* Palette d'export d'un fichier SPR : celle du décor de la pièce indiquée par le nom (A01_02 -> LOC01,
* C50_02 -> LOC50, LOC03A si LOC03 n'existe pas), PARTONE/PARTTWO/PARTTHR -> PT1BAK/PT2BAK/PT3BAK.
* Même règle que palette_for() de tools/hd_export.py. */
static const uint8_t *spr_palette(const char *name) {
char room[16];
const char *p = name;
const uint8_t *pal;
if (!strcmp(name, "PARTONE")) return pcx_palette("PT1BAK", NULL);
if (!strcmp(name, "PARTTWO")) return pcx_palette("PT2BAK", NULL);
if (!strcmp(name, "PARTTHR")) return pcx_palette("PT3BAK", NULL);
while (*p && !isdigit((unsigned char)*p)) p++;
if (!(isdigit((unsigned char)p[0]) && isdigit((unsigned char)p[1]))) return pcx_palette("LOC01", NULL);
snprintf(room, sizeof room, "LOC%c%c", p[0], p[1]);
if ((pal = pcx_palette(room, NULL))) return pal;
snprintf(room, sizeof room, "LOC%c%cA", p[0], p[1]);
return pcx_palette(room, NULL);
}
/* ------------------------------------------------------------------ fichiers SPR en mémoire */
typedef struct {
char name[16];
uint32_t addr, len; /* zone mémoire (linéaire) occupée par le fichier */
int nframes; /* 0 tant que la mise en page n'est pas calculée */
int *lay; /* par image : page, x, y */
int *page_wh; /* par page : largeur, hauteur */
} Region;
static Region reg[NREG];
static int nreg;
/* ------------------------------------------------------------------ sprites découpés dans des planches PCX */
typedef struct { uint32_t addr, len; int w, h, sx, sy; char sheet[16]; } Enc;
static Enc enc[NENC];
static int nenc;
static char cur_pcx[16], cur_pcx_host[600]; /* dernière planche PCX lue (celle que le jeu vient de décoder) */
/* ------------------------------------------------------------------ image en cours / affichée */
typedef struct { SprDraw d; uint32_t dest; } Pending;
static Pending cur[NDRAW];
static int ncur;
static SprDraw shown[NDRAW];
static int nshown;
static uint32_t serial;
static uint8_t *pool[2]; /* pool[0] : image en cours ; pool[1] : image affichée */
static int pool_used;
static void free_region(Region *r) { free(r->lay); free(r->page_wh); memset(r, 0, sizeof *r); }
void spr_reset(void) {
int i;
for (i = 0; i < nreg; i++) free_region(&reg[i]);
nreg = ncur = nshown = nenc = 0;
cur_pcx[0] = 0;
}
static void base_name(const char *rel, char *out, size_t n) {
const char *b = strrchr(rel, '\\'), *s = strrchr(rel, '/');
size_t i = 0;
if (s > b) b = s;
b = b ? b + 1 : rel;
while (b[i] && b[i] != '.' && i + 1 < n) { out[i] = (char)toupper((unsigned char)b[i]); i++; }
out[i] = 0;
}
static int ends_with(const char *s, const char *ext) {
size_t l = strlen(s), e = strlen(ext), i;
if (l < e) return 0;
for (i = 0; i < e; i++) if (toupper((unsigned char)s[l - e + i]) != ext[i]) return 0;
return 1;
}
static void drop_overlapping(uint32_t addr, uint32_t len) {
int i;
for (i = 0; i < nreg;)
if (reg[i].addr < addr + len && addr < reg[i].addr + reg[i].len) { free_region(&reg[i]); reg[i] = reg[--nreg]; memset(&reg[nreg], 0, sizeof reg[nreg]); }
else i++;
for (i = 0; i < nenc;)
if (enc[i].addr < addr + len && addr < enc[i].addr + enc[i].len) enc[i] = enc[--nenc];
else i++;
}
void spr_file_read(const char *rel, const char *host, long fileoff, uint32_t addr, uint32_t len) {
char name[16];
int i;
if (!len) return;
base_name(rel, name, sizeof name);
if (ends_with(rel, ".PCX")) { /* le jeu va décoder cette planche */
snprintf(cur_pcx, sizeof cur_pcx, "%s", name);
snprintf(cur_pcx_host, sizeof cur_pcx_host, "%s", host ? host : "");
return;
}
if (!ends_with(rel, ".SPR")) return;
if (fileoff > 0) { /* suite d'une lecture en plusieurs fois */
for (i = 0; i < nreg; i++)
if (!strcmp(reg[i].name, name) && reg[i].addr + reg[i].len == addr) { reg[i].len += len; return; }
return;
}
drop_overlapping(addr, len); /* mémoire réutilisée */
if (nreg == NREG) { free_region(&reg[0]); reg[0] = reg[--nreg]; memset(&reg[nreg], 0, sizeof reg[nreg]); }
memset(&reg[nreg], 0, sizeof reg[nreg]);
snprintf(reg[nreg].name, sizeof reg[nreg].name, "%s", name);
reg[nreg].addr = addr;
reg[nreg].len = len;
nreg++;
}
void spr_encode(uint32_t src, uint32_t dest, int w, int h, uint32_t len) {
uint32_t base = RD32(SB(S_DS) + ADR_TAMPON_PLANCHE), off = src - base, lin = (SB(S_DS) + dest) & MEM_MASK;
if (!cur_pcx[0] || off >= 320u * 200u || w <= 0 || h <= 0 || !len || len > 1u << 20) return;
drop_overlapping(lin, len);
if (nenc == NENC) nenc = 0;
enc[nenc].addr = lin;
enc[nenc].len = len;
enc[nenc].w = w;
enc[nenc].h = h;
enc[nenc].sx = (int)(off % 320);
enc[nenc].sy = (int)(off / 320);
snprintf(enc[nenc].sheet, sizeof enc[nenc].sheet, "%s", cur_pcx);
pcx_palette(cur_pcx, cur_pcx_host[0] ? cur_pcx_host : NULL); /* palette mise en cache tant que le chemin est connu */
nenc++;
}
/* Mise en page des images d'un SPR dans ses planches HD : rangées de gauche à droite, page suivante quand
* c'est plein. Doit rester identique à layout() de tools/hd_export.py. */
static int make_layout(Region *r) {
uint32_t n = RD32(r->addr), k;
int page = 0, x = SPR_PAD, y = SPR_PAD, rowh = 0, total_w = SPR_PAD, pw;
if (n == 0 || n > 4096 || 4 + 4 * n > r->len) return 0;
for (k = 0; k < n; k++) {
uint32_t o = RD32(r->addr + 4 + 4 * k);
if (o + 12 > r->len) return 0;
total_w += RD16(r->addr + o) + SPR_PAD;
}
r->lay = (int *)calloc(n * 3, sizeof(int));
r->page_wh = (int *)calloc(n * 2, sizeof(int));
if (!r->lay || !r->page_wh) return 0;
pw = total_w < SPR_PAGE_W ? total_w : SPR_PAGE_W;
for (k = 0; k < n; k++) {
uint32_t o = RD32(r->addr + 4 + 4 * k);
int w = RD16(r->addr + o), h = RD16(r->addr + o + 2);
if (x + w + SPR_PAD > pw && x > SPR_PAD) { x = SPR_PAD; y += rowh + SPR_PAD; rowh = 0; }
if (y + h + SPR_PAD > SPR_PAGE_H && y > SPR_PAD) {
r->page_wh[2 * page] = pw; r->page_wh[2 * page + 1] = y + rowh + SPR_PAD;
page++; x = SPR_PAD; y = SPR_PAD; rowh = 0;
}
r->lay[3 * k] = page; r->lay[3 * k + 1] = x; r->lay[3 * k + 2] = y;
x += w + SPR_PAD;
if (h > rowh) rowh = h;
}
r->page_wh[2 * page] = pw; r->page_wh[2 * page + 1] = y + rowh + SPR_PAD;
r->nframes = (int)n;
return 1;
}
/* RLE : octet non nul = pixel, 00 n = n pixels transparents (même lecture que FN_SPR_DESSINE) */
static void decode(uint32_t src, int w, int h, uint8_t *out) {
int i = 0, total = w * h;
while (i < total) {
uint8_t b = RD8(src++);
if (b) out[i++] = b;
else {
int n = RD8(src++);
if (n > total - i) n = total - i;
memset(out + i, 0, (size_t)n);
i += n;
}
}
}
void spr_blit(uint32_t dest, uint32_t src, int w, int h) {
uint32_t lin = (SB(S_DS) + src) & MEM_MASK;
Pending *p;
int i;
if (w <= 0 || h <= 0 || w > 640 || h > 400 || ncur == NDRAW) return;
if (!pool[0]) { pool[0] = (uint8_t *)malloc(POOL); pool[1] = (uint8_t *)malloc(POOL); if (!pool[0] || !pool[1]) return; }
if (pool_used + w * h > POOL) return;
p = &cur[ncur];
memset(p, 0, sizeof *p);
/* sprite découpé dans une planche PCX ? */
for (i = nenc - 1; i >= 0; i--)
if (enc[i].addr == lin && enc[i].w == w && enc[i].h == h) {
snprintf(p->d.key, sizeof p->d.key, "SHEETS\\%s", enc[i].sheet);
p->d.sx = enc[i].sx; p->d.sy = enc[i].sy;
p->d.img_w = 320; p->d.img_h = 200;
p->d.ref_pal = pcx_palette(enc[i].sheet, NULL);
break;
}
/* image d'un fichier SPR ? */
if (i < 0) {
Region *r = NULL;
int lo, hi, k = -1;
for (i = 0; i < nreg; i++) if (lin - reg[i].addr < reg[i].len) { r = &reg[i]; break; }
if (!r || (!r->nframes && !make_layout(r))) return;
lo = 0; hi = r->nframes - 1; /* table d'offsets croissante en tête de fichier */
while (lo <= hi) {
int m = (lo + hi) / 2;
uint32_t a = r->addr + RD32(r->addr + 4 + 4 * (uint32_t)m) + 12;
if (a == lin) { k = m; break; }
if (a < lin) lo = m + 1; else hi = m - 1;
}
if (k < 0) return;
if (r->lay[3 * k]) snprintf(p->d.key, sizeof p->d.key, "SPRITES\\%s_P%d", r->name, r->lay[3 * k]);
else snprintf(p->d.key, sizeof p->d.key, "SPRITES\\%s", r->name);
p->d.sx = r->lay[3 * k + 1]; p->d.sy = r->lay[3 * k + 2];
p->d.img_w = r->page_wh[2 * r->lay[3 * k]]; p->d.img_h = r->page_wh[2 * r->lay[3 * k] + 1];
p->d.ref_pal = spr_palette(r->name);
}
p->d.w = w; p->d.h = h;
p->d.pix = pool[0] + pool_used;
pool_used += w * h;
decode(lin, w, h, (uint8_t *)p->d.pix);
p->dest = dest;
ncur++;
}
/* fin d'image : src = première ligne copiée à l'écran (tampon de la pièce + défilement) */
void spr_present(uint32_t src) {
int i;
uint8_t *t;
nshown = 0;
for (i = 0; i < ncur; i++) {
long d = (long)(int32_t)(cur[i].dest - src);
long x = ((d % ROW) + ROW) % ROW, y = (d - x) / ROW;
if (x >= 480) { x -= ROW; y++; } /* sprite qui commence à gauche de l'écran */
cur[i].d.x = (int)x;
cur[i].d.y = (int)y;
if (x >= 320 || y >= 200 || x + cur[i].d.w <= 0 || y + cur[i].d.h <= 0) continue;
shown[nshown++] = cur[i].d;
}
if (rt_cfg.verbose && serial % 140 == 0) {
fprintf(stderr, "[sprites] image %u : %d sprite(s)", serial, nshown);
for (i = 0; i < nshown && i < 8; i++)
fprintf(stderr, " %s(%d,%d)@%d,%d", shown[i].key, shown[i].sx, shown[i].sy, shown[i].x, shown[i].y);
fprintf(stderr, "\n");
}
t = pool[0]; pool[0] = pool[1]; pool[1] = t; /* les pixels affichés restent valides */
ncur = 0;
pool_used = 0;
serial++;
}
int spr_frame(const SprDraw **list, uint32_t *ser) {
*list = shown;
if (ser) *ser = serial;
return nshown;
}