0

我见过的每个 SDL 或 SdlDotNet 教程都使用定义的 Surface 作为主屏幕。例如

private static Surface videoscreen;
videoscreen = SetVideoMode(800, 600, 16, false, false, false, true);
videoscreen.Fill(Color.Black);
videoscreen.Blit(sprite);
videoscreen.Update();

然而,在尝试使用 SdlDotNet 构建游戏时,我注意到我可以简单地使用 Video.Screen 来执行我通常会在 Surface 屏幕上执行的任何操作。例如:

Video.SetVideoMode(800, 600, 16, false, false, false, true);
Video.Screen.Fill(Color.Black);
Video.Screen.Blit(sprite);
Video.Screen.Update();

每个人仍然使用已定义的 Surface 是否有原因?我假设在我的小游戏范围内我没有遇到过某种性能或稳定性问题,但我想知道以防我以后遇到麻烦。

4

1 回答 1

1

我知道帖子很旧,但是!如果您仍然感兴趣,这是我对这个主题的看法:

方法和属性Surface返回的对象不相等,但它们都使用相同的句柄,即指向图形数据的指针。我想说这主要是风格问题,两者都是在主显示表面上工作的有效方式。Video.SetVideoMode()Video.Screen

  • Video.SetVideoMode()在 Video.cs 中导致以下内容:

    public static Surface SetVideoMode(int width, int height, int bitsPerPixel, bool resizable, bool openGL, bool fullScreen, bool hardwareSurface, bool frame)
    {
        /* ... */
        return new Surface(Sdl.SDL_SetVideoMode(width, height, bitsPerPixel, (int)flags), true);
    }
    

    ...在 Surface.cs 中调用以下内部构造函数:

    internal Surface(IntPtr handle, bool isVideoMode)
    {
        this.Handle = handle;
        this.isVideoMode = isVideoMode;
    }
    
  • 或者,这是Video.ScreenVideo.cs 中的属性定义:

    /// <summary>
    /// Gets the surface for the window or screen,
    /// must be preceded by a call to SetVideoMode
    /// </summary>
    /// <returns>The main screen surface</returns>
    public static Surface Screen
    {
        get
        {
            return Surface.FromScreenPtr(Sdl.SDL_GetVideoSurface());
        }
    }
    

    ...在 Surface.cs 中调用以下内部工厂方法:

    internal static Surface FromScreenPtr(IntPtr surfacePtr)
    {
        return new Surface(surfacePtr);
    }
    

    ...依次调用 Surface.cs 中的以下内部构造函数:

    internal Surface(IntPtr handle)
    {
        this.Handle = handle;
    }
    

如果您比较andSurface返回的对象的句柄,您将看到它们是相等的,这是您需要知道的所有内容,以确保您实际使用的是相同的数据。Video.SetVideoMode()Video.Screen

希望这可以帮助!

资料来源:

https://www.assembla.com/code/lightcs/subversion/nodes/trunk/sdldotnet-6.1.0/source/src/Graphics/Video.cs?rev=4

https://www.assembla.com/code/lightcs/subversion/nodes/trunk/sdldotnet-6.1.0/source/src/Graphics/Sur​​face.cs?rev=4

于 2013-04-22T17:16:53.843 回答