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

567 lines
26 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.
/* Rendu OpenGL 3.3 core (portable vers WebGL 2 / GLES 3 : mêmes shaders, autre en-tête #version).
*
* Passes :
* net : un pixel source = un bloc de pixels de sortie (texelFetch)
* lissé : « sharp bilinear » : agrandissement entier puis bilinéaire sur le reste (pas de flou, pas de pixels inégaux)
* xBR : détection de contours façon xBR niveau 2, rendue ×4 dans une texture intermédiaire, puis « lissé »
* CRT : lignes de balayage dont l'épaisseur dépend de la luminosité, masque à grille, légère courbure
* Pack HD : les images HD du décor sont dessinées d'abord, puis les sprites HD (personnages, objets : un
* rectangle texturé par sprite, avec un masque qui cache ce que le jeu a dessiné par-dessus), puis l'image du
* jeu, dont le canal alpha vaut 0 là où le décor ou un sprite d'origine est visible. Le CRT ignore le pack HD.
* Puis voile (menu) et surimpression. Toutes les couleurs passent par la palette VGA déjà convertie par l'hôte. */
#include <stdio.h>
#include <string.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include "gfx.h"
#include "menu.h"
#include "rt.h"
/* ------------------------------------------------------------------ fonctions GL > 1.1 (chargées à l'exécution) */
#define GL_FUNCS \
X(PFNGLCREATESHADERPROC, glCreateShader) X(PFNGLSHADERSOURCEPROC, glShaderSource) \
X(PFNGLCOMPILESHADERPROC, glCompileShader) X(PFNGLGETSHADERIVPROC, glGetShaderiv) \
X(PFNGLGETSHADERINFOLOGPROC, glGetShaderInfoLog) X(PFNGLDELETESHADERPROC, glDeleteShader) \
X(PFNGLCREATEPROGRAMPROC, glCreateProgram) X(PFNGLATTACHSHADERPROC, glAttachShader) \
X(PFNGLLINKPROGRAMPROC, glLinkProgram) X(PFNGLGETPROGRAMIVPROC, glGetProgramiv) \
X(PFNGLGETPROGRAMINFOLOGPROC, glGetProgramInfoLog) X(PFNGLDELETEPROGRAMPROC, glDeleteProgram) \
X(PFNGLUSEPROGRAMPROC, glUseProgram) X(PFNGLGETUNIFORMLOCATIONPROC, glGetUniformLocation) \
X(PFNGLUNIFORM1IPROC, glUniform1i) X(PFNGLUNIFORM1FPROC, glUniform1f) \
X(PFNGLUNIFORM2FPROC, glUniform2f) X(PFNGLUNIFORM4FPROC, glUniform4f) \
X(PFNGLGENVERTEXARRAYSPROC, glGenVertexArrays) X(PFNGLBINDVERTEXARRAYPROC, glBindVertexArray) \
X(PFNGLDELETEVERTEXARRAYSPROC, glDeleteVertexArrays) X(PFNGLGENFRAMEBUFFERSPROC, glGenFramebuffers) \
X(PFNGLBINDFRAMEBUFFERPROC, glBindFramebuffer) X(PFNGLFRAMEBUFFERTEXTURE2DPROC, glFramebufferTexture2D) \
X(PFNGLCHECKFRAMEBUFFERSTATUSPROC, glCheckFramebufferStatus) X(PFNGLDELETEFRAMEBUFFERSPROC, glDeleteFramebuffers) \
X(PFNGLACTIVETEXTUREPROC, glActiveTexture) X(PFNGLGENERATEMIPMAPPROC, glGenerateMipmap) \
X(PFNGLCOMPRESSEDTEXIMAGE2DPROC, glCompressedTexImage2D)
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
#endif
#define X(type, name) static type p_##name;
GL_FUNCS
#undef X
/* ------------------------------------------------------------------ shaders */
static const char *VS =
"uniform float u_flip;\n"
"out vec2 v_uv;\n"
"void main() {\n"
" vec2 p = vec2(float(gl_VertexID & 1), float(gl_VertexID >> 1)) * 2.0 - 1.0;\n"
" v_uv = vec2(p.x * 0.5 + 0.5, 0.5 - p.y * 0.5 * u_flip);\n" /* ligne 0 de la texture en haut de l'écran ; */
/* u_flip = -1 pour une texture intermédiaire */
" gl_Position = vec4(p, 0.0, 1.0);\n"
"}\n";
/* Sprite HD : rectangle u_rect (pixels d'origine 320×200) dans le rectangle du jeu */
static const char *VS_SPR =
"uniform vec4 u_rect;\n"
"out vec2 v_uv;\n"
"void main() {\n"
" vec2 c = vec2(float(gl_VertexID & 1), float(gl_VertexID >> 1));\n"
" vec2 p = u_rect.xy + c * u_rect.zw;\n"
" v_uv = c;\n"
" gl_Position = vec4(p.x / 160.0 - 1.0, 1.0 - p.y / 100.0, 0.0, 1.0);\n"
"}\n";
#define FS_HEAD \
"uniform sampler2D u_src;\n" \
"uniform vec2 u_src_size;\n" \
"uniform vec2 u_out_size;\n" \
"uniform float u_alpha;\n" \
"uniform vec4 u_color;\n" \
"in vec2 v_uv;\n" \
"out vec4 o_color;\n"
static const char *FS_NEAREST = FS_HEAD
"void main() {\n"
" ivec2 p = ivec2(clamp(floor(v_uv * u_src_size), vec2(0.0), u_src_size - 1.0));\n"
" o_color = texelFetch(u_src, p, 0);\n"
"}\n";
/* agrandissement entier implicite, interpolation limitée à la bordure entre deux pixels source */
static const char *FS_SMOOTH = FS_HEAD
"void main() {\n"
" vec2 t = v_uv * u_src_size;\n"
" vec2 k = max(floor(u_out_size / u_src_size), vec2(1.0));\n"
" vec2 c = fract(t) - 0.5;\n"
" vec2 r = 0.5 - 0.5 / k;\n"
" vec2 f = (c - clamp(c, -r, r)) * k + 0.5;\n"
" o_color = texture(u_src, (floor(t) + f) / u_src_size);\n"
"}\n";
static const char *FS_OVERLAY = FS_HEAD
"void main() {\n"
" vec4 c = texture(u_src, v_uv);\n"
" o_color = vec4(c.rgb, c.a * u_alpha);\n"
"}\n";
static const char *FS_SOLID = FS_HEAD
"void main() { o_color = u_color; }\n";
/* Décor HD : jusqu'à 4 images côte à côte (320 colonnes d'origine chacune), décalées de u_offset colonnes.
* Les dérivées sont prises sur la coordonnée continue, pour que le niveau de mipmap ne saute pas au raccord. */
static const char *FS_HD = FS_HEAD
"uniform sampler2D u_hd0, u_hd1, u_hd2, u_hd3;\n"
"uniform float u_offset;\n"
"uniform int u_parts;\n"
"void main() {\n"
" float gx = (v_uv.x * 320.0 + u_offset) / 320.0;\n"
" int i = clamp(int(floor(gx)), 0, u_parts - 1);\n"
" vec2 uv = vec2(gx - float(i), v_uv.y);\n"
" vec2 dx = dFdx(vec2(gx, v_uv.y)), dy = dFdy(vec2(gx, v_uv.y));\n"
" vec4 c;\n"
" if (i == 0) c = textureGrad(u_hd0, uv, dx, dy);\n"
" else if (i == 1) c = textureGrad(u_hd1, uv, dx, dy);\n"
" else if (i == 2) c = textureGrad(u_hd2, uv, dx, dy);\n"
" else c = textureGrad(u_hd3, uv, dx, dy);\n"
" o_color = vec4(c.rgb * u_color.rgb, 1.0);\n"
"}\n";
/* Sprite HD : u_uv = rectangle dans l'image HD. Atlas (un texel par pixel d'origine, rectangle u_mrect) :
* a = masque, 0 là où le jeu a dessiné autre chose par-dessus (premier plan, autre sprite, texte) ;
* rgb = correction de couleur (palette du jeu à cet instant - palette de l'export) codée (c + 1) / 2,
* interpolée entre pixels d'origine comme l'image HD l'est : la couleur suit la palette de la pièce et
* les fondus. */
static const char *FS_SPR = FS_HEAD
"uniform sampler2D u_mask;\n"
"uniform vec4 u_uv;\n"
"uniform vec4 u_mrect;\n"
"void main() {\n"
" vec2 h = 0.5 / vec2(textureSize(u_src, 0));\n" /* pas de texel voisin de la planche au bord */
" vec4 c = texture(u_src, clamp(mix(u_uv.xy, u_uv.zw, v_uv), u_uv.xy + h, u_uv.zw - h));\n"
" vec2 p = v_uv * u_mrect.zw;\n"
" vec2 q = (u_mrect.xy + clamp(p, vec2(0.5), u_mrect.zw - 0.5)) / vec2(textureSize(u_mask, 0));\n"
" vec4 k = texture(u_mask, q);\n"
" o_color = vec4(clamp(c.rgb + k.rgb * 2.0 - 1.0, 0.0, 1.0), c.a * k.a);\n"
"}\n";
/* Contours façon xBR (niveau 2) : pour chacun des 4 coins du pixel E, on compare deux sommes pondérées de
* différences de couleur le long des deux diagonales ; si le contour traverse le coin, on mélange E avec
* le voisin le plus proche, suivant une droite à 45°, ou à 30°/60° quand le contour se prolonge. */
static const char *FS_XBR = FS_HEAD
"vec3 Y[25];\n"
"vec4 P[25];\n"
"vec3 yuv(vec3 c) {\n"
" return vec3(dot(c, vec3(0.299, 0.587, 0.114)), dot(c, vec3(-0.169, -0.331, 0.5)), dot(c, vec3(0.5, -0.419, -0.081)));\n"
"}\n"
"float df(int a, int b) { vec3 d = abs(Y[a] - Y[b]); return 48.0 * d.x + 7.0 * d.y + 6.0 * d.z; }\n"
"bool eq(int a, int b) { return df(a, b) < 1.5; }\n"
"ivec2 AX, AY;\n"
"int at(int a, int b) { ivec2 d = a * AX + b * AY; return (d.x + 2) + (d.y + 2) * 5; }\n"
"void main() {\n"
" vec2 t = v_uv * u_src_size;\n"
" ivec2 base = ivec2(floor(t));\n"
" vec2 q = fract(t) - 0.5;\n"
" vec2 pw = fwidth(t);\n"
" float w = max(pw.x, pw.y) * 0.75 + 0.02;\n"
" ivec2 mx = ivec2(u_src_size) - 1;\n"
" for (int j = -2; j <= 2; j++)\n"
" for (int i = -2; i <= 2; i++) {\n"
" int k = (i + 2) + (j + 2) * 5;\n"
" P[k] = texelFetch(u_src, clamp(base + ivec2(i, j), ivec2(0), mx), 0);\n"
" Y[k] = yuv(P[k].rgb);\n"
" }\n"
" vec4 res = P[12];\n"
" for (int r = 0; r < 4; r++) {\n"
" if (r == 0) { AX = ivec2(1, 0); AY = ivec2(0, 1); }\n"
" else if (r == 1) { AX = ivec2(0, 1); AY = ivec2(-1, 0); }\n"
" else if (r == 2) { AX = ivec2(-1, 0); AY = ivec2(0, -1); }\n"
" else { AX = ivec2(0, -1); AY = ivec2(1, 0); }\n"
" vec2 fp = vec2(dot(q, vec2(AX)), dot(q, vec2(AY))) + 0.5;\n" /* repère du coin bas-droit */
" int E = 12, F = at(1, 0), H = at(0, 1), I = at(1, 1), B = at(0, -1), D = at(-1, 0);\n"
" int C = at(1, -1), G = at(-1, 1), F4 = at(2, 0), I4 = at(2, 1), H5 = at(0, 2), I5 = at(1, 2);\n"
" bool lv1 = !eq(E, F) && !eq(E, H) &&\n"
" ((!eq(F, B) && !eq(H, D)) || (eq(E, I) && !eq(F, I4) && !eq(H, I5)) || eq(E, G) || eq(E, C));\n"
" float wd1 = df(E, C) + df(E, G) + df(I, F4) + df(I, H5) + 4.0 * df(H, F);\n"
" float wd2 = df(H, D) + df(H, I5) + df(F, I4) + df(F, B) + 4.0 * df(E, I);\n"
" bool edr = wd1 < wd2 && lv1;\n"
" if (!edr) continue;\n"
" bool left = 2.0 * df(F, G) <= df(H, C) && !eq(E, G) && !eq(D, G);\n"
" bool up = df(F, G) >= 2.0 * df(H, C) && !eq(E, C) && !eq(B, C);\n"
" float a = smoothstep(1.5 - w, 1.5 + w, fp.x + fp.y);\n"
" if (left) a = max(a, smoothstep(1.0 - w, 1.0 + w, 0.5 * fp.x + fp.y));\n"
" if (up) a = max(a, smoothstep(1.0 - w, 1.0 + w, fp.x + 0.5 * fp.y));\n"
" vec4 px = df(E, F) <= df(E, H) ? P[F] : P[H];\n"
" res = mix(res, px, a);\n"
" }\n"
" o_color = res;\n"
"}\n";
/* Écran cathodique : calcul en lumière linéaire, deux lignes source voisines dont le faisceau (gaussien)
* s'élargit avec la luminosité, filtrage horizontal doux, masque à grille si la résolution le permet. */
static const char *FS_CRT = FS_HEAD
"vec3 lin(vec3 c) { return c * c * (c * 0.3 + 0.7); }\n" /* ~gamma 2.4, rapide */
"vec3 row(float y, float x) {\n"
" float xc = floor(x - 0.5), f = x - 0.5 - xc;\n"
" vec3 c = vec3(0.0); float ws = 0.0;\n"
" for (int i = -1; i <= 2; i++) {\n"
" float d = float(i) - f, wt = exp(-2.5 * d * d);\n"
" ivec2 p = ivec2(clamp(xc + float(i), 0.0, u_src_size.x - 1.0), clamp(y, 0.0, u_src_size.y - 1.0));\n"
" c += lin(texelFetch(u_src, p, 0).rgb) * wt; ws += wt;\n"
" }\n"
" return c / ws;\n"
"}\n"
"vec3 beam(float d, vec3 c) {\n"
" vec3 s = mix(vec3(0.27), vec3(0.42), clamp(c * 1.4, 0.0, 1.0));\n"
" vec3 z = d / s;\n"
" return exp(-0.5 * z * z);\n"
"}\n"
"void main() {\n"
" vec2 c = v_uv * 2.0 - 1.0;\n"
" c *= vec2(1.0 + c.y * c.y * 0.025, 1.0 + c.x * c.x * 0.035);\n"
" vec2 uv = c * 0.5 + 0.5;\n"
" if (uv.x < 0.0 || uv.y < 0.0 || uv.x > 1.0 || uv.y > 1.0) { o_color = vec4(0.0, 0.0, 0.0, 1.0); return; }\n"
" vec2 t = uv * u_src_size;\n"
" float y = t.y - 0.5, y0 = floor(y), dy = y - y0;\n"
" vec3 c0 = row(y0, t.x), c1 = row(y0 + 1.0, t.x);\n"
" vec3 col = c0 * beam(dy, c0) + c1 * beam(1.0 - dy, c1);\n"
" if (u_out_size.x / u_src_size.x >= 2.5) {\n"
" int m = int(mod(gl_FragCoord.x, 3.0));\n"
" vec3 mk = vec3(0.78);\n"
" mk[m] = 1.22;\n"
" col *= mk;\n"
" }\n"
" col *= 1.35;\n"
" vec2 e = smoothstep(vec2(0.0), vec2(0.012), uv) * smoothstep(vec2(0.0), vec2(0.012), 1.0 - uv);\n"
" col *= e.x * e.y;\n"
" o_color = vec4(sqrt(clamp(col, 0.0, 1.0)), 1.0);\n"
"}\n";
/* ------------------------------------------------------------------ état */
enum { PG_NEAREST, PG_SMOOTH, PG_XBR, PG_CRT, PG_OVERLAY, PG_SOLID, PG_HD, PG_SPR, PG_COUNT };
typedef struct { GLuint id; GLint src, src_size, out_size, alpha, color, flip; } Prog;
static float pass_flip = 1.f; /* -1 : la cible est une texture intermédiaire (lue ensuite à l'endroit) */
static Prog prog[PG_COUNT];
static GLuint vao, tex_game, tex_ovl, tex_xbr, fbo_xbr;
static int game_w = 320, game_h = 200;
#define XBR_SCALE 4
static GLuint tex_hd[GFX_HD_PARTS];
static int hd_w[GFX_HD_PARTS];
static struct { int active, parts, offset; float tint[3]; } hd;
/* sprites HD */
static GLuint spr_tex[GFX_SPR_TEX], tex_mask;
static int has_dxt; /* GL_EXT_texture_compression_s3tc */
static GfxSprite spr_list[GFX_SPR_MAX];
static int spr_n;
static GLint spr_u_rect, spr_u_uv, spr_u_mrect, spr_u_mask;
static GLuint compile(GLenum type, const char *body) {
const char *src[2] = {"#version 330 core\nprecision highp float;\n", body};
GLuint s = p_glCreateShader(type);
GLint ok = 0;
p_glShaderSource(s, 2, src, NULL);
p_glCompileShader(s);
p_glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
if (!ok) {
char log[2048];
p_glGetShaderInfoLog(s, sizeof log, NULL, log);
fprintf(stderr, "[gfx] shader : %s\n", log);
p_glDeleteShader(s);
return 0;
}
return s;
}
static int make_prog(Prog *p, const char *vs, const char *fs) {
GLuint v = compile(GL_VERTEX_SHADER, vs), f = compile(GL_FRAGMENT_SHADER, fs);
GLint ok = 0;
if (!v || !f) return 0;
p->id = p_glCreateProgram();
p_glAttachShader(p->id, v);
p_glAttachShader(p->id, f);
p_glLinkProgram(p->id);
p_glDeleteShader(v);
p_glDeleteShader(f);
p_glGetProgramiv(p->id, GL_LINK_STATUS, &ok);
if (!ok) {
char log[2048];
p_glGetProgramInfoLog(p->id, sizeof log, NULL, log);
fprintf(stderr, "[gfx] programme : %s\n", log);
return 0;
}
p->src = p_glGetUniformLocation(p->id, "u_src");
p->src_size = p_glGetUniformLocation(p->id, "u_src_size");
p->out_size = p_glGetUniformLocation(p->id, "u_out_size");
p->alpha = p_glGetUniformLocation(p->id, "u_alpha");
p->color = p_glGetUniformLocation(p->id, "u_color");
p->flip = p_glGetUniformLocation(p->id, "u_flip");
return 1;
}
static GLuint make_tex(int w, int h, GLint filter) {
GLuint t;
glGenTextures(1, &t);
glBindTexture(GL_TEXTURE_2D, t);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
return t;
}
int gfx_init(void) {
const char *const fs[PG_COUNT] = {FS_NEAREST, FS_SMOOTH, FS_XBR, FS_CRT, FS_OVERLAY, FS_SOLID, FS_HD, FS_SPR};
int i;
#define X(type, name) \
if (!(p_##name = (type)SDL_GL_GetProcAddress(#name))) { fprintf(stderr, "[gfx] fonction GL absente : %s\n", #name); return 0; }
GL_FUNCS
#undef X
for (i = 0; i < PG_COUNT; i++)
if (!make_prog(&prog[i], i == PG_SPR ? VS_SPR : VS, fs[i])) { fprintf(stderr, "[gfx] shader %d invalide\n", i); return 0; }
spr_u_rect = p_glGetUniformLocation(prog[PG_SPR].id, "u_rect");
spr_u_uv = p_glGetUniformLocation(prog[PG_SPR].id, "u_uv");
spr_u_mrect = p_glGetUniformLocation(prog[PG_SPR].id, "u_mrect");
spr_u_mask = p_glGetUniformLocation(prog[PG_SPR].id, "u_mask");
p_glGenVertexArrays(1, &vao);
tex_game = make_tex(game_w, game_h, GL_LINEAR);
tex_ovl = make_tex(MENU_W, MENU_H, GL_LINEAR);
tex_xbr = make_tex(game_w * XBR_SCALE, game_h * XBR_SCALE, GL_LINEAR);
glGenTextures(1, &tex_mask);
glBindTexture(GL_TEXTURE_2D, tex_mask);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, GFX_MASK_W, GFX_MASK_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
p_glGenFramebuffers(1, &fbo_xbr);
p_glBindFramebuffer(GL_FRAMEBUFFER, fbo_xbr);
p_glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_xbr, 0);
if (p_glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
fprintf(stderr, "[gfx] tampon intermédiaire incomplet\n");
return 0;
}
p_glBindFramebuffer(GL_FRAMEBUFFER, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
if (rt_cfg.verbose) fprintf(stderr, "[gfx] OpenGL %s, %s\n", glGetString(GL_VERSION), glGetString(GL_RENDERER));
has_dxt = SDL_GL_ExtensionSupported("GL_EXT_texture_compression_s3tc");
return 1;
}
void gfx_shutdown(void) {
int i;
if (!p_glDeleteProgram) return;
for (i = 0; i < PG_COUNT; i++) if (prog[i].id) p_glDeleteProgram(prog[i].id);
if (fbo_xbr) p_glDeleteFramebuffers(1, &fbo_xbr);
if (tex_game) glDeleteTextures(1, &tex_game);
if (tex_ovl) glDeleteTextures(1, &tex_ovl);
if (tex_xbr) glDeleteTextures(1, &tex_xbr);
for (i = 0; i < GFX_HD_PARTS; i++) if (tex_hd[i]) glDeleteTextures(1, &tex_hd[i]);
for (i = 0; i < GFX_SPR_TEX; i++) if (spr_tex[i]) glDeleteTextures(1, &spr_tex[i]);
if (tex_mask) glDeleteTextures(1, &tex_mask);
if (vao) p_glDeleteVertexArrays(1, &vao);
}
void gfx_set_game(const uint32_t *argb, int w, int h) {
glBindTexture(GL_TEXTURE_2D, tex_game);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_BYTE, argb);
}
void gfx_set_overlay(const uint32_t *argb, int w, int h) {
glBindTexture(GL_TEXTURE_2D, tex_ovl);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_BYTE, argb);
}
int gfx_hd_load(int part, const uint32_t *argb, int w, int h) {
GLint max = 0;
if (part < 0 || part >= GFX_HD_PARTS) return 0;
hd_w[part] = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
if (w > max || h > max) { fprintf(stderr, "[gfx] image HD %dx%d trop grande (max %d)\n", w, h, max); return 0; }
if (!tex_hd[part]) glGenTextures(1, &tex_hd[part]);
glBindTexture(GL_TEXTURE_2D, tex_hd[part]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, argb);
p_glGenerateMipmap(GL_TEXTURE_2D);
hd_w[part] = w;
return 1;
}
void gfx_hd_frame(int active, int parts, int offset, const float tint[3]) {
int i;
hd.active = active && parts > 0 && parts <= GFX_HD_PARTS;
for (i = 0; hd.active && i < parts; i++) if (!hd_w[i]) hd.active = 0;
hd.parts = parts;
hd.offset = offset;
for (i = 0; i < 3; i++) hd.tint[i] = tint ? tint[i] : 1.f;
}
static void draw_hd(GfxRect g, int out_h) {
static const char *names[GFX_HD_PARTS] = {"u_hd0", "u_hd1", "u_hd2", "u_hd3"};
const Prog *p = &prog[PG_HD];
int i;
glViewport(g.x, out_h - g.y - g.h, g.w, g.h);
p_glUseProgram(p->id);
for (i = 0; i < GFX_HD_PARTS; i++) {
p_glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, tex_hd[i < hd.parts ? i : 0]);
p_glUniform1i(p_glGetUniformLocation(p->id, names[i]), i);
}
p_glActiveTexture(GL_TEXTURE0);
p_glUniform1f(p_glGetUniformLocation(p->id, "u_offset"), (float)hd.offset);
p_glUniform1i(p_glGetUniformLocation(p->id, "u_parts"), hd.parts);
p_glUniform4f(p->color, hd.tint[0], hd.tint[1], hd.tint[2], 1.f);
if (p->flip >= 0) p_glUniform1f(p->flip, 1.f);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
int gfx_spr_tex_load(const uint32_t *argb, int w, int h) {
GLint max = 0;
int i;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
if (w > max || h > max) { fprintf(stderr, "[gfx] image de sprites %dx%d trop grande (max %d)\n", w, h, max); return 0; }
for (i = 0; i < GFX_SPR_TEX && spr_tex[i]; i++) {}
if (i == GFX_SPR_TEX) return 0;
glGenTextures(1, &spr_tex[i]);
glBindTexture(GL_TEXTURE_2D, spr_tex[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, argb);
p_glGenerateMipmap(GL_TEXTURE_2D);
return i + 1;
}
int gfx_spr_dxt_supported(void) { return has_dxt; }
int gfx_spr_tex_load_dxt5(const uint8_t *data, int w, int h, int levels) {
GLint max = 0;
int i, l;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
if (!has_dxt || w > max || h > max) return 0;
for (i = 0; i < GFX_SPR_TEX && spr_tex[i]; i++) {}
if (i == GFX_SPR_TEX) return 0;
glGenTextures(1, &spr_tex[i]);
glBindTexture(GL_TEXTURE_2D, spr_tex[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, levels > 1 ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, levels - 1);
for (l = 0; l < levels; l++) {
GLsizei n = (GLsizei)(((w + 3) / 4) * ((h + 3) / 4) * 16);
p_glCompressedTexImage2D(GL_TEXTURE_2D, l, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, n, data);
data += n;
w = w > 1 ? w / 2 : 1; h = h > 1 ? h / 2 : 1;
}
return i + 1;
}
void gfx_spr_tex_free(int tex) {
if (tex < 1 || tex > GFX_SPR_TEX || !spr_tex[tex - 1]) return;
glDeleteTextures(1, &spr_tex[tex - 1]);
spr_tex[tex - 1] = 0;
}
void gfx_spr_frame(const GfxSprite *list, int n, const uint8_t *atlas, int mask_rows) {
spr_n = n < 0 ? 0 : n > GFX_SPR_MAX ? GFX_SPR_MAX : n;
if (spr_n) memcpy(spr_list, list, (size_t)spr_n * sizeof *list);
if (spr_n && mask_rows > 0) {
glBindTexture(GL_TEXTURE_2D, tex_mask);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GFX_MASK_W, mask_rows > GFX_MASK_H ? GFX_MASK_H : mask_rows,
GL_RGBA, GL_UNSIGNED_BYTE, atlas);
}
}
static void draw_sprites(GfxRect g, int out_h) {
const Prog *p = &prog[PG_SPR];
int i;
glViewport(g.x, out_h - g.y - g.h, g.w, g.h);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
p_glUseProgram(p->id);
p_glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex_mask);
p_glUniform1i(spr_u_mask, 1);
p_glActiveTexture(GL_TEXTURE0);
p_glUniform1i(p->src, 0);
for (i = 0; i < spr_n; i++) {
const GfxSprite *s = &spr_list[i];
if (s->tex < 1 || s->tex > GFX_SPR_TEX || !spr_tex[s->tex - 1]) continue;
glBindTexture(GL_TEXTURE_2D, spr_tex[s->tex - 1]);
p_glUniform4f(spr_u_rect, s->x, s->y, s->w, s->h);
p_glUniform4f(spr_u_uv, s->u0, s->v0, s->u1, s->v1);
p_glUniform4f(spr_u_mrect, (float)s->mx, (float)s->my, s->w, s->h);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
}
GfxRect gfx_fit(int out_w, int out_h, int aw, int ah) {
GfxRect r;
if ((long long)out_w * ah > (long long)out_h * aw) { r.h = out_h; r.w = (int)((long long)out_h * aw / ah); }
else { r.w = out_w; r.h = (int)((long long)out_w * ah / aw); }
r.x = (out_w - r.w) / 2;
r.y = (out_h - r.h) / 2;
return r;
}
/* une passe : texture tex (sw × sh) → rectangle r de la cible courante (hauteur totale out_h) */
static void pass(const Prog *p, GLuint tex, int sw, int sh, GfxRect r, int out_h) {
glViewport(r.x, out_h - r.y - r.h, r.w, r.h);
p_glUseProgram(p->id);
p_glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
if (p->src >= 0) p_glUniform1i(p->src, 0);
if (p->src_size >= 0) p_glUniform2f(p->src_size, (float)sw, (float)sh);
if (p->out_size >= 0) p_glUniform2f(p->out_size, (float)r.w, (float)r.h);
if (p->flip >= 0) p_glUniform1f(p->flip, pass_flip);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void gfx_draw(int out_w, int out_h, int filter, int game_aspect_h, float veil, float overlay_alpha) {
GfxRect g = gfx_fit(out_w, out_h, 320, game_aspect_h);
p_glBindVertexArray(vao);
glDisable(GL_BLEND);
p_glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, out_w, out_h);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
if ((hd.active || spr_n) && filter != FILTER_CRT) {
if (hd.active) draw_hd(g, out_h);
if (spr_n) draw_sprites(g, out_h);
glEnable(GL_BLEND); /* image du jeu par-dessus, transparente là où le décor ou un sprite HD la remplace */
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
switch (filter) {
case FILTER_SMOOTH: pass(&prog[PG_SMOOTH], tex_game, game_w, game_h, g, out_h); break;
case FILTER_CRT: pass(&prog[PG_CRT], tex_game, game_w, game_h, g, out_h); break;
case FILTER_XBR: {
GfxRect f = {0, 0, game_w * XBR_SCALE, game_h * XBR_SCALE};
GLboolean blend = glIsEnabled(GL_BLEND);
glDisable(GL_BLEND); /* l'alpha est recopié tel quel dans la texture intermédiaire */
p_glBindFramebuffer(GL_FRAMEBUFFER, fbo_xbr);
pass_flip = -1.f;
pass(&prog[PG_XBR], tex_game, game_w, game_h, f, f.h);
pass_flip = 1.f;
p_glBindFramebuffer(GL_FRAMEBUFFER, 0);
if (blend) glEnable(GL_BLEND);
pass(&prog[PG_SMOOTH], tex_xbr, f.w, f.h, g, out_h);
break;
}
default: pass(&prog[PG_NEAREST], tex_game, game_w, game_h, g, out_h); break;
}
glDisable(GL_BLEND);
if (veil > 0 || overlay_alpha > 0) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
if (veil > 0) {
GfxRect all = {0, 0, out_w, out_h};
p_glUseProgram(prog[PG_SOLID].id);
p_glUniform4f(prog[PG_SOLID].color, 6 / 255.f, 3 / 255.f, 18 / 255.f, veil);
glViewport(0, 0, all.w, all.h);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
if (overlay_alpha > 0) {
p_glUseProgram(prog[PG_OVERLAY].id);
p_glUniform1f(prog[PG_OVERLAY].alpha, overlay_alpha);
pass(&prog[PG_OVERLAY], tex_ovl, MENU_W, MENU_H, gfx_fit(out_w, out_h, 4, 3), out_h);
}
}