Problems with Nim and SDL2 with Rect
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 at compile time:
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 the 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 you replace pos with nil with copy () and comment out queryTexture () everything will be fine, Please help me to solve this problem.
source to share
Your problem is that procs requires ptr
for the respective datatypes, not the data itself. For example required ptr cint
, but you will miss the easy one cint
. You need to do addr
cint
to receive ptr cint
. For example:
var w = pos.w
var h = pos.h
queryTexture(tex, nil, nil, w.addr, h.addr)
Note that you need a type variable to "accept an address" var
(see this question for details ). Since pos
there are var
, they should also work pos.w.addr
and pos.h.addr
. Similarly, you need to take pos.addr
for the last parameter copy
.
source to share