windows mobile 6 遊戲開發入門

74
Windows Mobile 6 遊戲開發入門 王寧疆 資深講師 資策會數位教育研究所 MCPD/MCT/MVP

Upload: chui-wen-chiu

Post on 11-Nov-2014

3.619 views

Category:

Documents


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Windows Mobile 6 遊戲開發入門

Windows Mobile 6 遊戲開發入門

王寧疆資深講師資策會數位教育研究所

MCPD/MCT/MVP

Page 2: Windows Mobile 6 遊戲開發入門

大綱

Windows Mobile行動裝置平台

Windows Mobile遊戲開發支援

Windows Mobile遊戲開發技術

認識GAPI, GAPINET, GAPIDraw, 與GAPIDrawNET

認識DirectX, DirectX Mobile, 與Managed DirectX

使用DirectX Mobile的基本動作

Windows Mobile遊戲開發體驗

好用的DirectX開發輔助工具

Page 3: Windows Mobile 6 遊戲開發入門

DisplayUSB or Serial

ProcessorMemory

GSM/GPRSCDMA/1xRTT

Device Hardware

WiFiBluetooth

Radio

Drivers

Windows Mobile CE DBActiveSync

SQL CE

Pocket OutlookPocket Internet ExplorerWindows Media Player

Windows CE

Software Platform (APIs)

Home ScreenUser Interface/ShellHTML ControlDOM

GAPICameraD3DMLocation*

Remote APIConfiguration*Bluetooth

Security

Notif Broker*

Pocket Watson

Connection ManagerTAPI*SMS*MAPI*POOM*

ActiveSync*

MultimediaCommunicationDevice Management Presentation

SQL CEADO CEXMLCE DBOLE DB

Data Access

Native

Win32

MFC ATL

Managed

.NET Compact

Framework

Server side

ASP .NETMobile Controls

* = Managed wrappers

Microsoft Visual Studio

Windows Mobile行動裝置平台

Page 4: Windows Mobile 6 遊戲開發入門

Windows Mobile遊戲開發支援

Page 5: Windows Mobile 6 遊戲開發入門

GDI/GDI+

功能薄弱, 以Bitmap類別的功能為例, MakeTransparent方法和SetResolution方法都無法使用, 而且也不能呼叫繼承自Image類別的RotateFlip方法

GAPI/GAPINet

支援高效能、而且即時的繪圖功能

GAPIDraw/GAPIDrawNet

適合Pocket PC 2003與Windows Mobile 5.0或更高版本的行動裝置的遊戲程式設計支援

DirectX/Managed DirectX/DirectX Mobile

支援Direct3D三維繪圖引擎

Windows Mobile遊戲開發技術

Page 6: Windows Mobile 6 遊戲開發入門

認識GAPI/GAPINET, GAPIDraw/GAPIDrawNET

認識GAPI

Game API的縮寫, 支援在行動裝置上執行高效能的圖形繪製

體積輕巧的遊戲設計介面(大約8KB)

認識GAPINET

將GAPI提供的C函數介面封裝成.NET平台能夠使用的類別介面, 協助使用C#或VB.NET的程式設計師開發行動裝置的遊戲程式

Page 7: Windows Mobile 6 遊戲開發入門

認識GAPI/GAPINET, GAPIDraw/GAPIDrawNET(續)

認識GAPIDraw

一組支援開發Windows Mobile平台遊戲, 導覽程式, 和進階使用者介面的C++類別和應用程式架構

認識GAPIDrawNET

將GAPIDraw提供的功能封

裝成.NET可用的類別

Page 8: Windows Mobile 6 遊戲開發入門

GAPIDraw的特性

GapiDraw的功能類似DirectX提供的DirectDraw, 可以視為DirectDraw功能的擴充, 包括功能更強的blit效果, 旋轉中放大功能, 從檔案或記憶體載入圖形, 多種繪圖工具, 碰撞遮罩, 圖形交集處理, 由執行緒控制的計時器, 以及點陣字型支援等等…

提供支援.NET語言(VB.NET和C#程式語言)開發遊戲程式的開放原始碼(Open Source)

Page 9: Windows Mobile 6 遊戲開發入門

GAPI的基本功能函數名稱 說明

GXBeginDraw 執行繪圖前的準備工作,傳回螢幕記憶體的位址,而不是DC的Handle(HDC)。

GXCloseDisplay 結束繪圖的動作,釋放GAPI所佔據的螢幕資源。通常在應用程式結束前呼叫。

GXCloseInput 釋放GAPI佔據的按鍵資源。

GXEndDraw 和GXBeginDraw成對使用,可以在完成一個完整的繪圖動作後呼叫,忘記呼叫GXEndDraw會造成記憶體很快就不敷使用。

GXGetDefaultKeys 提供建議的按鍵資訊。

GXGetDisplayProperties 查詢螢幕顯示記憶體的相關資訊並存放到GXDisplayProperties結構的變數中。

Page 10: Windows Mobile 6 遊戲開發入門

GAPI的基本功能(續)函數名稱 說明GXIsDisplayDRAMBuffer 判斷顯示螢幕是否標準的函數。

GXOpenDisplay 應用程式在執行繪圖動作之前必須先呼叫這個函數,取得顯示裝置的控制權,並拒絕其他的應用程式使用螢幕。

GXOpenInput 取得按鍵的控制權, 並將按鍵操作引發的訊息傳給應用程式, 而不是傳給Windows CE的Shell, 避免使用者在執行GAPI應用程式的時候啟動其他的應用程式。

GXResume 恢復被GXSuspend暫停的GAPI繪圖動作,應用程式可以在收到WM_SETFOCUS訊息時呼叫這個方法。

GXSetViewport 定義GAPI的視界(View Port)。GXSuspend 暫停繪圖工作, 應用程式可以在收到

WM_KILLFOCUS訊息時呼叫此方法。

Page 11: Windows Mobile 6 遊戲開發入門

GAPI的使用方式

Page 12: Windows Mobile 6 遊戲開發入門

認識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

Page 13: Windows Mobile 6 遊戲開發入門

DirectX的沿革

1992年Servan Keondjian創立RenderMorphics公司, 設立研究3D圖學的實驗室, 名稱Reality Lab

Microsoft於1995年購併RenderMorphics公司, 為Windows 95加入3D圖學功能, 即第一個版本的DirectX

目前DirectX的最新版本為Windows Vista支援的DirectX 10.0

DirectX 11.0目前正在研發中…

Page 14: Windows Mobile 6 遊戲開發入門

DirectX的重要功能

Direct3D

提供3D繪圖和動畫功能, 可以發揮顯示卡的硬體加速功能

DirectDraw

提供2D繪圖, 與直接存取顯示記憶體的能力

DirectInput

支援各種輸入裝置控制, 包括鍵盤, 滑鼠, 搖桿, 控制板, 方向盤, 腳踏板等等

DirectSound

支援播放與錄製WAV格式的聲音檔案, 提供立體聲與立體音效. 需要進階聲音播放控制功能時請用DirectMusic

Page 15: Windows Mobile 6 遊戲開發入門

DirectX的重要功能 (續)

DirectMusic

播放功能更強大的DirectSound. 支援動態音軌, 3D聲音定位, 以及硬體加速, 支援MIDI, WAV格式, 和DirectMusic Producer錄製的聲音檔案

DirectPlay

支援網路遊戲開發, 包括即時語音會議

支援GPRS, Bluetooth, Wi-Fi連線方式

DirectShow

支援聲音/影像錄製與播放, 支援TV電視卡

DirectSetup

支援將DirectX安裝到使用者的電腦

Page 16: Windows Mobile 6 遊戲開發入門

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代替

Page 17: Windows Mobile 6 遊戲開發入門

認識Direct3D Mobile

支援行動裝置的Direct3D Mobile的功能DirectX 9的功能精簡版, 功能接近DirectX 8

提供高效能3D繪圖和動畫功能

支援3D遊戲製作

支援全螢幕/非全螢幕顯示模式

體積精巧, 模組化

支援行動裝置的主流硬體與驅動程式

數值資料支援支援IEEE 32-bit浮點數

支援16.16 Fixed Point數值, 解決部分行動裝置處理器未提供浮點運算的問題

Page 18: Windows Mobile 6 遊戲開發入門

Windows Mobile與DirectDraw

支援行動裝置的DirectDraw的功能提供2維繪圖與硬體加速功能

支援影片播放

體積精巧

支援行動裝置螢幕旋轉功能

使用方法可以將所定義的Platform Invocation功能包裝成類別, 方便使用

Page 19: Windows Mobile 6 遊戲開發入門

DirectX常用的名詞與型態

使用DirectX Mobile的基本動作

繪圖與動畫

輸入控制

聲音控制

使用DirectX Mobile

Page 20: Windows Mobile 6 遊戲開發入門

Device:負責繪製內容, 播放聲音的型態DirectDraw Device, Direct3D Device,

DirectSound Device, …

Caps:描述Direct3D Device的能力

PresentParameters :描述Direct3D Device的特性

Surface:存放欲顯示的內容, 可以用來存取Video記憶體或主記憶體

SurfaceDescription:描述Surface的資訊

SurfaceCaps:描述Surface的能力

DirectX常用的名詞與型態

Page 21: Windows Mobile 6 遊戲開發入門

Buffer:存放欲顯示的內容存放DirectSound欲播放的聲音:Buffer,

SecondaryBuffer

存放Direct3D的內容:VertexBuffer, IndexBuffer

ColorKey:用來指定圖片的透空色彩

Pool:指定Direct3D的存放VertexBuffer的記憶體位置 VideoMemory:存放在顯示卡的記憶體中

SystemMemory:存放在主記憶體中

Managed:存放在主記憶體中, 視需要自動複製到顯示卡的記憶體中

DirectX常用的名詞與型態(續)

Page 22: Windows Mobile 6 遊戲開發入門

Vertex Buffer的內容

Index Buffer的內容

Vertex Buffer與Index Buffer

Page 23: Windows Mobile 6 遊戲開發入門

參考Microsoft.WindowsMobile.DirectX.dll組件

建立DirectX裝置(Device)

同一個時間只能建立一個Device

查詢Device的能力

建立Vertex Buffer

判斷螢幕的方向

輸入控制

使用DirectX Mobile的基本動作

Page 24: Windows Mobile 6 遊戲開發入門

建立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);

Page 25: Windows Mobile 6 遊戲開發入門

建立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)

Page 26: Windows Mobile 6 遊戲開發入門

查詢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");

}

Page 27: Windows Mobile 6 遊戲開發入門

建立Vertex Buffer

//建立Vertex Buffer

m_oVB = new VertexBuffer(

typeof(CustomVertex.PositionNormal), 100, m_oDevice, 0, CustomVertex.PositionNormal.Format, eD3DMPool);

//填入欲顯示的資料到VerTex Buffer

PopulateVertexBuffer(m_oVB);

Page 28: Windows Mobile 6 遊戲開發入門

顯示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();

Page 29: Windows Mobile 6 遊戲開發入門

判斷螢幕的方向

//判斷使用者是否旋轉過行動裝置的螢幕

if (SystemSettings.ScreenOrientation != ScreenOrientation.Angle0)

{

//使用者旋轉過行動裝置的螢幕

// ...

}

Page 30: Windows Mobile 6 遊戲開發入門

輸入控制

兩種選擇:

呼叫GAPI功能

處理事件 MouseUp, MouseDown, MouseMove

KeyUp, KeyDown, KeyPress

private void Form1_KeyDown(object sender, KeyEventArgs e)

{

if ((e.KeyCode == System.Windows.Forms.Keys.Up))

// Up

}

Page 31: Windows Mobile 6 遊戲開發入門

結束繪圖的動作

丟棄VerTex Buffer

m_oVB.Dispose();

丟棄Device

m_oDevice.Dispose();

Page 32: Windows Mobile 6 遊戲開發入門

使用Direct3D Mobile顯示動畫

Page 33: Windows Mobile 6 遊戲開發入門

聲音控制

使用Waveform Audio API

利用Platform Invocation技術

宣告[DllImport("CoreDll.dll", EntryPoint = "PlaySound",

SetLastError = true)]

private extern static bool PlaySound(string szSound, IntPtrhMod, int flags);

呼叫PlaySound(Path.Combine("Audios", fileName), IntPtr.Zero,

(int)(SND.SYNC | SND.FILENAME));

Page 34: Windows Mobile 6 遊戲開發入門

建立專案, 參考必要的組件

修改Program.cs中的Main方法

顯示遊戲背景

顯示太空船

移動太空船

發出雷射光

加入幽浮

幽浮投彈

命中檢查

加入音效

Windows Mobile遊戲開發體驗

Page 35: Windows Mobile 6 遊戲開發入門

加入控制程式執行的迴圈

using (Form1 app = new Form1())

{

app.Show();

while (app.Focused)

{

app.Render();

Application.DoEvents();

}

}

修改Program.cs中的Main方法

利用Render方法更新遊戲顯示的內容,

例如動畫public void Render()

{

顯示遊戲內容}

允許程式處理事件

Page 36: Windows Mobile 6 遊戲開發入門

為專案建立存放圖片的images資料夾, 加入當做主畫面的invasion.png和當做背景圖的backdrop2.png, 將兩張圖的[複製到輸出目錄]屬性設定成:有更新時才複製

為Form1類別加入以下的變數宣告:

private DirectDrawGraphics _graphics;

private Surface _screen;

private Surface _main;

private Surface _background;

tagRECT srcScreenRec, destScreenRec;

顯示遊戲背景(一)

Page 37: Windows Mobile 6 遊戲開發入門

為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, "images\\invasion.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();

顯示遊戲背景(二)

Page 38: Windows Mobile 6 遊戲開發入門

為Form1製作Closing事件處理程序:

_graphics.Dispose();

為Form1製作[File | Exit]功能表, 並為Exit功能表製作Click事件處理程序:

Close();

顯示遊戲背景(三)

Page 39: Windows Mobile 6 遊戲開發入門

於存放圖片的images資料夾, 加入當做太空船的ship.png, 將圖的[複製到輸出目錄]屬性設定成:有更新時才複製

為Form1類別加入以下的變數宣告:

private Surface _ship;

tagRECT srcShipRec, destShipRec;

const int _shipSize = 70;

const int _scoreBar = 20;

顯示太空船(一)

Page 40: Windows Mobile 6 遊戲開發入門

於Form1的Load事件處理程序加入:

_ship = _graphics.CreateSurfaceFromImage(new Bitmap(Path.Combine(AppPath, "images\\ship.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();

顯示太空船(二)

Page 41: Windows Mobile 6 遊戲開發入門

DirectX支援BMP, JPG, PNG, 等常見的圖形檔格式

利用單一圖形檔案內含多張不同的圖案, 再指定座標來取用

太空船的圖案

Page 42: Windows Mobile 6 遊戲開發入門

利用ColorKey指定圖片的背景色彩, 達到透空的效果

搭配DDBLT_KEYSRC效果進行顯示

未透空顯示

透空顯示

ColorKey為

白色

ColorKey與透空顯示

Page 43: Windows Mobile 6 遊戲開發入門

利用Flip方法切換Primary Buffer與Back Buffer可以提升顯示速度

傳統的顯示法 – 複製資料到Video Memory

DirectX的顯示法 – 切換Video Memory

Primary Buffer與Back Buffer

Page 44: Windows Mobile 6 遊戲開發入門

為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)) {

}

}

移動太空船

Page 45: Windows Mobile 6 遊戲開發入門

顯示幽浮

幽浮爆炸

DirectX與動畫顯示

Page 46: Windows Mobile 6 遊戲開發入門

於存放圖片的images資料夾, 加入當做雷射光的Laser.png, 將圖的[複製到輸出目錄]屬性設定成:有更新時才複製

為Form1類別加入以下的變數宣告:

private Surface _laser;

tagRECT srcLaserRec, destLaserRec;

const int _LaserSize = 20;

const int GunPosition = 15;

bool bShowLaser=false;

發射雷射光 (一)

Page 47: Windows Mobile 6 遊戲開發入門

於Form1的Load事件處理程序加入:

_laser = _graphics.CreateSurfaceFromImage(new Bitmap(Path.Combine(AppPath, "images\\Laser.png")));

_laser.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);

為Closing事件處理程序加入:

_laser.Dispose();

發射雷射光 (二)

Page 48: Windows Mobile 6 遊戲開發入門

於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;

}

發射雷射光 (三)

Page 49: Windows Mobile 6 遊戲開發入門

修改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;

}

發射雷射光 (四)

Page 50: Windows Mobile 6 遊戲開發入門

於存放圖片的images資料夾, 加入當做幽浮的Ufo.png, 將圖的[複製到輸出目錄]屬性設定成:有更新時才複製

為Form1類別加入以下的變數宣告:

private Surface _ufo;

tagRECT srcUfoRec, destUfoRec;

const int _UfoSize = 70;

Random rndUfo=null;

為Closing事件處理程序加入:_ufo.Dispose();

加入幽浮 (一)

Page 51: Windows Mobile 6 遊戲開發入門

於Form1的Load事件處理程序加入:_ufo = _graphics.CreateSurfaceFromImage(new

Bitmap(Path.Combine(AppPath, "images\\Ufo.png")));

_ufo.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);

rndUfo=new Random();

destUfoRec = new tagRECT(0, 0, _UfoSize, _UfoSize);

srcUfoRec = new tagRECT(0, 0, _UfoSize, _UfoSize);

加入幽浮 (二)

Page 52: Windows Mobile 6 遊戲開發入門

於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();

加入幽浮 (三)

Page 53: Windows Mobile 6 遊戲開發入門

將顯示幽浮的程式碼改成: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();

調整幽浮移動的速度

Page 54: Windows Mobile 6 遊戲開發入門

在Form1類別中宣告:

int UfoFrameNo=0;

在顯示幽浮之後加入以下的程式碼:

UfoFrameNo++;

UfoFrameNo=UfoFrameNo>4?0:UfoFrameNo;

srcUfoRec = new tagRECT(_UfoSize*UfoFrameNo, 0,

_UfoSize*UfoFrameNo+_UfoSize, _UfoSize);

旋轉幽浮

Page 55: Windows Mobile 6 遊戲開發入門

於存放圖片的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;

幽浮投彈 (一)

Page 56: Windows Mobile 6 遊戲開發入門

於Form1的Load事件處理程序加入:_bomb = _graphics.CreateSurfaceFromImage(new

Bitmap(Path.Combine(AppPath, "images\\bomb.png")));

_bomb.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);

srcBombRec = new tagRECT(0, 0, _BombSize, _BombSize);

dtBomb=DateTime.Now;

為Closing事件處理程序加入:_bomb.Dispose();

幽浮投彈 (二)

Page 57: Windows Mobile 6 遊戲開發入門

於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) { //省略

}

幽浮投彈 (三)

Page 58: Windows Mobile 6 遊戲開發入門

在Form1類別中宣告:bool bShowShip=true;

bool bShowUfoExplode=false;

修改顯示幽浮的語法:if (bShowUfo)

{

_screen.Draw(ref destUfoRec, _ufo, ref srcUfoRec, BltFlags.DDBLT_KEYSRC);

_graphics.Flip();

}

命中檢查 (一)

Page 59: Windows Mobile 6 遊戲開發入門

修改顯示太空船的語法:if (bShowShip)

{

_screen.Draw(ref destShipRec, _ship, ref srcShipRec, BltFlags.DDBLT_KEYSRC);

_graphics.Flip();

}

於Form1類別的Render方法加入CheckCollision方法呼叫:CheckCollision();

命中檢查 (二)

Page 60: Windows Mobile 6 遊戲開發入門

為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);

//省略

}

命中檢查 (三)

Page 61: Windows Mobile 6 遊戲開發入門

於存放圖片的images資料夾, 加入當做爆炸效果的shipexplode.png, 將圖的[複製到輸出目錄]屬性設定成:有更新時才複製

為Form1類別加入以下的變數宣告:private Surface _shipexplode;

tagRECT srcShipExplodeRec;

int ShipExplodeFrameNo=0;

為Closing事件處理程序加入:_shipexplode.Dispose();

顯示太空船爆炸效果 (一)

Page 62: Windows Mobile 6 遊戲開發入門

於Form1的Load事件處理程序加入:_shipexplode = _graphics.CreateSurfaceFromImage(new

Bitmap(Path.Combine(AppPath, "images\\shipexplode.png")));

_shipexplode.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);

srcShipExplodeRec = new tagRECT(0, 0, _shipSize, _shipSize);

顯示太空船爆炸效果 (二)

Page 63: Windows Mobile 6 遊戲開發入門

於Form1類別的Render方法的CheckCollision呼叫之後加入:if (bShowShipExplode) { //ship was hitted

_screen.Draw(ref destShipRec, _shipexplode, ref srcShipExplodeRec, BltFlags.DDBLT_KEYSRC);

_graphics.Flip();

ShipExplodeFrameNo++;

if (ShipExplodeFrameNo>6) {

bShowShipExplode = false;

ShipExplodeFrameNo=0;

}

else { 省略 }

}

顯示太空船爆炸效果 (三)

Page 64: Windows Mobile 6 遊戲開發入門

於存放圖片的images資料夾, 加入當做爆炸效果的ufoexplode.png, 將圖的[複製到輸出目錄]屬性設定成:有更新時才複製

為Form1類別加入以下的變數宣告:private Surface _ufoexplode;

tagRECT srcUfoExplodeRec;

int UfoExplodeFrameNo=0;

為Closing事件處理程序加入:_ufoexplode.Dispose();

顯示幽浮爆炸效果 (一)

Page 65: Windows Mobile 6 遊戲開發入門

於Form1的Load事件處理程序加入:_ufoexplode = _graphics.CreateSurfaceFromImage(new

Bitmap(Path.Combine(AppPath, "images\\ufoexplode.png")));

_ufoexplode.SetColorKey(Color.Black, ColorKeyFlags.SRCBLT);

srcUfoExplodeRec = new tagRECT(0, _UfoSize, _UfoSize, _UfoSize+_UfoSize);

顯示幽浮爆炸效果 (二)

Page 66: Windows Mobile 6 遊戲開發入門

於Form1類別的Render方法的CheckCollision呼叫之後加入:if (bShowUfoExplode) { //Ufo was hitted

_screen.Draw(ref destUfoRec, _ufoexplode, ref srcUfoExplodeRec, BltFlags.DDBLT_KEYSRC);

_graphics.Flip();

UfoExplodeFrameNo++;

if (UfoExplodeFrameNo>4) {

bShowUfoExplode = false;

UfoExplodeFrameNo=0;

}

else { 省略 }

}

顯示幽浮爆炸效果 (三)

Page 67: Windows Mobile 6 遊戲開發入門

為專案建立存放音效檔案的audios資料夾, 於audios資料夾加入laser.wmv, bomb.wmv, explosion.wmv, 將音效檔案的[複製到輸出目錄]屬性設定成:有更新時才複製

於Form1類別加入:[DllImport("CoreDll.dll", EntryPoint = "PlaySound",

SetLastError = true)]

private extern static bool PlaySound(string szSound, IntPtrhMod, int flags);

private enum SND {

SYNC = 0x0000,

//省略

}

加入音效 (一)

Page 68: Windows Mobile 6 遊戲開發入門

加入發射雷射音效: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));

加入音效 (二)

Page 69: Windows Mobile 6 遊戲開發入門

使用DirectX功能製作的Windows Mobile遊戲

Page 70: Windows Mobile 6 遊戲開發入門

1) 遊戲結束時詢問使用者是否再玩?

2) 計分, 排行

3) 彈藥存量

4) 船體受損程度

5) 將以下的內容製作成類別:

雷射光

幽浮

炸彈

程式改良

Page 71: Windows Mobile 6 遊戲開發入門

好用的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

Page 72: Windows Mobile 6 遊戲開發入門

複習

Windows Mobile行動裝置平台

Windows Mobile遊戲開發支援

Windows Mobile遊戲開發技術

認識GAPI, GAPINET, GAPIDraw, 與GAPIDrawNET

認識DirectX, DirectX Mobile, 與Managed DirectX

使用DirectX Mobile的基本動作

Windows Mobile遊戲開發體驗

好用的DirectX開發輔助工具

Page 74: Windows Mobile 6 遊戲開發入門

http://www.iiiedu.org.tw/taipei

資訊專區

下載-資策會台北中心