How to initialize surface variable in sdl.net?

走远了吗. 提交于 2019-12-25 03:27:16

问题


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);
        }
    }

回答1:


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.



来源:https://stackoverflow.com/questions/24517510/how-to-initialize-surface-variable-in-sdl-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!