Skip to content

Commit

Permalink
Added text console Renderable.
Browse files Browse the repository at this point in the history
* Added CC BY-SA 4.0 license for my silly little fonts.
  • Loading branch information
Spectere committed Apr 2, 2024
1 parent ac87335 commit aa3d958
Show file tree
Hide file tree
Showing 11 changed files with 944 additions and 20 deletions.
45 changes: 45 additions & 0 deletions Spectere.SdlKit.Demo/AppWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,51 @@ public AppWindow(decimal scale) : base(
_fontImage.Destination = new SdlRect(0, 0, 40, 50);
_fontImage.BlendMode = BlendMode.Alpha;
AddRenderable(_fontImage);

var testConsoleWidth = TargetPixelWidth / 2;
var testConsoleHeight = TargetPixelHeight / 2;
var smileConsole = new SdlKitConsole(this, testConsoleWidth, testConsoleHeight,
"Assets/SpectereFont-8x16.png", 8, 16);
smileConsole.ZOrder = 8;
smileConsole.DefaultGlyph = new Glyph {
GlyphIndex = ' ',
ForegroundColor = new SdlColor(192, 192, 192),
BackgroundColor = new SdlColor(0, 0, 192)
};
smileConsole.Destination = new SdlRect(0, 150, 200, 150);
smileConsole.PaddingColor = new SdlColor(64, 64, 255);
smileConsole.GlyphPadding = new Padding(1, 0);
smileConsole.ConsolePadding = new Padding(2, 0);
smileConsole.CenterTextArea();
smileConsole.Clear();
for(var y = 0; y < smileConsole.ConsoleHeight; y++)
for(var x = 0; x < smileConsole.ConsoleWidth; x++) {
smileConsole.SetCell(
x, y, _rng.Next(1, 3),
new SdlColor((byte)_rng.Next(0, 256), (byte)_rng.Next(0, 256), (byte)_rng.Next(0, 256)),
null
);
}
AddRenderable(smileConsole);

var textConsole = new SdlKitConsole(this, testConsoleWidth, testConsoleHeight,
"Assets/SpectereFont-8x16.png", 8, 16);
textConsole.ZOrder = 1000;
textConsole.Destination = new SdlRect(200, 150, 200, 150);
textConsole.CenterTextArea();
textConsole.Clear();
textConsole.WriteLine("Line 1...");
textConsole.WriteLine(" Line 2...");
textConsole.WriteLine(" Line 3...");
textConsole.WriteLine(" Line 4...");
textConsole.WriteLine("Line 5...");
textConsole.WriteLine(" Line 6...");
textConsole.WriteLine(" Line 7...");
textConsole.WriteLine(" Line 8...");
textConsole.WriteLine("Line 9...");
textConsole.WriteLine(" Line A...");
textConsole.Write("lmao \x01\b\x03\rlove");
AddRenderable(textConsole);
}

private bool _upPress;
Expand Down
3 changes: 3 additions & 0 deletions Spectere.SdlKit.Demo/Assets/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SpectereFont Bitmap Fonts (c) 2024 by Ian Burgmyer is licensed under CC BY-SA 4.0.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/

46 changes: 46 additions & 0 deletions Spectere.SdlKit/Interop/Sdl/Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ internal static class Render {
/// <returns>0 on success, -1 on error.</returns>
[DllImport(Lib.Sdl2, EntryPoint = "SDL_RenderClear", CallingConvention = CallingConvention.Cdecl)]
internal static extern int RenderClear(SdlRenderer renderer);

/// <summary>
/// Copy a portion of the texture to the current rendering target.
/// </summary>
/// <param name="renderer">The rendering context.</param>
/// <param name="texture">The source texture.</param>
/// <param name="srcRect">The source rectangle, or <c>null</c> for the entire texture.</param>
/// <param name="dstRect">The destination rectangle, or <c>null</c> For the entire rendering target.</param>
/// <returns>0 on success, or -1 on error.</returns>
[DllImport(Lib.Sdl2, EntryPoint = "SDL_RenderCopy", CallingConvention = CallingConvention.Cdecl)]
internal static extern int RenderCopy(SdlRenderer renderer, SdlTexture texture, ref SdlRect srcRect, ref SdlRect dstRect);

/// <summary>
/// Copy a portion of the texture to the current rendering target.
Expand All @@ -162,6 +173,28 @@ internal static extern int RenderCopyEx(
FlipDirection flip
);

/// <summary>
/// Fill a rectangle on the current rendering target with the drawing color.
/// </summary>
/// <param name="renderer">The rendering context.</param>
/// <param name="rect">The <see cref="SdlRect"/> structure representing the rectangle to fill.</param>
/// <returns>0 on success or a negative error code on failure; call <see cref="Error.GetError"/> for more
/// information.</returns>
[DllImport(Lib.Sdl2, EntryPoint = "SDL_RenderFillRect", CallingConvention = CallingConvention.Cdecl)]
internal static extern int RenderFillRect(SdlRenderer renderer, ref SdlRect rect);

/// <summary>
/// Fill some number of rectangles on the current rendering target with the drawing color.
/// </summary>
/// <param name="renderer">The rendering context.</param>
/// <param name="rects">An array of <see cref="SdlRect"/> structures representing the rectangles to be
/// filled.</param>
/// <param name="count">The number of rectangles.</param>
/// <returns>0 on success or a negative error code on failure; call <see cref="Error.GetError"/> for more
/// information.</returns>
[DllImport(Lib.Sdl2, EntryPoint = "SDL_RenderFillRects", CallingConvention = CallingConvention.Cdecl)]
internal static extern int RenderFillRects(SdlRenderer renderer, SdlRect[] rects, int count);

/// <summary>
/// Update the screen with the rendering performed.
/// </summary>
Expand Down Expand Up @@ -189,6 +222,19 @@ FlipDirection flip
[DllImport(Lib.Sdl2, EntryPoint = "SDL_SetTextureColorMod", CallingConvention = CallingConvention.Cdecl)]
internal static extern int SetTextureColorMod(SdlTexture texture, byte r, byte g, byte b);

/// <summary>
/// Set the color used for drawing operations (rect, line, and clear).
/// </summary>
/// <param name="renderer">The rendering context.</param>
/// <param name="r">The red value used to draw on the rendering target.</param>
/// <param name="g">The green value used to draw on the rendering target.</param>
/// <param name="b">The blue value used to draw on the rendering target.</param>
/// <param name="a">The alpha value used to draw on the rendering target.</param>
/// <returns>0 on success or a negative error code on failure; call <see cref="Error.GetError"/> for more
/// information.</returns>
[DllImport(Lib.Sdl2, EntryPoint = "SDL_SetRenderDrawColor", CallingConvention = CallingConvention.Cdecl)]
internal static extern int SetRenderDrawColor(SdlRenderer renderer, byte r, byte g, byte b, byte a);

/// <summary>
/// Sets a texture as the current rendering target.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions Spectere.SdlKit/Interop/SdlImage/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Image {
/// </summary>
/// <param name="anim">The <see cref="Animation"/> to dispose of.</param>
[DllImport(Lib.Sdl2Image, EntryPoint = "IMG_FreeAnimation", CallingConvention = CallingConvention.Cdecl)]
internal static extern void FreeAnimation(ref Animation anim);
internal static extern void FreeAnimation(Animation anim);

/// <summary>
/// Initialize SDL_image. This function loads dynamic libraries that SDL_image needs, and prepares them for use.
Expand Down Expand Up @@ -446,7 +446,7 @@ public class Image {
/// <param name="file">A path on the filesystem to load an image from.</param>
/// <returns>A new <see cref="SdlTexture"/>, or <c>null</c> on error.</returns>
[DllImport(Lib.Sdl2Image, EntryPoint = "IMG_LoadTexture", CallingConvention = CallingConvention.Cdecl)]
internal static extern ref SdlTexture LoadTexture(out SdlRenderer renderer, string file);
internal static extern ref SdlTexture LoadTexture(SdlRenderer renderer, string file);

/// <summary>
/// Loads an image from an SDL data source into a GPU texture.
Expand All @@ -461,7 +461,7 @@ public class Image {
/// <param name="freeSrc">Non-zero to close/free the SDL RWops before returning, zero to leave it open.</param>
/// <returns>A new <see cref="SdlTexture"/>, or <c>null</c> on error.</returns>
[DllImport(Lib.Sdl2Image, EntryPoint = "IMG_LoadTexture_RW", CallingConvention = CallingConvention.Cdecl)]
internal static extern ref SdlTexture LoadTextureRw(out SdlRenderer renderer, SdlRwOps src, int freeSrc);
internal static extern ref SdlTexture LoadTextureRw(SdlRenderer renderer, SdlRwOps src, int freeSrc);

/// <summary>
/// Loads an image from an SDL data source into a GPU texture.
Expand All @@ -477,7 +477,7 @@ public class Image {
/// <param name="type">A filename extension that represents this data ("BMP", "GIF", "PNG", etc.).</param>
/// <returns>A new <see cref="SdlTexture"/>, or <c>null</c> on error.</returns>
[DllImport(Lib.Sdl2Image, EntryPoint = "IMG_LoadTextureTyped_RW", CallingConvention = CallingConvention.Cdecl)]
internal static extern ref SdlTexture LoadTextureTypedRw(out SdlRenderer renderer, IntPtr src, int freeSrc, string type);
internal static extern ref SdlTexture LoadTextureTypedRw(SdlRenderer renderer, IntPtr src, int freeSrc, string type);

/// <summary>
/// Load an image in the TGA format directly. If you know the format of the image, you can call this function,
Expand Down
76 changes: 76 additions & 0 deletions Spectere.SdlKit/Padding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace Spectere.SdlKit;

/// <summary>
/// Defines an amount of padding, in pixels.
/// </summary>
public struct Padding {
/// <summary>
/// The amount of padding on the bottom of an object, in pixels.
/// </summary>
public int Bottom;

/// <summary>
/// The amount of padding to the left of an object, in pixels.
/// </summary>
public int Left;

/// <summary>
/// The amount of padding to the right of an object, in pixels.
/// </summary>
public int Right;

/// <summary>
/// The amount of padding on the top of an object, in pixels.
/// </summary>
public int Top;

public static bool operator ==(Padding left, Padding right) =>
left.Left == right.Left
&& left.Right == right.Right
&& left.Top == right.Top
&& left.Bottom == right.Bottom;

public static bool operator !=(Padding left, Padding right) => !(left == right);

/// <summary>
/// Initializes a new <see cref="Padding"/> structure.
/// </summary>
public Padding() {}

/// <summary>
/// Initializes a new <see cref="Padding"/> structure.
/// </summary>
/// <param name="horizontal">The amount of horizontal padding, in pixels.</param>
/// <param name="vertical">The amount of vertical padding, in pixels.</param>
public Padding(int horizontal, int vertical) {
Left = Right = horizontal;
Top = Bottom = vertical;
}

/// <summary>
/// Initializes a new <see cref="Padding"/> structure.
/// </summary>
/// <param name="left">The amount of left padding, in pixels.</param>
/// <param name="right">The amount of right padding, in pixels.</param>
/// <param name="top">The amount of top padding, in pixels.</param>
/// <param name="bottom">The amount of bottom padding, in pixels.</param>
public Padding(int left, int right, int top, int bottom) {
Left = left;
Right = right;
Top = top;
Bottom = bottom;
}

/// <summary>
/// Compares two <see cref="Padding"/> structures for equality.
/// </summary>
/// <param name="other">The <see cref="Padding"/> that should be compared to this instance.</param>
/// <returns><c>true</c> if the structures are equal, otherwise <c>false</c>.</returns>
public bool Equals(Padding other) => this == other;

/// <inheritdoc/>
public override bool Equals(object? obj) => obj is Padding other && Equals(other);

/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(Bottom, Left, Right, Top);
}
16 changes: 1 addition & 15 deletions Spectere.SdlKit/Renderable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using SdlHints = Spectere.SdlKit.Interop.Sdl.Support.Hints;
using Spectere.SdlKit.Exceptions;
using Spectere.SdlKit.Interop.Sdl;
using Spectere.SdlKit.Interop.Sdl.Support.Render;
Expand Down Expand Up @@ -236,7 +235,7 @@ internal Renderable(SdlRenderer renderer, TextureAccess access, int width, int h

// Set the scale quality (texture filtering) hint appropriately.
TextureFiltering = textureFiltering;
SetTextureFilteringMode(TextureFiltering);
SdlHintHelper.SetTextureFilteringMode(TextureFiltering);

// Finally, create the texture.
SdlTexture = Render.CreateTexture(SdlRenderer, _pixelFormat, TextureAccess, width, height);
Expand Down Expand Up @@ -334,19 +333,6 @@ public void Resize(int newWidth, int newHeight) {
}
}

/// <summary>
/// Sets the texture filtering hint. This must be used before a new SDL texture is created.
/// </summary>
/// <param name="textureFiltering">The <see cref="TextureFiltering"/> mode that should be set.</param>
internal static void SetTextureFilteringMode(TextureFiltering textureFiltering) {
_ = Hints.SetHint(
SdlHints.RenderScaleQuality.Name,
textureFiltering == TextureFiltering.Nearest
? SdlHints.RenderScaleQuality.Nearest
: SdlHints.RenderScaleQuality.Linear
);
}

/// <summary>
/// Updates this <see cref="Renderable"/>. This is usually done prior to drawing this object to the screen.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions Spectere.SdlKit/Renderables/Glyph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Spectere.SdlKit.Renderables;

/// <summary>
/// Represents a single glyph in an <see cref="SdlKitConsole"/>.
/// </summary>
public struct Glyph {
/// <summary>
/// The foreground color of this <see cref="Glyph"/>.
/// </summary>
public SdlColor ForegroundColor;

/// <summary>
/// The background color of this <see cref="Glyph"/>.
/// </summary>
public SdlColor BackgroundColor;

/// <summary>
/// The character index of this glyph.
/// </summary>
public int GlyphIndex;
}
2 changes: 1 addition & 1 deletion Spectere.SdlKit/Renderables/Image.Static.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal static Image FromFile(SdlRenderer renderer, string path, TextureFilteri
Surface.FreeSurface(loadedSurface);

// 4. Create a texture from the surface.
SetTextureFilteringMode(textureFiltering);
SdlHintHelper.SetTextureFilteringMode(textureFiltering);
var newTexture = Render.CreateTextureFromSurface(renderer, rgba32Surface);
if(newTexture.IsNull) {
var sdlError = Error.GetError();
Expand Down
Loading

0 comments on commit aa3d958

Please sign in to comment.