private IntPtr GetGlyphSurface(ushort index) { SDL.SDL_Color color = new SDL.SDL_Color(); color.r = color.g = color.b = color.a = 255; IntPtr surfacePtr = SDL_ttf.TTF_RenderGlyph_Blended(handle, index, color); if (surfacePtr == IntPtr.Zero) { return(IntPtr.Zero); //throw new Exception("Failed to render glyph: " + SDL.SDL_GetError()); } SDL.SDL_Surface surface = Marshal.PtrToStructure <SDL.SDL_Surface>(surfacePtr); SDL.SDL_PixelFormat surfaceFormat = Marshal.PtrToStructure <SDL.SDL_PixelFormat>(surface.format); if (surfaceFormat.format == SDL.SDL_PIXELFORMAT_INDEX8) { IntPtr convertedSurface = SDL.SDL_ConvertSurfaceFormat(surfacePtr, SDL.SDL_PIXELFORMAT_ARGB8888, 0); SDL.SDL_FreeSurface(surfacePtr); surfacePtr = convertedSurface; //surface = Marshal.PtrToStructure<SDL.SDL_Surface>(surfacePtr); //surfaceFormat = Marshal.PtrToStructure<SDL.SDL_PixelFormat>(surface.format); } else if (surfaceFormat.format != SDL.SDL_PIXELFORMAT_ARGB8888) { throw new Exception("Failed to map SDL surface format to Bgfx texture format: " + SDL.SDL_GetPixelFormatName(surfaceFormat.format)); } return(surfacePtr); }
private byte[] GetGlyphData(IntPtr surfacePtr, FontGlyph glyph, out int width, out int height) { SDL.SDL_Surface surface = Marshal.PtrToStructure <SDL.SDL_Surface>(surfacePtr); SDL.SDL_PixelFormat surfaceFormat = Marshal.PtrToStructure <SDL.SDL_PixelFormat>(surface.format); IntPtr surfaceData = surface.pixels; int surfacePitch = surface.pitch; int ascent = GetAscent(); int descent = GetDescent(); byte[] data = new byte[glyph.width * glyph.height * surfaceFormat.BytesPerPixel]; unsafe { byte *src = (byte *)surfaceData; int rowBytes = glyph.width * surfaceFormat.BytesPerPixel; int offsetX = glyph.minX; int offsetY = -glyph.minY + (ascent - glyph.height); // bug in SDL_ttf?: // some glyphs have parts rendered outside the surface area if (offsetY < 0) { offsetY = 0; } if (offsetX < 0) { offsetX = 0; } src += offsetY * surfacePitch; src += offsetX * surfaceFormat.BytesPerPixel; for (int i = 0; i < glyph.height; i++) { Marshal.Copy((IntPtr)src, data, i * rowBytes, rowBytes); src += surfacePitch; } } width = glyph.width; height = glyph.height; return(data); }
public void SetPixelFormatStr(SDL.SDL_PixelFormat ThePixelFormat) { Marshal.StructureToPtr(ThePixelFormat, myPtr, true); }
public SDLTexture(SDLRenderer Renderer, ref SDL.SDL_PixelFormat Format, SDL.SDL_TextureAccess Access, int W, int H) { myPtr = SDL.SDL_CreateTexture(Renderer.Ptr, Format.format, (int)Access, W, H); CheckPtr(); }