Skip to content

Instantly share code, notes, and snippets.

@BaronKiko
Created January 24, 2019 21:16
Show Gist options
  • Save BaronKiko/fd787881b0bb80fabcfa683f61d8b3f1 to your computer and use it in GitHub Desktop.
Save BaronKiko/fd787881b0bb80fabcfa683f61d8b3f1 to your computer and use it in GitHub Desktop.
Texture Error
using System;
using System.IO;
using System.Runtime.InteropServices;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using SharpFont;
namespace Ryujinx.Profiler.UI.SharpFontHelpers
{
public class FontService
{
private int[] characterTextures;
public void InitalizeTextures()
{
characterTextures = new int[94];
var font = new FontFace(File.OpenRead(Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), @"RyuFs\system\fonts\FontStandard.ttf")));
for (int i = 33; i < 127; i++)
{
var surface = RenderSurface((char)i, font);
characterTextures[i - 33] = LoadTexture(surface);
}
}
public void DrawText(string text)
{
GL.Viewport(0, 0, 500, 500);
GL.BindTexture(TextureTarget.Texture2D, characterTextures[text[0] - 33]);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(DepthFunction.Lequal);
GL.Enable(EnableCap.Texture2D);
GL.ClearDepth(1);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.ClearColor(Color.White);
GL.Begin(PrimitiveType.Triangles);
GL.Color4(Color.White);
GL.TexCoord2(0, 0); GL.Vertex2(100, 100);
GL.TexCoord2(0, 64); GL.Vertex2(100, 164);
GL.TexCoord2(32, 64); GL.Vertex2(132, 164);
GL.TexCoord2(32, 64); GL.Vertex2(132, 164);
GL.TexCoord2(32, 0); GL.Vertex2(132, 100);
GL.TexCoord2(0, 0); GL.Vertex2(100, 100);
GL.End();
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.Disable(EnableCap.Texture2D);
GL.Disable(EnableCap.DepthTest);
}
private void DrawChar(char character)
{
}
public unsafe Surface RenderSurface(char c, FontFace font)
{
var glyph = font.GetGlyph(c, 32);
var surface = new Surface
{
Bits = Marshal.AllocHGlobal(glyph.RenderWidth * glyph.RenderHeight),
Width = glyph.RenderWidth,
Height = glyph.RenderHeight,
Pitch = glyph.RenderWidth
};
var stuff = (byte*)surface.Bits;
for (int i = 0; i < surface.Width * surface.Height; i++)
*stuff++ = 0;
glyph.RenderTo(surface);
return surface;
}
private unsafe int LoadTexture(Surface surface)
{
int width = 32; //surface.Width;
int height = 64; //surface.Height;
int len = width * height;
byte[] data = new byte[len];
//Marshal.Copy(surface.Bits, data, 0, len);
int[] pixels = new int[len];
int index = 0;
for (int i = 0; i < len; i++)
{
int c = (byte)i; //data[i];
pixels[i] = (byte.MaxValue << 24) | (c << 16) | (c << 8) | c;
}
GL.Enable(EnableCap.Texture2D);
int textureID = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, textureID);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
fixed (int* dataptr = pixels)
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr)dataptr);
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.Disable(EnableCap.Texture2D);
Marshal.FreeHGlobal(surface.Bits); //Give the memory back!
return textureID;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment