Nim and SDL2 trouble with Rect

595 views Asked by At

I have the following Nim+official libsdl2 wrapper code

import sdl2

discard sdl2.init(INIT_EVERYTHING)

let
  window = createWindow("Tic-Tac-Toe", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 390, SDL_WINDOW_SHOWN)
  renderer = createRenderer(window, -1, Renderer_Accelerated or Renderer_PresentVsync or Renderer_TargetTexture)

proc loadImage(file: string): TexturePtr =
  let loadedImage = loadBMP(file)
  let texture = createTextureFromSurface(renderer, loadedImage)
  freeSurface(loadedImage)
  return texture

proc applySurface(x: cint, y: cint, tex: TexturePtr, rend: RendererPtr) =
  var pos: Rect
  pos.x = x
  pos.y = y
  queryTexture(tex, nil, nil, pos.w, pos.h)
  copy(rend, tex, nil, pos)

let
  background = loadImage("resources/bg.bmp")

clear(renderer)
applySurface(0, 0, background, renderer)
present(renderer)

var
  evt = sdl2.defaultEvent
  runGame = true

while runGame:
  while pollEvent(evt):
    if evt.kind == QuitEvent:
      runGame = false
      break

destroy window

And there is an error during compilation:

source.nim(19, 15) Error: type mismatch: got (TexturePtr, nil, nil, cint, cint)
but expected one of: 
sdl2.queryTexture(texture: TexturePtr, format: ptr uint32, access: ptr cint, w: ptr cint, h: ptr cint)

Same for 20th line:

source.nim(20, 7) Error: type mismatch: got (RendererPtr, TexturePtr, nil, Rect)
but expected one of: 
system.copy(s: string, first: int)
system.copy(s: string, first: int, last: int)
sdl2.copy(renderer: RendererPtr, texture: TexturePtr, srcrect: ptr Rect, dstrect: ptr Rect)

If replace pos with nil in copy() and comment queryTexture(), everything will be okay. Please, help me solve this problem.

1

There are 1 answers

0
bluenote10 On BEST ANSWER

Your problem is that the procs require a ptr to the respective data types, not the data itself. For instance a ptr cint is required, but you are passing a plain cint. What you have to do is take the addr of the cint to get a ptr cint. For example:

var w = pos.w
var h = pos.h
queryTexture(tex, nil, nil, w.addr, h.addr)

Note that in order to "take the address" you need a variable of the var type (for details on that see this question). Since pos is a var, pos.w.addr and pos.h.addr should also work. Similarly you have to take pos.addr for the last parameter of copy.