Running in Windowed mode, I needed to call CreateStretchedSurface wihtin every tick.
The following code demonstrates what I needed:
Surface surfaceBlit = screenBack.CreateStretchedSurface(new Size(sizeX, sizeY));
screen.Blit(surfaceBlit, new Point((screen.Width - sizeX) / 2, (screen.Height - sizeY) / 2));
surfaceBlit.Dispose();
(sizeX and sizeY are determined from screen.size, which changes as users resize the window, while keeping the screenBack at the original size)
This code brakes after around 150 ticks, with an AccessViolationException.
Looking into the source of Sdl.Net, I saw found that using CreateStretchedSurface creates a new surface, without ever doing cleanup. I then tried creating my own CreateStretchedSurface:
private Surface CreateStretchedSurface(Surface surface, Size destinationSize)
{
double stretchWidth = ((double)destinationSize.Width / (double)surface.Width);
double stretchHeight = ((double)destinationSize.Height / (double)surface.Height);
return surface.CreateScaledSurface(stretchWidth, stretchHeight, true);
}
Besides being able to pass on true in ScaledSurface, I was now able to run 670 ticks before getting the AccessViolationException.
Lastly I created my own disposing as well:
private void DisposeSurface(Surface surface)
{
IntPtr handle = surface.Handle;
surface.Dispose();
Sdl.SDL_FreeSurface(handle);
}
Now the program runs without getting the AccessViolationException.
My final modified workaround changes from this:
Surface surfaceBlit = screenBack.CreateStretchedSurface(new Size(sizeX, sizeY));
screen.Blit(surfaceBlit, new Point((screen.Width - sizeX) / 2, (screen.Height - sizeY) / 2));
surfaceBlit.Dispose();
to this:
Surface surfaceBlit = CreateStretchedSurface(screenBack, (new Size(sizeX, sizeY)));
screen.Blit(surfaceBlit, new Point((screen.Width - sizeX) / 2, (screen.Height - sizeY) / 2));
DisposeSurface(surfaceBlit);