You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SDL窗口调整大小时销毁重建Renderer后停止渲染问题

问题原因分析

问题出在resizeWindow()方法的两个关键错误上,导致重建渲染器后无法正常更新画面:

  1. 未更新类成员的窗口尺寸
    方法接收了新的窗口宽高参数,但没有同步更新类内部的windowWidth和windowHeight成员变量。这导致后续prepareRender()中计算像素数据拷贝长度时,仍然使用旧的窗口尺寸,无法正确填充纹理数据。

  2. 创建纹理时使用旧尺寸
    重建纹理时,你传入了this->windowWidth和this->windowHeight(旧尺寸),而非方法参数中的新尺寸。这使得新纹理的尺寸与渲染器设置的逻辑尺寸(新窗口大小)不匹配,SDL无法将更新后的纹理正确渲染到窗口上。

当你启用第二个窗口时,由于SDL不会销毁全局视频上下文(仍有活跃的渲染器存在),旧尺寸纹理的拉伸渲染仍能勉强工作,但这只是巧合,并非正确的解决方式。

修复方案

修改resizeWindow()方法,同步更新类成员尺寸,并使用新尺寸创建纹理:

virtual void resizeWindow(int newWindowHeight, int newWindowWidth){
    SDL_DestroyTexture(_texture);
    SDL_DestroyRenderer(_renderer);
    
    // 更新类内部的窗口尺寸
    this->windowWidth = newWindowWidth;
    this->windowHeight = newWindowHeight;
    
    _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    SDL_RenderSetLogicalSize(_renderer, this->windowWidth, this->windowHeight);
    _texture = SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, this->windowWidth, this->windowHeight);
}

内容的提问来源于stack exchange,提问作者Mabbus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:08:21