ChanServ changed the topic of #zig to: zig programming language | ziglang.org | be excellent to each other | channel logs: https://irclog.whitequark.org/zig/
steveno has joined #zig
porky11 has quit [Ping timeout: 268 seconds]
suirad has quit [Ping timeout: 256 seconds]
suirad has joined #zig
<daurnimator>
andrewrk: what is your view on aliasing?
<daurnimator>
andrewrk: have you though about no-alias by default?
<ibebrett>
does not assume that parameter is comptime
<ibebrett>
is there a reason why it couldn't? It would help me with using a "child allocator" as an implementation for a debug checking allocator i'd like to create
ibebrett has quit [Ping timeout: 256 seconds]
<suirad>
ibebrett: i made something similar if you need a hand
<forgot-password>
Hey :) I'm currently playing around with OpenGL in zig, but the glShaderSource function is giving me a few problems.
davr0s has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
<forgot-password>
I have a source string of type `[]const u8` and glShaderSource has the signature /Users/hannes/Documents/playground/zig-test/hello.zig:52:29: error: expected type 'type', found 'extern fn(u32, i32
<forgot-password>
And I'm not sure how the get a [*]const [*]const u8 from my source string. I at least got it compile with @ptrCast(const[*] const[*]const u8, &vsrc.ptr), where vsrc is my shader source. It also doesn't crash at runtime, but it doesn't seem like the way to go :p
<Hejsil>
Well, [*]const [*]const u8 smells a lot like a 2 dim array. Sure this argument is suppose to just point to 1 string?
<Hejsil>
"Specifies an array of pointers to strings containing the source code to be loaded into the shader."
<forgot-password>
No it can actually take an array of strings
<forgot-password>
Wait if I do &vsrc.ptr don't I have a pointer to a pointer to a char, which is basically a two-dimensional array of chars or an array of strings?
<Hejsil>
That is true. Sadly @typeOf(&vsrc.ptr) will be *const [*]const u8
<forgot-password>
Thank you, will get back to you soon :)
<forgot-password>
Is the `c"a"` saying that it should be interpreted as a single character and not a string?
davr0s has joined #zig
<Hejsil>
No, it a c string literal. It gives you a null terminated string
<Hejsil>
Which you'll most likely need when calling into C :)
<forgot-password>
Ohh, that's how I do that... Does it work for multiline strings?
<Hejsil>
Yes, just append c to all lines
<Hejsil>
c\\
<forgot-password>
Sorry if I'm bothering you with such basic questions, but the langref.html isn't the most complete documentation I know of :P
<forgot-password>
Would I have to import strlen/build it myself in order to get the length of a c string or is there a std function? Because I can't find any
<Hejsil>
There is std.cstr.len and mem.len (they do the same).
<Hejsil>
mem.len is the generic implementation for getting the length of any null terminated array
<Hejsil>
Ops, sorry. I just read this comment on cstr.len: "Deprecated, use mem.len"
<Hejsil>
mem.len it is
davr0s has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
<daurnimator>
Hejsil: I'm not sure why that exists TBH
<daurnimator>
I feel like mem.indexOf(x, '0'); would be more self explanatory
davr0s has joined #zig
<forgot-password>
Hi, it's me again. I just created a gist where I commented a few lines I need help with. Thanks in advance to anybody who takes a look at it :)
<Hejsil>
Currently, you could do @ptrCast([*]u32, &logLength) instead of declaring the array, but it feels pretty bad to do ptr casts
<Hejsil>
As for allocators
<MajorLag>
If you have an idea the maximum size that you care about for the log, you can just create a static buffer and pass that directly without an allocator: `var staticMemory: [max_size]u8 = undefined;` `c.glShaderInfoLog(shader, std.math.min(logLength, max_size), null, &staticMemory);`. If you'd rather dynamically allocate to ensure you can get the entire log: `var direct_allocator = std.hea.DirectAllocator.init(); var allocator = &direct_alloca
<MajorLag>
there may need to be a pointer cast in there, I've not had to interface with C for a while.
<MajorLag>
Oh, and you'll probably want to free that memory at some point `defer allocator.free(allocatedMemory);` will ensure it is freed at scope exit.
<Hejsil>
Well, was making an example, but you summed it up
steveno has joined #zig
<forgot-password>
Thank you very much, that helps me a lot :)
<forgot-password>
MajorLag, do I need to initialize staticMemory? I updated the gist from earlier (the function of concern is `getShaderInfoLog`) if you want to take a look at it. I'm currently working on a Mac and OpenGL support was deprecated by Apple earlier this year, so I'm not sure whether my problems are caused by that or if they happen due to my code. The latter is obviously much more likely
<MajorLag>
Your buffer can't be const, because you're going to modify it.
<MajorLag>
also you can't return it as a slice, since it is on the stack, it isn't valid outside the scope of the function.
<forgot-password>
I see, that makes sense
<MajorLag>
Two options: move the buffer to the global scope (and make it var) and return a slice of it, or dynamically allocate it, in which case you'll need the allocator to be declared and initialized in the global scope and you'll need to free the memory later.
<forgot-password>
Wouldn't passing the allocator as an argument be a lot cleaner? Or is that not possible?
<Hejsil>
That is the recommended way to do allocations (passing allocators around)
<forgot-password>
I'm gonna keep it global for now... I can still do some cleanup when I got it to work ^^
<forgot-password>
Alright, now I can get the log, but when I try to log it via `warn` it crashes with “unreachable code“. If I put a `warn` after my `c.glGetShaderInfoLog`-call it still gets written, so I'm guessing that's not the problem
<MajorLag>
Does it say which line number and file the unreachable is in?
<MajorLag>
And yes, typically allocators are passed in as the first parameter to functions that need them. You'd still have to declare the allocator itself globally so that it is in scope for the duration of the allocation's lifetime.
<MajorLag>
Well, or in main
<MajorLag>
` @ptrCast([*]u8, &buffer)` allocator.alloc returns a slice, so you don't want `&buffer` here since buffer is already a pointer. You don't even need `@ptrCast` here, you can do `buffer.ptr` to get the `[*]u8` prat of the slice.
<forgot-password>
Oh, that actually caused the crash... It works now :)
<forgot-password>
Is a std.heap.DirectAllocator not a subset of std.mem.Allocator?
Hejsil has quit [Quit: Page closed]
<MajorLag>
std.heap.DirectAllocator is an implementation of the std.mem.Allocator interface. The actuall interface is the `.allocator` part of the initilaized DirectAllocator instance.
<MajorLag>
`var allocator = &direct_allocator.allocator;` <= allocator is of type *std.mem.Allocator.
<MajorLag>
`direct_allocator` is of type std.heap.DirectAllocator.
qazo has quit [Ping timeout: 250 seconds]
Hejsil has joined #zig
<forgot-password>
Thank you everybody, the code now works without a problem :)