add the ability to render text labels and stars

This commit is contained in:
Colin McMillen 2023-07-24 12:13:13 -04:00
parent ec6be80143
commit f853ef7cda

View File

@ -167,19 +167,6 @@ public class Photo {
public Texture Texture() { public Texture Texture() {
if (texture == placeholder && image != null) { if (texture == placeholder && image != null) {
IPath yourPolygon = new Star(x: 2000.0f, y: 1000.0f, prongs: 5, innerRadii: 20.0f, outerRadii: 50.0f, angle: Util.PI);
image.Mutate(x => x.Fill(Color.White, yourPolygon));
Font font = SystemFonts.CreateFont("Impact", 120);
TextOptions options = new(font) {
WrappingLength = image.Width,
HorizontalAlignment = HorizontalAlignment.Center,
Origin = new PointF(image.Width / 2, image.Height / 2 - 60),
};
string text = "oh no i made a memegen";
IBrush brush = Brushes.Solid(Color.White);
IPen pen = Pens.Solid(Color.Black, 2.5f);
image.Mutate(x => x.DrawText(options, text, brush, pen));
Console.WriteLine("making texture for " + file);
texture = new Texture(image); texture = new Texture(image);
image.Dispose(); image.Dispose();
image = null; image = null;
@ -198,7 +185,10 @@ public class Texture : IDisposable {
image.CopyPixelDataTo(pixelBytes); image.CopyPixelDataTo(pixelBytes);
Handle = GL.GenTexture(); Handle = GL.GenTexture();
Console.WriteLine("GL.GenTexture #" + Handle); if (Handle > maxHandle) {
Console.WriteLine("GL.GenTexture #" + Handle);
maxHandle = Handle;
}
GL.ActiveTexture(TextureUnit.Texture0); GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, Handle); GL.BindTexture(TextureTarget.Texture2D, Handle);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Size.X, Size.Y, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixelBytes); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Size.X, Size.Y, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixelBytes);
@ -213,6 +203,7 @@ public class Texture : IDisposable {
//GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); //GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
} }
private static int maxHandle = -1;
private bool disposedValue = false; private bool disposedValue = false;
protected virtual void Dispose(bool disposing) { protected virtual void Dispose(bool disposing) {
@ -265,6 +256,33 @@ public static class Util {
public static Box2i makeBox(int left, int top, int width, int height) { public static Box2i makeBox(int left, int top, int width, int height) {
return new Box2i(left, top, left + width, top + height); return new Box2i(left, top, left + width, top + height);
} }
public static Image<Rgba32> makeImage(float width, float height) {
return new((int) Math.Ceiling(width), (int) Math.Ceiling(height), new Rgba32(0x00, 0x00, 0x00, 0x00));
}
public static Texture renderText(string text) {
Font font = SystemFonts.CreateFont("Consolas", 36, FontStyle.Bold);
TextOptions options = new(font);
FontRectangle size = TextMeasurer.Measure(text, new TextOptions(font));
Image<Rgba32> image = makeImage(size.Width, size.Height);
IBrush brush = Brushes.Solid(Color.White);
IPen pen = Pens.Solid(Color.Black, 1f);
image.Mutate(x => x.DrawText(options, text, brush, pen));
Texture texture = new Texture(image);
image.Dispose();
return texture;
}
public static Texture renderStar(float radius) {
IPath path = new Star(x: radius, y: radius, prongs: 5, innerRadii: radius * 0.4f, outerRadii: radius, angle: Util.PI);
Image<Rgba32> image = makeImage(path.Bounds.Width, path.Bounds.Height);
image.Mutate(x => x.Fill(Color.White, path));
Texture texture = new Texture(image);
image.Dispose();
return texture;
}
} }
public class Game : GameWindow { public class Game : GameWindow {
@ -372,6 +390,9 @@ public class Game : GameWindow {
GL.ClearColor(0f, 0f, 0f, 1f); GL.ClearColor(0f, 0f, 0f, 1f);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
VertexArrayObject = GL.GenVertexArray(); VertexArrayObject = GL.GenVertexArray();
GL.BindVertexArray(VertexArrayObject); GL.BindVertexArray(VertexArrayObject);
@ -450,6 +471,10 @@ public class Game : GameWindow {
DrawBox(box, 3, Color4.White); DrawBox(box, 3, Color4.White);
} }
} }
Texture label = Util.renderText("hello world what is going on");
// Texture label = Util.renderStar(10);
DrawTexture(label, Util.makeBox(10, 10, label.Size.X, label.Size.Y));
label.Dispose();
SwapBuffers(); SwapBuffers();
} }