Wiley.Games.on.Symbian.OS.A.Handbook.for.Mobile.Development.Apr.2008 (779888), страница 54
Текст из файла (страница 54)
The spec for OpenGL ES 2.0was only finalized in March 2007, so it’s probably going to be a whilebefore we see support in phones – although having said that, ImaginationTechnologies’ PowerVR SGX GPU series is already OpenGL ES 2.0 compliant, and they have already released an SDK for PC emulation.9 Giventhat standards are always evolving to meet new challenges, it shouldn’tbe long until Symbian smartphones support this amazing technology;Symbian has penciled in Symbian OS v9.5 as the one that delivers areference implementation of OpenGL ES 2.0. I just hope it’s not too long,because when we get hardware accelerated 3D graphics using shaders,it’s going to make a lot of FlashLite games look ‘so 2007’!7.9 Get Your FixWhile it’s all very well to go on about how good all these standards are,I wanted to finish off on a slightly different note.
This section doesn’t talkabout any standard in particular, but I thought it would be appropriate topresent a quick discussion of fixed point numbers, since they play suchan important part in OpenGL ES development.As we’ve already said, many manufacturers don’t put FPUs in handsetsdue to increased production costs and power constraints. Conversely,sometimes FP calculations are emulated in software instead – but theseare too slow to be useful in games; even if they weren’t, their powerconsumption is also prohibitive – a rather interesting Catch-22.So the solution is fixed point mathematics – using integer arithmeticto yield decimal results.
But like everything in life, it comes with a price;what you gain in speed, you lose in precision and in representationalrange. After all, an integer is not a decimal, so clearly an approximaterepresentation needs to be employed.A decimal number consist of two parts – the integral part to the leftof the decimal point, and the fractional part to the right. Now, in afixed point representation, the location of the decimal point is fixed(wow – how insightful). So if you have a number made up of N bits9The SDK can be found at www.imgtec.com/PowerVR/insider/toolsSDKs/KhronosOpenGLES2xSGX.GET YOUR FIX235and you use M bits for the integral part, you have N-M bits left over torepresent the fractional part.You’ll also want to be able to represent negative numbers so the integral portion is in fact a 2’s complement number with the most significantbit used as the sign bit.
If we use a 32-bit integer and reserve the bottom16 bits for the fractional part, we say that we have either a 16.16 fixedpoint number or an S15.16 fixed point number (where the ‘S’ indicatesthe sign bit).Now let’s see what this looks like. In fixed point arithmetic, all the digitsto the right of the ‘decimal’ point are multiplied by negative exponentsof the base.
Sounds scary, so check this out this representation of π inbase 10:(3.14159)10 = 3 * 100 + 1 * 10−1 + 4 * 10−2 + 1 * 10−3 + 5 * 10−4 + 9 * 10−5= 3 +0.1 +0.04 +0.001 +0.0005 +0.00009Makes sense doesn’t it? Now let’s convert another example expressedusing base 2:(110.01000)2 = 1 * 22 + 1 * 21 + 0 * 20 + 0 * 2−1 + 1 * 2−2 + 0 * 2−3 +* 2−4 + 0 * 2−5= 4 +2 +0 +0 +1/4 +0 + 0 + 0= 6.25But wait! This is a binary number, so we can do bit ops, like shifting,on it. So let’s get rid of the fractional part by shifting left by 5. We nowhave 11001000 which is equal to 200. It doesn’t matter whether weuse binary or decimal, the result is the same, so we can now say that6.25 = 200 5.
And, whacko, we’ve represented a decimal number byusing an integer with a bit shift.The final piece of cleverness is that if you’re using a fixed format suchas 16.16, which is what OpenGL ES uses, you know that you’ll always beshifting right or left by exactly 16 bits. This is the same as multiplying ordividing by 216 = 65536 respectively, and should explain the meaningof the following four macros found in utils.h in the S60 SDK OpenGLES examples:#define INT_2_FIXED(__a) (((GLfixed)(__a))<<16)#define FIXED_2_INT(__a) ((GLint)((__a)>>16))#define FLOAT_2_FIXED(__a) ((GLfixed)(65536.0f*(__a)))#define FIXED_2_FLOAT(__a) (((GLfloat)(__a))* (1/65536.0f))You should be able to see for yourself that the normal arithmeticaloperations like addition and multiplication are actually performed asinteger operations, which are obviously a lot faster.236 C/C++ STANDARDS SUPPORT FOR GAMES DEVELOPERS ON SYMBIAN OSHowever, a word of caution is in order here: in non-trivial calculations,the danger lies in temporary results occasionally being inexpressible inthe fixed point format being used – this can happen when there are notenough digits to express the number (overflow), or when the number istoo small to be represented (underflow).Now that you know a bit about fixed point arithmetic, it’s worthremembering that the Symbian OS build tools will build user-side code asThumb unless you tell them not to by using the BUILD_AS_ARM specifierin your bld.inf file, or the ALWAYS_BUILD_AS_ARM keyword in theproject MMP.A key benefit of using the ARM instruction set when using fixedpoint numbers is that you get ‘free’ support for bit shifting using theASR (arithmetic shift right) and the LSL and LSR (logical shift left/right)instructions.
This is because while these instructions are also present in theThumb instruction set, under ARM, the bit-shift ops can be piggy-backedinto another instruction such as MOV, ADD, SUB and so on; therefore, theshift doesn’t have any impact on the instructions’ execution time. Clearly,this can translate into a huge performance gain when you’re heavilyusing fixed point arithmetic for physics simulations and 3D renderingusing libraries such as Mascot or OpenGL ES.Hopefully, this brief introduction will help you use the GLfixed datatype and APIs in OpenGL ES.
For a thorough treatment of fixed pointarithmetic, refer to [2] and [3] in the Further Reading section below.7.10Enough Already!We’ve covered a lot of ground in this chapter, and it doesn’t have codein it to break it up so, if you got this far, well done! You may havealso noticed that, while the chapter title is ‘C/C++ Standards Support forGames Developers on Symbian OS’ there was nothing on support forstandard C++ libraries like the STL.
At the time of writing, there is noofficial support for standard C++ on Symbian OS, although there are anumber of unofficial versions.As regards POSIX, it’s also unlikely that Symbian OS is going tointroduce support for select, fork, exec or signal in the shortor medium term, without a significant re-design of some of the coreoperating system parameters.
Regardless, there is nearly always a wayaround the problem, outlined in detail by the guys who built P.I.P.S.in the Using P.I.P.S. booklet from Symbian Press, found at developer.symbian.com/main/learning/press/books/pdf/P.I.P.S..pdf.Symbian OS has evolved over time to meet a wide variety of industrystandards while maintaining its position as the premier mobile phoneoperating system, which is no mean feat. This support should makeporting games to Symbian OS a fairly easy task both now and in theFURTHER READING237future.
But if you’re considering the use of a middleware solution to easethe effort of porting between different platforms, you should turn to theAppendix on Ideaworks3D’s Airplay SDK, found at the end of this book.7.11 Further Reading[1] Core Techniques and Algorithms in Game Programming, D.
SanchezCrespo Dalmau. New Riders, 2003.[2] Mobile 3D Graphics, A. Malizia. Springer-Verlag, 2006.[3] OpenGL ES Game Development, D. Astle and D. Durnil. PremierPress, 2004.8The N-Gage PlatformPeter Lykke Nielsen and Roland Geisler (Nokia)Jo Stichbury8.1 A Brief History of N-GageMention the N-Gage ‘game deck’ mobile device by Nokia to anyone whohas worked in mobile games, and you’ll find they have an opinion.
TheN-Gage devices continue to polarize opinions – people either love themor hate them!As a mobile device manufacturer, Nokia has always been committedto mobile games. It was the first manufacturer to put a game, Snake, ontoa mobile device, back in 1997. Nokia found that the game caught onand quickly became popular.