Wp8 game development for beginners

1,449 views
1,211 views

Published on

http://winstore.vn

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,449
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
40
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Wp8 game development for beginners

  1. 1. Windows Phone 8 2D Game Development For Beginners Cao Phong DeveloperTechSupport Manager Nokia,Vietnam
  2. 2. Content • Setting up a basic framework with XNA • Setting up a basic framework with Direct3D:  DirectX TK  Direct3D (native)  Direct3D with XAML and XAML with Direct3D app • Porting XNA to Asha Touch
  3. 3. XNA vs Direct3D XNA Pros: - Simple setup and easy to use - Legacy support - Can be quickly ported to Asha Touch Cons: - Unable to useWP8 APIs: In-app purchase, NFC, Bluetooth… - Lack of native code support Direct3D Pros: - Best performance - Leverage onWP8 APIs: In-app purchase, NFC, Bluetooth… - Native code support - Flexibility to add nice game effects Cons: - Requiring quite a lot steps to set up at the beginning - Fairly complex to use
  4. 4. XNA Choose project template
  5. 5. Main loop protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here base.Draw(gameTime); }
  6. 6. Fill Rectangle Texture2D m_pRectTexture = null; Rectangle m_rect; Color m_color; public void FillRect(int color, int x, int y, int w, int h) { if (m_pRectTexture == null) { m_pRectTexture = new Texture2D(GraphicsDevice, 1, 1); m_pRectTexture.SetData<Color>(new Color[] { Color.White }); } m_rect.X = x; m_rect.Y = y; m_rect.Width = w; m_rect.Height = h; m_color.A = (byte)((color & 0xFF000000) >> 24); m_color.R = (byte)((color & 0x00FF0000) >> 16); m_color.G = (byte)((color & 0x0000FF00) >> 8); m_color.B = (byte)((color & 0x000000FF)); spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque); spriteBatch.Draw(m_pRectTexture, m_rect, m_color); spriteBatch.End(); }
  7. 7. Draw Image protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // Draw a rectangle (color=red, x=50, y=100, w=100, h=50) FillRect(0xFF0000, 50, 100, 100, 50); // Draw an image (x=10, y=10) spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied); spriteBatch.Draw(imgGirl, new Vector2(10, 10), Color.White); spriteBatch.End(); base.Draw(gameTime); } public Texture2D LoadImage(String file) { try { Stream fs = TitleContainer.OpenStream(file); Texture2D img = Texture2D.FromStream(GraphicsDevice, fs); fs.Close(); fs = null; return img; } catch (Exception e) { // Handle error return null; } }
  8. 8. Direct3D DirectX TK (Toolkit) There is a collection of helper classes united under the name of DirectX Tool Kit [4] - or in short DirectX TK - started by Shawn Hargreaves. This library contains convenience classes that help write code that uses Direct X. It also contains a tool called MakeSpriteFont that can create fonts to be used in game. Download link: http://directxtk.codeplex.com/releases/view/98986
  9. 9. Direct3D Choose project template
  10. 10. Fill Rectangle void GameRenderer::FillRect(int color, int x, int y, int w, int h) { // Create Texture CD3D11_TEXTURE2D_DESC texDesc( DXGI_FORMAT_R8G8B8A8_UNORM,// DXGI_FORMAT 1,// width 1,// height 1,// arraySize 1,// mipLevels D3D11_BIND_SHADER_RESOURCE// bindFlags ); D3D11_SUBRESOURCE_DATA colorData = { 0 }; Microsoft::WRL::ComPtr<ID3D11Texture2D> texture; int a[] = {0xFF000000 | ((color & 0xFF) << 16) | (((color & 0xFF00) >> 8) << 8) | ((color & 0xFF0000) >> 16)}; // argb => abgr colorData.pSysMem = &a; colorData.SysMemPitch = sizeof(a); colorData.SysMemSlicePitch = 0; m_d3dDevice->CreateTexture2D(&texDesc, &colorData, texture.GetAddressOf()); Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> view; m_d3dDevice->CreateShaderResourceView(texture.Get(), 0, &view); // Draw In a Rectangle RECT rect = {x, y, x + w, y + h}; // left, top, right, bottom spriteBatch->Begin(); spriteBatch->Draw(view.Get(), rect, Colors::White); spriteBatch->End(); }
  11. 11. DirectDraw Surface (DDS) DirectDraw Surface file format • Filename extension: DDS • Data compressed with the lossy S3 Texture Compression (S3TC) algorithm • Support phone GPUs and consoles (PSP, PS3, Xbox 360…) Converting images to .dds files • DirectXTex library: http://directxtex.codeplex.com/SourceControl/changeset/22746 • NVIDIA Texture Tools: https://developer.nvidia.com/nvidia-texture-tools-adobe-photoshop https://developer.nvidia.com/content/texture-tools-208-64-bit • Easy2Convert PNG to DDS: http://www.easy2convert.com/png2dds/
  12. 12. Draw Image Load resource 1. Declare texture in GameRenderer.h: ID3D11ShaderResourceView* m_catTexture; 2. Include DDS loader header in GameRenderer.cpp: #include <DDSTextureLoader.h> 3. Load texture in CreateDeviceResources method: void GameRenderer::CreateDeviceResources() { Direct3DBase::CreateDeviceResources(); spriteBatch = std::shared_ptr<SpriteBatch>(new SpriteBatch(m_d3dContext.Get())); DX::ThrowIfFailed(CreateDDSTextureFromFile(m_d3dDevice.Get(), L"AssetsCatTexture.dds", NULL, &m_catTexture, NULL)); m_loadingComplete = true; }
  13. 13. Draw Image void GameRenderer::Render(float timeTotal, float timeDelta) { const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f }; m_d3dContext->ClearRenderTargetView(m_renderTargetView.Get(), midnightBlue); m_d3dContext->ClearDepthStencilView(m_depthStencilView.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0); // Only draw once the resources are loaded (loading is asynchronous). if (!m_loadingComplete) { return; } m_d3dContext->OMSetRenderTargets(1, m_renderTargetView.GetAddressOf(), m_depthStencilView.Get()); // Fill a rectangle FillRect(0xFF0000, 50, 100, 100, 50); // Draw an image (x=10, y=10) spriteBatch->Begin(); spriteBatch->Draw(m_catTexture, XMFLOAT2(10, 10)); spriteBatch->End(); }
  14. 14. Direct3D App Choose project template
  15. 15. 3D Coordinate Systems Left-handed versus Right-handed • DirectX: usually uses Left-handed coordinate system • OpenGL: usually uses Right-handed coordinate system
  16. 16. HLSL (High Level Shader Language) The language used to write shaders for GPUs in DirectX SimpleVertexShader.hlsl VertexShaderOutput main(VertexShaderInput input) { VertexShaderOutput output; float4 pos = float4(input.pos, 1.0f); // Transform the vertex position into projected space. pos = mul(pos, model); pos = mul(pos, view); pos = mul(pos, projection); output.pos = pos; // Pass through the color without modification. output.color = input.color; return output; } SimplePixelShader.hlsl float4 main(PixelShaderInput input) : SV_TARGET { return float4(input.color,1.0f); }
  17. 17. DirectX Math and HLSL Matrix Vector OpenGL Column-major Column vectors GLSL Column-major Column vectors DirectX Math Row-major Row vectors HLSL (by default) Column-major Row vectors Solutions: • Transpose matrices before sending to HLSL • Use the #pragma pack_matrix directive in shaders: #pragma pack_matrix( row_major )
  18. 18. DirectX Primitives Point Lists Line Lists Line Strips Triangle Lists Triangle Strips
  19. 19. Face and Vertex Normal Vectors • Each face in a mesh has a perpendicular unit normal vector • The vector's direction is determined by the order in which the vertices are defined and by whether the coordinate system is right- or left-handed • The face normal points away from the front side of the face • In Direct3D, only the front of a face is visible • A front face is one in which vertices are defined in clockwise order.
  20. 20. Use row-major in HLSL so that you don’t need to transpose matrices from DirectX Math 1. Add directive in vertex shader: Direct3D App (Cont.) #pragma pack_matrix( row_major ) SimpleVertexShader.hlsl 2. Remove all transpose matrices in CubeRenderer.cpp: void CubeRenderer::CreateWindowSizeDependentResources() { Direct3DBase::CreateWindowSizeDependentResources(); float aspectRatio = m_windowBounds.Width / m_windowBounds.Height; float fovAngleY = 70.0f * XM_PI / 180.0f; if (aspectRatio < 1.0f) { fovAngleY /= aspectRatio; } XMStoreFloat4x4(&m_constantBufferData.projection, XMMatrixTranspose(XMMatrixPerspectiveFovRH(fovAngleY, aspectRatio,0.01f,100.0f))); } void CubeRenderer::Update(float timeTotal, float timeDelta) { (void) timeDelta; // Unused parameter. XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f); XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up))); XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4) )); }
  21. 21. Apply orthographic projection for our 2D game framework with Left-handed coordinate system Direct3D App void CubeRenderer::CreateWindowSizeDependentResources() { Direct3DBase::CreateWindowSizeDependentResources(); XMStoreFloat4x4( &m_constantBufferData.projection, XMMatrixOrthographicOffCenterLH ( 0, // ViewLeft m_windowBounds.Width, // ViewRight m_windowBounds.Height, // ViewBottom 0, // ViewTop -1.0f, // NearZ 1.0f // FarZ ) ; }
  22. 22. Update view matrix with Left-handed coordinate system Direct3D App void CubeRenderer::Update(float timeTotal, float timeDelta) { (void) timeDelta; // Unused parameter. XMVECTOR eye = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f); XMVECTOR at = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixLookAtLH(eye, at, up)); XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixIdentity()); }
  23. 23. Disabling Z buffer in 2D mode to enable back-to-front rendering Direct3D App // Initialize the Direct3D resources required to run. void Direct3DBase::Initialize(CoreWindow^ window) { m_window = window; CreateDeviceResources(); CreateWindowSizeDependentResources(); D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc; ID3D11DepthStencilState* depthDisabledStencilState; depthDisabledStencilDesc.DepthEnable = false; depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; depthDisabledStencilDesc.StencilEnable = true; depthDisabledStencilDesc.StencilReadMask = 0xFF; depthDisabledStencilDesc.StencilWriteMask = 0xFF; depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // Create the state using the device. m_d3dDevice->CreateDepthStencilState(&depthDisabledStencilDesc, &depthDisabledStencilState); m_d3dContext->OMSetDepthStencilState(depthDisabledStencilState, 1); } Direct3DBase.cpp
  24. 24. Render 3 rectangles using Triangle Lists Direct3D App VertexPositionColor cubeVertices[] = { {XMFLOAT3(0.0f, 0.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)}, {XMFLOAT3(100.0f, 0.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)}, {XMFLOAT3(100.0f, 100.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)}, {XMFLOAT3(0.0f, 100.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)}, {XMFLOAT3(50.0f, 50.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)}, {XMFLOAT3(150.0f, 50.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)}, {XMFLOAT3(150.0f, 150.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)}, {XMFLOAT3(50.0f, 150.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)}, {XMFLOAT3(100.0f, 100.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)}, {XMFLOAT3(200.0f, 100.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)}, {XMFLOAT3(200.0f, 200.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)}, {XMFLOAT3(100.0f, 200.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)}, }; unsigned short cubeIndices[] = { 0,1,2, 0,2,3, 4,5,6, 4,6,7, 8,9,10, 8,10,11, }; CubeRenderer.cpp
  25. 25. Direct3D App 0 (0,0) 1 (100,0) 2 (100,100)3 (0,100)
  26. 26. Texture Mapping 0 (0,0) 1 (300,0) 2 (300,300)3 (0,300) 0.0 1.0 1.0 U V • Texturing allows adding photographs and other images onto polygon faces • The format of the textures are Direct Draw Surface (.dds) files • In the texel coordinate system, the width value is named "U" and the height value is named "V“: • The width goes from 0.0 on the left to 1.0 on the right • The height goes from 0.0 on the top to 1.0 on the bottom Texel Coordinate System
  27. 27. Update texture coordinates and render VertexPositionTexture cubeVertices[] = { {XMFLOAT3(0.0f, 0.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 0.0f)}, {XMFLOAT3(300.0f, 0.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)}, {XMFLOAT3(300.0f, 300.0f, 0.0f), XMFLOAT3(1.0f, 1.0f, 0.0f)}, {XMFLOAT3(0.0f, 300.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)}, }; Map texture to a square having width=300, height=300 and render it at (0, 0):
  28. 28. Choosing the right project template for your game Direct3D Direct3D with XAML XAML with Direct3D UI (XAML) No Yes Yes Exclusive Features No Yes Yes Acts as a XAML control (can be placed anywhere on the screen, above or next to other XAML controls) No No Yes Features Exclusive Features: • Background Agents • Browser Control • Map Control • Background Transfer Service • Live Tiles • Push Notifications • The Windows Phone Ad Control • Localized resources • XAML controls
  29. 29. Choosing the right project template for your game for WP8 Direct3D Direct3D with XAML XAML with Direct3D Startup time 2 1 1 Rendering 3 2 1 Performance Unofficial tests: • Direct3D with XAML app is 18 – 20% slower than Direct3D app • XAML with Direct3D app is 5 – 7% slower than Direct3D with XAML app * Note: higher number means faster performance
  30. 30. Direct3D with XAML app Choose project template
  31. 31. Direct3D with XAML app Uses the DrawingSurfaceBackgroundGrid control • Render graphics that are displayed across the entire background of your app, behind any XAML controls in your UI • Must be the root element of the XAML tree and is always the full size of the screen Structure • Made up of two components: a XAML-based Windows Phone app and a component that’s based on the Windows Phone Runtime • The XAML engine creates and maintains a Direct3D graphics device. Once per frame, the XAML engine passes the graphics device and an associated render target to the Windows Phone Runtime component Main classes • MainPage: hosts your XAML-based UI and your managed code • CubeRenderer: where the actual drawing to the shared texture is performed • PhoneDirect3DXamlAppComponent: contains touch input handling
  32. 32. XAML with Direct3D app Choose project template
  33. 33. XAML with Direct3D app Uses the DrawingSurface control • Render graphics that are displayed behind or inline with XAML controls and content • The size and layout of your DrawingSurface are handled just as they are with other XAML controls Structure • Made up of two components: a XAML-based Windows Phone app and a component that’s based on the Windows Phone Runtime • The Windows Phone Runtime component creates a Direct3D graphics device, which it uses to create a Texture that is shared with the XAML app. The Windows Phone Runtime component uses Direct3D to draw onto the Texture. The XAML engine maintains a swap chain that copies the texture to the screen within the bounds of the DrawingSurface control Main classes • MainPage: hosts your XAML-based UI and your managed code • CubeRenderer: where the actual drawing to the shared texture is performed • Direct3DInterop: contains touch input handling
  34. 34. 3 5 Porting2DXNAGames toAshaFullTouch
  35. 35. • Minimal porting effort: 1 – 5 working days • Leverage on Nokia Store payment system to maximize revenue:  In-app purchasing with 145 operator billing integrations  In-app advertising with NokiaAd Exchange (NAX): 120 ad networks in over 200 countries Why porting toAsha FullTouch?
  36. 36. Screen Resolution 480x800 240x400  All images are scaled down by half  All coordinates are divided by half
  37. 37. Images 68x74 34x37
  38. 38. Coordinates andAlignment (x, y) (x / 2, y / 2)
  39. 39. XNA & J2ME - C# and Java language C# Java namespace package const final bool boolean override N/A
  40. 40. XNA and Java ME Frameworks XNA Java ME Microsoft.Xna.Framework.Game javax.microedition.lcdui.Canvas java.lang.Runnable GraphicsDeviceManager GraphicsDevice SpriteBatch RenderTarget2D Graphics protected override void Update(GameTime gameTime) protected override void Draw(GameTime gameTime) public void run() public void paint(Graphics g) Color int color Texture2D Image
  41. 41. XNA and Java ME Frameworks
  42. 42. Rule #1 – All coordinates divided by half k_Factor: • 1:Windows Phone • 2: AshaFT
  43. 43. Rule #2 – Do relative alignment, no hard code DON'T's DO’s
  44. 44. Rule #3 – Use J2ME wrapper classes in XNA
  45. 45. Rule #3 – Use J2ME wrapper classes in XNA
  46. 46. 1. Develop or modify XNA source code with J2ME wrapper classes: • Graphics • Image • Touch input and media related classes 2. Resize all images to one half and copy to AshaFT project 3. Update all coordinates with scale factor = ½ 4. Replace Microsoft.Xna.Framework.Game by Javax.microedition.lcdui.Canvas and java.lang.Runnable 5. Remove all wrapper classes, go with the ones in MIDP such as javax.microedition.lcdui.Graphics… 6. Optimize memory and performance Porting Steps
  47. 47. mcpp preprocessor http://mcpp.sourceforge.net/ • Implements all ofC90, C99 and C++98 specifications. • Provides a validation suite to test C/C++ preprocessor's conformance and quality comprehensively.When this validation suite is applied, mcpp distinguishesitself among many existing preprocessors. • Has plentiful and on-target diagnostics to check all the preprocessing problems such as latent bug or lack of portability in source code. • Has #pragma directives to output debugging information. • Is portable and has been ported to many compiler-systems, includingGCC andVisual C++, on UNIX-like systems andWindows. • Has various behavior modes. • Can be built either as a compiler-specificpreprocessor to replace the resident preprocessor of a particular compiler system, or as a compiler- independent command, or even as a subroutine called from some other main program. • Provides comprehensive documents both in Japanese and in English. • Is an open source software released under BSD-style-license.
  48. 48. Develop a game fully in XNA
  49. 49. Building jar and jad automatically
  50. 50. Launch the file with Nokia Java Emulator
  51. 51. Thank you!  https://www.facebook.com/groups/viet.nok.dev Let’s discuss more:

×