Skip to content

Instantly share code, notes, and snippets.

@Groogy
Last active September 23, 2017 16:47
Show Gist options
  • Save Groogy/d0913a9e12997a7a4b665f5e11c312bb to your computer and use it in GitHub Desktop.
Save Groogy/d0913a9e12997a7a4b665f5e11c312bb to your computer and use it in GitHub Desktop.
class Page
# ....
private def render_glyph_to_texture(glyph, width, height) : Nil
bitmap = @face.value.glyph.value.bitmap
buffer = Bytes.new bitmap.buffer, bitmap.width * bitmap.rows
show_pixels bitmap.buffer, bitmap.width, bitmap.rows
@texture.update(bitmap.buffer, bitmap.width, bitmap.rows, 0, 0, 8)
end
def show_pixels(pixels, width, height)
height.times do |y|
width.times do |x|
pixel = pixels[x + y * width]
char = pixel == 0 ? ' ' : pixel < 128 ? '+' : '*'
print char
end
print '\n'
end
end
# ....
end
class Texture
# ....
def update(pixels, width, height, x_dest, y_dest, bpp)
activate do
GL.safe_call do
external_format = self.class.translate_bpp(bpp)
pp x_dest, y_dest, width, height, @size
LibGL.texSubImage2D LibGL::TEXTURE_2D, 0, x_dest, y_dest, width, height, external_format, LibGL::UNSIGNED_BYTE, pixels
end
end
end
def self.translate_bpp(bpp)
case bpp
when 8; LibGL::RED
when 16; LibGL::RG
when 24; LibGL::RGB
when 32; LibGL::RGBA
else
raise ArgumentError.new "Invalid bits per pixel format given!(#{bpp})"
end
end
# ....
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment