How can I get the palette of an 8-bit surface in SDL.NET/Tao.SDL?

Posted by lolmaster on Game Development See other posts from Game Development or by lolmaster
Published on 2010-10-18T21:12:53Z Indexed on 2012/12/04 11:31 UTC
Read the original article Hit count: 453

Filed under:
|
|

I'm looking to get the palette of an 8-bit surface in SDL.NET if possible, or (more than likely) using Tao.SDL. This is because I want to do palette swapping with the palette directly, instead of blitting surfaces together to replace colours like how you would do it with a 32-bit surface.

I've gotten the SDL_Surface and the SDL_PixelFormat, however when I go to get the palette in the same way, I get a System.ExecutionEngineException:

private Tao.Sdl.Sdl.SDL_Palette GetPalette(Surface surf)
{

// Get surface.
Tao.Sdl.Sdl.SDL_Surface sdlSurface = (Tao.Sdl.Sdl.SDL_Surface)System.Runtime.InteropServices.Marshal.PtrToStructure(surf.Handle,
    typeof(Tao.Sdl.Sdl.SDL_Surface));

// Get pixel format.
Tao.Sdl.Sdl.SDL_PixelFormat pixelFormat = (Tao.Sdl.Sdl.SDL_PixelFormat)System.Runtime.InteropServices.Marshal.PtrToStructure(sdlSurface.format,
    typeof(Tao.Sdl.Sdl.SDL_PixelFormat));

// Execution exception here.
Tao.Sdl.Sdl.SDL_Palette palette = (Tao.Sdl.Sdl.SDL_Palette)System.Runtime.InteropServices.Marshal.PtrToStructure(pixelFormat.palette,
    typeof(Tao.Sdl.Sdl.SDL_Palette));

return palette;
}

When I used unsafe code to get the palette, I got a compile time error: "Cannot take the address of, get the size of, or declare a pointer to a managed type ('Tao.Sdl.Sdl.SDL_Palette')".

My unsafe code to get the palette was this:

unsafe
{
    Tao.Sdl.Sdl.SDL_Palette* pal = (Tao.Sdl.Sdl.SDL_Palette*)pixelFormat.palette;
}

From what I've read, a managed type in this case is when a structure has some sort of reference inside it as a field. The SDL_Palette structure happens to have an array of SDL_Color's, so I'm assuming that's the reference type that is causing issues. However I'm still not sure how to work around that to get the underlying palette.

So if anyone knows how to get the palette from an 8-bit surface, whether it's through safe or unsafe code, the help would be greatly appreciated.

© Game Development or respective owner

Related posts about c#

Related posts about image