Nim 和 SDL2 的 Rect 问题
Nim and SDL2 trouble with Rect
我有以下Nim+官方libsdl2 wrapper代码
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
编译时出现错误:
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)
第 20 行相同:
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)
如果在copy()中将pos替换为nil并注释queryTexture(),一切都会好起来的。请帮我解决这个问题。
你的问题是过程需要 ptr
到各自的数据类型,而不是数据本身。例如 ptr cint
是必需的,但您传递的是普通的 cint
。你要做的就是拿cint
的addr
得到一个ptr cint
。例如:
var w = pos.w
var h = pos.h
queryTexture(tex, nil, nil, w.addr, h.addr)
请注意,为了 "take the address",您需要一个 var
类型的变量(有关详细信息,请参阅 )。由于 pos
是 var
,因此 pos.w.addr
和 pos.h.addr
也应该有效。同样,您必须将 pos.addr
作为 copy
.
的最后一个参数
我有以下Nim+官方libsdl2 wrapper代码
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
编译时出现错误:
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)
第 20 行相同:
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)
如果在copy()中将pos替换为nil并注释queryTexture(),一切都会好起来的。请帮我解决这个问题。
你的问题是过程需要 ptr
到各自的数据类型,而不是数据本身。例如 ptr cint
是必需的,但您传递的是普通的 cint
。你要做的就是拿cint
的addr
得到一个ptr cint
。例如:
var w = pos.w
var h = pos.h
queryTexture(tex, nil, nil, w.addr, h.addr)
请注意,为了 "take the address",您需要一个 var
类型的变量(有关详细信息,请参阅 pos
是 var
,因此 pos.w.addr
和 pos.h.addr
也应该有效。同样,您必须将 pos.addr
作为 copy
.