/// <summary> /// Adds the console font to a new list of console fonts with the specified family name. If the list already exists, the font will be added to it. /// </summary> /// <param name="familyName">The family name for the font.</param> /// <param name="font">The console font.</param> /// <param name="defaultFont">Mark this font as default for the family.</param> public void Add(string familyName, Font font, bool defaultFont) { if (_fontFamilies.ContainsKey(familyName)) { var fonts = _fontFamilies[familyName]; if (!fonts.Contains(font)) fonts.Add(font); } else { var fonts = new List<Font>(); fonts.Add(font); _fontFamilies.Add(familyName, fonts); } if (defaultFont) { if (_defaultFonts.ContainsKey(familyName)) { _defaultFonts[familyName].IsDefault = false; _defaultFonts.Remove(familyName); } _defaultFonts.Add(familyName, font); } }
public static CellSurface ToSurface(this Texture2D image, SadConsole.Font font, bool blockMode = false) { int imageWidth = image.Width; int imageHeight = image.Height; Color[] pixels = new Color[imageWidth * imageHeight]; image.GetData <Color>(pixels); CellSurface surface = new CellSurface(imageWidth / font.Size.X, imageHeight / font.Size.Y); global::System.Threading.Tasks.Parallel.For((int)0, (int)(imageHeight / font.Size.Y), (h) => //for (int h = 0; h < imageHeight / font.Size.Y; h++) { int startY = (h * font.Size.Y); //System.Threading.Tasks.Parallel.For(0, imageWidth / font.Size.X, (w) => for (int w = 0; w < imageWidth / font.Size.X; w++) { int startX = (w * font.Size.X); float allR = 0; float allG = 0; float allB = 0; for (int y = 0; y < font.Size.Y; y++) { for (int x = 0; x < font.Size.X; x++) { int cY = y + startY; int cX = x + startX; Color color = pixels[cY * imageWidth + cX]; allR += color.R; allG += color.G; allB += color.B; } } byte sr = (byte)(allR / (font.Size.X * font.Size.Y)); byte sg = (byte)(allG / (font.Size.X * font.Size.Y)); byte sb = (byte)(allB / (font.Size.X * font.Size.Y)); var newColor = new Color(sr, sg, sb); float sbri = newColor.GetBrightness() * 255; if (blockMode) { if (sbri > 204) { surface.SetGlyph(w, h, 219, newColor); //█ } else if (sbri > 152) { surface.SetGlyph(w, h, 178, newColor); //▓ } else if (sbri > 100) { surface.SetGlyph(w, h, 177, newColor); //▒ } else if (sbri > 48) { surface.SetGlyph(w, h, 176, newColor); //░ } } else { if (sbri > 230) { surface.SetGlyph(w, h, (int)'#', newColor); } else if (sbri > 207) { surface.SetGlyph(w, h, (int)'&', newColor); } else if (sbri > 184) { surface.SetGlyph(w, h, (int)'$', newColor); } else if (sbri > 161) { surface.SetGlyph(w, h, (int)'X', newColor); } else if (sbri > 138) { surface.SetGlyph(w, h, (int)'x', newColor); } else if (sbri > 115) { surface.SetGlyph(w, h, (int)'=', newColor); } else if (sbri > 92) { surface.SetGlyph(w, h, (int)'+', newColor); } else if (sbri > 69) { surface.SetGlyph(w, h, (int)';', newColor); } else if (sbri > 46) { surface.SetGlyph(w, h, (int)':', newColor); } else if (sbri > 23) { surface.SetGlyph(w, h, (int)'.', newColor); } } } } ); return(surface); }
public unsafe void DrawCell(Cell cell, Rectangle screenRect, Rectangle solidRect, Color defaultBackground, SadConsole.Font font) { if (lastDrawCall.Texture != font.FontImage && lastDrawCall.Texture != null) { End(); lastDrawCall.VertIndex = 0; } lastDrawCall.Texture = font.FontImage; if (lastDrawCall.VertIndex >= maxIndex) { global::System.Array.Resize(ref lastDrawCall.Verticies, lastDrawCall.Verticies.Length + lastDrawCall.Verticies.Length / 2); maxIndex = lastDrawCall.Verticies.Length - 200; } var glyphRect = font.GlyphIndexRects[cell.ActualGlyphIndex]; if ((cell.ActualSpriteEffect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally) { var temp = glyphRect.Left; glyphRect.Left = glyphRect.Width; glyphRect.Width = temp; } if ((cell.ActualSpriteEffect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically) { var temp = glyphRect.Top; glyphRect.Top = glyphRect.Height; glyphRect.Height = temp; } fixed(Vertex *verts = lastDrawCall.Verticies) { if (cell.ActualBackground != Color.Transparent && cell.ActualBackground != defaultBackground) { // Background verts[lastDrawCall.VertIndex].Position.X = screenRect.Left; verts[lastDrawCall.VertIndex].Position.Y = screenRect.Top; verts[lastDrawCall.VertIndex].TexCoords.X = solidRect.Left; verts[lastDrawCall.VertIndex].TexCoords.Y = solidRect.Top; verts[lastDrawCall.VertIndex].Color = cell.ActualBackground; lastDrawCall.VertIndex++; verts[lastDrawCall.VertIndex].Position.X = screenRect.Width; // SadConsole w/SFML changed Width to be left + width... verts[lastDrawCall.VertIndex].Position.Y = screenRect.Top; verts[lastDrawCall.VertIndex].TexCoords.X = solidRect.Width; verts[lastDrawCall.VertIndex].TexCoords.Y = solidRect.Top; verts[lastDrawCall.VertIndex].Color = cell.ActualBackground; lastDrawCall.VertIndex++; verts[lastDrawCall.VertIndex].Position.X = screenRect.Width; verts[lastDrawCall.VertIndex].Position.Y = screenRect.Height; verts[lastDrawCall.VertIndex].TexCoords.X = solidRect.Width; verts[lastDrawCall.VertIndex].TexCoords.Y = solidRect.Height; verts[lastDrawCall.VertIndex].Color = cell.ActualBackground; lastDrawCall.VertIndex++; verts[lastDrawCall.VertIndex].Position.X = screenRect.Left; verts[lastDrawCall.VertIndex].Position.Y = screenRect.Height; verts[lastDrawCall.VertIndex].TexCoords.X = solidRect.Left; verts[lastDrawCall.VertIndex].TexCoords.Y = solidRect.Height; verts[lastDrawCall.VertIndex].Color = cell.ActualBackground; lastDrawCall.VertIndex++; //lastDrawCall.Verticies.AddRange(singleDrawVerticies); } if (cell.ActualForeground != Color.Transparent) { // Foreground verts[lastDrawCall.VertIndex].Position.X = screenRect.Left; verts[lastDrawCall.VertIndex].Position.Y = screenRect.Top; verts[lastDrawCall.VertIndex].TexCoords.X = glyphRect.Left; verts[lastDrawCall.VertIndex].TexCoords.Y = glyphRect.Top; verts[lastDrawCall.VertIndex].Color = cell.ActualForeground; lastDrawCall.VertIndex++; verts[lastDrawCall.VertIndex].Position.X = screenRect.Width; // SadConsole w/SFML changed Width to be left + width... verts[lastDrawCall.VertIndex].Position.Y = screenRect.Top; verts[lastDrawCall.VertIndex].TexCoords.X = glyphRect.Width; verts[lastDrawCall.VertIndex].TexCoords.Y = glyphRect.Top; verts[lastDrawCall.VertIndex].Color = cell.ActualForeground; lastDrawCall.VertIndex++; verts[lastDrawCall.VertIndex].Position.X = screenRect.Width; verts[lastDrawCall.VertIndex].Position.Y = screenRect.Height; verts[lastDrawCall.VertIndex].TexCoords.X = glyphRect.Width; verts[lastDrawCall.VertIndex].TexCoords.Y = glyphRect.Height; verts[lastDrawCall.VertIndex].Color = cell.ActualForeground; lastDrawCall.VertIndex++; verts[lastDrawCall.VertIndex].Position.X = screenRect.Left; verts[lastDrawCall.VertIndex].Position.Y = screenRect.Height; verts[lastDrawCall.VertIndex].TexCoords.X = glyphRect.Left; verts[lastDrawCall.VertIndex].TexCoords.Y = glyphRect.Height; verts[lastDrawCall.VertIndex].Color = cell.ActualForeground; lastDrawCall.VertIndex++; //lastDrawCall.Verticies.AddRange(singleDrawVerticies); } } }
public unsafe void DrawCell(Cell cell, Rectangle screenRect, Color defaultBackground, SadConsole.Font font) { if (cell.IsVisible) { var glyphRect = font.GlyphIndexRects[cell.ActualGlyphIndex]; if ((cell.ActualSpriteEffect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally) { var temp = glyphRect.Left; glyphRect.Left = glyphRect.Width; glyphRect.Width = temp; } if ((cell.ActualSpriteEffect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically) { var temp = glyphRect.Top; glyphRect.Top = glyphRect.Height; glyphRect.Height = temp; } fixed(Vertex *verts = m_verticies) { if (cell.ActualBackground != Color.Transparent && cell.ActualBackground != defaultBackground) { // Background verts[vertIndexCounter].Position.X = screenRect.Left; verts[vertIndexCounter].Position.Y = screenRect.Top; verts[vertIndexCounter].TexCoords.X = solidRect.Left; verts[vertIndexCounter].TexCoords.Y = solidRect.Top; verts[vertIndexCounter].Color = cell.ActualBackground; vertIndexCounter++; verts[vertIndexCounter].Position.X = screenRect.Width; // SadConsole w/SFML changed Width to be left + width... verts[vertIndexCounter].Position.Y = screenRect.Top; verts[vertIndexCounter].TexCoords.X = solidRect.Width; verts[vertIndexCounter].TexCoords.Y = solidRect.Top; verts[vertIndexCounter].Color = cell.ActualBackground; vertIndexCounter++; verts[vertIndexCounter].Position.X = screenRect.Width; verts[vertIndexCounter].Position.Y = screenRect.Height; verts[vertIndexCounter].TexCoords.X = solidRect.Width; verts[vertIndexCounter].TexCoords.Y = solidRect.Height; verts[vertIndexCounter].Color = cell.ActualBackground; vertIndexCounter++; verts[vertIndexCounter].Position.X = screenRect.Left; verts[vertIndexCounter].Position.Y = screenRect.Height; verts[vertIndexCounter].TexCoords.X = solidRect.Left; verts[vertIndexCounter].TexCoords.Y = solidRect.Height; verts[vertIndexCounter].Color = cell.ActualBackground; vertIndexCounter++; } if (cell.ActualForeground != Color.Transparent) { // Foreground verts[vertIndexCounter].Position.X = screenRect.Left; verts[vertIndexCounter].Position.Y = screenRect.Top; verts[vertIndexCounter].TexCoords.X = glyphRect.Left; verts[vertIndexCounter].TexCoords.Y = glyphRect.Top; verts[vertIndexCounter].Color = cell.ActualForeground; vertIndexCounter++; verts[vertIndexCounter].Position.X = screenRect.Width; // SadConsole w/SFML changed Width to be left + width... verts[vertIndexCounter].Position.Y = screenRect.Top; verts[vertIndexCounter].TexCoords.X = glyphRect.Width; verts[vertIndexCounter].TexCoords.Y = glyphRect.Top; verts[vertIndexCounter].Color = cell.ActualForeground; vertIndexCounter++; verts[vertIndexCounter].Position.X = screenRect.Width; verts[vertIndexCounter].Position.Y = screenRect.Height; verts[vertIndexCounter].TexCoords.X = glyphRect.Width; verts[vertIndexCounter].TexCoords.Y = glyphRect.Height; verts[vertIndexCounter].Color = cell.ActualForeground; vertIndexCounter++; verts[vertIndexCounter].Position.X = screenRect.Left; verts[vertIndexCounter].Position.Y = screenRect.Height; verts[vertIndexCounter].TexCoords.X = glyphRect.Left; verts[vertIndexCounter].TexCoords.Y = glyphRect.Height; verts[vertIndexCounter].Color = cell.ActualForeground; vertIndexCounter++; } } } }
/// <summary> /// Draws a single cell using the specified SpriteBatch. /// </summary> /// <param name="batch">Rendering batch.</param> /// <param name="position">Pixel position on the screen to render.</param> /// <param name="size">Rendering size of the cell.</param> /// <param name="font">Font used to draw the cell.</param> public void Render(SpriteBatch batch, Point position, Point size, Font font) { Render(batch, new Rectangle(position.X, position.Y, size.X, size.Y), font); }
/// <summary> /// Draws a single cell using the specified SpriteBatch. /// </summary> /// <param name="batch">Rendering batch.</param> /// <param name="drawingRectangle">Where on the sreen to draw the cell, in pixels.</param> /// <param name="font">Font used to draw the cell.</param> public void Render(SpriteBatch batch, Rectangle drawingRectangle, Font font) { if (ActualBackground != Color.Transparent) batch.Draw(font.FontImage, drawingRectangle, font.GlyphIndexRects[font.SolidGlyphIndex], ActualBackground, 0f, Vector2.Zero, SpriteEffects.None, 0.3f); if (ActualForeground != Color.Transparent) batch.Draw(font.FontImage, drawingRectangle, font.GlyphIndexRects[ActualGlyphIndex], ActualForeground, 0f, Vector2.Zero, ActualSpriteEffect, 0.4f); }
public BoardConsole(int width, int height, Font font) : base(width, height, font) { }
/// <summary> /// Gets the cell coordinates of the <paramref name="targetFont"/> based on a cell in the <paramref name="sourceFont"/>. /// </summary> /// <param name="point">The position of the cell in the <paramref name="sourceFont"/>.</param> /// <param name="sourceFont">The source font translating from.</param> /// <param name="targetFont">The target font translating to.</param> /// <returns>The position of the cell in the <paramref name="targetFont"/>.</returns> public static Point TranslateFont(this Point point, Font sourceFont, Font targetFont) { var world = point.ConsoleLocationToWorld(sourceFont.Size.X, sourceFont.Size.Y); return world.WorldLocationToConsole(targetFont.Size.X, targetFont.Size.Y); }
public void DrawCell(Cell cell, Rectangle screenRect, Rectangle solidRect, Color defaultBackground, SadConsole.Font font) { if (lastDrawCall.Texture != font.Image && lastDrawCall.Texture != null) { End(); lastDrawCall.VertIndex = 0; } lastDrawCall.Texture = font.Image; if (lastDrawCall.VertIndex >= maxIndex) { global::System.Array.Resize(ref lastDrawCall.Verticies, lastDrawCall.Verticies.Length + lastDrawCall.Verticies.Length / 2); maxIndex = lastDrawCall.Verticies.Length - 200; } var glyphRect = font.GlyphIndexRects[cell.ActualGlyphIndex]; //if ((cell.ActualSpriteEffect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally) //{ // var temp = glyphRect.X; // glyphRect.X = glyphRect.Width; // glyphRect.Width = temp; //} //if ((cell.ActualSpriteEffect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically) //{ // var temp = glyphRect.Y; // glyphRect.Y = glyphRect.Height; // glyphRect.Height = temp; //} //public static Image FlipImage(Image image, bool flipHorizontally, bool flipVertically) //{ // Bitmap flippedImage = new Bitmap(image.Width, image.Height); // using (Graphics g = Graphics.FromImage(flippedImage)) // { // //Matrix transformation // Matrix m = null; // if (flipVertically && flipHorizontally) // { // m = new Matrix(-1, 0, 0, -1, 0, 0); // m.Translate(flippedImage.Width, flippedImage.Height, MatrixOrder.Append); // } // else if (flipVertically) // { // m = new Matrix(1, 0, 0, -1, 0, 0); // m.Translate(0, flippedImage.Height, MatrixOrder.Append); // } // else if (flipHorizontally) // { // m = new Matrix(-1, 0, 0, 1, 0, 0); // m.Translate(flippedImage.Width, 0, MatrixOrder.Append); // } // //Draw // g.Transform = m; // g.DrawImage(image, 0, 0); // //clean up // m.Dispose(); // } // return (Image)flippedImage; //} if (cell.ActualBackground != Color.Transparent && cell.ActualBackground != defaultBackground) { // Background lastDrawCall.Verticies[lastDrawCall.VertIndex].Source = solidRect; lastDrawCall.Verticies[lastDrawCall.VertIndex].Target = screenRect; lastDrawCall.Verticies[lastDrawCall.VertIndex].Color = cell.ActualBackground; lastDrawCall.VertIndex++; } if (cell.ActualForeground != Color.Transparent) { // Foreground lastDrawCall.Verticies[lastDrawCall.VertIndex].Source = glyphRect; lastDrawCall.Verticies[lastDrawCall.VertIndex].Target = screenRect; lastDrawCall.Verticies[lastDrawCall.VertIndex].Color = cell.ActualForeground; lastDrawCall.VertIndex++; } }
public CharacterPicker(Color foreground, Color fill, Color selectedCharacterColor, Font characterFont) : this(foreground, fill, selectedCharacterColor) { _characterSurface.AlternateFont = characterFont; }
/// <summary> /// Returns the amount of cells (X,Y) given the specified <see cref="Font"/> and current <see cref="Engine.WindowWidth"/> and <see cref="Engine.WindowHeight"/> properties. /// </summary> /// <param name="font">The font.</param> /// <returns>The amount of cells along the X and Y axis.</returns> public static Point GetScreenSizeInCells(Font font) { return(new Point(WindowWidth / font.CellWidth, WindowHeight / font.CellHeight)); }