Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL2 ttf creates font textures with random colors #22

Open
greenfork opened this issue Apr 18, 2020 · 0 comments
Open

SDL2 ttf creates font textures with random colors #22

greenfork opened this issue Apr 18, 2020 · 0 comments

Comments

@greenfork
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant