Files
DoubleTroubleRecomp/runtime/gfx_gl.c
T
linkatplugandClaude Opus 5.5 37acf64362 Mods (remplacement de fichiers) et pack HD des décors
- mods/<nom>/files/ : remplace tout fichier lu par le jeu, sans toucher aux originaux ;
  ordre alphabétique des mods, le dernier l'emporte ; dossiers _x et .x ignorés.
- mods/<nom>/hd/<PCX>.png : décors HD. Le moteur décode le PCX d'origine, le retrouve
  dans l'écran 320×200 (décalage de défilement compris, pièces LOCxxA/B/C/D) et ne
  laisse voir l'image HD que là où le décor d'origine est visible ; personnages,
  objets et panneau restent ceux du jeu. Les fondus de palette suivent.
- Décodeur PNG intégré (inflate inclus), vérifié contre zlib sur tous les formats.
- Rendu : couche HD avec mipmaps sous l'image du jeu (alpha = masque) pour les
  filtres net, lissé et xBR ; le CRT garde l'image d'origine.
- Réglages : [display] hd_pack, [mods] enabled/dir/disabled (valeurs texte, clés
  reconnues par section) ; menu F12 « Décors HD (mods) ».
- Options : --mods DOSSIER, --sans-mods, --sans-hd (session seulement).
- tools/hd_export.py --upscayl : pack HD complet avec Upscayl (Real-ESRGAN).
- docs/MODS.md, docs/SETTINGS.md ; mods/ exclu du dépôt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 03:30:28 +01:00

429 lines
20 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 l'image du jeu par-dessus, dont le canal alpha
* (masque de hd.c) vaut 0 là où le décor 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)
#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";
#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";
/* 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_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;
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 *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};
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], fs[i])) { fprintf(stderr, "[gfx] shader %d invalide\n", i); return 0; }
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);
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));
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]);
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);
}
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 && filter != FILTER_CRT) {
draw_hd(g, out_h);
glEnable(GL_BLEND); /* image du jeu par-dessus, transparente là où le décor 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);
}
}