You posted it here thinking it's a horror and now you're trying to make it sound like I'm horrified. Honestly if I came across this I'd leave it be happily.
Though if it's not in a header file I might move it into one.
Lmao no problemo, but tbh I'd throw it in a header either way just because I like my macros out of sight and out of mind once they're created. To each their own
They are NOT cool. They're a major pain in the ass because they're a textual replacement instead of manipulating token streams or at the very leas being values and functions called at compile-time.
They might seem temporarily simpler, but we have constexpr for a reason
Everyone saying to optimize it by implementing an O(n2) runtime solution is really weird to me. Ya it’s ugly but what OP has done is infinitely more optimized by being O(1) at compile time. Am I missing something?
I'm not a C expert, can anyone explain why #undefs are at the end? Is this to prevent leakage to other potential translation units? I've just never seen this structure before, even in C land lol
They aren't strictly nessacery, and absolutely nothing would change if I removed them, but it's more just cause I'm not using them anymore and I don't want to just leave them in there. It shouldn't leak since this isnt in a header, so at least that's fine
I guess my question is what is the problem that portion would normally solve (in an ideal scenario, not necessarily in this specific one)? I'm assuming some sort of global scope leak type deal
It doesn't really matter in this case, it's more for 'good practice', like freeing all the resources at the end of the program even though it automatically happens when it exits.
Just create an html page with a javascript canvas, launch it up as a sub process and screenshot the window, crop out the outlines and you have your texture
I did that one time, to make it cleaner I just did something like cc -E file.h > tmp, the I removed annotation with vim magic and copy pasted the hardcoded thing in the file. This I impressive though
this is not horrible at all. A modern approach would be to use constexpr / consteval which is more expressive & flexible at generating compile time data but for such a simple case just using the preprocessor is fine and i'd argue this kind of utilization even falls perfectly under the intended purpose (unlike what some people would say who believe the preprocessor is a hack and should only be used for include-guards and whatnot). You shouldn't use external codegen for such a simple case, the preprocessor IS the languages built in codegen for simple cases such as this.
It's not too much worse than what abomination I did to create tagged unions (Rust's enums) with a pseudo-generic type. It basically generated a brand new typedef-d struct for each type
Just lean into code generation. About 5 lines of Python can output a header file with those definitions. And then it’s easier to change. Integrating it into the build system is not difficult (at least with cmake and bazel, which are the two build systems I have most experience with).
Honestly kinda impressive, for such a trainwreck. Leave it as is, it's essentially a human-readable self-documenting magic number. I'm not sure if it's possible to communicate "alternating blocks of black & purple" in a cleaner way while still having perfect compile-time efficiency, at least not without breaking the C array interface; best alternative I can think of is either something involving a constexpr array of wrappers, or some sort of abomination that masquerades as a raw C array but provides uint8_t operator[](size_t i) { return arr[i / 5]; }... and that last one would be a lot worse than this! xD
this generates a 25 by 25 pixel grid of altenating black and purple squares, so yeah i could do that, but this is honestly just easier to do, and more compact (rather than generating a huge file and #including it at compile time)
Granted, the iteration part is not necessary. But the other thing - macros. Avoid those if you don't really need them. They can easily do stuff that you do not intend them to do.
Define "need." Avoiding macros as a general practice is sensible, but I think sometimes this rule-of-thumb can be applied even when it doesn't make sense.
If a macro solves the problem quickly, simply, and easily, and doesn't do anything you didn't intend for it to do... what's the problem?
The problem is that it might *do* something you didn't intend, but you just aren't realizing it. And that it works for *now* but can in the future easily cause havoc.
Basically you could ask the same question for UB. A program with UB often totally works as the programmer intended. Until it doesn't, and then it sucks to debug.
The classic standard for troublesome macros that look perfectly harmless would be
#define SQUARE(x) x*x
If you see immediately why this an issue then it is most likely because you saw that one in the past. Further, have you ever debugged something where the macro was the issue? That is not pretty.
And seriously, if you don't do *very* tricky stuff, in a lot of cases some simple template function does the job with just a few more lines of code.
C++ is about *control*. If you give it away, you give a strength of the language away.
55
u/Konfituren Apr 09 '26
Honestly don't actually hate it. Could just paste in an actual literal, but this is probably fine.
4
u/MurasakiGames Apr 09 '26
as a purple person myself, you know 255, 0, 255, 255 is hot pink, not purple, right?
2
u/_AscendedLemon_ Apr 09 '26
Why not function that returns given color with 1 five times?
23
u/ivancea Apr 09 '26
Better with constexpr nowadays, but I don't see the issue here. This isn't a complicated or weird macro at all
15
u/dfx_dj Apr 09 '26
One man's horror is another man's genius hack
2
u/JudgeFae Apr 09 '26
Which language is it? Macros and aliases are cool
5
u/skillzz_24 Apr 09 '26
Everyone saying to optimize it by implementing an O(n2) runtime solution is really weird to me. Ya it’s ugly but what OP has done is infinitely more optimized by being O(1) at compile time. Am I missing something?
9
u/MeLittleThing Apr 09 '26
black block generator
#ifndef __ANTIFA__1
u/MistakeIndividual690 Apr 09 '26
Is VA_ARGS a compiler/preprocessor extension? I recall trying to do something like this before and my preprocessor at the time didn’t support it.
1
u/Trainzkid Apr 09 '26
I'm not a C expert, can anyone explain why
#undefs are at the end? Is this to prevent leakage to other potential translation units? I've just never seen this structure before, even in C land lol1
u/MarinoAndThePearls Apr 13 '26
The horror is naming magenta as purple.
3
u/isaycongrats Apr 09 '26
Just create an html page with a javascript canvas, launch it up as a sub process and screenshot the window, crop out the outlines and you have your texture
2
u/un_virus_SDF Apr 09 '26
I did that one time, to make it cleaner I just did something like
cc -E file.h > tmp, the I removed annotation with vim magic and copy pasted the hardcoded thing in the file. This I impressive though2
u/Popular-Light-3457 Apr 09 '26 edited Apr 09 '26
this is not horrible at all. A modern approach would be to use constexpr / consteval which is more expressive & flexible at generating compile time data but for such a simple case just using the preprocessor is fine and i'd argue this kind of utilization even falls perfectly under the intended purpose (unlike what some people would say who believe the preprocessor is a hack and should only be used for include-guards and whatnot). You shouldn't use external codegen for such a simple case, the preprocessor IS the languages built in codegen for simple cases such as this.
2
u/Probable_Foreigner Apr 09 '26
Ah my child you know nothing of my pain. Of Bjarne's never ending torment
1
u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Apr 09 '26
It's not too much worse than what abomination I did to create tagged unions (Rust's enums) with a pseudo-generic type. It basically generated a brand new typedef-d struct for each type
1
u/tudorb Apr 09 '26
Just lean into code generation. About 5 lines of Python can output a header file with those definitions. And then it’s easier to change. Integrating it into the build system is not difficult (at least with cmake and bazel, which are the two build systems I have most experience with).
1
u/theunixman Apr 09 '26
Look at the history of the IOCCC for some OG bangers
1
u/UltimatePeace05 Apr 09 '26
This is, honestly, fine imo.
I would personally just generate the data at runtime or, if I had access, use #embed, but this works just as well and is quite clever, nice!
1
u/Far-Passion4866 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Apr 09 '26
WHY
1
u/conundorum Apr 11 '26 edited Apr 11 '26
Honestly kinda impressive, for such a trainwreck. Leave it as is, it's essentially a human-readable self-documenting magic number. I'm not sure if it's possible to communicate "alternating blocks of black & purple" in a cleaner way while still having perfect compile-time efficiency, at least not without breaking the C array interface; best alternative I can think of is either something involving a
constexprarray of wrappers, or some sort of abomination that masquerades as a raw C array but providesuint8_t operator[](size_t i) { return arr[i / 5]; }... and that last one would be a lot worse than this! xD1
u/Infinite_Self_5782 Apr 11 '26
this is acceptable
not the best, but acceptable
1
u/bem981 Apr 12 '26
I had to do it XD
148
u/[deleted] Apr 09 '26
[removed] — view removed comment