Contenu connexe Similaire à Windows Mobile 6 遊戲開發入門 Similaire à Windows Mobile 6 遊戲開發入門 (20) Plus de Chui-Wen Chiu (20) Windows Mobile 6 遊戲開發入門3. Windows Mobile行動裝置平台
Microsoft Visual Studio
Native Managed Server side
MFC ATL .NET Compact ASP .NET
Win32 Framework Mobile Controls
Software Platform (APIs)
Device Management Communication Presentation Data Access Multimedia
Remote API Connection Manager Home Screen SQL CE GAPI
Configuration* TAPI* User Interface/Shell ADO CE Camera
Bluetooth SMS* HTML Control XML D3DM
Security MAPI* DOM CE DB Location*
Notif Broker* POOM* OLE DB
Pocket Watson ActiveSync*
Windows Mobile Windows CE CE DB Pocket Outlook
ActiveSync Pocket Internet Explorer
SQL CE Windows Media Player
Drivers
Device Processor Display Radio GSM/GPRS WiFi
Hardware Memory USB or Serial CDMA/1xRTT Bluetooth
* = Managed wrappers
5. Windows Mobile遊戲開發技術
GDI/GDI+
功能薄弱, 以Bitmap類別的功能為例,
MakeTransparent方法和SetResolution方法都
無法使用, 而且也不能呼叫繼承自Image類別
的RotateFlip方法
GAPI/GAPINet
支援高效能、而且即時的繪圖功能
GAPIDraw/GAPIDrawNet
適合Pocket PC 2003與Windows Mobile 5.0或
更高版本的行動裝置的遊戲程式設計支援
DirectX/Managed DirectX/DirectX Mobile
支援Direct3D三維繪圖引擎
9. GAPI的基本功能
函數名稱 說明
GXBeginDraw 執行繪圖前的準備工作,傳回螢幕記憶
體的位址,而不是DC的Handle(HDC)。
GXCloseDisplay 結束繪圖的動作,釋放GAPI所佔據的螢
幕資源。通常在應用程式結束前呼叫。
GXCloseInput 釋放GAPI佔據的按鍵資源。
GXEndDraw 和GXBeginDraw成對使用,可以在完成
一個完整的繪圖動作後呼叫,忘記呼叫
GXEndDraw會造成記憶體很快就不敷使
用。
GXGetDefaultKeys 提供建議的按鍵資訊。
GXGetDisplayProperties 查詢螢幕顯示記憶體的相關資訊並存放
到GXDisplayProperties結構的變數中。
10. GAPI的基本功能(續)
函數名稱 說明
GXIsDisplayDRAMBuffer 判斷顯示螢幕是否標準的函數。
GXOpenDisplay 應用程式在執行繪圖動作之前必須先
呼叫這個函數,取得顯示裝置的控制
權,並拒絕其他的應用程式使用螢幕。
GXOpenInput 取得按鍵的控制權, 並將按鍵操作引發
的訊息傳給應用程式, 而不是傳給
Windows CE的Shell, 避免使用者在執
行GAPI應用程式的時候啟動其他的應
用程式。
GXResume 恢復被GXSuspend暫停的GAPI繪圖動
作,應用程式可以在收到
WM_SETFOCUS訊息時呼叫這個方法。
GXSetViewport 定義GAPI的視界(View Port)。
GXSuspend 暫停繪圖工作, 應用程式可以在收到
WM_KILLFOCUS訊息時呼叫此方法。
12. 認識DirectX
Microsoft Windows平台支援多媒體, 遊戲開
發, 以及影音播放的技術, 其內容包括:
Direct3D, DirectDraw, DirectMusic, DirectPlay,
DirectSound, DirectInput等等
Managed DirectX (簡稱MDX)
支援利用Visual Basic .NET或C#程式語言使用
DirectX功能控制現代化的顯示卡
Managed DirectX版本:
1.1版, 2.0版, 新版已改名為XNA
DirectX Mobile
支援Windows Mobile行動裝置的DirectX
13. DirectX的沿革
1992年Servan Keondjian創立
RenderMorphics公司, 設立研究3D圖學的實
驗室, 名稱Reality Lab
Microsoft於1995年購併RenderMorphics公
司, 為Windows 95加入3D圖學功能, 即第一
個版本的DirectX
目前DirectX的最新版本為Windows Vista支
援的DirectX 10.0
DirectX 11.0目前正在研發中…
14. DirectX的重要功能
Direct3D
提供3D繪圖和動畫功能, 可以發揮顯示卡的硬
體加速功能
DirectDraw
提供2D繪圖, 與直接存取顯示記憶體的能力
DirectInput
支援各種輸入裝置控制, 包括鍵盤, 滑鼠, 搖桿,
控制板, 方向盤, 腳踏板等等
DirectSound
支援播放與錄製WAV格式的聲音檔案, 提供立體
聲與立體音效. 需要進階聲音播放控制功能時請
用DirectMusic
15. DirectX的重要功能 (續)
DirectMusic
播放功能更強大的DirectSound. 支援動態音軌,
3D聲音定位, 以及硬體加速, 支援MIDI, WAV格
式, 和DirectMusic Producer錄製的聲音檔案
DirectPlay
支援網路遊戲開發, 包括即時語音會議
支援GPRS, Bluetooth, Wi-Fi連線方式
DirectShow
支援聲音/影像錄製與播放, 支援TV電視卡
DirectSetup
支援將DirectX安裝到使用者的電腦
16. DirectX For Windows Mobile
Direct3D Mobile
支援Windows Mobile的Direct3D
需要Microsoft Windows Mobile 5.0或更高的版本
DirectDraw
可以利用Platform Invocation技術進行呼叫
DirectInput
目前Windows Mobile 5.0/6.0皆未支援
DirectInput相關的功能, 行動裝置應用程式可以
依賴GAPI進行輸入控制, 或直接處理鍵盤事件
DirectSound
請使用Waveform Audio API代替
17. 認識Direct3D Mobile
支援行動裝置的Direct3D Mobile的功能
DirectX 9的功能精簡版, 功能接近DirectX 8
提供高效能3D繪圖和動畫功能
支援3D遊戲製作
支援全螢幕/非全螢幕顯示模式
體積精巧, 模組化
支援行動裝置的主流硬體與驅動程式
數值資料支援
支援IEEE 32-bit浮點數
支援16.16 Fixed Point數值, 解決部分行動裝置處理
器未提供浮點運算的問題
20. DirectX常用的名詞與型態
Device:負責繪製內容, 播放聲音的型態
DirectDraw Device, Direct3D Device,
DirectSound Device, …
Caps:描述Direct3D Device的能力
PresentParameters :描述Direct3D Device的
特性
Surface:存放欲顯示的內容, 可以用來存取
Video記憶體或主記憶體
SurfaceDescription:描述Surface的資訊
SurfaceCaps:描述Surface的能力
24. 建立DirectX裝置(Device) – C#
//設定Direct3D Mobile device需要的參數
PresentParameters sPresentParameters = new
PresentParameters();
sPresentParameters.Windowed = true;
sPresentParameters.SwapEffect = SwapEffect.Discard;
sPresentParameters.BackBufferFormat = Format.Unknown;
sPresentParameters.EnableAutoDepthStencil = true;
sPresentParameters.AutoDepthStencilFormat =
DepthFormat.D16;
//建立Direct3D Mobile device
m_oDevice = new Device(0, DeviceType.Default, this,
CreateFlags.None, sPresentParameters);
25. 建立DirectX裝置(Device) – VB
‘設定Direct3D Mobile device需要的參數
Dim sPresentParameters As PresentParameters = New _
PresentParameters()
sPresentParameters.Windowed = True
sPresentParameters.SwapEffect = SwapEffect.Discard
sPresentParameters.BackBufferFormat = Format.Unknown
sPresentParameters.EnableAutoDepthStencil = True
sPresentParameters.AutoDepthStencilFormat = _
DepthFormat.D16
'建立Direct3D Mobile device
m_oDevice = New Device(0, DeviceType.Default, Me, _
CreateFlags.None, sPresentParameters)
26. 查詢Device的能力
//查詢Device的能力
Caps m_sDeviceCaps = m_oDevice.DeviceCaps;
//判斷Device是否支援 floating point values
if (m_sDeviceCaps.DeviceCaps.NativeFloat)
{
Trace.WriteLine("Device Supports Floating Point Values");
}
//判斷Device是否支援directional lights
if (m_sDeviceCaps.VertexProcessingCaps.SupportsDirectionalLights)
{
Trace.WriteLine("Device Supports Directional Lights");
}
27. 建立Vertex Buffer
//建立Vertex Buffer
m_oVB = new VertexBuffer(
typeof(CustomVertex.PositionNormal), 100,
m_oDevice, 0,
CustomVertex.PositionNormal.Format,
eD3DMPool);
//填入欲顯示的資料到VerTex Buffer
PopulateVertexBuffer(m_oVB);
28. 顯示VerTex Buffer的內容
m_oDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
Color.Bisque, 1.0F, 0);
m_oDevice.BeginScene();
SetupMatrices();
SetupLights();
SetupMaterials();
m_oDevice.SetStreamSource(0, m_oVB, 0);
m_oDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 98);
m_oDevice.EndScene();
m_oDevice.Present();
30. 輸入控制
兩種選擇:
呼叫GAPI功能
處理事件
MouseUp, MouseDown, MouseMove
KeyUp, KeyDown, KeyPress
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
// Up
…
}
33. 聲音控制
使用Waveform Audio API
利用Platform Invocation技術
宣告
[DllImport("CoreDll.dll", EntryPoint = "PlaySound",
SetLastError = true)]
private extern static bool PlaySound(string szSound, IntPtr
hMod, int flags);
呼叫
PlaySound(Path.Combine("Audios", fileName), IntPtr.Zero,
(int)(SND.SYNC | SND.FILENAME));
37. 顯示遊戲背景(二)
為Form1製作Load事件處理程序:
string AppPath = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
_graphics = new DirectDrawGraphics(this, CooperativeFlags.Fullscreen,
BackbufferMode.Any);
_screen = _graphics.ScreenSurface;
_main = _graphics.CreateSurfaceFromImage(new
Bitmap(Path.Combine(AppPath, "imagesinvasion.png")));
destScreenRec = new tagRECT(0, 0, _screen.Width, _screen.Height);
srcScreenRec = new tagRECT(0, 0, _main.Width, _main.Height);
_screen.Draw(ref destScreenRec, _main, ref srcScreenRec,
BltFlags.DDBLT_WAITNOTBUSY);
_graphics.Flip();
40. 顯示太空船(二)
於Form1的Load事件處理程序加入:
_ship = _graphics.CreateSurfaceFromImage(new
Bitmap(Path.Combine(AppPath, "imagesship.png")));
_ship.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);
destShipRec = new tagRECT(0, _screen.Height - _shipSize -
_scoreBar, _shipSize, _screen.Height - _scoreBar);
srcShipRec = new tagRECT(0, 0, _shipSize, _shipSize);
_screen.Draw(ref destShipRec, _ship, ref srcShipRec,
BltFlags.DDBLT_KEYSRC);
_graphics.Flip();
43. Primary Buffer與Back Buffer
利用Flip方法切換Primary Buffer與Back
Buffer可以提升顯示速度
傳統的顯示法 – 複製資料到Video Memory
DirectX的顯示法 – 切換Video Memory
44. 移動太空船
為Form1加入keyDown事件處理程序
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Left)) {
if (destShipRec.left > 0 ) {
destShipRec.left -= 10;
destShipRec.right -= 10;
}
}
if ((e.KeyCode == System.Windows.Forms.Keys.Right)) {
…
}
}
46. 發射雷射光 (一)
於存放圖片的images資料夾, 加入當做雷射
光的Laser.png, 將圖的[複製到輸出目錄]屬性
設定成:有更新時才複製
為Form1類別加入以下的變數宣告:
private Surface _laser;
tagRECT srcLaserRec, destLaserRec;
const int _LaserSize = 20;
const int GunPosition = 15;
bool bShowLaser=false;
47. 發射雷射光 (二)
於Form1的Load事件處理程序加入:
_laser = _graphics.CreateSurfaceFromImage(new
Bitmap(Path.Combine(AppPath, "imagesLaser.png")));
_laser.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);
為Closing事件處理程序加入:
_laser.Dispose();
48. 發射雷射光 (三)
於Form1類別的Render方法加入:
if (bShowLaser) {
_screen.Draw(ref destLaserRec, _laser, ref srcLaserRec,
BltFlags.DDBLT_KEYSRC);
_graphics.Flip();
if (destLaserRec.top > destScreenRec.top) {
destLaserRec.top -= 10;
destLaserRec.bottom -= 10;
}
else {
bShowLaser = false;
}
49. 發射雷射光 (四)
修改Form1類別的KeyDown事件處理程序:
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
destLaserRec = new tagRECT(destShipRec.left +
GunPosition, destShipRec.top, destShipRec.left
+ GunPosition + _LaserSize, destShipRec.top +
_LaserSize);
srcLaserRec = new tagRECT(0, 0, 20, 20);
bShowLaser = true;
}
50. 加入幽浮 (一)
於存放圖片的images資料夾, 加入當做幽浮
的Ufo.png, 將圖的[複製到輸出目錄]屬性設
定成:有更新時才複製
為Form1類別加入以下的變數宣告:
private Surface _ufo;
tagRECT srcUfoRec, destUfoRec;
const int _UfoSize = 70;
Random rndUfo=null;
為Closing事件處理程序加入:
_ufo.Dispose();
51. 加入幽浮 (二)
於Form1的Load事件處理程序加入:
_ufo = _graphics.CreateSurfaceFromImage(new
Bitmap(Path.Combine(AppPath, "imagesUfo.png")));
_ufo.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);
rndUfo=new Random();
destUfoRec = new tagRECT(0, 0, _UfoSize, _UfoSize);
srcUfoRec = new tagRECT(0, 0, _UfoSize, _UfoSize);
52. 加入幽浮 (三)
於Form1類別的Render方法加入:
int left=rndUfo.Next(0, _screen.Width+1-_UfoSize);
int top=rndUfo.Next(0, _screen.Height+1-_UfoSize);
destUfoRec=new tagRECT(left, top, left+_UfoSize,
top+_UfoSize);
_screen.Draw(ref destUfoRec, _ufo, ref srcUfoRec,
BltFlags.DDBLT_KEYSRC);
_graphics.Flip();
53. 調整幽浮移動的速度
將顯示幽浮的程式碼改成:
int Direction = rndUfo.Next(0, 2);
if (Direction == 0) {
if (destUfoRec.right < destScreenRec.right) {
destUfoRec.left += 5;
destUfoRec.right = destUfoRec.left + _UfoSize;
}
}
else { //省略
}
_screen.Draw(ref destUfoRec, _ufo, ref srcUfoRec,
BltFlags.DDBLT_KEYSRC);
_graphics.Flip();
55. 幽浮投彈 (一)
於存放圖片的images資料夾, 加入當做炸彈
的bomb.png, 將圖的[複製到輸出目錄]屬性
設定成:有更新時才複製
為Form1類別加入以下的變數宣告:
private Surface _bomb;
tagRECT srcBombRec, destBombRec;
const int _BombSize = 20;
int BombFrameNo = 0;
DateTime dtBomb;
bool bShowBomb = false;
bool bShowUfo=true;
bool bShowShipExplode=false;
56. 幽浮投彈 (二)
於Form1的Load事件處理程序加入:
_bomb = _graphics.CreateSurfaceFromImage(new
Bitmap(Path.Combine(AppPath, "imagesbomb.png")));
_bomb.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);
srcBombRec = new tagRECT(0, 0, _BombSize, _BombSize);
dtBomb=DateTime.Now;
為Closing事件處理程序加入:
_bomb.Dispose();
57. 幽浮投彈 (三)
於Form1類別的Render方法加入:
if (bShowUfo) {
if (DateTime.Now.Subtract(dtBomb).Seconds > 3) {
bShowBomb = true;
int left=destUfoRec.left+(_UfoSize-_BombSize)/2;
destBombRec = new tagRECT(left, destUfoRec.bottom,
left + _BombSize, destUfoRec.bottom +
_BombSize);
dtBomb = DateTime.Now;
}
}
if (bShowBomb && !bShowShipExplode) { //省略
}
59. 命中檢查 (二)
修改顯示太空船的語法:
if (bShowShip)
{
_screen.Draw(ref destShipRec, _ship, ref srcShipRec,
BltFlags.DDBLT_KEYSRC);
_graphics.Flip();
}
於Form1類別的Render方法加入
CheckCollision方法呼叫:
CheckCollision();
60. 命中檢查 (三)
為Form1類別加入CheckCollision方法:
private void CheckCollision() {
Rectangle rtShip = new Rectangle(destShipRec.left,
destShipRec.top, _shipSize, _shipSize);
int BombCenterX = (destBombRec.left +
destBombRec.right) / 2;
int BombCenterY = (destBombRec.top +
destBombRec.bottom) / 2;
Rectangle rtBomb = new Rectangle(destBombRec.left-5,
destBombRec.top-5,10, 10);
Rectangle rtUfo = new Rectangle(destUfoRec.left,
destUfoRec.top, _UfoSize, _UfoSize);
//省略
}
62. 顯示太空船爆炸效果 (二)
於Form1的Load事件處理程序加入:
_shipexplode = _graphics.CreateSurfaceFromImage(new
Bitmap(Path.Combine(AppPath,
"imagesshipexplode.png")));
_shipexplode.SetColorKey(Color.Black,
ColorKeyFlags.SRCBLT);
srcShipExplodeRec = new tagRECT(0, 0, _shipSize, _shipSize);
65. 顯示幽浮爆炸效果 (二)
於Form1的Load事件處理程序加入:
_ufoexplode = _graphics.CreateSurfaceFromImage(new
Bitmap(Path.Combine(AppPath,
"imagesufoexplode.png")));
_ufoexplode.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);
srcUfoExplodeRec = new tagRECT(0, _UfoSize, _UfoSize,
_UfoSize+_UfoSize);
67. 加入音效 (一)
為專案建立存放音效檔案的audios資料夾, 於
audios資料夾加入laser.wmv, bomb.wmv,
explosion.wmv, 將音效檔案的[複製到輸出目
錄]屬性設定成:有更新時才複製
於Form1類別加入:
[DllImport("CoreDll.dll", EntryPoint = "PlaySound",
SetLastError = true)]
private extern static bool PlaySound(string szSound, IntPtr
hMod, int flags);
private enum SND {
SYNC = 0x0000,
//省略
}
68. 加入音效 (二)
加入發射雷射音效:
PlaySound(Path.Combine("audios", "laser.wmv"), IntPtr.Zero,
(int)(SND.SYNC | SND.FILENAME));
加入投擲炸彈音效:
PlaySound(Path.Combine("audios", "bomb.wmv"),
IntPtr.Zero, (int)(SND.SYNC | SND.FILENAME));
加入命中太空船和幽浮音效:
PlaySound(Path.Combine("audios", "explosion.wmv"),
IntPtr.Zero, (int)(SND.SYNC | SND.FILENAME));
71. 好用的DirectX開發輔助工具
Audio Console
DirectX Caps Viewer
DirectX Console Panel
DirectX Error Lookup
DirectX Texture Tool
DirectX Viewer
Game Definition File Editor
Microsoft Cross-Platform Audio Creation
Tool(XACT)
PIX for Windows
73. 下載
GAPINet
http://msdn.microsoft.com/en-
us/library/aa446577.aspx
GAPIDraw
http://www.gapidraw.com/download.php
GAPIDrawNet
http://code.google.com/p/gapidrawnet/
DirectX SDK
http://msdn.microsoft.com/en-
us/directx/aa937788.aspx
74. 下載-資策會台北中心
http://www.iiiedu.org.tw/taipei
資訊專區