News for hackers. Stuff that matters.
Re:Minimal Cross-Platform Graphics by unwind on Tuesday January 24, 2023 @05:20AM (#34501855)
Very cool!

This example I think needs editing:

    memset(f->buf, rgb, f->width*f->height*sizeoof(uint32_t));
it's described as a way to "fill the complete framebuffer with a solid colour", but 'rgb' is shown in the previous snipped to be 'uint32_t'. That is not how 'memset()' [1] works, it will only use the least significant 8 bits of the 'int'-typed value argument. So it would use whatever blue bits where in 'rgb', only.

For clearing (all zero) it's usable, and then I would recommend writing it as:

    memset(f->buf, 0, f->width * f->height * sizeof *f->buf);
this both fixes the typo ("sizeoof" sounds like a C lecturer being punched in the guts), and avoids duplicating the type and instead inferring it from the actual object, which is more DRY and a style I've been advocating for ... years.

[1]: https://man7.org/linux/man-pages/man3/memset.3.html

Re:Minimal Cross-Platform Graphics by aa-jv on Tuesday January 24, 2023 @06:19AM (#34502231)
Fine, but at least fix this bug:

    .. (sizeof *f->buf) ..
Because otherwise thats one stray space char away from a bad day ..
Re:Minimal Cross-Platform Graphics by dahart on Tuesday January 24, 2023 @10:34AM (#34504840)
Or ... sizeof(*f->buf)? I’d forgotten that sizeof even allows dropping the parens, but isn’t that asking for trouble? “sizeof a <op> b” is sometimes parsed as “sizeof(a) <op> b” and sometimes as “sizeof(a <op> b) depending on the precedence of operator <op>. Is there any downside to always using parens and writing sizeof() like a function call?
Re:Minimal Cross-Platform Graphics by unwind on Tuesday January 24, 2023 @01:18PM (#34507617)
Sure, many people like that style, I don't.

But no, I don't think there are any downsides, but there is this notion that things should look like what they are, which is popular in C.

With parens, I would suggest spaces to make it

    sizeof (*f->buf)
that makes it look less like a function call in many styles.