Skip to main content
gamedev.net
🔒 Locked

Preprocessor Magic - Probably Impossible...

Started by Tergiver Mar 31, 2004 at 10:05 PM 5 replies 1.3k views
Original Post
Tergiver
Tergiver
Check out this string literal
t("\xFF")t("\xFF")t("\x00")t("\x00") t("This part is red ") t("\xFF")t("\x00")t("\xFF")t("\x00") t("This part is green");  
Yes, I'm embedding an escape sequence for colorizing strings, but what a mess. I've got a lot of these to generate over the course of my project. Something like this would be cooler
E(FF,00,00) t("This part is red ") E(00,FF,00) t("This part is green");  
But I don't see any way to create the E(r,g,b) #define for that. Is it possible? What are some other ideas for escape sequences that would allow me the full range of color values (0-255) for each color channel? Its still early enough that I could change the sequence itself without too much hassle. Sure, I could create a format string and build a parser
t("#This part is red #This part is green"), 0xFF0000, 0x00FF00  
But that requires all my string literals that have escape sequences to be parsed at run-time. I could build a parser that processed my source files before they are compiled, but that's a nightmare waiting to happen. Not the work to build the parser, that's simple enough, but the potential for failing to get files parsed as the years go by and not being able to check out every instance of a literal as it should appear in my engine. Any thoughts are welcome. * edited for formatting [edited by - Tergiver on March 31, 2004 11:11:33 PM]
hplus0603
hplus0603
You can simplify that:

#define RED "\xff\xff\x00\x00"
#define GREEN "\xff\x00\xff\x00"

#define MYSTRING RED "this is red" GREEN "this is green"



However, loading strings at runtime is not bad. It allows you to drop in a data-driven string editing system, which will allow for easy localization.
enum Bool { True, False, FileNotFound };
Zahlman
Zahlman
quote:
Original post by Tergiver
Check out this string literal
t("\xFF")t("\xFF")t("\x00")t("\x00") t("This part is red ") t("\xFF")t("\x00")t("\xFF")t("\x00") t("This part is green");   


Something like this would be cooler
E(FF,00,00) t("This part is red ") E(00,FF,00) t("This part is green");   





Er, did you try

#define E(a,b,c) t("\xFF")t("\xa")t("\xb")t("\xc")

? :/
Tergiver
Tergiver
quote:
Original post by Zahlman
Er, did you try

#define E(a,b,c) t("\xFF")t("\xa")t("\xb")t("\xc")

? :/



Yes, of course I did

You may have tried it and didn''t get compiler errors, but turn on the /P switch (MSVC) and look at the preprocessor output. It turns E(00,00,FF) into "\xFF\xa\xb\xc".

You can see why no errors, \xa is a valid c++ escape character, but its not the one we want.
Tergiver
Tergiver
The closes I think I can get is using the old #pragma warn trick:

#define _x2(x) #x
#define _x(x) _x2(x)
#define E(a,b,c) t("\xFF") t(_x(a)) t(_x(b)) t(_x(c))

The you type it as:
E(\xFF,\x00,\xFF) 

which isn''t too bad, however it doesn''t work when t(lit) is defined as L ## lit (which it is when using wide character strings). Then it produces:
L"\xFF" L_x(\x00) L_x(\x00) L_x(\xFF) 

instead of
L"\xFF" L"\x00" L"\x00" L"\xFF" 


weird no?
Sneftel
Sneftel
Merry Christmas.


#define RGB_aux(r,g,b) t("\xFF") t(# r) t(# g) t(# b)
#define RGB(r,g,b) RGB_aux(\x ## r, \x ## g, \x ## b)



"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on April 1, 2004 1:51:23 AM]
Tergiver
Tergiver
Woo Hoo!

Thank you!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.