planetis-m / naylib

Yet another raylib Nim wrapper
MIT License
234 stars 10 forks source link

A casted seq fails the assertion check: `getPixelDataSize(width, height, kind(T)) == pixels.len * sizeof(T)` #83

Closed planetis-m closed 1 year ago

planetis-m commented 1 year ago
var pixels = newSeq[byte](width*height*4) # len: 1843200
let tex = loadTextureFromData(cast[seq[Color]](pixels), width, height) # fail here
# vs
var pixels = newSeq[Color](width*height) # len: 460800
let tex = loadTextureFromData(pixels, width, height) # correct

internally the formula `pixels.len * sizeof(Color) is obv wrong in the first case but correct in the second

planetis-m commented 1 year ago

Potential skeleton converter from base type to Color:

template toColorArray(a): untyped =
  toOpenArray(cast[ptr UncheckedArray[Color]](addr a[0]), 0, a.len*sizeof(T) div sizeof(Color) - 1)

or better:

template toColorArray(a: openArray[byte]): untyped =
  toOpenArray(cast[ptr UncheckedArray[Color]](addr a[0]), 0, a.len div sizeof(Color) - 1)