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); }
//TODO: use the flags? http://wiki.libsdl.org/SDL_WindowFlags public PixelWindow(int width, int height, bool yAxisPointsUpwards = false) { if (width <= 0 || height <= 0) { throw new ArgumentOutOfRangeException("Invalid width or height"); } ClientWidth = width; ClientHeight = height; this.yAxisPointsUpwards = yAxisPointsUpwards; IsOpen = true; windowDoneCreating = false; windowThread = new Thread(delegate() { SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING); //Create window pWindow = SDL.SDL_CreateWindow("", 0, 0, width, height, SDL.SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI); if (pWindow == IntPtr.Zero) { throw new Exception("Window creation failed."); } SDL.SDL_SetWindowPosition(pWindow, SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED); //Create renderer pRenderer = SDL.SDL_CreateRenderer(pWindow, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_TARGETTEXTURE); if (pRenderer == IntPtr.Zero) { throw new Exception("Renderer creation failed."); } DoAndCheckError(SDL.SDL_SetRenderDrawBlendMode(pRenderer, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND), "Error setting blend mode."); //Create backbuffer surface pBackBufferSurface = SDL.SDL_CreateRGBSurface(0, width, height, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); unsafe { backBufferSurface = *((SDL.SDL_Surface *)pBackBufferSurface); } //Unfortunately, because the 2nd parameter is a ref, I can't just pass in null. SDL.SDL_Rect r = new SDL.SDL_Rect { x = 0, y = 0, w = width, h = height }; //Fill surface with black so that savepng output matches screen. Otherwise all the pixels initialize to alpha=0 DoAndCheckError(SDL.SDL_FillRect(pBackBufferSurface, ref r, 0xFF000000), "Error filling backbuffer surface."); //backBufferSurface = (SDL.SDL_Surface)Marshal.PtrToStructure(pBackBufferSurface, typeof(SDL.SDL_Surface)); windowDoneCreating = true; EventLoop(); SDL.SDL_FreeSurface(pBackBufferSurface); //TODO: dispose stuff? Or wait for Dispose() call? }); windowThread.Start(); SpinWait.SpinUntil(() => windowDoneCreating); }
public static SDL.SDL_PixelFormat GetPixelFormat(SDL.SDL_Surface surface) { IntPtr dataPtr = surface.format; var pixelFormat = (SDL.SDL_PixelFormat)Marshal.PtrToStructure( dataPtr, typeof(SDL.SDL_PixelFormat) ); return(pixelFormat); }
private void LoadAndPrepareResources() { music_mp3 = SDL_mixer.Mix_LoadMUS("ringtone.mp3"); if (music_mp3 == IntPtr.Zero) { Console.WriteLine("Failed to load mp3! SDL_mixer error {0}", SDL.SDL_GetError()); } SDL_mixer.Mix_PlayMusic(music_mp3, -1); music_wav = SDL_mixer.Mix_LoadWAV("beat.wav"); if (music_wav == IntPtr.Zero) { Console.WriteLine("Failed to load wav! SDL_mixer error {0}", SDL.SDL_GetError()); } font = SDL_ttf.TTF_OpenFont("lazy.ttf", 64); if (font == IntPtr.Zero) { Console.WriteLine("Failed to load lazy font! SDL_ttf Error: {0}", SDL.SDL_GetError()); } //Render text surface var textColor = new SDL.SDL_Color(); var textSurface = SDL_ttf.TTF_RenderText_Solid(font, "Hello SDL2 World!", textColor); if (textSurface == IntPtr.Zero) { Console.WriteLine("Unable to render text surface! SDL_ttf Error: {0}", SDL.SDL_GetError()); } texture_text = SDL.SDL_CreateTextureFromSurface(renderer, textSurface); if (texture_text == IntPtr.Zero) { Console.WriteLine("Unable to create texture from rendered text! SDL Error: {0}", SDL.SDL_GetError()); } s_text = Marshal.PtrToStructure <SDL.SDL_Surface>(textSurface); IntPtr image_png = SDL_image.IMG_Load("transparent.png"); if (image_png == IntPtr.Zero) { Console.WriteLine("Unable to load image! SDL_image Error: {0}", SDL.SDL_GetError()); } s_png = Marshal.PtrToStructure <SDL.SDL_Surface>(image_png); SDL.SDL_SetColorKey(image_png, (int)SDL.SDL_bool.SDL_TRUE, SDL.SDL_MapRGB(s_png.format, 0, 0xFF, 0xFF)); texture_png = SDL.SDL_CreateTextureFromSurface(renderer, image_png); if (texture_png == IntPtr.Zero) { Console.WriteLine("Unable to create texture from png! SDL Error: {0}", SDL.SDL_GetError()); } }
private void GetSurfaceMetaData() { if (Handle == IntPtr.Zero) { throw new InvalidOperationException(Errors.E_FONT_NULL); } SDL.SDL_Surface rawSurface = (SDL.SDL_Surface)Marshal.PtrToStructure(Handle, typeof(SDL.SDL_Surface)); Width = rawSurface.w; Height = rawSurface.h; //Pixels = rawSurface.pixels; //Pitch = rawSurface.pitch; }
void InitializeSurfaces() { SDL.SDL_GetWindowSize(window, out int w, out int h); //Border sBorderRect = new SDL.SDL_Rect { w = 256, h = 224, x = 0, y = 0 }; tBorderRect = new SDL.SDL_Rect { w = w, h = h, x = 0, y = 0 }; borderPtr = SDL.SDL_CreateRGBSurface(0, 256, 224, 32, 0, 0, 0, 0); borderSurface = Struct <SDL.SDL_Surface>(borderPtr); borderSurface.pitch = 256 * 4; //Screen sScreenRect = new SDL.SDL_Rect { w = 160, h = 144, x = 0, y = 0 }; tScreenRect = new SDL.SDL_Rect { w = 160, h = 144, x = 0, y = 0 }; screenPtr = SDL.SDL_CreateRGBSurface(0, 160, 144, 32, 0, 0, 0, 0); screenSurface = Struct <SDL.SDL_Surface>(screenPtr); screenSurface.pitch = 160 * 4; }
static void DrawText(string text, int x, int y, SDL.SDL_Color color, System.IntPtr renderer) { var src = new SDL.SDL_Rect(); var dest = new SDL.SDL_Rect(); //System.IntPtr temp = SDL_ttf.TTF_RenderUTF8_Blended(Font, text, TextColor); System.IntPtr temp = SDL_ttf.TTF_RenderUTF8_Solid(Font, text, color); SDL.SDL_Surface sur = (SDL.SDL_Surface)Marshal.PtrToStructure(temp, typeof(SDL.SDL_Surface)); src.x = 0; src.y = 0; src.w = sur.w; src.h = sur.h; dest.x = x; dest.y = y; dest.w = sur.w; dest.h = sur.h; var tex = SDL.SDL_CreateTextureFromSurface(renderer, temp); SDL.SDL_FreeSurface(temp); SDL.SDL_RenderCopy(renderer, tex, ref src, ref dest); SDL.SDL_DestroyTexture(tex); }
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 DrawText(float x, float y, string text, SDL.SDL_Color color, TextAlignment alignment = TextAlignment.Default, DisplayFont font = null) { var surfacePtr = SDL_ttf.TTF_RenderUNICODE_Solid(font?.Font ?? _font, text, color); SDL.SDL_Surface surface = (SDL.SDL_Surface)Marshal.PtrToStructure(surfacePtr, typeof(SDL.SDL_Surface)); var texture = SDL.SDL_CreateTextureFromSurface(_renderer, surfacePtr); int w = surface.w; int h = surface.h; int xx = (int)(x * ActualWidth); int yy = (int)(y * ActualHeight); if (alignment.HasFlag(TextAlignment.HorizontalCenter)) { xx -= w / 2; } else if (alignment.HasFlag(TextAlignment.HorizontalRight)) { xx -= w; } if (alignment.HasFlag(TextAlignment.VerticalCenter)) { yy -= h / 2; } else if (alignment.HasFlag(TextAlignment.VerticalBottom)) { yy -= h; } SDL.SDL_FreeSurface(surfacePtr); var src = new SDL.SDL_Rect() { x = 0, y = 0, w = w, h = h }; var target = new SDL.SDL_Rect() { x = xx, y = yy, w = w, h = h }; SDL.SDL_RenderCopy(_renderer, texture, ref src, ref target); SDL.SDL_DestroyTexture(texture); }
public void Load(AtlasPage page, string path) { IntPtr tmp = SDL_image.IMG_Load(path); tmp = SDL.SDL_ConvertSurfaceFormat(tmp, SDL.SDL_PIXELFORMAT_ABGR8888, 0); SDL.SDL_Surface surface = Marshal.PtrToStructure <SDL.SDL_Surface>(tmp); var textureIdList = new uint[1]; _gl.GenTextures(1, textureIdList); var textureId = textureIdList[0]; _gl.BindTexture(OpenGL.GL_TEXTURE_2D, textureId); _gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, surface.w, surface.h, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, surface.pixels); _gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR); _gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR); SDL.SDL_FreeSurface(tmp); page.rendererObject = textureId; }
static int LoadSprite(string file, System.IntPtr renderer) { System.IntPtr temp = SDL.SDL_LoadBMP(file); if (temp == null) { return(-1); } SDL.SDL_Surface sur = (SDL.SDL_Surface)Marshal.PtrToStructure(temp, typeof(SDL.SDL_Surface)); Sprite_w = sur.w; Sprite_h = sur.h; Sprite = SDL.SDL_CreateTextureFromSurface(renderer, temp); if (Sprite == null) { } SDL.SDL_FreeSurface(temp); return(0); }
private void GetSurfaceMetaData() { SDL.SDL_Surface rawSurface = (SDL.SDL_Surface)Marshal.PtrToStructure(Handle, typeof(SDL.SDL_Surface)); Width = rawSurface.w; Height = rawSurface.h; }
public static SDL.SDL_Rect ToSDL_Rect(this SDL.SDL_Surface sdl_surface) { return(new SDL.SDL_Rect { x = 0, y = 0, w = sdl_surface.w, h = sdl_surface.h }); }
public void SetSurfaceStr(SDL.SDL_Surface TheSurface) { Marshal.StructureToPtr(TheSurface, myPtr, true); }