I have the following code that loads a png into a texture:
import sdl2, sdl2/image
var
win: WindowPtr
ren: RendererPtr
ladybug: TexturePtr
discard sdl2.init(INIT_EVERYTHING)
win = createWindow("Hello World!", 100, 100, 740, 580, SDL_WINDOW_SHOWN)
ren = createRenderer(win, -1, RendererAccelerated or RendererPresentVsync)
ladybug = loadTexture(ren, "ladybug.png")
var ladybugpos:Rect
ladybugpos.x = 0
ladybugpos.y = 0
ladybugpos.w = 80
ladybugpos.h = 60
discard ren.clear()
discard ren.copy(ladybug, nil, ladybugpos.addr)
ren.present()
delay(10000)
I'd like to use SDL_RenderCopyEx that in nim is called copyEx to copy the texture ladybug and create many clones like 10,000 of them. How do I create these objects/pointers dynamically and how do I delete them once I don't need anymore
Some theory: http://gameprogrammingpatterns.com/flyweight.html
is there a way around to avoid having to write it like this ?
diamonds : array[1..13, array[1..2, Rect]]
for x in 1..13:
diamonds[x][1].x = 0
diamonds[x][1].y = 0
diamonds[x][1].w = 120
diamonds[x][1].h = 160
diamonds[x][2].x = cint (card_w * x)
diamonds[x][2].y = card_h * 2
diamonds[x][2].w = card_w
diamonds[x][2].h = card_h
copy(ren, cards, diamonds[y][2].addr, diamonds[y][1].addr )
in this example I am considering a card game where I store in the first column 13 rows with the size and location of the destination texture and in the second 13 rows I store the size and location the source texture
Why do you need 13 similar [x][1].*?
I'd rather do something like this:
proc draw(cards: TexturePtr, rank: range[0..12], suit: range[0..3], x, y: int) =
var
dstRect = Rect(x: x, y: y, w: card_w, h: card_h)
srcRect = Rect(x: card_w * rank, y: card_h * suit, w: card_w, h: card_h)
copy(ren, cards, dstRect.addr, srcRect.addr)
the reason for storing the width and height of each card in an array was to avoid calculating the position of where to get each card every time each card is displayed
it tells me : object constructor needs an object type
Try it now. Also, it'll be more useful if you would tell exact string on which error is occured.
to avoid calculating the position of where to get each card every time each card is displayed
You may also note, that such calculations generally take only a few ns (1E-9 s) in Nim and a few byte in code size. So as in C that cost is negligible. (I Python it may be some us, but for a card game even that should be not critical.)
proc draw(cards: TexturePtr, rank: range[0..12], suit: range[0..3], x, y: int) =
var
dstRect = Rect(x: x, y: y, w: card_w, h: card_h)
srcRect = Rect(x: card_w * rank, y: card_h * suit, w: card_w, h: card_h)
copy(ren, cards, dstRect.addr, srcRect.addr)
(3, 19) Error: object constructor needs an object type
nyl, it seems Rect in official wrapper is tuple and not object. Just use
… = proc rect(x, y, w, h)
then.