Sprites HD : personnages qui disparaissent (images Upscayl vides)
- 218 des 866 PNG du pack hd_upscayl sont entièrement transparents (échec d'Upscayl, ex. C32_02/C32_05 : le gorille cuisinier). Le moteur dessinait ces régions vides à la place du sprite d'origine. - Une image HD vide est désormais ignorée (log « image vide, sprites d'origine gardés ») ; chaque image garde une carte d'occupation par blocs de 8 px, et un sprite dont la région HD couvre moins de 60 % des pixels d'origine est dessiné avec le sprite d'origine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
53efd91531
commit
671b04f4ab
1 file changed
+58
-6
+58
-6
@@ -26,8 +26,15 @@ typedef struct {
|
||||
int state, tex, w, h;
|
||||
uint32_t last; /* dernière image où le sprite était à l'écran */
|
||||
size_t bytes;
|
||||
uint8_t *occ; /* carte des blocs OCC_B × OCC_B non transparents (1 bit chacun) */
|
||||
int ow, oh; /* taille de la carte en blocs ; ligne de (ow + 7) / 8 octets */
|
||||
} Ent;
|
||||
|
||||
/* Une image HD peut être vide en partie ou en entier (agrandissement raté, faute de mémoire vidéo) :
|
||||
* un sprite dont la zone HD est presque vide garde son image d'origine. */
|
||||
#define OCC_B 8
|
||||
#define OCC_MIN 60 /* % des pixels d'origine du sprite à retrouver dans l'image HD */
|
||||
|
||||
static Ent ent[NENT];
|
||||
static size_t used_bytes;
|
||||
static uint32_t frame_no;
|
||||
@@ -35,7 +42,23 @@ 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;
|
||||
typedef struct { int ent; uint32_t *px; uint8_t *dxt; size_t dxt_size; int levels, w, h; uint8_t *occ; int ow, oh; } Res;
|
||||
|
||||
/* carte des blocs non transparents ; NULL si l'image est entièrement transparente (ou mémoire insuffisante) */
|
||||
static uint8_t *occupancy(const uint32_t *px, int w, int h, int *ow, int *oh) {
|
||||
int bw = (w + OCC_B - 1) / OCC_B, bh = (h + OCC_B - 1) / OCC_B, stride = (bw + 7) / 8, x, y, any = 0;
|
||||
uint8_t *m = (uint8_t *)calloc((size_t)stride * bh, 1);
|
||||
if (!m) return NULL;
|
||||
for (y = 0; y < h; y++) {
|
||||
const uint32_t *row = px + (size_t)y * w;
|
||||
uint8_t *mr = m + (size_t)(y / OCC_B) * stride;
|
||||
for (x = 0; x < w; x++)
|
||||
if (row[x] >> 24) { mr[(x / OCC_B) >> 3] |= (uint8_t)(1 << ((x / OCC_B) & 7)); any = 1; }
|
||||
}
|
||||
if (!any) { free(m); return NULL; }
|
||||
*ow = bw; *oh = bh;
|
||||
return m;
|
||||
}
|
||||
static Req req[NQ];
|
||||
static Res res[NQ];
|
||||
static int rq_n, rs_n, quit, use_dxt;
|
||||
@@ -58,12 +81,19 @@ static int SDLCALL worker(void *unused) {
|
||||
r = req[0];
|
||||
memmove(req, req + 1, (size_t)--rq_n * sizeof *req);
|
||||
SDL_UnlockMutex(mtx);
|
||||
uint8_t *occ = NULL;
|
||||
int ow = 0, oh = 0;
|
||||
px = png_load(r.path, &w, &h, &err);
|
||||
if (!px) fprintf(stderr, "[sprites HD] %s : %s\n", r.path, err ? err : "illisible");
|
||||
else if (!(occ = occupancy(px, w, h, &ow, &oh))) {
|
||||
fprintf(stderr, "[sprites HD] %s : image vide, sprites d'origine gardés (à réexporter)\n", r.path);
|
||||
free(px); px = NULL;
|
||||
}
|
||||
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;
|
||||
res[rs_n].occ = occ; res[rs_n].ow = ow; res[rs_n].oh = oh;
|
||||
rs_n++; /* place assurée : au plus NQ chargements en cours */
|
||||
}
|
||||
SDL_UnlockMutex(mtx);
|
||||
@@ -85,9 +115,9 @@ void hds_shutdown(void) {
|
||||
SDL_WaitThread(thr, NULL);
|
||||
thr = NULL;
|
||||
}
|
||||
for (i = 0; i < rs_n; i++) { free(res[i].px); free(res[i].dxt); }
|
||||
for (i = 0; i < rs_n; i++) { free(res[i].px); free(res[i].dxt); free(res[i].occ); }
|
||||
rs_n = rq_n = 0;
|
||||
for (i = 0; i < NENT; i++) if (ent[i].tex) gfx_spr_tex_free(ent[i].tex);
|
||||
for (i = 0; i < NENT; i++) { if (ent[i].tex) gfx_spr_tex_free(ent[i].tex); free(ent[i].occ); }
|
||||
memset(ent, 0, sizeof ent);
|
||||
used_bytes = 0; pending = 0;
|
||||
if (cv) SDL_DestroyCond(cv);
|
||||
@@ -104,6 +134,7 @@ static int ntex(void) {
|
||||
|
||||
static void release(Ent *e) {
|
||||
if (e->tex) gfx_spr_tex_free(e->tex);
|
||||
free(e->occ);
|
||||
used_bytes -= e->bytes;
|
||||
memset(e, 0, sizeof *e);
|
||||
}
|
||||
@@ -162,15 +193,16 @@ int hds_poll(void) {
|
||||
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; }
|
||||
if (e->state != E_LOADING) { free(got[i].px); free(got[i].dxt); free(got[i].occ); continue; }
|
||||
e->state = E_MISSING;
|
||||
if (got[i].px || got[i].dxt) {
|
||||
if ((got[i].px || got[i].dxt) && got[i].occ) {
|
||||
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;
|
||||
e->occ = got[i].occ; e->ow = got[i].ow; e->oh = got[i].oh; got[i].occ = NULL;
|
||||
used_bytes += bytes;
|
||||
changed = 1;
|
||||
if (rt_cfg.verbose)
|
||||
@@ -179,6 +211,7 @@ int hds_poll(void) {
|
||||
}
|
||||
free(got[i].px);
|
||||
free(got[i].dxt);
|
||||
free(got[i].occ);
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
@@ -294,6 +327,25 @@ static void release_own_background(const SprDraw *s, int k, uint8_t *owner, uint
|
||||
#undef BG_LIKE
|
||||
}
|
||||
|
||||
/* 1 si l'image HD couvre au moins OCC_MIN % des pixels non transparents du sprite (sc = agrandissement) */
|
||||
static int hd_covers(const Ent *e, const SprDraw *s, float sc) {
|
||||
int x, y, n = 0, hit = 0, stride = (e->ow + 7) / 8;
|
||||
if (!e->occ) return 0;
|
||||
for (y = 0; y < s->h; y++) {
|
||||
int by = (int)(((float)(s->sy + y) + 0.5f) * sc) / OCC_B;
|
||||
const uint8_t *row = s->pix + y * s->w;
|
||||
if (by < 0 || by >= e->oh) continue;
|
||||
for (x = 0; x < s->w; x++) {
|
||||
int bx;
|
||||
if (!row[x]) continue;
|
||||
n++;
|
||||
bx = (int)(((float)(s->sx + x) + 0.5f) * sc) / OCC_B;
|
||||
if (bx >= 0 && bx < e->ow && (e->occ[(size_t)by * stride + (bx >> 3)] >> (bx & 7) & 1)) hit++;
|
||||
}
|
||||
}
|
||||
return n == 0 || hit * 100 >= n * OCC_MIN;
|
||||
}
|
||||
|
||||
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];
|
||||
@@ -347,7 +399,7 @@ int hds_compose(const uint8_t *fb, const uint8_t *dac, uint8_t *alpha, const uin
|
||||
if (sx >= 0 && sx < W0 && owner[sy * W0 + sx] == k + 1) { visible = 1; break; }
|
||||
}
|
||||
}
|
||||
if (!visible) continue;
|
||||
if (!visible || !hd_covers(e, s, sc)) continue;
|
||||
if (bg && own_background(s)) 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;
|
||||
|
||||
Reference in new issue
Block a user