From 8d94ed516c794a65361dd0bda245fbea6c8e7fce Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 2 Oct 2025 22:00:30 +0200 Subject: [PATCH] fix: Renderizar textura antes de inicializar shaders MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit En constructor: SDL_RenderTexture() antes de initShaders() Esto asegura que la textura tiene contenido válido. Revertido lazy initialization que no funcionaba. Vuelta a la solución original que sí funcionaba. --- source/rendering/opengl/opengl_shader.cpp | 46 ++++++++--------------- source/screen.cpp | 23 ++++-------- 2 files changed, 24 insertions(+), 45 deletions(-) diff --git a/source/rendering/opengl/opengl_shader.cpp b/source/rendering/opengl/opengl_shader.cpp index df3fb89..3c296c2 100644 --- a/source/rendering/opengl/opengl_shader.cpp +++ b/source/rendering/opengl/opengl_shader.cpp @@ -189,34 +189,29 @@ void OpenGLShader::createQuadGeometry() { } GLuint OpenGLShader::getTextureID(SDL_Texture* texture) { - if (!texture) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "texture es nullptr"); - return 0; - } + if (!texture) return 1; SDL_PropertiesID props = SDL_GetTextureProperties(texture); GLuint texture_id = 0; - // Probar diferentes nombres de properties (Desktop vs ES) - const char* property_names[] = { - SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_NUMBER, // OpenGL ES - SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER, // OpenGL Desktop - "SDL.texture.opengl.texture", // Fallback legacy - "texture.opengl.texture" // Fallback legacy - }; + // Intentar obtener ID de textura OpenGL + texture_id = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "SDL.texture.opengl.texture", nullptr); - for (const char* prop_name : property_names) { - texture_id = (GLuint)SDL_GetNumberProperty(props, prop_name, 0); - if (texture_id != 0) { - SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, - "Textura OpenGL obtenida via '%s': ID=%u", prop_name, texture_id); - return texture_id; - } + if (texture_id == 0) { + texture_id = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "texture.opengl.texture", nullptr); } - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "No se pudo obtener ID de textura OpenGL con ninguna property"); - return 0; + 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, @@ -350,15 +345,6 @@ void OpenGLShader::render() { // Obtener y bindear textura GLuint texture_id = getTextureID(back_buffer_); - if (texture_id == 0) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "No se pudo obtener texture_id, abortando render"); - // Restaurar estados y hacer fallback - SDL_RenderTexture(renderer_, back_buffer_, nullptr, nullptr); - SDL_RenderPresent(renderer_); - return; - } - glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture_id); checkGLError("glBindTexture"); diff --git a/source/screen.cpp b/source/screen.cpp index ebcf4f5..5c952d5 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -55,8 +55,12 @@ Screen::Screen() setDebugInfoEnabled(true); #endif - // Los shaders se inicializarán lazy en el primer render si están activos - // Esto evita problemas con texturas vacías al inicio + // Renderizar una vez la textura vacía para que tenga contenido válido + // antes de inicializar los shaders (evita pantalla negra) + SDL_RenderTexture(renderer_, game_canvas_, nullptr, nullptr); + + // Ahora sí inicializar los shaders + initShaders(); } // Destructor @@ -96,19 +100,8 @@ void Screen::renderPresent() { SDL_SetRenderTarget(renderer_, nullptr); clean(); - if (Options::video.shaders) { - // Lazy initialization: inicializar shaders la primera vez que se usan - if (!shader_backend_) { - initShaders(); - } - - if (shader_backend_) { - shader_backend_->render(); - } else { - // Fallback si falla la inicialización - SDL_RenderTexture(renderer_, game_canvas_, nullptr, nullptr); - SDL_RenderPresent(renderer_); - } + if (Options::video.shaders && shader_backend_) { + shader_backend_->render(); } else { SDL_RenderTexture(renderer_, game_canvas_, nullptr, nullptr); SDL_RenderPresent(renderer_);