3

I can't understand why one variable initialises ok and other is null, though both methods are described here

Surface (System.Drawing.Bitmap bitmap)

Create a SdlImage instance from a System.Drawing.Bitmap object. Loads a bitmap from a System.Drawing.Bitmap object, usually obtained from a resource.

Surface (int width, int height)

Create surface of a given width and height.

Code example:

        Surface surf = new Surface((Bitmap)Bitmap.FromFile("example.png")); //this works {SdlDotNet.Graphics.Surface}
        surfaceControl1.Blit(surf, new Point(0, 0));
        surfaceControl1.Blit(surf, new Point(20, 20));
        
        Surface surf2 = new Surface(20, 20); //this is {null} and throws exception
        surf2.Fill(Color.White);
        surfaceControl1.Blit(surf2);

Full code:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    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); //<-this throws null exception, details below
        surf2.Fill(Color.White);
        surfaceControl1.Blit(surf2);
    }
}

Stack Trace: line18 on program.main is Application.Run(new Form1());

sdl_control_surfaces_test.exe!WindowsFormsApplication1.Form1.surfaceControl1_Click(object sender, System.EventArgs e) Line 34   
sdl_control_surfaces_test.exe!WindowsFormsApplication1.Program.Main() Line 18 + 0x1d bytes

error:

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.

I found a more verbose stack trace:

   at SdlDotNet.Graphics.VideoInfo.get_VideoInfoStruct()
   at SdlDotNet.Graphics.VideoInfo.get_PixelFormat()
   at SdlDotNet.Graphics.Surface..ctor(Int32 width, Int32 height)
   at WindowsFormsApplication1.Form1.surfaceControl1_Click(Object sender, EventArgs e) in C:\Users\Saska\documents\visual studio 2010\Projects\sdl_control_surfaces_test\sdl_control_surfaces_test\Form1.cs:line 34
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at WindowsFormsApplication1.Program.Main() in C:\Users\Saska\documents\visual studio 2010\Projects\sdl_control_surfaces_test\sdl_control_surfaces_test\Program.cs:line 18
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

I have created another question prior to this here, i researched some tutorials and examples which i found only few, and they also throw same exception, in the evening i will turn on my old laptop and check if those examples will work there, because i swear they did.

Community
  • 1
  • 1
Klemkas
  • 107
  • 8
  • Is the constructor of `Surface` perhaps throwing an exception that's being handled somewhere before `surf2` is used? – aevitas Jul 02 '14 at 07:42
  • 1
    What is the exception it is throwing. can you post the stack trace please? – K D Jul 02 '14 at 07:46
  • I updated the description. Surf and surf2 are only used in this button code, the code is plain simple. Then why 'surf' works, as they are identical? Can it be, that the sdl.net library has a bug? – Klemkas Jul 02 '14 at 08:05
  • It would really help if you would tell us which reference is `null` – David Heffernan Jul 02 '14 at 08:25
  • Surface surf2 = new Surface(20, 20); local variables shows that surf2 is null. – Klemkas Jul 02 '14 at 08:27
  • Did you try without new keyword? Documentation says Usage : Surface image = Surface("mybitmap.jpg") image.Blit(screen, new Rectangle(new Point(0,0),image.Size)) – Faisal Jul 03 '14 at 10:58
  • Thanks for suggestion @Faisal, but then it won't compile: 'SdlDotNet.Graphics.Surface' is a 'type' but is used like a 'variable'. Probably it's a mistake to use dead library :) – Klemkas Jul 03 '14 at 13:41
  • 1
    Dumb question perhaps... but how do you know that it is the 2nd usage that's excepting and not the first one? Could you throw a try catch around them (separate ones) and post the exception(s) they catch? –  Jul 03 '14 at 13:47
  • 1
    This is OSS, so get the source and see what is expected, or step thru it with the debugger... – leppie Jul 03 '14 at 14:11

1 Answers1

1

The library is open source, so you can go to Sourceforge and look up the code.

http://sourceforge.net/p/cs-sdl/code/HEAD/tree/trunk/SdlDotNet/src/Graphics/Surface.cs

In line 204 you see that that the constructor that fails for you is just calling another

public Surface(int width, int height) : this(width, height, VideoInfo.BitsPerPixel){ }

-and if you go to http://sourceforge.net/p/cs-sdl/code/HEAD/tree/trunk/SdlDotNet/src/Graphics/VideoInfo.cs you can see that BitsPerPixel is a shortcut to PixelFormat.BitsPerPixel and PixelFormat is some nasty stuff:

private static Sdl.SDL_PixelFormat PixelFormat
{
    get
    {
        return (Sdl.SDL_PixelFormat)
            Marshal.PtrToStructure(VideoInfoStruct.vfmt,
            typeof(Sdl.SDL_PixelFormat));
    }
}

And VideFormat has a "IsInitialized" property. So my guess is that you need to initialize the VideoInfo class in some way.

Kenned
  • 568
  • 4
  • 11
  • Wow, thats some heavy stuff. Digging graphics library source code is really too hard for me. But you mentioned VideoInfo class. I just few minutes ago looked up my working game with sdl.net made with the help of [this great tutorial](http://www.matrix44.net/old/sdl/sdlnet.html), and found that this line works and i can draw on a surface and blit it. Surface surf2 = Video.CreateRgbSurface(20, 20, 32, 0, 0, 0, 0, true); Should i mark your answer as "answer"? Because it's probably some sdl.net bug or nuance. – Klemkas Jul 03 '14 at 17:52
  • 1
    I don't know if it's a bug or if you just need to initialize the VideoInfo class somehow, but it's good you founda workaround. I actually meant to show how easy it is to look up something in an open source library. :) I don't know the slightest thing about the SDL library, and it took me just a few minutes. You are already doing the hard stuff - developing apps and writing the code, so don't be afraid to look into the library code when something like this shows up. :-) – Kenned Jul 04 '14 at 07:46