|

楼主 |
发表于 2006-5-8 11:24:00
|
显示全部楼层
Re:站长有空看一下blog系统的问题
没想到和站长碰个正着。
论坛上也不正常,看看下面的文字就知道了,主要问题集中体现在 SIZE 和 CODE 标志上。CODE标志好像不能嵌套。其他标志不知道是否也存在这样的问题。
另外想问一下站长,GameRes最近是否在进行大的调整?亦或遇到了一些困难,我们能否帮上忙?
§3 Writing Endian Independent Code
So we finally get down to the most important part; how does one go about writing code that isn't bound to a certain endian? There are many different ways of doing this; the one I'm going to present here was used in Quake 2, and most of the code you'll see here is somewhat modified code out of the Quake 2 source code. It's mostly geared towards fixing files that are written in a certain endian, since the type casting problem is much harder to deal with. The best thing to do is to avoid casts that assume a certain byte order.
3.1 Step 1: Switching Endians
The first step is to write functions that will automatically switch the endian of a given parameter. First, ShortSwap:
- short ShortSwap( short s )
- {
- unsigned char b1, b2;
- b1 = s & 255;
- b2 = (s >> 8) & 255;
- return (b1 << 8) + b2;
- }
复制代码
This function is fairly straightforward once you wrap your head around the bit math. We take apart the two bytes of the short parameter s with some simple bit math and then glue them back together in reverse order. If you understand bit shifts and bit ANDs, this should make perfect sense. As a companion to ShortSwap, we'll have ShortNoSwap, which is very simple:
- short ShortNoSwap( short s )
- {
- return s;
- }
复制代码
This seems utterly pointless at the moment, but you'll see why we need this function in a moment.
Next, we want to swap longs:
- int LongSwap (int i)
- {
- unsigned char b1, b2, b3, b4;
- b1 = i & 255;
- b2 = ( i >> 8 ) & 255;
- b3 = ( i>>16 ) & 255;
- b4 = ( i>>24 ) & 255;
- return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
- }
- int LongNoSwap( int i )
- {
- return i;
- }
复制代码
LongSwap is more or less the same idea as ShortSwap, but it switches around 4 bytes instead of 2. Again, this is straightforward bit math.
Lastly, we need to be able to swap floats: |
|