public override void RenderSprite(AABB view, Sprite sprite, Vector2f spritePosition) { if (sprite == null) { return; } var scale = CurrentWindowSize / view.Size; var screenSpace = (spritePosition - view.Min) * scale; var sourceRect = new SDL2.SDL.SDL_Rect { x = 0, y = 0, w = sprite.Size.X, h = sprite.Size.Y }; var destinationRect = new SDL2.SDL.SDL_Rect { x = (int)screenSpace.X, y = (int)((view.Size.Y - sprite.Size.Y) * scale.Y - screenSpace.Y), w = (int)(sprite.Size.X * scale.X), h = (int)(sprite.Size.Y * scale.Y) }; SDL2.SDL.SDL_RenderCopy(RendererPtr, sprite.TexturePtr, ref sourceRect, ref destinationRect); }
public override void RenderRectangle(AABB view, AABB rectangle, Color4b borderColor, Color4b fillColor) { var scale = CurrentWindowSize / view.Size; var screenSpace = (rectangle.Min - view.Min) * scale; var rect = new SDL2.SDL.SDL_Rect { x = (int)screenSpace.X, y = (int)((view.Size.Y - rectangle.Size.Y) * scale.Y - screenSpace.Y), w = (int)(rectangle.Size.X * scale.X), h = (int)(rectangle.Size.Y * scale.Y), }; if (fillColor.A > 0) { // fill rectangle SDL2.SDL.SDL_SetRenderDrawColor(RendererPtr, fillColor.R, fillColor.G, fillColor.B, fillColor.A); SDL2.SDL.SDL_RenderFillRect(RendererPtr, ref rect); } if (borderColor != fillColor) { if (borderColor.A > 0) { // border rectangle SDL2.SDL.SDL_SetRenderDrawColor(RendererPtr, borderColor.R, borderColor.G, borderColor.B, borderColor.A); SDL2.SDL.SDL_RenderDrawRect(RendererPtr, ref rect); } } }
public void Use(Window window, SDL2.SDL.SDL_Rect target) { if (!loaded) { Load(window); } window.renderer.DrawTexture(texture, target); }
/// <summary> /// Draw a texture, stretched to fill the given target rectangle. /// </summary> public void DrawStretchedTexture(Texture texture, Rectangle targetRectangle) { var srcRect = new SDL2.SDL.SDL_Rect { x = 0, y = 0, w = texture.Dimensions.Width, h = texture.Dimensions.Height }; var dstRect = ToSDLRect(targetRectangle); SDL2.SDL.SDL_RenderCopy(_rendererHandle, texture.TextureHandle, ref srcRect, ref dstRect); }
public static SDL2.SDL.SDL_Rect[] ToNativeRects(this IList <SdlRect> rects) { var result = new SDL2.SDL.SDL_Rect[rects.Count]; for (var i = 0; i < result.Length; i++) { result[i] = (SDL2.SDL.SDL_Rect)rects[i]; } return(result); }
/// <summary> /// render text lines in screen space /// </summary> /// <param name="view"></param> /// <param name="position">origin in topleft</param> /// <param name="lines"></param> /// <param name="color"></param> /// <param name="fontSize"></param> public override void RenderTextLinesOverlay(AABB view, Vector2i position, TextLine[] lines, double fontSize = 1) { var font = CurrentFont; if (font == null) { return; } var fontSprite = SpriteCache.Get(font.SpriteId); var scale = CurrentWindowSize / view.Size * fontSize; var sourceRect = new SDL2.SDL.SDL_Rect(); var destinationRect = new SDL2.SDL.SDL_Rect(); var textPosition = position; foreach (var line in lines) { if (line is Text text) { SDL2.SDL.SDL_SetTextureColorMod(fontSprite.TexturePtr, text.Color.R, text.Color.G, text.Color.B); for (var i = 0; i < text.Value.Length; i++) { var charIndex = (int)text.Value[i] - 32; var charAabb = font.CharBoxes[charIndex]; sourceRect.x = font.CharSize.X * charIndex + (int)charAabb.Min.X; sourceRect.y = (int)charAabb.Min.Y; sourceRect.w = (int)charAabb.Size.X; sourceRect.h = (int)charAabb.Size.Y; destinationRect.x = (int)(textPosition.X * scale.X); destinationRect.y = (int)(textPosition.Y * scale.Y); destinationRect.w = (int)(charAabb.Size.X * scale.X); destinationRect.h = (int)(charAabb.Size.Y * scale.Y); SDL2.SDL.SDL_RenderCopy(RendererPtr, fontSprite.TexturePtr, ref sourceRect, ref destinationRect); textPosition.X += (int)charAabb.Size.X + font.Spacing.X; } textPosition.Y += font.CharSize.Y + font.Spacing.Y; textPosition.X = position.X; } else if (line is EmptyLine) { textPosition.Y += font.CharSize.Y + font.Spacing.Y; } } }
/// <summary> /// Draw a texture at given position, at original image size. /// </summary> public void DrawTexture1to1(Texture texture, Point topLeft) { var dstRect = new SDL2.SDL.SDL_Rect { x = topLeft.x, y = topLeft.y, w = texture.Dimensions.Width, h = texture.Dimensions.Height }; var srcRect = new SDL2.SDL.SDL_Rect { x = 0, y = 0, w = texture.Dimensions.Width, h = texture.Dimensions.Height }; if (SDL2.SDL.SDL_RenderCopy(_rendererHandle, texture.TextureHandle, ref srcRect, ref dstRect) != 0) { throw new Exception("Failed to draw Texture with DrawTexture1to1."); } }
/// <summary> /// /// </summary> /// <param name="layer"></param> /// <param name="view"></param> public override void RenderLayer(AABB view, Layer layer) { var colorSprite = SpriteCache.Get(layer.ColorMapId); var sourceRect = new SDL2.SDL.SDL_Rect { x = (int)view.Left, y = (int)(colorSprite.Size.Y - view.Top), w = (int)view.Size.X, h = (int)view.Size.Y }; SDL2.SDL.SDL_RenderCopy(RendererPtr, colorSprite.TexturePtr, ref sourceRect, IntPtr.Zero); }
public override void RenderRectangleOverlay(AABB view, AABB rectangle, Color4b fillColor) { var scale = CurrentWindowSize / view.Size; var rect = new SDL2.SDL.SDL_Rect { x = (int)(rectangle.Left * scale.X), y = (int)(rectangle.Bottom * scale.Y), w = (int)(rectangle.Size.X * scale.X), h = (int)(rectangle.Size.Y * scale.Y), }; SDL2.SDL.SDL_SetRenderDrawColor(RendererPtr, fillColor.R, fillColor.G, fillColor.B, fillColor.A); SDL2.SDL.SDL_RenderFillRect(RendererPtr, ref rect); }
public override void DrawRectangle(Vec2 from, Vec2 size, bool filled, Color color, IAnchor2d anchor = null) { from += anchor?.ScreenBounds.Pos ?? Vec2.Zero; SDL2.SDL.SDL_SetRenderDrawColor(SDLRenderer, (byte)color.R, (byte)color.G, (byte)color.B, (byte)color.A); var rect = new SDL2.SDL.SDL_Rect { x = (int)from.X, y = (int)from.Y, w = (int)size.X, h = (int)size.Y }; if (filled) { SDL2.SDL.SDL_RenderFillRect(SDLRenderer, ref rect); } else { SDL2.SDL.SDL_RenderDrawRect(SDLRenderer, ref rect); } }
private void InternalDrawImage(Texture texture, Vec2 pos, Vec2 scale, double rotation, Vec2 origin, Color color, Flipped flipped, Rectangle?source, IAnchor2d anchor) { const double degreeToRadianConst = 57.2957795131; if (!(texture is SDLTexture sdltexture)) { Logger.Log(LogLevel.Warning, "Texture is not of type SDLTexture"); return; } pos += anchor?.ScreenBounds.Pos ?? Vec2.Zero; if (anchor is Entity2d entityAnchor) { scale *= entityAnchor?.Scale ?? Vec2.One; rotation += entityAnchor?.Rotation ?? 0; origin += entityAnchor?.Origin ?? Vec2.Zero; //TODO: Blend color and flip entity } var drect = new SDL2.SDL.SDL_Rect { x = (int)(pos.X - origin.X), y = (int)(pos.Y - origin.Y), w = (int)(texture.Size.X * scale.X), h = (int)(texture.Size.Y * scale.Y) }; var sdlPoint = new SDL2.SDL.SDL_Point { x = (int)origin.X, y = (int)origin.Y }; SDL2.SDL.SDL_SetTextureColorMod(sdltexture.SDL_Image, (byte)color.R, (byte)color.G, (byte)color.B); SDL2.SDL.SDL_SetTextureAlphaMod(sdltexture.SDL_Image, (byte)color.A); var sdlRenderFlip = SDL2.SDL.SDL_RendererFlip.SDL_FLIP_NONE; if (flipped == Flipped.Horizontal) { sdlRenderFlip = SDL2.SDL.SDL_RendererFlip.SDL_FLIP_HORIZONTAL; } else if (flipped == Flipped.Vertical) { sdlRenderFlip = SDL2.SDL.SDL_RendererFlip.SDL_FLIP_VERTICAL; } else if (flipped == Flipped.Both) { sdlRenderFlip = SDL2.SDL.SDL_RendererFlip.SDL_FLIP_HORIZONTAL | SDL2.SDL.SDL_RendererFlip.SDL_FLIP_VERTICAL; } if (!source.HasValue) { SDL2.SDL.SDL_RenderCopyEx(this.SDLRenderer, sdltexture.SDL_Image, (IntPtr)null, ref drect, rotation * degreeToRadianConst, ref sdlPoint, sdlRenderFlip); } else { SDL2.SDL.SDL_Rect srect = new SDL2.SDL.SDL_Rect { x = (int)source?.Pos.X, y = (int)source?.Pos.Y, w = (int)source?.Size.X, h = (int)source?.Size.Y }; SDL2.SDL.SDL_RenderCopyEx(this.SDLRenderer, sdltexture.SDL_Image, ref srect, ref drect, rotation * degreeToRadianConst, ref sdlPoint, sdlRenderFlip); } }
private void InternalDrawText(Font font, string text, Vec2 pos, Vec2 scale, double rotation, Vec2 origin, Color color, Flipped flipped, Rectangle?source, IAnchor2d anchor) { const double degreeToRadianConst = 57.2957795131; if (!(font is SDLFont sdlFont)) { Logger.Log(LogLevel.Warning, "Font is not of type SDLFont"); return; } pos += anchor?.ScreenBounds.Pos ?? Vec2.Zero; var sdlColor = new SDL2.SDL.SDL_Color { r = (byte)color.R, g = (byte)color.G, b = (byte)color.B, a = (byte)color.A }; var fontSur = SDL_ttf.TTF_RenderText_Solid(sdlFont.SDL_Font, text, sdlColor); var fontTex = SDL2.SDL.SDL_CreateTextureFromSurface(this.SDLRenderer, fontSur); SDL2.SDL.SDL_QueryTexture(fontTex, out _, out _, out int w, out int h); var fontSize = new Vec2(w, h); var drect = new SDL2.SDL.SDL_Rect { x = (int)(pos.X - origin.X), y = (int)(pos.Y - origin.Y), w = (int)(fontSize.X * scale.X), h = (int)(fontSize.Y * scale.Y) }; var sdlPoint = new SDL2.SDL.SDL_Point { x = (int)origin.X, y = (int)origin.Y }; var sdlRenderFlip = SDL2.SDL.SDL_RendererFlip.SDL_FLIP_NONE; if (flipped == Flipped.Horizontal) { sdlRenderFlip = SDL2.SDL.SDL_RendererFlip.SDL_FLIP_HORIZONTAL; } else if (flipped == Flipped.Vertical) { sdlRenderFlip = SDL2.SDL.SDL_RendererFlip.SDL_FLIP_VERTICAL; } else if (flipped == Flipped.Both) { sdlRenderFlip = SDL2.SDL.SDL_RendererFlip.SDL_FLIP_HORIZONTAL | SDL2.SDL.SDL_RendererFlip.SDL_FLIP_VERTICAL; } if (!source.HasValue) { SDL2.SDL.SDL_RenderCopyEx(this.SDLRenderer, fontTex, (IntPtr)null, ref drect, rotation * degreeToRadianConst, ref sdlPoint, sdlRenderFlip); } else { SDL2.SDL.SDL_Rect srect = new SDL2.SDL.SDL_Rect { x = (int)source?.Pos.X, y = (int)source?.Pos.Y, w = (int)source?.Size.X, h = (int)source?.Size.Y }; SDL2.SDL.SDL_RenderCopyEx(this.SDLRenderer, fontTex, ref srect, ref drect, rotation * degreeToRadianConst, ref sdlPoint, sdlRenderFlip); } SDL2.SDL.SDL_FreeSurface(fontSur); SDL2.SDL.SDL_DestroyTexture(fontTex); }