Tips for game development: Graphics, Audio, Sensor bada Developer Day in Seoul Dec 08, 2010 Copyright 2010 Samsung Electronics, Co., Ltd. All rights reserved
Contents Games in bada Audio tips Sensor tips 3D rendering tips Summary *This material is based on bada SDK 1.2.0 2
Games in bada Many games in Appstore Videos or live demos 3
bada features for games Input Output Sensors Weather Graphics Sound Face detection Service-centric Vibration Audio $2 $3 In-App purchasing Remote contents SNS Gateway 4
1 Audio tips
Two ways to play sound General-purpose audio/video playback Player AudioOut Multiple audio playback (max.: 10) Supports streaming* MP3, WMA, MIDI,.. Plays only PCM data *Streaming protocol: RTSP (API 1.0), HTTP (API 1.1) Multiple audio playback (max.: 64) Double buffering is possible 6
Player Step 1: Create a Player instance pplayer = new Player; pplayer->construct(*plistener); // IPlayerEventListener Step 2: Load a file and play it result r = pplayer->openfile(path); pplayer->play(); Step 3: Stop playing and close it pplayer->stop(); pplayer->close(); 7
AudioOut Step 1: Create an AudioOut instance pao = new AudioOut(); pao->construct(*plistener);// IAudioOutEventListener //Prepares the audio output device pao->prepare(audio_type_pcm_s16_le, AUDIO_CHANNEL_TYPE_MONO, SAMPLERATE); Step 2: Write PCM data // Fill buffer with PCM data pao->writebuffer(*poutbuffer); Step 3: Play the sound pao->start(); 8
AudioOut Step 4: Process events void EventListener::OnAudioOutBufferEndReached (Osp::Media::AudioOut &src) { //Write Next PCM data } Step 5: Stop the sound pao->stop(); //or Reset() 9
Audio tips Preferred PCM format Double buffering Sound mixing Handled by platform Handled by application Use the preferred PCM format to reduce the internal operations Field Values Channels Stereo Sample rate 44.1Khz Bit per sample 16-bit 10
Audio tips Preferred PCM format Double buffering Sound mixing Handled by platform Handled by application Reduce latency AudioOut Device Event: Buffer End Reached Application Buffer1 Buffer2 Buffer3 Step 1. Initialize AudioOut Step 2. Write a buffer two times Step 3. Start playback Step 4. Playback is ended Step 5. Event is fired Buffer2 is used by AudioOut Step 6. Write a new buffer 11
Audio tips Preferred PCM format Double buffering Sound mixing Handled by platform Handled by application Sound mixing Combine multiple sounds into one Sound mixing by application reduces the internal resource and event processing 12
Audio tips Preferred PCM format Double buffering Sound mixing Handled by platform Handled by application Headset Platform will change the sound path automatically* Incoming call Player and AudioOut are stopped by the platform** The application should resume them*** * Osp::System::DeviceManager ** IAudioOutEventListener::OnAudioOutInterrupted *** IAudioOutEventListener::OnAudioOutRelased 13
Audio tips Preferred PCM format Double buffering Sound mixing Handled by platform Handled by application Silent mode No event is fired Check the silent mode* Side volume key** Foreground/Background Stop the audio on background *Osp::System::SettingInfo **Osp::Ui::IKeyEventListener 14
2 Sensor tips
Sensor Sensors Type Acceleration sensor Magnetic sensor (compass) Proximity sensor Tilt sensor Properties x, y, z x, y, z 1 (on) or 0 (off) Azimuth, Pitch, Roll SensorManager Manages built-in sensors Polls sensors for data and delivers it to applications at specified intervals. 16
Getting sensor data Step 1: Create a SensorManager Osp::Uix::SensorManager sensormgr; sensormgr.construct(); Step 2: Add a listener sensormgr.getmaxinterval( SENSOR_TYPE_MAGNETIC, interval); sensormgr.addsensorlistener( *this, SENSOR_TYPE_MAGNETIC, interval, false); Step 3: Get sensor data from event-listener* Void OnDataReceived(SensorType sensortype, SensorData& sensordata, result r) { } * Event hander should return immediately, else event delay or event handling problem will occur 17
Multi-touch The number of points depends on the hardware 18
Multi-touch Step 1: Enable multi-touch Touch touch; touch.setmultipointenabled(*this, true); Step 2: Get multi-touch information Touch touch; IList* plist = null; plist = touch.gettouchinfolistn(source); if (plist) { for(int i = 0; i < plist->getcount(); i++ ) TouchInfo *ptouchinfo = static_cast<touchinfo *>(plist->getat(i)); plist->removeall(true); delete plist; } 19
3 3D rendering tips
3D rendering in bada bada supports OpenGL ES 1.1/2.0 1.1 Fixed function hardware 2.0 Fully programmable 3D graphics EGL Manages contexts and drawing surfaces Timer-based rendering loop Rendering on every kind of controls 21
3D rendering in bada Timer-based rendering loop Step 1: Create a timer ptimer= new Timer; ptimer->construct(*this); ptimer->start(time_out); Step 2: Implement event listener class GlesCube11 : public Osp::Base::Runtime::ITimerEventListener void OnTimerExpired(Osp::Base::Runtime::Timer& timer); Step 3: Handle timer event void GlesCube11::OnTimerExpired(Timer& timer){ ptimer->start(time_out); Draw(); // draw something.. } 22
3D rendering in bada Initialize EGL bool GlesCube11::InitEGL() { EGLint numconfigs = 1; EGLint eglconfiglist[] = {/* */}; EGLint eglcontextlist[] = {/* */}; eglbindapi(egl_opengl_es_api); egldisplay = eglgetdisplay( (EGLNativeDisplayType)EGL_DEFAULT_DISPLAY); eglinitialize(egldisplay, null, null); eglchooseconfig(egldisplay, eglconfiglist, &eglconfig, 1, &numconfigs); eglsurface = eglcreatewindowsurface(egldisplay, eglconfig, (EGLNativeWindowType)pForm, null);... } 23
3D rendering in bada Checks OpenGLES version of device void Test1::CheckGLES(void) { String key(l"openglesversion"); String GLESVersion; } SystemInfo::GetValue(key,GLESVersion); AppLog("%ls", GLESVersion.GetPointer()); Result string Device Result Wave/Wave II 1.1/2.0 Wave525 24
3D accelerator of Wave/Wave II POWERVR SGX Unified Shader Architecture Load balancing Mostly identical capabilities of shaders Tile-based Deferred Rendering (TBDR) 25
3D accelerator of Wave/Wave II Related resources Visit http://www.imgtec.com 26
Tips for 3D rendering Texture Bitmaps are added to the surface of geometry Geometry Represents 3D object using geometric entities such as triangles, lines, and so on Shader Program to add special effects to 3D rendering 27
Texture tips Use texture atlas Use mipmap Load all textures on loading time Texture atlas Contains many sub images Use FBO instead of pbuffer Reduces the CPU overhead to load texture 28
Texture tips Use texture atlas Use mipmap Load all textures on loading time Mipmap Image set of different scales Use FBO instead of pbuffer Improves speed and quality Removes cache miss Removes aliasing 29
Texture tips Use texture atlas Use mipmap Load all textures on loading time Use FBO instead of pbuffer Reduces the frame-drops during run-time Loading texture has overhead 30
Texture tips Use texture atlas Use mipmap Load all textures on loading time Frame Buffer Object (FBO) For off-screen rendering Use FBO instead of pbuffer FBO is more efficient than pbuffer 31
Geometry tips Simplify geometry Order 3D geometry optimally Use VBO if possible You do not need a very complex model for mobile devices Use object culling 4M triangles 500 triangles 500 triangles + normal map 32
Geometry tips Simplify geometry Order 3D geometry optimally Traversal order of vertex data V1 V3 V5 Use VBO if possible Use object culling V0 V2 V4 V6 Interleaved attributes are better than separate arrays Interleaved attr.: Separate array: Position Normal Color 33
Geometry tips Simplify geometry Order 3D geometry optimally Use VBO if possible Vertex Buffer Object (VBO) Uploads data(vertex, normal..) to video device Use object culling Get more performance from driver and hardware optimization 34
Geometry tips Simplify geometry Order 3D geometry optimally Object culling Do not render invisible object Use VBO if possible Use object culling Viewpoint View Frustum Improve speed 35
Geometry tips Simplify geometry Order 3D geometry optimally Object culling Do not render invisible object Use VBO if possible Use object culling Viewpoint View Frustum Improve speed 36
Shader tips Prefer vertex shader to fragment shader Rendering pipeline Choose the appropriate precision Evaluator Vertex shader Rasterizati on Fragment shader Frame Buffer Vector and scalar operation Texture Memory Avoid discard operation Pixel Operations The number of vertex is much smaller than the number of fragment 37
Shader tips Prefer vertex shader to fragment shader Choose the appropriate precision Vector and scalar operation Avoid discard operation Consider the tradeoff between speed and quality highp: 32-bit Midiump: 16-bit Reduce the storage space Texture coordinate Lowp : 10-bit Color, normal.. 38
Shader tips Prefer vertex shader to fragment shader Choose the appropriate precision Vector and scalar operation Avoid discard operation Be careful with the order of vector and scalar operations highp vec4 v1, v2; highp float x, y; //Bad operation: 8 v2 = (v1 * x) * y; //Good operation: 5 v2 = v1 * (x * y); 39
Shader tips Prefer vertex shader to fragment shader Choose the appropriate precision discard operation is expensive Use alpha blend instead of discard Vector and scalar operation Avoid discard operation 40
4 Summary
Games in bada 42
We ve learned.. Audio tips Sensor tips 3D rendering tips Texture Geometry Shader bada games 43
Find out more.. Tutorial bada Tutorial.UI and Graphics.pdf bada Tutorial.UI Extension.pdf bada Tutorial.Media.pdf POWERVR SDK SGX 3D Application Recommendations 44
Thank you.