Must use copyMem when I create and init it, Very troublesome
const
ATLAS_WIDTH = 128
ATLAS_HEIGHT = 128
var atlas_texture : array[ATLAS_WIDTH * ATLAS_HEIGHT, byte]
let atlas_texture_src = [ # it only 128 * 120 byte
0x00.byte, 0x00, 0x22, 0x11, 0x00, 0x33, 0x11, 0x22, ... ]
copyMem(atlas_texture[0].addr, atlas_texture_src[0].addr, atlas_texture_src.len)
Want easy init T_T like this
var atlas_texture : array[ATLAS_WIDTH * ATLAS_HEIGHT, byte] = [ 0x0 ... # these val only 128 * 120, hope after val all 0x0 ]
]
2nd question: Some c function is
void pp(const int* x) # pp( (int[]){11, 22, 33} )
generator nim is
proc pp(x: ptr int)
# if want use it must
var a = [11.int,22,33]
pp( cast[ptr int](a[0].addr)
# I want easy, so test some type but all error
pp ([11.int, 22, 33])
# proc pp(nz: openArray[int])
# proc pp(nz: UncheckedArray[int])
# proc pp(nz: array[0..0, int])
#only use them ok, but array haven't easy way to determine, must use array|seq[int], why can't use array[int] or UncheckArray[int]
# proc pp(nz: array|seq[int]) # ok
# now I write tempate use this func, It's ok, I think is best way
template pp(nz: untyped) =
let t = nz
pp(cast[ptr int](t[0].addr)
# but more c libs too large not suitable for manual alteration, I've use converter, but converter have bug, can't use ptr type, if have ptr myint, cant converter
Who can tell me have a good idea?
https://github.com/Angluca/mui/blob/master/mui.nim#L287
https://github.com/Angluca/mui/blob/master/demo/mui-sokol/main.nim#L117
https://github.com/Angluca/mui/blob/master/demo/mui-sokol/atlas.nim#L11
I'm a newbie holp nim can directly use c code, not need c2nim convert *.c ( T_T Maybe I'm a slacker)
Don't understand the first question. What's atlas_texture_src? Does it have a mode?
For the second question:
converter toAddr[I, T](a: array[I, T]): ptr T = addr(a[0])
proc f(p: ptr int) =
echo p[]
var arr = [1, 2, 3]
f(arr)
#works on 2.0.2
Oh, I see. I think you can do this
var atlas_texture = static:
var tmp: array[ATLAS_WIDTH * ATLAS_HEIGHT, byte]
let atlas_texture_src = [0x00.byte, 0x00, 0x22, 0x11, 0x00, 0x33, 0x11, 0x22, ...]
for i in 0 ..< len(atlas_texture_src):
tmp[i] = atlas_texture_src[i]
tmp
given that atlas_texture_src is known at the compile-time.atlas_texture_src( array[128*120,byte] ) is a tmp value, copy array[128*120] bytes to array[128*128, byte] value, I don't know better way
just to copy values to altals_texture( array:[128*128,byte] ) Because wan create a array[128*128, byte] and have values
If It's len haven't 128*128 will crash or write more code ( if xxx.len > 128*120: ... T_T )
you 2nd question converter func looks like good, I'll test it, Thank u.
You probably don't want to use array, you probably want to use seq instead. Arrays are usually meant for small, compile time things that can fit on the stack. The 128 * 128 = 16384 can, on some systems, overflow your stack and cause a crash.
I would use seq[int] everywhere instead of openArray[int], UncheckedArray[int], array[0..0, int] and only use them in special cases. This would obliviate the need for pp.
You can also use staticRead or static block to read in data during compile time.
Oops... I think you should use var parameter in that converter, or it might be considered unsafe to take the address, especially with an old version of the compiler.
converter toAddr[I; T](a: var array[I, T]): ptr T
I mean, it's always unsafe to do, but Nim 1.6 or earlier will refuse to compile if trying to take the address of a non-var parameter. Sorry for forgetting about that.
As for the static keyword, it tells the compiler to resolve the expression at the compile-time. In this case it should copy at the compile-time and not at the runtime. If the array is just used as global variable, it's probably fine. But if used as a local variable or an argument, it can still be pushed (and copied) onto the stack and thus cause the stack to overflow, but I'm not sure.
Yeah, Your are right If want shallow copy them add 'var' is better ... It's ok now! Thank u tell me :)
can use {.global.} when you want define global var in local, Like c (static int n)
I think nim can add a new func parameter type array[typed] or upgrade UncheckArray[typed] let it can use, Because UncheckArray[typed]/openArray[typed]/varargs[typed] can't used in this func
xxx( [11.cint, 22, 33] )
# proc xxx( a: array[cint] ) # Ideally with this parameter type
proc xxx( a: array|seq[cint] ) ... # But only use these parameter type now or converter xx[I;T]...
I'm newbie T_T, I just think converters tend to breed a lot of errors so that's why I try to use the converter as little as possible ... Maybe T_T
# xxx.nim
...
converter nb(n: int): bool = n != 0
# myapp.nim
import xxx
var
x, x2: int
b, b2, b3: int
...
while (not (x and x2)) and ((b and b2) or not b3):
... # `and, or, not` can use for SomeInterger|char|bool ...
Converters are (in my opinion, as somebody that approaches this from a more high level POV) something to be used rarely. Not never, just rarely.
And even the, personally I mostly use it on API borders where user inputs a data-type with a more "generic" meaning (like string and int) and convert it to a more meaningful distinct type from there (like type Password = distinct string).
If usage patterns differ in low-level land I cannot say.