Created
January 10, 2020 20:18
-
-
Save treeform/632f4ae284226fd5b2bf567e3d36ec7a to your computer and use it in GitHub Desktop.
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
## Bare-bones SDL2 example | |
import sdl2 | |
import typography, vmath, flippy, chroma, tables, times | |
discard sdl2.init(INIT_EVERYTHING) | |
var | |
window: WindowPtr | |
render: RendererPtr | |
window = createWindow("SDL Skeleton", 100, 100, 640,480, SDL_WINDOW_SHOWN) | |
render = createRenderer(window, -1, Renderer_Accelerated or Renderer_PresentVsync or Renderer_TargetTexture) | |
# load font | |
var font = readFontTtf("fonts/Moon Bold.otf") | |
font.size = 16 | |
font.lineHeight = 20 | |
var | |
evt = sdl2.defaultEvent | |
runGame = true | |
type GlyphEntry = object | |
image: Image | |
texture: TexturePtr | |
glyphOffset: Vec2 | |
var glyphCache = newTable[string, GlyphEntry]() | |
proc texture(image: Image): TexturePtr = | |
# convert a flippy image to a SDL texture | |
const | |
rmask = uint32 0x000000ff | |
gmask = uint32 0x0000ff00 | |
bmask = uint32 0x00ff0000 | |
amask = uint32 0xff000000 | |
var serface = createRGBSurface(0, cint image.width, cint image.height, 32, rmask, gmask, bmask, amask) | |
serface.pixels = addr image.data[0] | |
var texture = render.createTextureFromSurface(serface) | |
return texture | |
while runGame: | |
while pollEvent(evt): | |
if evt.kind == QuitEvent: | |
runGame = false | |
break | |
render.setDrawColor 0,0,0,255 | |
render.clear | |
let start = epochTime() | |
when false: | |
# draw a single letter | |
font.size = 300 | |
font.lineHeight = 300 | |
var image = font.getGlyphImage("Q") | |
var destRect = sdl2.rect( | |
cint 0, | |
cint 0, | |
cint image.width, | |
cint image.height | |
) | |
sdl2.copy(render, image.texture, nil, addr destRect) | |
when false: | |
# compute layout and draw full layout at once | |
var layout = font.typeset(""" | |
Two roads diverged in a yellow wood, | |
And sorry I could not travel both | |
And be one traveler, long I stood | |
And looked down one as far as I could | |
To where it bent in the undergrowth;""") | |
# draw text at a layout | |
var image = newImage(500, 100, 4) | |
image.drawText(layout) | |
var destRect = sdl2.rect( | |
cint 0, | |
cint 0, | |
cint image.width, | |
cint image.height | |
) | |
sdl2.copy(render, image.texture, nil, addr destRect) | |
when false: | |
# draw single letter at a time caching the letters | |
var layout = font.typeset(""" | |
Two roads diverged in a yellow wood, | |
And sorry I could not travel both | |
And be one traveler, long I stood | |
And looked down one as far as I could | |
To where it bent in the undergrowth;""") | |
for pos in layout: | |
var font = pos.font | |
if pos.character in font.glyphs: | |
var glyph = font.glyphs[pos.character] | |
var glyphOffset: Vec2 | |
let image = font.getGlyphImage(glyph, glyphOffset, subPixelShift=pos.subPixelShift) | |
var destRect = sdl2.rect( | |
cint pos.rect.x + glyphOffset.x, | |
cint pos.rect.y + glyphOffset.y, | |
cint image.width, | |
cint image.height | |
) | |
sdl2.copy(render, image.texture, nil, addr destRect) | |
when true: | |
# draw single letter at a time caching the letters | |
var layout = font.typeset(""" | |
Two roads diverged in a yellow wood, | |
And sorry I could not travel both | |
And be one traveler, long I stood | |
And looked down one as far as I could | |
To where it bent in the undergrowth;""") | |
for pos in layout: | |
var font = pos.font | |
if pos.character in font.glyphs: | |
let key = pos.character & $font.size & $pos.subPixelShift | |
if key notin glyphCache: | |
var glyph = font.glyphs[pos.character] | |
var glyphOffset: Vec2 | |
let image = font.getGlyphImage(glyph, glyphOffset, subPixelShift=pos.subPixelShift) | |
glyphCache[key] = GlyphEntry( | |
image: image, | |
texture: image.texture, | |
glyphOffset: glyphOffset | |
) | |
let glyphEntry = glyphCache[key] | |
var destRect = sdl2.rect( | |
cint pos.rect.x + glyphEntry.glyphOffset.x, | |
cint pos.rect.y + glyphEntry.glyphOffset.y, | |
cint glyphEntry.image.width, | |
cint glyphEntry.image.height | |
) | |
sdl2.copy(render, glyphEntry.image.texture, nil, addr destRect) | |
echo epochTime() - start | |
render.present | |
destroy render | |
destroy window |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://dl3.pushbulletusercontent.com/SvLx0axaQYNtaLvxABCg4cCagcffh2oX/Moon%20Bold.otf