diff --git a/CMakeLists.txt b/CMakeLists.txt index 7227afa..67681ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,9 +73,16 @@ set(APP_SOURCES source/sprite/surface_animated_sprite.cpp source/sprite/surface_moving_sprite.cpp source/sprite/surface_sprite.cpp +) +# Fuentes de librerías de terceros +set(EXTERNAL_SOURCES source/external/jail_audio.cpp - source/external/jail_shader.cpp +) + +# Fuentes del sistema de renderizado +set(RENDERING_SOURCES + source/rendering/opengl/opengl_shader.cpp ) # Configuración de SDL3 @@ -83,7 +90,7 @@ find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}") # --- 2. AÑADIR EJECUTABLE --- -add_executable(${PROJECT_NAME} ${APP_SOURCES}) +add_executable(${PROJECT_NAME} ${APP_SOURCES} ${EXTERNAL_SOURCES} ${RENDERING_SOURCES}) # --- 3. DIRECTORIOS DE INCLUSIÓN --- target_include_directories(${PROJECT_NAME} PUBLIC diff --git a/data/shaders/crtpi_192.glsl b/data/shaders/crtpi_192.glsl deleted file mode 100644 index 3f128f7..0000000 --- a/data/shaders/crtpi_192.glsl +++ /dev/null @@ -1,234 +0,0 @@ -/* - crt-pi - A Raspberry Pi friendly CRT shader. - - Copyright (C) 2015-2016 davej - - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the Free - Software Foundation; either version 2 of the License, or (at your option) - any later version. - - -Notes: - -This shader is designed to work well on Raspberry Pi GPUs (i.e. 1080P @ 60Hz on a game with a 4:3 aspect ratio). It pushes the Pi's GPU hard and enabling some features will slow it down so that it is no longer able to match 1080P @ 60Hz. You will need to overclock your Pi to the fastest setting in raspi-config to get the best results from this shader: 'Pi2' for Pi2 and 'Turbo' for original Pi and Pi Zero. Note: Pi2s are slower at running the shader than other Pis, this seems to be down to Pi2s lower maximum memory speed. Pi2s don't quite manage 1080P @ 60Hz - they drop about 1 in 1000 frames. You probably won't notice this, but if you do, try enabling FAKE_GAMMA. - -SCANLINES enables scanlines. You'll almost certainly want to use it with MULTISAMPLE to reduce moire effects. SCANLINE_WEIGHT defines how wide scanlines are (it is an inverse value so a higher number = thinner lines). SCANLINE_GAP_BRIGHTNESS defines how dark the gaps between the scan lines are. Darker gaps between scan lines make moire effects more likely. - -GAMMA enables gamma correction using the values in INPUT_GAMMA and OUTPUT_GAMMA. FAKE_GAMMA causes it to ignore the values in INPUT_GAMMA and OUTPUT_GAMMA and approximate gamma correction in a way which is faster than true gamma whilst still looking better than having none. You must have GAMMA defined to enable FAKE_GAMMA. - -CURVATURE distorts the screen by CURVATURE_X and CURVATURE_Y. Curvature slows things down a lot. - -By default the shader uses linear blending horizontally. If you find this too blury, enable SHARPER. - -BLOOM_FACTOR controls the increase in width for bright scanlines. - -MASK_TYPE defines what, if any, shadow mask to use. MASK_BRIGHTNESS defines how much the mask type darkens the screen. - -*/ - -#pragma parameter CURVATURE_X "Screen curvature - horizontal" 0.10 0.0 1.0 0.01 -#pragma parameter CURVATURE_Y "Screen curvature - vertical" 0.15 0.0 1.0 0.01 -#pragma parameter MASK_BRIGHTNESS "Mask brightness" 0.70 0.0 1.0 0.01 -#pragma parameter SCANLINE_WEIGHT "Scanline weight" 6.0 0.0 15.0 0.1 -#pragma parameter SCANLINE_GAP_BRIGHTNESS "Scanline gap brightness" 0.12 0.0 1.0 0.01 -#pragma parameter BLOOM_FACTOR "Bloom factor" 1.5 0.0 5.0 0.01 -#pragma parameter INPUT_GAMMA "Input gamma" 2.4 0.0 5.0 0.01 -#pragma parameter OUTPUT_GAMMA "Output gamma" 2.2 0.0 5.0 0.01 - -// Haven't put these as parameters as it would slow the code down. -#define SCANLINES -#define MULTISAMPLE -#define GAMMA -//#define FAKE_GAMMA -//#define CURVATURE -//#define SHARPER -// MASK_TYPE: 0 = none, 1 = green/magenta, 2 = trinitron(ish) -#define MASK_TYPE 2 - - -#ifdef GL_ES -#define COMPAT_PRECISION mediump -precision mediump float; -#else -#define COMPAT_PRECISION -#endif - -#ifdef PARAMETER_UNIFORM -uniform COMPAT_PRECISION float CURVATURE_X; -uniform COMPAT_PRECISION float CURVATURE_Y; -uniform COMPAT_PRECISION float MASK_BRIGHTNESS; -uniform COMPAT_PRECISION float SCANLINE_WEIGHT; -uniform COMPAT_PRECISION float SCANLINE_GAP_BRIGHTNESS; -uniform COMPAT_PRECISION float BLOOM_FACTOR; -uniform COMPAT_PRECISION float INPUT_GAMMA; -uniform COMPAT_PRECISION float OUTPUT_GAMMA; -#else -#define CURVATURE_X 0.05 -#define CURVATURE_Y 0.1 -#define MASK_BRIGHTNESS 0.80 -#define SCANLINE_WEIGHT 6.0 -#define SCANLINE_GAP_BRIGHTNESS 0.12 -#define BLOOM_FACTOR 3.5 -#define INPUT_GAMMA 2.4 -#define OUTPUT_GAMMA 2.2 -#endif - -/* COMPATIBILITY - - GLSL compilers -*/ - -//uniform vec2 TextureSize; -#if defined(CURVATURE) -varying vec2 screenScale; -#endif -varying vec2 TEX0; -varying float filterWidth; - -#if defined(VERTEX) -//uniform mat4 MVPMatrix; -//attribute vec4 VertexCoord; -//attribute vec2 TexCoord; -//uniform vec2 InputSize; -//uniform vec2 OutputSize; - -void main() -{ -#if defined(CURVATURE) - screenScale = vec2(1.0, 1.0); //TextureSize / InputSize; -#endif - filterWidth = (768.0 / 192.0) / 3.0; - TEX0 = vec2(gl_MultiTexCoord0.x, 1.0-gl_MultiTexCoord0.y)*1.0001; - gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; -} -#elif defined(FRAGMENT) - -uniform sampler2D Texture; - -#if defined(CURVATURE) -vec2 Distort(vec2 coord) -{ - vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y); - // Barrel distortion shrinks the display area a bit, this will allow us to counteract that. - vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION); - coord *= screenScale; - coord -= vec2(0.5); - float rsq = coord.x * coord.x + coord.y * coord.y; - coord += coord * (CURVATURE_DISTORTION * rsq); - coord *= barrelScale; - if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5) - coord = vec2(-1.0); // If out of bounds, return an invalid value. - else - { - coord += vec2(0.5); - coord /= screenScale; - } - - return coord; -} -#endif - -float CalcScanLineWeight(float dist) -{ - return max(1.0-dist*dist*SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS); -} - -float CalcScanLine(float dy) -{ - float scanLineWeight = CalcScanLineWeight(dy); -#if defined(MULTISAMPLE) - scanLineWeight += CalcScanLineWeight(dy-filterWidth); - scanLineWeight += CalcScanLineWeight(dy+filterWidth); - scanLineWeight *= 0.3333333; -#endif - return scanLineWeight; -} - -void main() -{ - vec2 TextureSize = vec2(256.0, 192.0); -#if defined(CURVATURE) - vec2 texcoord = Distort(TEX0); - if (texcoord.x < 0.0) - gl_FragColor = vec4(0.0); - else -#else - vec2 texcoord = TEX0; -#endif - { - vec2 texcoordInPixels = texcoord * TextureSize; -#if defined(SHARPER) - vec2 tempCoord = floor(texcoordInPixels) + 0.5; - vec2 coord = tempCoord / TextureSize; - vec2 deltas = texcoordInPixels - tempCoord; - float scanLineWeight = CalcScanLine(deltas.y); - vec2 signs = sign(deltas); - deltas.x *= 2.0; - deltas = deltas * deltas; - deltas.y = deltas.y * deltas.y; - deltas.x *= 0.5; - deltas.y *= 8.0; - deltas /= TextureSize; - deltas *= signs; - vec2 tc = coord + deltas; -#else - float tempY = floor(texcoordInPixels.y) + 0.5; - float yCoord = tempY / TextureSize.y; - float dy = texcoordInPixels.y - tempY; - float scanLineWeight = CalcScanLine(dy); - float signY = sign(dy); - dy = dy * dy; - dy = dy * dy; - dy *= 8.0; - dy /= TextureSize.y; - dy *= signY; - vec2 tc = vec2(texcoord.x, yCoord + dy); -#endif - - vec3 colour = texture2D(Texture, tc).rgb; - -#if defined(SCANLINES) -#if defined(GAMMA) -#if defined(FAKE_GAMMA) - colour = colour * colour; -#else - colour = pow(colour, vec3(INPUT_GAMMA)); -#endif -#endif - scanLineWeight *= BLOOM_FACTOR; - colour *= scanLineWeight; - -#if defined(GAMMA) -#if defined(FAKE_GAMMA) - colour = sqrt(colour); -#else - colour = pow(colour, vec3(1.0/OUTPUT_GAMMA)); -#endif -#endif -#endif -#if MASK_TYPE == 0 - gl_FragColor = vec4(colour, 1.0); -#else -#if MASK_TYPE == 1 - float whichMask = fract((gl_FragCoord.x*1.0001) * 0.5); - vec3 mask; - if (whichMask < 0.5) - mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS); - else - mask = vec3(1.0, MASK_BRIGHTNESS, 1.0); -#elif MASK_TYPE == 2 - float whichMask = fract((gl_FragCoord.x*1.0001) * 0.3333333); - vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS); - if (whichMask < 0.3333333) - mask.x = 1.0; - else if (whichMask < 0.6666666) - mask.y = 1.0; - else - mask.z = 1.0; -#endif - - gl_FragColor = vec4(colour * mask, 1.0); -#endif - } -} -#endif diff --git a/data/shaders/crtpi_240.glsl b/data/shaders/crtpi_240.glsl deleted file mode 100644 index 068a6f1..0000000 --- a/data/shaders/crtpi_240.glsl +++ /dev/null @@ -1,234 +0,0 @@ -/* - crt-pi - A Raspberry Pi friendly CRT shader. - - Copyright (C) 2015-2016 davej - - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the Free - Software Foundation; either version 2 of the License, or (at your option) - any later version. - - -Notes: - -This shader is designed to work well on Raspberry Pi GPUs (i.e. 1080P @ 60Hz on a game with a 4:3 aspect ratio). It pushes the Pi's GPU hard and enabling some features will slow it down so that it is no longer able to match 1080P @ 60Hz. You will need to overclock your Pi to the fastest setting in raspi-config to get the best results from this shader: 'Pi2' for Pi2 and 'Turbo' for original Pi and Pi Zero. Note: Pi2s are slower at running the shader than other Pis, this seems to be down to Pi2s lower maximum memory speed. Pi2s don't quite manage 1080P @ 60Hz - they drop about 1 in 1000 frames. You probably won't notice this, but if you do, try enabling FAKE_GAMMA. - -SCANLINES enables scanlines. You'll almost certainly want to use it with MULTISAMPLE to reduce moire effects. SCANLINE_WEIGHT defines how wide scanlines are (it is an inverse value so a higher number = thinner lines). SCANLINE_GAP_BRIGHTNESS defines how dark the gaps between the scan lines are. Darker gaps between scan lines make moire effects more likely. - -GAMMA enables gamma correction using the values in INPUT_GAMMA and OUTPUT_GAMMA. FAKE_GAMMA causes it to ignore the values in INPUT_GAMMA and OUTPUT_GAMMA and approximate gamma correction in a way which is faster than true gamma whilst still looking better than having none. You must have GAMMA defined to enable FAKE_GAMMA. - -CURVATURE distorts the screen by CURVATURE_X and CURVATURE_Y. Curvature slows things down a lot. - -By default the shader uses linear blending horizontally. If you find this too blury, enable SHARPER. - -BLOOM_FACTOR controls the increase in width for bright scanlines. - -MASK_TYPE defines what, if any, shadow mask to use. MASK_BRIGHTNESS defines how much the mask type darkens the screen. - -*/ - -#pragma parameter CURVATURE_X "Screen curvature - horizontal" 0.10 0.0 1.0 0.01 -#pragma parameter CURVATURE_Y "Screen curvature - vertical" 0.15 0.0 1.0 0.01 -#pragma parameter MASK_BRIGHTNESS "Mask brightness" 0.70 0.0 1.0 0.01 -#pragma parameter SCANLINE_WEIGHT "Scanline weight" 6.0 0.0 15.0 0.1 -#pragma parameter SCANLINE_GAP_BRIGHTNESS "Scanline gap brightness" 0.12 0.0 1.0 0.01 -#pragma parameter BLOOM_FACTOR "Bloom factor" 1.5 0.0 5.0 0.01 -#pragma parameter INPUT_GAMMA "Input gamma" 2.4 0.0 5.0 0.01 -#pragma parameter OUTPUT_GAMMA "Output gamma" 2.2 0.0 5.0 0.01 - -// Haven't put these as parameters as it would slow the code down. -#define SCANLINES -#define MULTISAMPLE -#define GAMMA -//#define FAKE_GAMMA -//#define CURVATURE -//#define SHARPER -// MASK_TYPE: 0 = none, 1 = green/magenta, 2 = trinitron(ish) -#define MASK_TYPE 2 - - -#ifdef GL_ES -#define COMPAT_PRECISION mediump -precision mediump float; -#else -#define COMPAT_PRECISION -#endif - -#ifdef PARAMETER_UNIFORM -uniform COMPAT_PRECISION float CURVATURE_X; -uniform COMPAT_PRECISION float CURVATURE_Y; -uniform COMPAT_PRECISION float MASK_BRIGHTNESS; -uniform COMPAT_PRECISION float SCANLINE_WEIGHT; -uniform COMPAT_PRECISION float SCANLINE_GAP_BRIGHTNESS; -uniform COMPAT_PRECISION float BLOOM_FACTOR; -uniform COMPAT_PRECISION float INPUT_GAMMA; -uniform COMPAT_PRECISION float OUTPUT_GAMMA; -#else -#define CURVATURE_X 0.05 -#define CURVATURE_Y 0.1 -#define MASK_BRIGHTNESS 0.80 -#define SCANLINE_WEIGHT 6.0 -#define SCANLINE_GAP_BRIGHTNESS 0.12 -#define BLOOM_FACTOR 3.5 -#define INPUT_GAMMA 2.4 -#define OUTPUT_GAMMA 2.2 -#endif - -/* COMPATIBILITY - - GLSL compilers -*/ - -//uniform vec2 TextureSize; -#if defined(CURVATURE) -varying vec2 screenScale; -#endif -varying vec2 TEX0; -varying float filterWidth; - -#if defined(VERTEX) -//uniform mat4 MVPMatrix; -//attribute vec4 VertexCoord; -//attribute vec2 TexCoord; -//uniform vec2 InputSize; -//uniform vec2 OutputSize; - -void main() -{ -#if defined(CURVATURE) - screenScale = vec2(1.0, 1.0); //TextureSize / InputSize; -#endif - filterWidth = (768.0 / 240.0) / 3.0; - TEX0 = vec2(gl_MultiTexCoord0.x, 1.0-gl_MultiTexCoord0.y)*1.0001; - gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; -} -#elif defined(FRAGMENT) - -uniform sampler2D Texture; - -#if defined(CURVATURE) -vec2 Distort(vec2 coord) -{ - vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y); - // Barrel distortion shrinks the display area a bit, this will allow us to counteract that. - vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION); - coord *= screenScale; - coord -= vec2(0.5); - float rsq = coord.x * coord.x + coord.y * coord.y; - coord += coord * (CURVATURE_DISTORTION * rsq); - coord *= barrelScale; - if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5) - coord = vec2(-1.0); // If out of bounds, return an invalid value. - else - { - coord += vec2(0.5); - coord /= screenScale; - } - - return coord; -} -#endif - -float CalcScanLineWeight(float dist) -{ - return max(1.0-dist*dist*SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS); -} - -float CalcScanLine(float dy) -{ - float scanLineWeight = CalcScanLineWeight(dy); -#if defined(MULTISAMPLE) - scanLineWeight += CalcScanLineWeight(dy-filterWidth); - scanLineWeight += CalcScanLineWeight(dy+filterWidth); - scanLineWeight *= 0.3333333; -#endif - return scanLineWeight; -} - -void main() -{ - vec2 TextureSize = vec2(320.0, 240.0); -#if defined(CURVATURE) - vec2 texcoord = Distort(TEX0); - if (texcoord.x < 0.0) - gl_FragColor = vec4(0.0); - else -#else - vec2 texcoord = TEX0; -#endif - { - vec2 texcoordInPixels = texcoord * TextureSize; -#if defined(SHARPER) - vec2 tempCoord = floor(texcoordInPixels) + 0.5; - vec2 coord = tempCoord / TextureSize; - vec2 deltas = texcoordInPixels - tempCoord; - float scanLineWeight = CalcScanLine(deltas.y); - vec2 signs = sign(deltas); - deltas.x *= 2.0; - deltas = deltas * deltas; - deltas.y = deltas.y * deltas.y; - deltas.x *= 0.5; - deltas.y *= 8.0; - deltas /= TextureSize; - deltas *= signs; - vec2 tc = coord + deltas; -#else - float tempY = floor(texcoordInPixels.y) + 0.5; - float yCoord = tempY / TextureSize.y; - float dy = texcoordInPixels.y - tempY; - float scanLineWeight = CalcScanLine(dy); - float signY = sign(dy); - dy = dy * dy; - dy = dy * dy; - dy *= 8.0; - dy /= TextureSize.y; - dy *= signY; - vec2 tc = vec2(texcoord.x, yCoord + dy); -#endif - - vec3 colour = texture2D(Texture, tc).rgb; - -#if defined(SCANLINES) -#if defined(GAMMA) -#if defined(FAKE_GAMMA) - colour = colour * colour; -#else - colour = pow(colour, vec3(INPUT_GAMMA)); -#endif -#endif - scanLineWeight *= BLOOM_FACTOR; - colour *= scanLineWeight; - -#if defined(GAMMA) -#if defined(FAKE_GAMMA) - colour = sqrt(colour); -#else - colour = pow(colour, vec3(1.0/OUTPUT_GAMMA)); -#endif -#endif -#endif -#if MASK_TYPE == 0 - gl_FragColor = vec4(colour, 1.0); -#else -#if MASK_TYPE == 1 - float whichMask = fract((gl_FragCoord.x*1.0001) * 0.5); - vec3 mask; - if (whichMask < 0.5) - mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS); - else - mask = vec3(1.0, MASK_BRIGHTNESS, 1.0); -#elif MASK_TYPE == 2 - float whichMask = fract((gl_FragCoord.x*1.0001) * 0.3333333); - vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS); - if (whichMask < 0.3333333) - mask.x = 1.0; - else if (whichMask < 0.6666666) - mask.y = 1.0; - else - mask.z = 1.0; -#endif - - gl_FragColor = vec4(colour * mask, 1.0); -#endif - } -} -#endif diff --git a/data/shaders/crtpi_fragment.glsl b/data/shaders/crtpi_fragment.glsl new file mode 100644 index 0000000..cd63e80 --- /dev/null +++ b/data/shaders/crtpi_fragment.glsl @@ -0,0 +1,157 @@ +#version 330 core + +// Configuración +#define SCANLINES +#define MULTISAMPLE +#define GAMMA +//#define FAKE_GAMMA +//#define CURVATURE +//#define SHARPER +#define MASK_TYPE 2 + +#define CURVATURE_X 0.05 +#define CURVATURE_Y 0.1 +#define MASK_BRIGHTNESS 0.80 +#define SCANLINE_WEIGHT 6.0 +#define SCANLINE_GAP_BRIGHTNESS 0.12 +#define BLOOM_FACTOR 3.5 +#define INPUT_GAMMA 2.4 +#define OUTPUT_GAMMA 2.2 + +// Inputs desde vertex shader +in vec2 vTexCoord; +in float vFilterWidth; +#if defined(CURVATURE) +in vec2 vScreenScale; +#endif + +// Output +out vec4 FragColor; + +// Uniforms +uniform sampler2D Texture; +uniform vec2 TextureSize; + +#if defined(CURVATURE) +vec2 Distort(vec2 coord) +{ + vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y); + vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION); + coord *= vScreenScale; + coord -= vec2(0.5); + float rsq = coord.x * coord.x + coord.y * coord.y; + coord += coord * (CURVATURE_DISTORTION * rsq); + coord *= barrelScale; + if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5) + coord = vec2(-1.0); + else + { + coord += vec2(0.5); + coord /= vScreenScale; + } + return coord; +} +#endif + +float CalcScanLineWeight(float dist) +{ + return max(1.0 - dist * dist * SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS); +} + +float CalcScanLine(float dy) +{ + float scanLineWeight = CalcScanLineWeight(dy); +#if defined(MULTISAMPLE) + scanLineWeight += CalcScanLineWeight(dy - vFilterWidth); + scanLineWeight += CalcScanLineWeight(dy + vFilterWidth); + scanLineWeight *= 0.3333333; +#endif + return scanLineWeight; +} + +void main() +{ +#if defined(CURVATURE) + vec2 texcoord = Distort(vTexCoord); + if (texcoord.x < 0.0) { + FragColor = vec4(0.0); + return; + } +#else + vec2 texcoord = vTexCoord; +#endif + + vec2 texcoordInPixels = texcoord * TextureSize; + +#if defined(SHARPER) + vec2 tempCoord = floor(texcoordInPixels) + 0.5; + vec2 coord = tempCoord / TextureSize; + vec2 deltas = texcoordInPixels - tempCoord; + float scanLineWeight = CalcScanLine(deltas.y); + vec2 signs = sign(deltas); + deltas.x *= 2.0; + deltas = deltas * deltas; + deltas.y = deltas.y * deltas.y; + deltas.x *= 0.5; + deltas.y *= 8.0; + deltas /= TextureSize; + deltas *= signs; + vec2 tc = coord + deltas; +#else + float tempY = floor(texcoordInPixels.y) + 0.5; + float yCoord = tempY / TextureSize.y; + float dy = texcoordInPixels.y - tempY; + float scanLineWeight = CalcScanLine(dy); + float signY = sign(dy); + dy = dy * dy; + dy = dy * dy; + dy *= 8.0; + dy /= TextureSize.y; + dy *= signY; + vec2 tc = vec2(texcoord.x, yCoord + dy); +#endif + + vec3 colour = texture(Texture, tc).rgb; + +#if defined(SCANLINES) +#if defined(GAMMA) +#if defined(FAKE_GAMMA) + colour = colour * colour; +#else + colour = pow(colour, vec3(INPUT_GAMMA)); +#endif +#endif + scanLineWeight *= BLOOM_FACTOR; + colour *= scanLineWeight; + +#if defined(GAMMA) +#if defined(FAKE_GAMMA) + colour = sqrt(colour); +#else + colour = pow(colour, vec3(1.0 / OUTPUT_GAMMA)); +#endif +#endif +#endif + +#if MASK_TYPE == 0 + FragColor = vec4(colour, 1.0); +#elif MASK_TYPE == 1 + float whichMask = fract(gl_FragCoord.x * 0.5); + vec3 mask; + if (whichMask < 0.5) + mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS); + else + mask = vec3(1.0, MASK_BRIGHTNESS, 1.0); + FragColor = vec4(colour * mask, 1.0); +#elif MASK_TYPE == 2 + float whichMask = fract(gl_FragCoord.x * 0.3333333); + vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS); + if (whichMask < 0.3333333) + mask.x = 1.0; + else if (whichMask < 0.6666666) + mask.y = 1.0; + else + mask.z = 1.0; + FragColor = vec4(colour * mask, 1.0); +#endif +} diff --git a/data/shaders/crtpi_fragment_es.glsl b/data/shaders/crtpi_fragment_es.glsl new file mode 100644 index 0000000..363b6a9 --- /dev/null +++ b/data/shaders/crtpi_fragment_es.glsl @@ -0,0 +1,160 @@ +#version 300 es + +// OpenGL ES 3.0 - Compatible con Raspberry Pi 5 +precision highp float; + +// Configuración +#define SCANLINES +#define MULTISAMPLE +#define GAMMA +//#define FAKE_GAMMA +//#define CURVATURE +//#define SHARPER +#define MASK_TYPE 2 + +#define CURVATURE_X 0.05 +#define CURVATURE_Y 0.1 +#define MASK_BRIGHTNESS 0.80 +#define SCANLINE_WEIGHT 6.0 +#define SCANLINE_GAP_BRIGHTNESS 0.12 +#define BLOOM_FACTOR 3.5 +#define INPUT_GAMMA 2.4 +#define OUTPUT_GAMMA 2.2 + +// Inputs desde vertex shader +in vec2 vTexCoord; +in float vFilterWidth; +#if defined(CURVATURE) +in vec2 vScreenScale; +#endif + +// Output +out vec4 FragColor; + +// Uniforms +uniform sampler2D Texture; +uniform vec2 TextureSize; + +#if defined(CURVATURE) +vec2 Distort(vec2 coord) +{ + vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y); + vec2 barrelScale = vec2(1.0) - (0.23 * CURVATURE_DISTORTION); + coord *= vScreenScale; + coord -= vec2(0.5); + float rsq = coord.x * coord.x + coord.y * coord.y; + coord += coord * (CURVATURE_DISTORTION * rsq); + coord *= barrelScale; + if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5) + coord = vec2(-1.0); + else + { + coord += vec2(0.5); + coord /= vScreenScale; + } + return coord; +} +#endif + +float CalcScanLineWeight(float dist) +{ + return max(1.0 - dist * dist * SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS); +} + +float CalcScanLine(float dy) +{ + float scanLineWeight = CalcScanLineWeight(dy); +#if defined(MULTISAMPLE) + scanLineWeight += CalcScanLineWeight(dy - vFilterWidth); + scanLineWeight += CalcScanLineWeight(dy + vFilterWidth); + scanLineWeight *= 0.3333333; +#endif + return scanLineWeight; +} + +void main() +{ +#if defined(CURVATURE) + vec2 texcoord = Distort(vTexCoord); + if (texcoord.x < 0.0) { + FragColor = vec4(0.0); + return; + } +#else + vec2 texcoord = vTexCoord; +#endif + + vec2 texcoordInPixels = texcoord * TextureSize; + +#if defined(SHARPER) + vec2 tempCoord = floor(texcoordInPixels) + vec2(0.5); + vec2 coord = tempCoord / TextureSize; + vec2 deltas = texcoordInPixels - tempCoord; + float scanLineWeight = CalcScanLine(deltas.y); + vec2 signs = sign(deltas); + deltas.x *= 2.0; + deltas = deltas * deltas; + deltas.y = deltas.y * deltas.y; + deltas.x *= 0.5; + deltas.y *= 8.0; + deltas /= TextureSize; + deltas *= signs; + vec2 tc = coord + deltas; +#else + float tempY = floor(texcoordInPixels.y) + 0.5; + float yCoord = tempY / TextureSize.y; + float dy = texcoordInPixels.y - tempY; + float scanLineWeight = CalcScanLine(dy); + float signY = sign(dy); + dy = dy * dy; + dy = dy * dy; + dy *= 8.0; + dy /= TextureSize.y; + dy *= signY; + vec2 tc = vec2(texcoord.x, yCoord + dy); +#endif + + vec3 colour = texture(Texture, tc).rgb; + +#if defined(SCANLINES) +#if defined(GAMMA) +#if defined(FAKE_GAMMA) + colour = colour * colour; +#else + colour = pow(colour, vec3(INPUT_GAMMA)); +#endif +#endif + scanLineWeight *= BLOOM_FACTOR; + colour *= scanLineWeight; + +#if defined(GAMMA) +#if defined(FAKE_GAMMA) + colour = sqrt(colour); +#else + colour = pow(colour, vec3(1.0 / OUTPUT_GAMMA)); +#endif +#endif +#endif + +#if MASK_TYPE == 0 + FragColor = vec4(colour, 1.0); +#elif MASK_TYPE == 1 + float whichMask = fract(gl_FragCoord.x * 0.5); + vec3 mask; + if (whichMask < 0.5) + mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS); + else + mask = vec3(1.0, MASK_BRIGHTNESS, 1.0); + FragColor = vec4(colour * mask, 1.0); +#elif MASK_TYPE == 2 + float whichMask = fract(gl_FragCoord.x * 0.3333333); + vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS); + if (whichMask < 0.3333333) + mask.x = 1.0; + else if (whichMask < 0.6666666) + mask.y = 1.0; + else + mask.z = 1.0; + FragColor = vec4(colour * mask, 1.0); +#endif +} diff --git a/data/shaders/crtpi_vertex.glsl b/data/shaders/crtpi_vertex.glsl new file mode 100644 index 0000000..66102c6 --- /dev/null +++ b/data/shaders/crtpi_vertex.glsl @@ -0,0 +1,48 @@ +#version 330 core + +// Configuración +#define SCANLINES +#define MULTISAMPLE +#define GAMMA +//#define FAKE_GAMMA +//#define CURVATURE +//#define SHARPER +#define MASK_TYPE 2 + +#define CURVATURE_X 0.05 +#define CURVATURE_Y 0.1 +#define MASK_BRIGHTNESS 0.80 +#define SCANLINE_WEIGHT 6.0 +#define SCANLINE_GAP_BRIGHTNESS 0.12 +#define BLOOM_FACTOR 3.5 +#define INPUT_GAMMA 2.4 +#define OUTPUT_GAMMA 2.2 + +// Inputs (desde VAO) +layout(location = 0) in vec2 aPosition; +layout(location = 1) in vec2 aTexCoord; + +// Outputs al fragment shader +out vec2 vTexCoord; +out float vFilterWidth; +#if defined(CURVATURE) +out vec2 vScreenScale; +#endif + +// Uniforms +uniform vec2 TextureSize; + +void main() +{ +#if defined(CURVATURE) + vScreenScale = vec2(1.0, 1.0); +#endif + // Calcula filterWidth dinámicamente basándose en la altura de la textura + vFilterWidth = (768.0 / TextureSize.y) / 3.0; + + // Pasar coordenadas de textura (invertir Y para SDL) + vTexCoord = vec2(aTexCoord.x, 1.0 - aTexCoord.y) * 1.0001; + + // Posición del vértice (ya en espacio de clip [-1, 1]) + gl_Position = vec4(aPosition, 0.0, 1.0); +} diff --git a/data/shaders/crtpi_vertex_es.glsl b/data/shaders/crtpi_vertex_es.glsl new file mode 100644 index 0000000..dd04db5 --- /dev/null +++ b/data/shaders/crtpi_vertex_es.glsl @@ -0,0 +1,51 @@ +#version 300 es + +// OpenGL ES 3.0 - Compatible con Raspberry Pi 5 +precision highp float; + +// Configuración +#define SCANLINES +#define MULTISAMPLE +#define GAMMA +//#define FAKE_GAMMA +//#define CURVATURE +//#define SHARPER +#define MASK_TYPE 2 + +#define CURVATURE_X 0.05 +#define CURVATURE_Y 0.1 +#define MASK_BRIGHTNESS 0.80 +#define SCANLINE_WEIGHT 6.0 +#define SCANLINE_GAP_BRIGHTNESS 0.12 +#define BLOOM_FACTOR 3.5 +#define INPUT_GAMMA 2.4 +#define OUTPUT_GAMMA 2.2 + +// Inputs (desde VAO) +layout(location = 0) in vec2 aPosition; +layout(location = 1) in vec2 aTexCoord; + +// Outputs al fragment shader +out vec2 vTexCoord; +out float vFilterWidth; +#if defined(CURVATURE) +out vec2 vScreenScale; +#endif + +// Uniforms +uniform vec2 TextureSize; + +void main() +{ +#if defined(CURVATURE) + vScreenScale = vec2(1.0, 1.0); +#endif + // Calcula filterWidth dinámicamente basándose en la altura de la textura + vFilterWidth = (768.0 / TextureSize.y) / 3.0; + + // Pasar coordenadas de textura (invertir Y para SDL) + vTexCoord = vec2(aTexCoord.x, 1.0 - aTexCoord.y) * 1.0001; + + // Posición del vértice (ya en espacio de clip [-1, 1]) + gl_Position = vec4(aPosition, 0.0, 1.0); +} diff --git a/source/director.cpp b/source/director.cpp index 7019c98..19896ba 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -360,8 +360,10 @@ bool Director::setFileList() { Asset::get()->add(prefix + "/data/palette/steam-lords.pal", AssetType::PALETTE); // Shaders - Asset::get()->add(prefix + "/data/shaders/crtpi_192.glsl", AssetType::DATA); - Asset::get()->add(prefix + "/data/shaders/crtpi_240.glsl", AssetType::DATA); + Asset::get()->add(prefix + "/data/shaders/crtpi_vertex.glsl", AssetType::DATA); + Asset::get()->add(prefix + "/data/shaders/crtpi_fragment.glsl", AssetType::DATA); + Asset::get()->add(prefix + "/data/shaders/crtpi_vertex_es.glsl", AssetType::DATA); + Asset::get()->add(prefix + "/data/shaders/crtpi_fragment_es.glsl", AssetType::DATA); // Datos Asset::get()->add(prefix + "/data/input/gamecontrollerdb.txt", AssetType::DATA); diff --git a/source/external/jail_shader.cpp b/source/external/jail_shader.cpp deleted file mode 100644 index 1861745..0000000 --- a/source/external/jail_shader.cpp +++ /dev/null @@ -1,266 +0,0 @@ -#include "external/jail_shader.h" - -#include - -#include // Para strncmp -#include // Para basic_ostream, operator<<, endl, cout -#include // Para runtime_error -#include // Para vector - -#ifdef __APPLE__ -#include // Para OpenGL en macOS - -#include "CoreFoundation/CoreFoundation.h" // Para Core Foundation en macOS -#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 -#include // Para OpenGL 3 en macOS -#else // NO ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 -#include // Para OpenGL (compatibilidad) en macOS -#endif // ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 -#else // SI NO ES __APPLE__ -#include // Para GLuint, glTexCoord2f, glVertex2f, GLfloat -#endif // __APPLE__ - -namespace shader { -SDL_Window* win = nullptr; -SDL_Renderer* renderer = nullptr; -GLuint programId = 0; -SDL_Texture* backBuffer = nullptr; -SDL_FPoint win_size = {320 * 4, 256 * 4}; -SDL_FPoint tex_size = {320, 256}; -bool usingOpenGL = false; - -#ifndef __APPLE__ -// Declaración de funciones de extensión de OpenGL (evitando GLEW) -PFNGLCREATESHADERPROC glCreateShader; -PFNGLSHADERSOURCEPROC glShaderSource; -PFNGLCOMPILESHADERPROC glCompileShader; -PFNGLGETSHADERIVPROC glGetShaderiv; -PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog; -PFNGLDELETESHADERPROC glDeleteShader; -PFNGLATTACHSHADERPROC glAttachShader; -PFNGLCREATEPROGRAMPROC glCreateProgram; -PFNGLLINKPROGRAMPROC glLinkProgram; -PFNGLVALIDATEPROGRAMPROC glValidateProgram; -PFNGLGETPROGRAMIVPROC glGetProgramiv; -PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog; -PFNGLUSEPROGRAMPROC glUseProgram; - -bool initGLExtensions() { - glCreateShader = (PFNGLCREATESHADERPROC)SDL_GL_GetProcAddress("glCreateShader"); - glShaderSource = (PFNGLSHADERSOURCEPROC)SDL_GL_GetProcAddress("glShaderSource"); - glCompileShader = (PFNGLCOMPILESHADERPROC)SDL_GL_GetProcAddress("glCompileShader"); - glGetShaderiv = (PFNGLGETSHADERIVPROC)SDL_GL_GetProcAddress("glGetShaderiv"); - glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)SDL_GL_GetProcAddress("glGetShaderInfoLog"); - glDeleteShader = (PFNGLDELETESHADERPROC)SDL_GL_GetProcAddress("glDeleteShader"); - glAttachShader = (PFNGLATTACHSHADERPROC)SDL_GL_GetProcAddress("glAttachShader"); - glCreateProgram = (PFNGLCREATEPROGRAMPROC)SDL_GL_GetProcAddress("glCreateProgram"); - glLinkProgram = (PFNGLLINKPROGRAMPROC)SDL_GL_GetProcAddress("glLinkProgram"); - glValidateProgram = (PFNGLVALIDATEPROGRAMPROC)SDL_GL_GetProcAddress("glValidateProgram"); - glGetProgramiv = (PFNGLGETPROGRAMIVPROC)SDL_GL_GetProcAddress("glGetProgramiv"); - glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)SDL_GL_GetProcAddress("glGetProgramInfoLog"); - glUseProgram = (PFNGLUSEPROGRAMPROC)SDL_GL_GetProcAddress("glUseProgram"); - - return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv && - glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram && - glLinkProgram && glValidateProgram && glGetProgramiv && glGetProgramInfoLog && - glUseProgram; -} -#endif - -// Función para compilar un shader a partir de un std::string -GLuint compileShader(const std::string& source, GLuint shaderType) { - if (source.empty()) { - throw std::runtime_error("ERROR FATAL: El código fuente del shader está vacío."); - } - - // Crear identificador del shader - GLuint resultado = glCreateShader(shaderType); - - // Agregar una directiva según el tipo de shader - std::string directiva = (shaderType == GL_VERTEX_SHADER) - ? "#define VERTEX\n" - : "#define FRAGMENT\n"; - - const char* sources[2] = {directiva.c_str(), source.c_str()}; - - // Especificar el código fuente del shader - glShaderSource(resultado, 2, sources, nullptr); - - // Compilar el shader - glCompileShader(resultado); - - // Verificar si la compilación fue exitosa - GLint compiladoCorrectamente = GL_FALSE; - glGetShaderiv(resultado, GL_COMPILE_STATUS, &compiladoCorrectamente); - if (compiladoCorrectamente != GL_TRUE) { - std::cout << "Error en la compilación del shader (" << resultado << ")!" << std::endl; - GLint longitudLog; - glGetShaderiv(resultado, GL_INFO_LOG_LENGTH, &longitudLog); - if (longitudLog > 0) { - std::vector log(longitudLog); - glGetShaderInfoLog(resultado, longitudLog, &longitudLog, log.data()); - std::cout << "Registro de compilación del shader: " << log.data() << std::endl; - } - glDeleteShader(resultado); - resultado = 0; - } - return resultado; -} - -// Función para compilar un programa de shaders (vertex y fragment) a partir de std::string -GLuint compileProgram(const std::string& vertexShaderSource, const std::string& fragmentShaderSource) { - GLuint idPrograma = glCreateProgram(); - - // Si el fragment shader está vacío, reutilizamos el código del vertex shader - GLuint idShaderVertice = compileShader(vertexShaderSource, GL_VERTEX_SHADER); - GLuint idShaderFragmento = compileShader(fragmentShaderSource.empty() ? vertexShaderSource : fragmentShaderSource, GL_FRAGMENT_SHADER); - - if (idShaderVertice && idShaderFragmento) { - // Asociar los shaders al programa - glAttachShader(idPrograma, idShaderVertice); - glAttachShader(idPrograma, idShaderFragmento); - glLinkProgram(idPrograma); - glValidateProgram(idPrograma); - - // Verificar el estado del enlace - GLint longitudLog; - glGetProgramiv(idPrograma, GL_INFO_LOG_LENGTH, &longitudLog); - if (longitudLog > 0) { - std::vector log(longitudLog); - glGetProgramInfoLog(idPrograma, longitudLog, &longitudLog, log.data()); - std::cout << "Registro de información del programa:" << std::endl - << log.data() << std::endl; - } - } - if (idShaderVertice) { - glDeleteShader(idShaderVertice); - } - if (idShaderFragmento) { - glDeleteShader(idShaderFragmento); - } - return idPrograma; -} - -bool init(SDL_Window* ventana, SDL_Texture* texturaBackBuffer, const std::string& vertexShader, const std::string& fragmentShader) { - shader::win = ventana; - shader::renderer = SDL_GetRenderer(ventana); - shader::backBuffer = texturaBackBuffer; - SDL_GetWindowSize(ventana, &win_size.x, &win_size.y); - - int acceso; - SDL_QueryTexture(texturaBackBuffer, nullptr, &acceso, &tex_size.x, &tex_size.y); - if (acceso != SDL_TEXTUREACCESS_TARGET) { - throw std::runtime_error("ERROR FATAL: La textura debe tener definido SDL_TEXTUREACCESS_TARGET."); - } - - SDL_RendererInfo infoRenderer; - SDL_GetRendererInfo(renderer, &infoRenderer); - - // Verificar que el renderer sea OpenGL - if (!strncmp(infoRenderer.name, "opengl", 6)) { -#ifndef __APPLE__ - if (!initGLExtensions()) { - std::cout << "ADVERTENCIA: No se han podido inicializar las extensiones de OpenGL." << std::endl; - usingOpenGL = false; - return false; - } -#endif - // Compilar el programa de shaders utilizando std::string - programId = compileProgram(vertexShader, fragmentShader); - } else { - std::cout << "ADVERTENCIA: El driver del renderer no es OpenGL." << std::endl; - usingOpenGL = false; - return false; - } - usingOpenGL = true; - return true; -} - -void render() { - GLint oldProgramId; - // Establece el color de fondo - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - SDL_SetRenderTarget(renderer, nullptr); - SDL_RenderClear(renderer); - - if (usingOpenGL) { - SDL_GL_BindTexture(backBuffer, nullptr, nullptr); - if (programId != 0) { - glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgramId); - glUseProgram(programId); - } - - // Recupera el tamaño lógico configurado con SDL_RenderSetLogicalSize - int logicalW, logicalH; - SDL_RenderGetLogicalSize(renderer, &logicalW, &logicalH); - if (logicalW == 0 || logicalH == 0) { - logicalW = win_size.x; - logicalH = win_size.y; - } - - // Cálculo del viewport - int viewportX = 0, viewportY = 0, viewportW = win_size.x, viewportH = win_size.y; - SDL_bool useIntegerScale = SDL_RenderGetIntegerScale(renderer); - if (useIntegerScale) { - // Calcula el factor de escalado entero máximo que se puede aplicar - int scaleX = win_size.x / logicalW; - int scaleY = win_size.y / logicalH; - int scale = (scaleX < scaleY ? scaleX : scaleY); - if (scale < 1) - scale = 1; - viewportW = logicalW * scale; - viewportH = logicalH * scale; - viewportX = (win_size.x - viewportW) / 2; - viewportY = (win_size.y - viewportH) / 2; - } else { - // Letterboxing: preserva la relación de aspecto usando una escala flotante - float windowAspect = static_cast(win_size.x) / win_size.y; - float logicalAspect = static_cast(logicalW) / logicalH; - if (windowAspect > logicalAspect) { - viewportW = static_cast(logicalAspect * win_size.y); - viewportX = (win_size.x - viewportW) / 2; - } else { - viewportH = static_cast(win_size.x / logicalAspect); - viewportY = (win_size.y - viewportH) / 2; - } - } - glViewport(viewportX, viewportY, viewportW, viewportH); - - // Configurar la proyección ortográfica usando el espacio lógico - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - // Queremos que el origen esté en la esquina superior izquierda del espacio lógico. - glOrtho(0, static_cast(logicalW), static_cast(logicalH), 0, -1, 1); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - // Dibuja el quad con las coordenadas ajustadas. - // Se asignan las coordenadas de textura "normales" para que no quede espejado horizontalmente, - // y se mantiene el flip vertical para que la imagen no aparezca volteada. - glBegin(GL_TRIANGLE_STRIP); - // Vértice superior izquierdo - glTexCoord2f(0.0f, 1.0f); - glVertex2f(0.0f, 0.0f); - // Vértice superior derecho - glTexCoord2f(1.0f, 1.0f); - glVertex2f(static_cast(logicalW), 0.0f); - // Vértice inferior izquierdo - glTexCoord2f(0.0f, 0.0f); - glVertex2f(0.0f, static_cast(logicalH)); - // Vértice inferior derecho - glTexCoord2f(1.0f, 0.0f); - glVertex2f(static_cast(logicalW), static_cast(logicalH)); - glEnd(); - - SDL_GL_SwapWindow(win); - - if (programId != 0) { - glUseProgram(oldProgramId); - } - } else { - SDL_RenderCopy(renderer, backBuffer, nullptr, nullptr); - SDL_RenderPresent(renderer); - } -} -} // namespace shader \ No newline at end of file diff --git a/source/external/jail_shader.h b/source/external/jail_shader.h deleted file mode 100644 index 3a928c2..0000000 --- a/source/external/jail_shader.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include - -#include - -// TIPS: -// ======================================================================= -// Abans de crear el renderer, cridar a la següent funció: -// -// SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); -// -// Aixó li diu que volem un renderer que use especificament opengl. A més, -// al crear el renderer li tenim que dir que el volem que use acceeració -// per hardware, i que soporte render a textura. Per exemple: -// -// SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | -// SDL_RENDERER_TARGETTEXTURE); -// -// Per altra part, al crear la textura tenim que definir que puga ser target -// de renderitzat (SDL_TEXTUREACCESS_TARGET), per exemple: -// -// SDL_Texture *tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, -// SDL_TEXTUREACCESS_TARGET, 320, 240); -// -// Els shaders li'ls passem com una cadena, som nosaltres els que s'encarreguem -// de carregarlos de disc, amb fopen, ifstream, jfile o el que vullgues. -// Si els tens en un std::string, passa-li-la com "cadena.c_str()". -// -// Poden ser els dos el mateix arxiu, com fa libRetro, jo desde dins ja fique -// els defines necessaris. Si es el mateix arxiu, pots no ficar el quart paràmetre. -// -// Els shaders de libRetro no funcionen directament, hi ha que fer algunes modificacions. -// -// El pintat final de la teua escena l'has de fer com si "backBuffer" fora la pantalla. -// -// Ah! una cosa mes: al compilar, en Linux afegir "-lGL", en Windows afegir "-lopengl32". -// En Mac ni idea - -namespace shader { -// const bool init(SDL_Window *ventana, SDL_Texture *texturaBackBuffer, const char *vertexShader, const char *fragmentShader = nullptr); -bool init(SDL_Window* ventana, SDL_Texture* texturaBackBuffer, const std::string& vertexShader, const std::string& fragmentShader = ""); -void render(); -} // namespace shader \ No newline at end of file diff --git a/source/input.cpp b/source/input.cpp index 1cac197..8e671f7 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -224,7 +224,7 @@ std::string Input::getControllerName(int controller_index) const { return num_ga int Input::getNumControllers() const { return num_gamepads_; } // Obtiene el indice del controlador a partir de un event.id -int Input::getJoyIndex(int id) const { +int Input::getJoyIndex(SDL_JoystickID id) const { for (int i = 0; i < num_joysticks_; ++i) { if (SDL_GetJoystickID(joysticks_[i]) == id) { return i; diff --git a/source/input.h b/source/input.h index f4a61ec..1f75f79 100644 --- a/source/input.h +++ b/source/input.h @@ -128,7 +128,7 @@ class Input { std::string getControllerName(int controller_index) const; // Obtiene el indice del controlador a partir de un event.id - int getJoyIndex(int id) const; + int getJoyIndex(SDL_JoystickID id) const; // Obtiene el SDL_GamepadButton asignado a un input SDL_GamepadButton getControllerBinding(int controller_index, InputAction input) const; diff --git a/source/item.h b/source/item.h index 3c33422..40ff3f0 100644 --- a/source/item.h +++ b/source/item.h @@ -9,8 +9,8 @@ class SSprite; struct ItemData { std::string tile_set_file; // Ruta al fichero con los gráficos del item - int x; // Posición del item en pantalla - int y; // Posición del item en pantalla + float x; // Posición del item en pantalla + float y; // Posición del item en pantalla int tile; // Número de tile dentro de la textura int counter; // Contador inicial. Es el que lo hace cambiar de color Uint8 color1; // Uno de los dos colores que se utiliza para el item @@ -29,7 +29,7 @@ struct ItemData { class Item { private: // Constantes - static constexpr int ITEM_SIZE_ = 8; + static constexpr float ITEM_SIZE_ = 8; // Objetos y punteros std::shared_ptr sprite_; // SSprite del objeto diff --git a/source/player.cpp b/source/player.cpp index 2e965ef..52ecd2d 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -252,7 +252,7 @@ void Player::move() { // Si ha tocado alguna rampa mientras camina (sin saltar), asciende if (state_ != PlayerState::JUMPING) { - const LineVertical LEFT_SIDE = {static_cast(x_), static_cast(y_) + HEIGHT_ - 2, static_cast(y_) + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo + const LineVertical LEFT_SIDE = {x_, y_ + HEIGHT_ - 2, y_ + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo const int LY = room_->checkLeftSlopes(&LEFT_SIDE); if (LY > -1) { y_ = LY - HEIGHT_; @@ -269,8 +269,8 @@ void Player::move() { else if (vx_ > 0.0f) { // Crea el rectangulo de proyección en el eje X para ver si colisiona SDL_FRect proj; - proj.x = static_cast(x_) + WIDTH_; - proj.y = static_cast(y_); + proj.x = x_ + WIDTH_; + proj.y = y_; proj.h = HEIGHT_; proj.w = ceil(vx_); // Para evitar que tenga un ancho de 0 pixels @@ -292,7 +292,7 @@ void Player::move() { // Si ha tocado alguna rampa mientras camina (sin saltar), asciende if (state_ != PlayerState::JUMPING) { - const LineVertical RIGHT_SIDE = {static_cast(x_) + WIDTH_ - 1, static_cast(y_) + HEIGHT_ - 2, static_cast(y_) + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo + const LineVertical RIGHT_SIDE = {x_ + WIDTH_ - 1, y_ + HEIGHT_ - 2, y_ + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo const int RY = room_->checkRightSlopes(&RIGHT_SIDE); if (RY > -1) { y_ = RY - HEIGHT_; diff --git a/source/player.h b/source/player.h index 00637e6..5a456a2 100644 --- a/source/player.h +++ b/source/player.h @@ -193,7 +193,7 @@ class Player { void switchBorders(); // Obtiene el rectangulo que delimita al jugador - SDL_FRect getRect() { return {static_cast(x_), static_cast(y_), WIDTH_, HEIGHT_}; } + SDL_FRect getRect() { return {x_, y_, WIDTH_, HEIGHT_}; } // Obtiene el rectangulo de colision del jugador SDL_FRect& getCollider() { return collider_box_; } diff --git a/source/rendering/opengl/opengl_shader.cpp b/source/rendering/opengl/opengl_shader.cpp new file mode 100644 index 0000000..3c296c2 --- /dev/null +++ b/source/rendering/opengl/opengl_shader.cpp @@ -0,0 +1,462 @@ +#include "opengl_shader.h" + +#include +#include +#include +#include + +namespace Rendering { + +OpenGLShader::~OpenGLShader() { + cleanup(); +} + +#ifndef __APPLE__ +bool OpenGLShader::initGLExtensions() { + glCreateShader = (PFNGLCREATESHADERPROC)SDL_GL_GetProcAddress("glCreateShader"); + glShaderSource = (PFNGLSHADERSOURCEPROC)SDL_GL_GetProcAddress("glShaderSource"); + glCompileShader = (PFNGLCOMPILESHADERPROC)SDL_GL_GetProcAddress("glCompileShader"); + glGetShaderiv = (PFNGLGETSHADERIVPROC)SDL_GL_GetProcAddress("glGetShaderiv"); + glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)SDL_GL_GetProcAddress("glGetShaderInfoLog"); + glDeleteShader = (PFNGLDELETESHADERPROC)SDL_GL_GetProcAddress("glDeleteShader"); + glAttachShader = (PFNGLATTACHSHADERPROC)SDL_GL_GetProcAddress("glAttachShader"); + glCreateProgram = (PFNGLCREATEPROGRAMPROC)SDL_GL_GetProcAddress("glCreateProgram"); + glLinkProgram = (PFNGLLINKPROGRAMPROC)SDL_GL_GetProcAddress("glLinkProgram"); + glValidateProgram = (PFNGLVALIDATEPROGRAMPROC)SDL_GL_GetProcAddress("glValidateProgram"); + glGetProgramiv = (PFNGLGETPROGRAMIVPROC)SDL_GL_GetProcAddress("glGetProgramiv"); + glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)SDL_GL_GetProcAddress("glGetProgramInfoLog"); + glUseProgram = (PFNGLUSEPROGRAMPROC)SDL_GL_GetProcAddress("glUseProgram"); + glDeleteProgram = (PFNGLDELETEPROGRAMPROC)SDL_GL_GetProcAddress("glDeleteProgram"); + glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)SDL_GL_GetProcAddress("glGetUniformLocation"); + glUniform2f = (PFNGLUNIFORM2FPROC)SDL_GL_GetProcAddress("glUniform2f"); + glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)SDL_GL_GetProcAddress("glGenVertexArrays"); + glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)SDL_GL_GetProcAddress("glBindVertexArray"); + glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC)SDL_GL_GetProcAddress("glDeleteVertexArrays"); + glGenBuffers = (PFNGLGENBUFFERSPROC)SDL_GL_GetProcAddress("glGenBuffers"); + glBindBuffer = (PFNGLBINDBUFFERPROC)SDL_GL_GetProcAddress("glBindBuffer"); + glBufferData = (PFNGLBUFFERDATAPROC)SDL_GL_GetProcAddress("glBufferData"); + glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)SDL_GL_GetProcAddress("glDeleteBuffers"); + glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)SDL_GL_GetProcAddress("glVertexAttribPointer"); + glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)SDL_GL_GetProcAddress("glEnableVertexAttribArray"); + + return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv && + glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram && + glLinkProgram && glValidateProgram && glGetProgramiv && glGetProgramInfoLog && + glUseProgram && glDeleteProgram && glGetUniformLocation && glUniform2f && + glGenVertexArrays && glBindVertexArray && glDeleteVertexArrays && + glGenBuffers && glBindBuffer && glBufferData && glDeleteBuffers && + glVertexAttribPointer && glEnableVertexAttribArray; +} +#endif + +void OpenGLShader::checkGLError(const char* operation) { + GLenum error = glGetError(); + if (error != GL_NO_ERROR) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error OpenGL en %s: 0x%x", operation, error); + } +} + +GLuint OpenGLShader::compileShader(const std::string& source, GLenum shader_type) { + if (source.empty()) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "ERROR: El código fuente del shader está vacío"); + return 0; + } + + GLuint shader_id = glCreateShader(shader_type); + if (shader_id == 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al crear shader"); + checkGLError("glCreateShader"); + return 0; + } + + const char* sources[1] = {source.c_str()}; + glShaderSource(shader_id, 1, sources, nullptr); + checkGLError("glShaderSource"); + + glCompileShader(shader_id); + checkGLError("glCompileShader"); + + // Verificar compilación + GLint compiled = GL_FALSE; + glGetShaderiv(shader_id, GL_COMPILE_STATUS, &compiled); + if (compiled != GL_TRUE) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error en compilación del shader"); + GLint log_length; + glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &log_length); + if (log_length > 0) { + std::vector log(log_length); + glGetShaderInfoLog(shader_id, log_length, &log_length, log.data()); + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Log de compilación: %s", log.data()); + } + glDeleteShader(shader_id); + return 0; + } + + return shader_id; +} + +GLuint OpenGLShader::linkProgram(GLuint vertex_shader, GLuint fragment_shader) { + GLuint program = glCreateProgram(); + if (program == 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error al crear programa de shaders"); + return 0; + } + + glAttachShader(program, vertex_shader); + checkGLError("glAttachShader(vertex)"); + glAttachShader(program, fragment_shader); + checkGLError("glAttachShader(fragment)"); + + glLinkProgram(program); + checkGLError("glLinkProgram"); + + // Verificar enlace + GLint linked = GL_FALSE; + glGetProgramiv(program, GL_LINK_STATUS, &linked); + if (linked != GL_TRUE) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error al enlazar programa"); + GLint log_length; + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length); + if (log_length > 0) { + std::vector log(log_length); + glGetProgramInfoLog(program, log_length, &log_length, log.data()); + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Log de enlace: %s", log.data()); + } + glDeleteProgram(program); + return 0; + } + + glValidateProgram(program); + checkGLError("glValidateProgram"); + + return program; +} + +void OpenGLShader::createQuadGeometry() { + // Datos del quad: posición (x, y) + coordenadas de textura (u, v) + // Formato: x, y, u, v + float vertices[] = { + // Posición // TexCoords + -1.0f, -1.0f, 0.0f, 0.0f, // Inferior izquierda + 1.0f, -1.0f, 1.0f, 0.0f, // Inferior derecha + 1.0f, 1.0f, 1.0f, 1.0f, // Superior derecha + -1.0f, 1.0f, 0.0f, 1.0f // Superior izquierda + }; + + // Índices para dibujar el quad con dos triángulos + unsigned int indices[] = { + 0, 1, 2, // Primer triángulo + 2, 3, 0 // Segundo triángulo + }; + + // Generar y configurar VAO + glGenVertexArrays(1, &vao_); + glBindVertexArray(vao_); + checkGLError("glBindVertexArray"); + + // Generar y configurar VBO + glGenBuffers(1, &vbo_); + glBindBuffer(GL_ARRAY_BUFFER, vbo_); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + checkGLError("glBufferData(VBO)"); + + // Generar y configurar EBO + glGenBuffers(1, &ebo_); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + checkGLError("glBufferData(EBO)"); + + // Atributo 0: Posición (2 floats) + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + checkGLError("glVertexAttribPointer(position)"); + + // Atributo 1: Coordenadas de textura (2 floats) + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float))); + glEnableVertexAttribArray(1); + checkGLError("glVertexAttribPointer(texcoord)"); + + // Desvincular + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +GLuint OpenGLShader::getTextureID(SDL_Texture* texture) { + if (!texture) return 1; + + SDL_PropertiesID props = SDL_GetTextureProperties(texture); + GLuint texture_id = 0; + + // Intentar obtener ID de textura OpenGL + texture_id = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "SDL.texture.opengl.texture", nullptr); + + if (texture_id == 0) { + texture_id = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "texture.opengl.texture", nullptr); + } + + if (texture_id == 0) { + texture_id = (GLuint)SDL_GetNumberProperty(props, "SDL.texture.opengl.texture", 1); + } + + if (texture_id == 0) { + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, + "No se pudo obtener ID de textura OpenGL, usando 1 por defecto"); + texture_id = 1; + } + + return texture_id; +} + +bool OpenGLShader::init(SDL_Window* window, + SDL_Texture* texture, + const std::string& vertex_source, + const std::string& fragment_source) { + window_ = window; + back_buffer_ = texture; + renderer_ = SDL_GetRenderer(window); + + if (!renderer_) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error: No se pudo obtener el renderer"); + return false; + } + + // Obtener tamaños + SDL_GetWindowSize(window_, &window_width_, &window_height_); + SDL_GetTextureSize(back_buffer_, &texture_width_, &texture_height_); + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Inicializando shaders: ventana=%dx%d, textura=%.0fx%.0f", + window_width_, window_height_, texture_width_, texture_height_); + + // Verificar que es OpenGL + const char* renderer_name = SDL_GetRendererName(renderer_); + if (!renderer_name || strncmp(renderer_name, "opengl", 6) != 0) { + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, + "Renderer no es OpenGL: %s", renderer_name ? renderer_name : "unknown"); + return false; + } + +#ifndef __APPLE__ + // Inicializar extensiones OpenGL en Windows/Linux + if (!initGLExtensions()) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error al inicializar extensiones OpenGL"); + return false; + } +#endif + + // Limpiar shader anterior si existe + if (program_id_ != 0) { + glDeleteProgram(program_id_); + program_id_ = 0; + } + + // Compilar shaders + GLuint vertex_shader = compileShader(vertex_source, GL_VERTEX_SHADER); + GLuint fragment_shader = compileShader(fragment_source, GL_FRAGMENT_SHADER); + + if (vertex_shader == 0 || fragment_shader == 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error al compilar shaders"); + if (vertex_shader != 0) glDeleteShader(vertex_shader); + if (fragment_shader != 0) glDeleteShader(fragment_shader); + return false; + } + + // Enlazar programa + program_id_ = linkProgram(vertex_shader, fragment_shader); + + // Limpiar shaders (ya no necesarios tras el enlace) + glDeleteShader(vertex_shader); + glDeleteShader(fragment_shader); + + if (program_id_ == 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error al crear programa de shaders"); + return false; + } + + // Crear geometría del quad + createQuadGeometry(); + + // Obtener ubicación del uniform TextureSize + glUseProgram(program_id_); + texture_size_location_ = glGetUniformLocation(program_id_, "TextureSize"); + if (texture_size_location_ != -1) { + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Configurando TextureSize uniform: %.0fx%.0f", + texture_width_, texture_height_); + glUniform2f(texture_size_location_, texture_width_, texture_height_); + checkGLError("glUniform2f(TextureSize)"); + } else { + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, + "Uniform 'TextureSize' no encontrado en shader"); + } + glUseProgram(0); + + is_initialized_ = true; + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "** OpenGL 3.3 Shader Backend inicializado correctamente"); + + return true; +} + +void OpenGLShader::render() { + if (!is_initialized_ || program_id_ == 0) { + // Fallback: renderizado SDL normal + SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255); + SDL_SetRenderTarget(renderer_, nullptr); + SDL_RenderClear(renderer_); + SDL_RenderTexture(renderer_, back_buffer_, nullptr, nullptr); + SDL_RenderPresent(renderer_); + return; + } + + // Obtener tamaño actual de ventana (puede haber cambiado) + int current_width, current_height; + SDL_GetWindowSize(window_, ¤t_width, ¤t_height); + + // Guardar estados OpenGL + GLint old_program; + glGetIntegerv(GL_CURRENT_PROGRAM, &old_program); + + GLint old_viewport[4]; + glGetIntegerv(GL_VIEWPORT, old_viewport); + + GLboolean was_texture_enabled = glIsEnabled(GL_TEXTURE_2D); + GLint old_texture; + glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture); + + GLint old_vao; + glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &old_vao); + + // Preparar renderizado + SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255); + SDL_SetRenderTarget(renderer_, nullptr); + SDL_RenderClear(renderer_); + + // Obtener y bindear textura + GLuint texture_id = getTextureID(back_buffer_); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, texture_id); + checkGLError("glBindTexture"); + + // Usar nuestro programa + glUseProgram(program_id_); + checkGLError("glUseProgram"); + + // Configurar viewport (obtener tamaño lógico de SDL) + int logical_w, logical_h; + SDL_RendererLogicalPresentation mode; + SDL_GetRenderLogicalPresentation(renderer_, &logical_w, &logical_h, &mode); + + if (logical_w == 0 || logical_h == 0) { + logical_w = current_width; + logical_h = current_height; + } + + // Calcular viewport considerando aspect ratio + int viewport_x = 0, viewport_y = 0; + int viewport_w = current_width, viewport_h = current_height; + + if (mode == SDL_LOGICAL_PRESENTATION_INTEGER_SCALE) { + int scale_x = current_width / logical_w; + int scale_y = current_height / logical_h; + int scale = (scale_x < scale_y) ? scale_x : scale_y; + if (scale < 1) scale = 1; + + viewport_w = logical_w * scale; + viewport_h = logical_h * scale; + viewport_x = (current_width - viewport_w) / 2; + viewport_y = (current_height - viewport_h) / 2; + } else { + float window_aspect = static_cast(current_width) / current_height; + float logical_aspect = static_cast(logical_w) / logical_h; + + if (window_aspect > logical_aspect) { + viewport_w = static_cast(logical_aspect * current_height); + viewport_x = (current_width - viewport_w) / 2; + } else { + viewport_h = static_cast(current_width / logical_aspect); + viewport_y = (current_height - viewport_h) / 2; + } + } + + glViewport(viewport_x, viewport_y, viewport_w, viewport_h); + checkGLError("glViewport"); + + // Dibujar quad usando VAO + glBindVertexArray(vao_); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + checkGLError("glDrawElements"); + + // Presentar + SDL_GL_SwapWindow(window_); + + // Restaurar estados OpenGL + glUseProgram(old_program); + glBindTexture(GL_TEXTURE_2D, old_texture); + if (!was_texture_enabled) { + glDisable(GL_TEXTURE_2D); + } + glBindVertexArray(old_vao); + glViewport(old_viewport[0], old_viewport[1], old_viewport[2], old_viewport[3]); +} + +void OpenGLShader::setTextureSize(float width, float height) { + if (!is_initialized_ || program_id_ == 0) { + return; + } + + texture_width_ = width; + texture_height_ = height; + + GLint old_program; + glGetIntegerv(GL_CURRENT_PROGRAM, &old_program); + + glUseProgram(program_id_); + + if (texture_size_location_ != -1) { + glUniform2f(texture_size_location_, width, height); + checkGLError("glUniform2f(TextureSize)"); + } + + glUseProgram(old_program); +} + +void OpenGLShader::cleanup() { + if (vao_ != 0) { + glDeleteVertexArrays(1, &vao_); + vao_ = 0; + } + + if (vbo_ != 0) { + glDeleteBuffers(1, &vbo_); + vbo_ = 0; + } + + if (ebo_ != 0) { + glDeleteBuffers(1, &ebo_); + ebo_ = 0; + } + + if (program_id_ != 0) { + glDeleteProgram(program_id_); + program_id_ = 0; + } + + is_initialized_ = false; + window_ = nullptr; + renderer_ = nullptr; + back_buffer_ = nullptr; +} + +} // namespace Rendering diff --git a/source/rendering/opengl/opengl_shader.h b/source/rendering/opengl/opengl_shader.h new file mode 100644 index 0000000..957d211 --- /dev/null +++ b/source/rendering/opengl/opengl_shader.h @@ -0,0 +1,98 @@ +#pragma once + +#include "../shader_backend.h" + +#ifdef __APPLE__ +#include +#else +#include +#endif + +namespace Rendering { + +/** + * @brief Backend de shaders usando OpenGL 3.3 Core Profile + * + * Implementa el renderizado de shaders usando APIs modernas de OpenGL: + * - VAO (Vertex Array Objects) + * - VBO (Vertex Buffer Objects) + * - Shaders GLSL #version 330 core + */ +class OpenGLShader : public ShaderBackend { +public: + OpenGLShader() = default; + ~OpenGLShader() override; + + bool init(SDL_Window* window, + SDL_Texture* texture, + const std::string& vertex_source, + const std::string& fragment_source) override; + + void render() override; + void setTextureSize(float width, float height) override; + void cleanup() override; + bool isHardwareAccelerated() const override { return is_initialized_; } + +private: + // Funciones auxiliares + bool initGLExtensions(); + GLuint compileShader(const std::string& source, GLenum shader_type); + GLuint linkProgram(GLuint vertex_shader, GLuint fragment_shader); + void createQuadGeometry(); + GLuint getTextureID(SDL_Texture* texture); + void checkGLError(const char* operation); + + // Estado SDL + SDL_Window* window_ = nullptr; + SDL_Renderer* renderer_ = nullptr; + SDL_Texture* back_buffer_ = nullptr; + + // Estado OpenGL + GLuint program_id_ = 0; + GLuint vao_ = 0; // Vertex Array Object + GLuint vbo_ = 0; // Vertex Buffer Object + GLuint ebo_ = 0; // Element Buffer Object + + // Ubicaciones de uniforms + GLint texture_size_location_ = -1; + + // Tamaños + int window_width_ = 0; + int window_height_ = 0; + float texture_width_ = 0.0f; + float texture_height_ = 0.0f; + + // Estado + bool is_initialized_ = false; + +#ifndef __APPLE__ + // Punteros a funciones OpenGL en Windows/Linux + PFNGLCREATESHADERPROC glCreateShader = nullptr; + PFNGLSHADERSOURCEPROC glShaderSource = nullptr; + PFNGLCOMPILESHADERPROC glCompileShader = nullptr; + PFNGLGETSHADERIVPROC glGetShaderiv = nullptr; + PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog = nullptr; + PFNGLDELETESHADERPROC glDeleteShader = nullptr; + PFNGLATTACHSHADERPROC glAttachShader = nullptr; + PFNGLCREATEPROGRAMPROC glCreateProgram = nullptr; + PFNGLLINKPROGRAMPROC glLinkProgram = nullptr; + PFNGLVALIDATEPROGRAMPROC glValidateProgram = nullptr; + PFNGLGETPROGRAMIVPROC glGetProgramiv = nullptr; + PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog = nullptr; + PFNGLUSEPROGRAMPROC glUseProgram = nullptr; + PFNGLDELETEPROGRAMPROC glDeleteProgram = nullptr; + PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = nullptr; + PFNGLUNIFORM2FPROC glUniform2f = nullptr; + PFNGLGENVERTEXARRAYSPROC glGenVertexArrays = nullptr; + PFNGLBINDVERTEXARRAYPROC glBindVertexArray = nullptr; + PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays = nullptr; + PFNGLGENBUFFERSPROC glGenBuffers = nullptr; + PFNGLBINDBUFFERPROC glBindBuffer = nullptr; + PFNGLBUFFERDATAPROC glBufferData = nullptr; + PFNGLDELETEBUFFERSPROC glDeleteBuffers = nullptr; + PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer = nullptr; + PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray = nullptr; +#endif +}; + +} // namespace Rendering diff --git a/source/rendering/shader_backend.h b/source/rendering/shader_backend.h new file mode 100644 index 0000000..b9fd170 --- /dev/null +++ b/source/rendering/shader_backend.h @@ -0,0 +1,55 @@ +#pragma once + +#include +#include + +namespace Rendering { + +/** + * @brief Interfaz abstracta para backends de renderizado con shaders + * + * Esta interfaz define el contrato que todos los backends de shaders + * deben cumplir (OpenGL, Metal, Vulkan, etc.) + */ +class ShaderBackend { +public: + virtual ~ShaderBackend() = default; + + /** + * @brief Inicializa el backend de shaders + * @param window Ventana SDL + * @param texture Textura de backbuffer a la que aplicar shaders + * @param vertex_source Código fuente del vertex shader + * @param fragment_source Código fuente del fragment shader + * @return true si la inicialización fue exitosa + */ + virtual bool init(SDL_Window* window, + SDL_Texture* texture, + const std::string& vertex_source, + const std::string& fragment_source) = 0; + + /** + * @brief Renderiza la textura con los shaders aplicados + */ + virtual void render() = 0; + + /** + * @brief Establece el tamaño de la textura como parámetro del shader + * @param width Ancho de la textura + * @param height Alto de la textura + */ + virtual void setTextureSize(float width, float height) = 0; + + /** + * @brief Limpia y libera recursos del backend + */ + virtual void cleanup() = 0; + + /** + * @brief Verifica si el backend está usando aceleración por hardware + * @return true si usa aceleración (OpenGL/Metal/Vulkan) + */ + virtual bool isHardwareAccelerated() const = 0; +}; + +} // namespace Rendering diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 710c6bb..8e96559 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -16,8 +16,8 @@ Scoreboard::Scoreboard(std::shared_ptr data) : item_surface_(Resource::get()->getSurface("items.gif")), data_(data), clock_(ClockData()) { - const int SURFACE_WIDTH_ = options.game.width; - constexpr int SURFACE_HEIGHT_ = 6 * BLOCK; + const float SURFACE_WIDTH_ = options.game.width; + constexpr float SURFACE_HEIGHT_ = 6.0F * BLOCK; // Reserva memoria para los objetos auto player_texture = Resource::get()->getSurface(options.cheats.alternate_skin == Cheat::CheatState::ENABLED ? "player2.gif" : "player.gif"); diff --git a/source/screen.cpp b/source/screen.cpp index 59d8d0d..c1538b1 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -9,14 +9,14 @@ #include // Para istreambuf_iterator, operator== #include // Para char_traits, string, operator+, operator== -#include "asset.h" // Para Asset, AssetType -#include "external/jail_shader.h" // Para init, render -#include "mouse.h" // Para updateCursorVisibility -#include "options.h" // Para Options, options, OptionsVideo, Border -#include "resource.h" // Para Resource -#include "surface.h" // Para Surface, readPalFile -#include "text.h" // Para Text -#include "ui/notifier.h" // Para Notifier +#include "asset.h" // Para Asset, AssetType +#include "mouse.h" // Para updateCursorVisibility +#include "options.h" // Para Options, options, OptionsVideo, Border +#include "rendering/opengl/opengl_shader.h" // Para OpenGLShader +#include "resource.h" // Para Resource +#include "surface.h" // Para Surface, readPalFile +#include "text.h" // Para Text +#include "ui/notifier.h" // Para Notifier // [SINGLETON] Screen* Screen::screen_ = nullptr; @@ -103,7 +103,6 @@ Screen::Screen(SDL_Window* window, SDL_Renderer* renderer) // Muestra la ventana show(); - resetShaders(); // Extrae el nombre de las paletas desde su ruta processPaletteList(); @@ -148,9 +147,6 @@ void Screen::setVideoMode(bool mode) { SDL_SetWindowFullscreen(window_, options.video.fullscreen); adjustWindowSize(); adjustRenderLogicalSize(); - - // Reinicia los shaders - resetShaders(); } // Camibia entre pantalla completa y ventana @@ -266,17 +262,6 @@ int Screen::getMaxZoom() { return MAX_ZOOM; } -// Reinicia los shaders -void Screen::resetShaders() { - if (options.video.shaders) { - const std::string GLSL_FILE = options.video.border.enabled ? "crtpi_240.glsl" : "crtpi_192.glsl"; - std::ifstream f(Asset::get()->get(GLSL_FILE).c_str()); - std::string source((std::istreambuf_iterator(f)), std::istreambuf_iterator()); - - shader::init(window_, shaders_texture_, source); - } -} - // Establece el renderizador para las surfaces void Screen::setRendererSurface(std::shared_ptr surface) { (surface) ? renderer_surface_ = std::make_shared>(surface) : renderer_surface_ = std::make_shared>(game_surface_); @@ -341,11 +326,11 @@ void Screen::surfaceToTexture() { void Screen::textureToRenderer() { SDL_Texture* texture_to_render = options.video.border.enabled ? border_texture_ : game_texture_; - if (options.video.shaders) { + if (options.video.shaders && shader_backend_) { SDL_SetRenderTarget(renderer_, shaders_texture_); SDL_RenderTexture(renderer_, texture_to_render, nullptr, nullptr); SDL_SetRenderTarget(renderer_, nullptr); - shader::render(); + shader_backend_->render(); } else { SDL_SetRenderTarget(renderer_, nullptr); SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF); @@ -439,4 +424,80 @@ void Screen::toggleIntegerScale() { // Getters SDL_Renderer* Screen::getRenderer() { return renderer_; } std::shared_ptr Screen::getRendererSurface() { return (*renderer_surface_); } -std::shared_ptr Screen::getBorderSurface() { return border_surface_; } \ No newline at end of file +std::shared_ptr Screen::getBorderSurface() { return border_surface_; } + +std::vector loadData(const std::string& filepath) { + // Fallback a filesystem + std::ifstream file(filepath, std::ios::binary | std::ios::ate); + if (!file) { + return {}; + } + + std::streamsize fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + std::vector data(fileSize); + if (!file.read(reinterpret_cast(data.data()), fileSize)) { + return {}; + } + + return data; +} + +// Carga el contenido de los archivos GLSL +void Screen::loadShaders() { + if (vertex_shader_source_.empty()) { + // Detectar si necesitamos OpenGL ES (Raspberry Pi) + // Intentar cargar versión ES primero si existe + std::string VERTEX_FILE = "crtpi_vertex_es.glsl"; + auto data = loadData(Asset::get()->get(VERTEX_FILE)); + + if (data.empty()) { + // Si no existe versión ES, usar versión Desktop + VERTEX_FILE = "crtpi_vertex.glsl"; + data = loadData(Asset::get()->get(VERTEX_FILE)); + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Usando shaders OpenGL Desktop 3.3"); + } else { + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Usando shaders OpenGL ES 3.0 (Raspberry Pi)"); + } + + if (!data.empty()) { + vertex_shader_source_ = std::string(data.begin(), data.end()); + } + } + if (fragment_shader_source_.empty()) { + // Intentar cargar versión ES primero si existe + std::string FRAGMENT_FILE = "crtpi_fragment_es.glsl"; + auto data = loadData(Asset::get()->get(FRAGMENT_FILE)); + + if (data.empty()) { + // Si no existe versión ES, usar versión Desktop + FRAGMENT_FILE = "crtpi_fragment.glsl"; + data = loadData(Asset::get()->get(FRAGMENT_FILE)); + } + + if (!data.empty()) { + fragment_shader_source_ = std::string(data.begin(), data.end()); + } + } +} + +// Inicializa los shaders +void Screen::initShaders() { +#ifndef __APPLE__ + if (options.video.shaders) { + loadShaders(); + if (!shader_backend_) { + shader_backend_ = std::make_unique(); + } + shader_backend_->init(window_, game_texture_, vertex_shader_source_, fragment_shader_source_); + } +#else + // En macOS, OpenGL está deprecated y rinde mal + // TODO: Implementar backend de Metal para shaders en macOS + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, + "Shaders no disponibles en macOS (OpenGL deprecated). Usa Metal backend."); +#endif +} \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index 2872cff..1cedc91 100644 --- a/source/screen.h +++ b/source/screen.h @@ -9,6 +9,9 @@ #include "utils.h" // Para Color struct Surface; +namespace Rendering { +class ShaderBackend; +} // Tipos de filtro enum class ScreenFilter : Uint32 { @@ -62,6 +65,7 @@ class Screen { std::shared_ptr game_surface_; // Surface principal para manejar game_surface_data_ std::shared_ptr border_surface_; // Surface para pintar el el borde de la pantalla std::shared_ptr> renderer_surface_; // Puntero a la Surface que actua + std::unique_ptr shader_backend_; // Backend de shaders (OpenGL/Metal/Vulkan) // Variables int window_width_; // Ancho de la pantalla o ventana @@ -73,6 +77,8 @@ class Screen { bool notifications_enabled_ = false; // indica si se muestran las notificaciones FPS fps_; // Variable para gestionar los frames por segundo std::string info_resolution_; // Texto con la informacion de la pantalla + std::string vertex_shader_source_; // Almacena el vertex shader + std::string fragment_shader_source_; // Almacena el fragment shader #ifdef DEBUG bool show_debug_info_ = false; // Indica si ha de mostrar/ocultar la información de la pantalla @@ -89,9 +95,6 @@ class Screen { // Ajusta el tamaño lógico del renderizador void adjustRenderLogicalSize(); - // Reinicia los shaders - void resetShaders(); - // Extrae los nombres de las paletas void processPaletteList(); @@ -110,6 +113,9 @@ class Screen { // Recrea la textura para los shaders void createShadersTexture(); + void initShaders(); // Inicializa los shaders + void loadShaders(); // Carga el contenido del archivo GLSL + // Muestra información por pantalla void renderInfo(); diff --git a/source/sections/game.cpp b/source/sections/game.cpp index aa16f47..054ac01 100644 --- a/source/sections/game.cpp +++ b/source/sections/game.cpp @@ -215,8 +215,8 @@ void Game::renderDebugInfo() { // Comprueba los eventos void Game::checkDebugEvents(const SDL_Event& event) { - if (event.type == SDL_KEYDOWN && event.key.repeat == 0) { - switch (event.key.keysym.scancode) { + if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0) { + switch (event.key.key) { case SDL_SCANCODE_G: Debug::get()->toggleEnabled(); options.cheats.invincible = static_cast(Debug::get()->getEnabled()); @@ -581,7 +581,7 @@ void Game::createRoomNameTexture() { room_name_surface_ = std::make_shared(options.game.width, text->getCharacterSize() * 2); // Establece el destino de la textura - room_name_rect_ = {0, PLAY_AREA_HEIGHT, options.game.width, text->getCharacterSize() * 2}; + room_name_rect_ = {0.0F, PLAY_AREA_HEIGHT, options.game.width, text->getCharacterSize() * 2.0F}; } // Hace sonar la música diff --git a/source/surface.h b/source/surface.h index e993467..3e87c27 100644 --- a/source/surface.h +++ b/source/surface.h @@ -22,8 +22,8 @@ Palette readPalFile(const std::string& file_path); struct SurfaceData { std::shared_ptr data; // Usa std::shared_ptr para gestión automática - float width; // Ancho de la imagen - float height; // Alto de la imagen + float width; // Ancho de la imagen + float height; // Alto de la imagen // Constructor por defecto SurfaceData() @@ -123,7 +123,7 @@ class Surface { // Color transparente Uint8 getTransparentColor() const { return transparent_color_; } - void setTransparentColor(Uint8 color = static_cast(PaletteColor::TRANSPARENT)) { transparent_color_ = color; } + void setTransparentColor(Uint8 color = 255) { transparent_color_ = color; } // Paleta void setPalette(const std::array& palette) { palette_ = palette; }