Patched raygui to make it both C and C++ compliant #44
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Explanation
I encountered a lot of errors and warning when using raygui in my C++ project.
I modified raygui.h to make it both C and C++ compliant.
The most significant changes that I made are:
For curly intialisations I created a macro named
CURLY_INIT
which expands toStructName { }
in C++ and(StructName) { }
in C.I had to add a lot of casts to float because in C++ I would get the error "Conversion from 'int' to 'float' requires a narrowing conversion".
I tested by compiling an example in C and one in C++ using the MSVC compiler, but I didn't add my testing setup to the project since I didn't want to mess with it that much.
In my testing setup I also didn't set the warning level manually, I just left it on the default, so there might be more warnings when compiling for C++ on higher warning levels.
I also didn't check for compliance with other compilers, though the changes are made are straightforward enough that I don't think it will be a problem.
Conclusion
Of course the cost of this change is that from now on when using curly brace initialisation we should use the macro
CURLY_INIT
which is not as nice and considering this is a C project this does add a bit of ugliness/friction to the code for the sake of compatibility. I guess we could give a nicer name to the macro.Also people would need to be careful since certain implicit conversions are not allowed in C++ and require casts.
It would also mean that the code would need to be tested with both a C and C++ compiler in order to maintain compatibility.
Personally I use this library in a C++ codebase so I am willing to update it to ensure C++ compatibility when people make breaking changes.