Last active
February 20, 2020 17:02
-
-
Save gregoryfmartin/8fdc96eb2e8b5700f32d16677a30903f to your computer and use it in GitHub Desktop.
SFML.NET - Draw Font
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using SFML.Graphics; | |
using SFML.Window; | |
namespace SFMLNetFontPlayground { | |
static class SFMLNetFontPlayground { | |
public static void Main (String [] args) { | |
GameWindow gw = new GameWindow (); | |
// This directory is located in the Debug build directory for the binary | |
Font PlexMonoRegular = new Font (".\\Assets\\Fonts\\IBMPlex\\Regular.ttf"); | |
Text SampleText = new Text ("Sample String", PlexMonoRegular); | |
CircleShape Circle = new CircleShape (25.0f) { | |
FillColor = Color.Blue | |
}; | |
/* | |
* This bit of code here is a little disturbing, and I'm unsure if what's on display is a consequence | |
* of my not knowing enough about C# or not. | |
* | |
* The original idea was to simply set either the value of Position.X or Position.Y directly in response | |
* to changes. However, the problem was that this code kept returning an error that indicated that accessing | |
* either X or Y in this fashion was only doing so on a value type. Ergo, what's shown here is what appears | |
* to be the most legal way to accomplish this. Further, it's not obvious that this is the most efficient | |
* way to trigger a transformation on this object, but it's the only thing that works so far. | |
*/ | |
Circle.Position = new SFML.System.Vector2f (50.0f, 50.0f); | |
SampleText.Position = new SFML.System.Vector2f (50.0f, 100.0f); | |
gw.ToRender.Add (Circle); | |
gw.ToRender.Add (SampleText); | |
gw.Run (); | |
} | |
} | |
class GameWindow { | |
/// <summary> | |
/// The actual window itself. I know the identifier is horrible, but I can't think of anything better. | |
/// </summary> | |
public RenderWindow PrincipalWindow { get; } | |
/// <summary> | |
/// Holds everything that gets drawn to the Game Window | |
/// </summary> | |
public List<Drawable> ToRender { get; } | |
/// <summary> | |
/// Do the usual stuff here. | |
/// </summary> | |
public GameWindow () { | |
ToRender = new List<Drawable> (); | |
PrincipalWindow = new RenderWindow (new VideoMode (800, 600), "SFML.NET Font Playground"); | |
PrincipalWindow.KeyPressed += this.Window_KeyPressed; | |
} | |
/// <summary> | |
/// The main program function will call into this. All the juicy stuff happens here. | |
/// </summary> | |
public void Run () { | |
while (PrincipalWindow.IsOpen) { | |
PrincipalWindow.DispatchEvents (); | |
PrincipalWindow.Clear (Color.Black); | |
if (ToRender.Count > 0) { | |
foreach (Drawable d in ToRender) { | |
PrincipalWindow.Draw (d); | |
} | |
} | |
PrincipalWindow.Display (); | |
} | |
} | |
/// <summary> | |
/// Handle key presses. Right now, the only thing that's done is the window will close on Escape, which is | |
/// quite bad. | |
/// </summary> | |
/// <param name="sender"><see cref="Window"/></param> | |
/// <param name="e"><see cref="Window.KeyEventArgs"/></param> | |
private void Window_KeyPressed (Object sender, KeyEventArgs e) { | |
Window win = sender as Window; | |
if (e.Code == Keyboard.Key.Escape) { | |
win.Close (); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment