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.
Your problem is that the procs require a
ptr
to the respective data types, not the data itself. For instance aptr cint
is required, but you are passing a plaincint
. What you have to do is take theaddr
of thecint
to get aptr cint
. For example:Note that in order to "take the address" you need a variable of the
var
type (for details on that see this question). Sincepos
is avar
,pos.w.addr
andpos.h.addr
should also work. Similarly you have to takepos.addr
for the last parameter ofcopy
.