0

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);
        }
    }
Klemkas
  • 107
  • 8
  • It's not crappy because it does not suite my needs, it's because it's not informative, not enough to understand or make working example from it. The example code does not work and provides no explanation why not. I used other tutorial which was great in comparison with this (http://www.matrix44.net/old/sdl/sdlnet.html). – Klemkas Jul 02 '14 at 05:12
  • then stop using such word! – Parimal Raj Jul 02 '14 at 05:15
  • I'm sorry for my disrespect. – Klemkas Jul 02 '14 at 05:20
  • @L.B Can you please remove the "duplicate" notation, because i thouroughly read the suggested link about null exceptions, but nothing helps, i think this is sdl.net problem, not ordinary c# code error. The link has no sdl.net examples that would fit this problem. – Klemkas Jul 02 '14 at 09:15

1 Answers1

0

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.

toejam
  • 71
  • 7