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

Raylib窗口调整大小时背景变黑问题咨询及解决方案求助

Raylib可调整窗口背景变黑问题解决

问题描述

使用raylib开发项目时,设置FLAG_WINDOW_RESIZABLE启用窗口可调整大小功能,但拖动窗口改变尺寸时,新扩展的区域会显示黑色,而非代码中设置的RAYWHITE背景。

窗口调整时背景变黑演示

问题代码

// RaylibCMakeProject.cpp : Defines the entry point for the application.
//
#include "raylib.h"

#include "RaylibCMakeProject.h"

using namespace std;

int main()
{
    cout << "Hello CMake." << endl;
    // Initialization
    //--------------------------------------------------------------------------------------
    static int screenWidth = 800;
    static int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
    //SetWindowState(FLAG_VSYNC_HINT);
    SetWindowState(FLAG_WINDOW_RESIZABLE);
    //SetWindowState(FLAG_MSAA_4X_HINT);

    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        // TODO: Update your variables here
        //----------------------------------------------------------------------------------
        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

        ClearBackground(RAYWHITE);

        DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

原因分析

窗口尺寸改变时,raylib的OpenGL视口(viewport)未自动同步更新到新的窗口大小,导致新扩展的区域没有被ClearBackground指令正确覆盖,显示为OpenGL默认的黑色背景。

解决办法

在游戏循环的更新阶段,添加窗口尺寸变化的检测,同步更新内部的宽高变量并重置视口,确保渲染区域覆盖整个窗口:

修改后的核心代码片段:

// Main game loop
while (!WindowShouldClose())    // Detect window close button or ESC key
{
    // Update
    //----------------------------------------------------------------------------------
    if (IsWindowResized())
    {
        screenWidth = GetScreenWidth();
        screenHeight = GetScreenHeight();
        SetViewport(0, 0, screenWidth, screenHeight);
    }
    //----------------------------------------------------------------------------------
    // Draw
    //----------------------------------------------------------------------------------
    BeginDrawing();

    ClearBackground(RAYWHITE);

    DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);

    EndDrawing();
    //----------------------------------------------------------------------------------
}

此外,取消注释SetWindowState(FLAG_VSYNC_HINT);启用垂直同步,可缓解窗口调整时的帧率波动问题,进一步避免类似渲染异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 05:41:11