Skip to content

SDL2 ttf creates font textures with random colors #22

Description

@greenfork

I use official nim library https://github.com/nim-lang/sdl2/blob/master/src/sdl2/ttf.nim to create font textures.

In my config.nims I have

switch("passL", "-I/usr/include/SDL2")
switch("passL", "-L/usr/lib")
switch("passL", "-lSDL2")
switch("passL", "-lSDL2_image")
switch("passL", "-lSDL2_ttf")
switch("passL", "-lSDL2_mixer")

and it works fine so far.

This is condensed code of how I create font texture:

type
  Texture* = object
    data*: TexturePtr
    width*, height*: int

proc textureFromText*(r: RendererPtr; font: FontPtr, text: string,
                      fg: tuple[r,g,b,a: int]): Texture =
  let surf = font.renderTextSolid(
    text.cstring,
    (fg.r.uint8,fg.g.uint8,fg.b.uint8,fg.a.uint8)
  )
  assert(surf != nil, &"Failed to render text: {text}")

  let texture = r.createTextureFromSurface(surf)
  assert(texture != nil, &"Failed to create texture from text: {text}")
  result.data = texture
  result.width = surf.w
  result.height = surf.h
  freeSurface(surf)

let ggf = render.textureFromText(headerFont, "Gotta go fast!", (0xFF,0xFF,0xFF,0xFF))
render.copy(ggf.data, nil, nil)

When I render this texture on screen, each time it is different. I can compile once and run multiple times - texture differs in all 4 channels each time. Can it be the uint8 conversion? I have no idea what this might be.

Versions:
nim 1.2
nlvm 1.2, compiled without static linking with llvm
SDL2 2.0.12
llvm9 9.0.1

PS in description it says that nlvm has faster compile times. For this program nim compiles in 1.2 seconds, nlvm in 5.4 seconds. Is it supposed to be like this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions