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

使用go-sdl2首次运行后窗口表面无法清除的问题求助

Fixing Window Surface Clearing Issue in go-sdl2 SDL Lesson 01 Port

Hey there! Let's work through that window surface clearing problem you're facing while porting SDL Lesson 01 to Go with go-sdl2. From your code snippet, it looks like a few key pieces are missing that prevent the window surface from being properly cleared and displayed.

The Core Issues in Your Current Code

  • Your CreateWindow call is incomplete (missing position and window flags parameters)
  • You haven't retrieved the window's surface to perform clearing operations on
  • There's no code to fill the surface with a color (the "clear" operation)
  • You're not updating the window to show the cleared surface
  • No event loop to keep the window open long enough to see the result

Fixed & Complete Code

Here's the full working version of your port, with comments explaining each critical step:

package main

import (
    "github.com/veandco/go-sdl2/sdl"
)

const screenWidth, screenHeight = 800, 600

func main() {
    // Ensure SDL cleans up resources when the program exits
    defer sdl.Quit()

    // Initialize SDL video subsystem
    if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
        panic(err)
    }

    // Create a centered, visible window with the specified dimensions
    window, err := sdl.CreateWindow(
        "SDL Tutorial",
        sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
        screenWidth, screenHeight,
        sdl.WINDOW_SHOWN,
    )
    if err != nil {
        panic(err)
    }
    // Make sure we destroy the window when done
    defer window.Destroy()

    // Get the window's underlying surface to draw on
    screenSurface, err := window.GetSurface()
    if err != nil {
        panic(err)
    }

    // Define the color to clear the surface with (white in this case)
    clearColor := sdl.Color{R: 255, G: 255, B: 255, A: 255}
    // Fill the entire surface with the clear color (nil = entire rect)
    if err := screenSurface.FillRect(nil, screenSurface.MapRGB(&clearColor)); err != nil {
        panic(err)
    }

    // Update the window to display the newly cleared surface
    window.UpdateSurface()

    // Event loop to keep the window open until the user closes it
    running := true
    for running {
        // Process all pending events
        for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
            switch event.(type) {
            case *sdl.QuitEvent:
                // Exit the loop when the user closes the window
                running = false
            }
        }
    }
}

Key Fixes Explained

  1. Complete Window Creation: The CreateWindow call now includes all required parameters, ensuring a valid window is created and shown.
  2. Surface Retrieval: window.GetSurface() gives us access to the drawable surface attached to the window.
  3. Surface Clearing: FillRect with nil fills the entire surface with your chosen color—this is the "clear" operation you were missing. MapRGB converts the color to a format compatible with the surface.
  4. Surface Update: window.UpdateSurface() flushes the offscreen surface to the visible window, so your cleared surface actually shows up.
  5. Event Loop: Without this, the program would exit immediately after creating the window, making it look like the surface never cleared. The loop keeps the program running until the user closes the window.

Give this code a run, and you should see a persistent white window—exactly what Lesson 01 is meant to demonstrate!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:40:33