0

Every SDL or SdlDotNet tutorial I have seen has used a defined Surface as the main screen. For example

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

However, while trying to build a game with SdlDotNet I noticed that I can simply use Video.Screen for any action I normally would have preformed on the Surface screen. For example:

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

Is there a reason why everyone still uses a defined Surface? I'm assuming there is some sort of performance or stability issue that I haven't encountered within the scope of my little game, but I would like to know in case I might run into trouble later on.

DaveH15
  • 1
  • 2

1 Answers1

1

I know the post is old but! If you're still interested, here's my take on the subject:

The Surface objects returned by the Video.SetVideoMode() method and the Video.Screen property are not equal but they both use the same Handle, which is the pointer to the graphical data. I would say it's mostly a matter of style, both are valid ways to work on your main display surface.

  • Video.SetVideoMode() leads to the following in 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);
    }
    

    ... calling the following internal constructor in Surface.cs:

    internal Surface(IntPtr handle, bool isVideoMode)
    {
        this.Handle = handle;
        this.isVideoMode = isVideoMode;
    }
    
  • Alternatively, this is the Video.Screen property definition in Video.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());
        }
    }
    

    ... calling the following internal factory method in Surface.cs:

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

    ... in turn calling the following internal constructor in Surface.cs:

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

If you compare the Handles of the Surface objects returned by Video.SetVideoMode() and Video.Screen, you will see that they are equal, which is all you need to know to make sure that you are actually working with the same data.

Hope this helps!

Sources:

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/Surface.cs?rev=4

Olivier
  • 858
  • 8
  • 17