Search code examples
c#visual-studio-2010sdl.net

How to initialize surface variable in sdl.net?


I'm working with Visual C# 2010 Express and SDL.net graphics library.

I try to paint on winform with SurfaceControl Class, and can't create an empty surface to draw on. I found only one working example with bitmap, although there is such method in http://cs-sdl.sourceforge.net/apidocs/html/class_sdl_dot_net_1_1_graphics_1_1_surface.html

Surface (int width, int height) // "Create surface of a given width and height."`

My code:

private void surfaceControl1_Click(object sender, EventArgs e)
{
  Surface surf = new Surface((Bitmap)Bitmap.FromFile("example.png"));
  surfaceControl1.Blit(surf, new Point(0, 0));
  surfaceControl1.Blit(surf, new Point(20, 20));
  // this works

  Surface surf2 = new Surface(20, 20); // <- throws exception on click
  surf2.Fill(Color.White);
  surfaceControl1.Blit(surf2);
}

Also tried:

  Surface surf2 = new Surface(this.surfaceControl1.Width,this.surfaceControl1.Height);

NullReferenceException was unhandled Object reference not set to an instance of an object. Troubleshooting tips: Use the "new" keyword to create an object instance.. etc.

SDl.net has example files with sources, it uses same method to initialize surface variable as i do, but mine throws exception. Can't find examples or tutorials with SurfaceControl usage. Any ideas what i am doing wrong?

Also found this tutorial http://www.microbasic.net/2011/08/using-sdl-with-c/ It uses this code:

Surface surface = new Surface(100, 100); //same error here
Surface item = new Surface((Bitmap)Bitmap.FromFile(“example.png”));
surface.Blit(item, new Point(0, 0));
surface.Blit(item, new Point(20, 20));
this.surfaceControl.Blit(surface);

But this code also throws same exception.

More information: I managed to launch sdl.net SdlDotNetCDPlayer example, and surprizingly it throws same exception! Though i had these examples working on my laptop half year ago.

protected override void OnResize(EventArgs e)
    {
        try
        {
            surf =
                new Surface(
                this.surfaceControl.Width,
                this.surfaceControl.Height); //exception error
            base.OnResize(e);
        }
        catch (AccessViolationException ex)
        {
            Console.WriteLine(ex.StackTrace);
        }
    }

Solution

  • It may be a bug in the SdlDotNet.dll. When I reference version 6.1 of the DLL, I receive the same run-time error. When I reference version 5.0 of the DLL, it runs fine.

    Note that v5 uses a slightly different set of initializes for the Surface, so you may need to tweak your code.