Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

Maybe this can help you:

http://www.libsdl.org/docs/html/sdlsurface.html

On the documentation, the struct SDL_Surface have an attribute SDL_PixelFormat *format. And the struct SDL_PixelFormat have an attribute SDL_Palette *palette. I think you just need to do this:

SDL_Surface* targetSurface;
SDL_Palette* myPalette = targetSurface->format->palette; 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.