#4877 - Link - Sun Apr 13, 2003 10:56 pm
this:
#define RGB(r,g,b) (r+(g<<5)+(b<<10))
??
above all these: (r+(g<<5)+(b<<10)) why i put 5 in g and 10 in b?
who the compiler understand r, g, b are integer or other type of variables?
#4878 - pollier - Sun Apr 13, 2003 11:44 pm
for Nayru's sake, read the tutorials, please!
GBA defines 16-bit colours using 5 bits for each colour component; so R takes the 5 lowest bits, G takes the next 5, and B takes the last 5 before the unused bit. Therefore, G needs to shifted to the left 5 bits, and B 10 bits.
The compiler sticks in whatever variables you provide; as long as you're using integer-like variables or constants the compiler won't have any more trouble than it would have with any old equation. Just don't use a string in there and the compiler will handle the rest for you, 'cause that's what compilers are for.
_________________
(Works for me!)
#4881 - delbogun - Sun Apr 13, 2003 11:59 pm
what you need is some basic c programming knowledge. you should seek up some c programming tutorials and learn.
#4904 - jenswa - Mon Apr 14, 2003 1:16 pm
or you shouldn't ask these questions and just use the function.
#4975 - Link - Wed Apr 16, 2003 11:20 am
i know C++...these things are not used in C++! is it?
futhermore how can i do it?
Quote: |
To achieve good performance in C++: don't use virtual methods, turn off support for exceptions and RTTI (run-time type identification), and use template types only sparingly |
#4984 - crossraleigh - Wed Apr 16, 2003 5:47 pm
They are called macros and are not used much in C++. (Replaced by inline functions) Use them just like any normal function, e.g., RGB(0,0,0).
When you call gcc, it goes through four distinct steps: preprocessing, converting the C to ASM, compiling the ASM, and linking the compiled code. The compiler doesn't need to knows if the arguments are integers because of the preprocessor; #defines, #includes, etc. are taken care of by the preprocessor in step one.
Sorry for any inaccuracies, this is just off the top of my head.
#4988 - Link - Wed Apr 16, 2003 9:07 pm
ok, but so i can use C++ as well as C without differances?
#4990 - tepples - Wed Apr 16, 2003 9:18 pm
Link wrote: |
ok, but so i can use C++ as well as C without differances? |
Almost all valid C is valid C++ as well. For instance, the RGB macro works in both. Here it is again, in the canonical form:
Code: |
#define RGB(r,g,b) ((r)|(g)<<5|(b)<<10) |
When making a macro that acts like a function, always enclose the whole macro in parentheses, and enclose the variables in parentheses as well. This way, you won't run into nasty precedence problems between the preprocessor and compiler.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#4995 - SmileyDude - Thu Apr 17, 2003 12:14 am
the easiest way to explain macros is that when ever you see RGB(0,0,0), the preprocessor will substitute the following in it's place:
Code: |
((0)|(0)<<5|(0)<<10) |
This is similar to a C++ inline call, but the key difference is that there is no type checking. This is both a positive and a negative -- for example, you can use any integer type here -- int, short, long, unsigned/signed. The compiler doesn't care. On the other hand, you can also put in a string pointer...
In general, as a C++ programmer, you don't have to worry about the specifics of macros -- just know that you can pretty much treat them like a normal function call. Any code that you would create would probally just use inline functions -- if you wanted to inline them at all.
_________________
dennis
#5001 - Link - Thu Apr 17, 2003 11:56 am
the GCC compiler support Object Oriented programs? Do it GBA?
#5009 - niltsair - Thu Apr 17, 2003 2:38 pm
Errrr... of course. The code produced isn't some object assembly. Object programming is just a way to program, to better organize logic and data. The end result is the same assembly commands as if you had typed them. It's only really just a way to tell the computer what you want, it then decodes it and produce the same machine intructions.
The only thing is that it is a little less optimized than if you had coded in C or assembly, but you make up for it in clarity of code.