ãã®ã®ã£ãããåããŠã次ã®ããšã説æãããã£ãã®ã§ãã
- åãããŒãžã§SilverlightãšXNAã䜿çšãã
- XNAã§æãã·ã³ãã«ãªãžã§ã¹ãã£ãŒåŠç
- XNAã«ã¡ã©ã®åºæ¬
ãã®èšäºã®äŸã¯ãã¹ã¿ã¹ããããã®ã€ããã³ã°ã¹ã¯ãŒã«ã®æåã®ã¹ããªãŒã ã®èšªåè ã«ã¯ããªãã¿ãããããŸãã
ãŸãã1ã€ã®ã¢ããªã±ãŒã·ã§ã³ã§SilverlightãšXNAã䜿çšã§ãããã®ãèŠãŠã¿ãŸãããã
ããã¯éçºè ã«äœãäžããŸããïŒ
ãŸããã²ãŒã éçºè ã«ã¯æ¬¡ã®æ©èœããããŸãã
- Silverlightã®ã³ã³ãããŒã«ã䜿çšããã¯ã€ãã¯UIäœæ
- ããã²ãŒã·ã§ã³ãµãŒãã¹ã䜿çšããããŒãžéã®äŸ¿å©ãªããã²ãŒã·ã§ã³
- WebClientã䜿çšããŠããŸããŸãªãœãŒã·ã£ã«ãµãŒãã¹ãçµ±åãã
- Expression Blendã§ã€ã³ã¿ãŒãã§ã€ã¹ãäœæãã
ãŸãããã¹ãŠã®ããŒãžãSilverlightãšXNAã®äž¡æ¹ãçµã¿åãããå¿ èŠãããããã§ã¯ãªãããšã«æ³šæããŠãã ããã ãŸãããã®æ©èœã¯ã²ãŒã éçºè ã ãã§ãªã圹ã«ç«ã€ãšããäºå®ã«ã泚æãåèµ·ããããšæããŸãã ããšãã°ãXNAã«ã¯çŽ æŽãããçµã¿èŸŒã¿ã®ãžã§ã¹ãã£ãµããŒããããããã¯ã¹ãã£ãæäœããããã®éåžžã«äŸ¿å©ãªæ¹æ³ããããŸãã SilverlightãšXNAã䜿çšããåªããéã²ãŒã ã¢ããªã±ãŒã·ã§ã³ã®äŸã¯ã Holiday Photoã§ãã
æ¹æ³ïŒæåã®Silverlight + XNAã¢ããªã±ãŒã·ã§ã³ã®éçº
äŸãšããŠãç§ã¯éåžžã«æåãªæ°åŠã²ãŒã Lifeãéžã³ãŸãããããã¯HabrÃ©ïŒ 1ã2ã3ãããã³ä»ã®å€ãïŒã§ç¹°ãè¿ãæžãããŠããŸãã èŠããã«ãã²ãŒã ã¯ããŒããã¬ãŒã€ãŒã²ãŒã ãã§ããã€ãŸãããã¬ãŒã€ãŒã¯åæç¶æ ã®ã¿ãèšå®ããŸãã ããã«ãåäžä»£ã®åã»ã«ã®ç¶æ ã¯ãããªãåçŽãªèŠåã«åŸã£ãŠãåšå²ã®8ã€ã®ã»ã«ã®ç¶æ ã«åºã¥ããŠèšç®ãããŸãã
2ã€ã®é åã䜿çšããæãåçŽãªã¢ã«ãŽãªãºã ã䜿çšããããšãããã«äºçŽãããã®ã§ãããããã¯ã¢ã«ãŽãªãºã ãšããŒã¿æ§é ã«é¢ããæçš¿ã§ã¯ãããŸããã ãããã£ãŠãæé©ãã€é«éãªã¢ã«ãŽãªãºã ã®å®è£ ã¯ã宿é¡ã®1ã€ã«ãªããŸãã
ã¹ããã1.ãããžã§ã¯ãã®äœæãšã€ã³ã¿ãŒãã§ãŒã¹ã®ã¬ã³ããªã³ã°
æ°ãããããžã§ã¯ããäœæãããšãã¯ã ãWindows Phone Silverlightããã³XNAã¢ããªã±ãŒã·ã§ã³ããéžæããå¿ èŠããããŸãã 2 ã€ã® MainPageããŒãžãšGamePageããŒãžã§æ°ãããããžã§ã¯ããäœæãããŸã ã
MainPage -Silverlightã¢ããªã±ãŒã·ã§ã³ã®éåžžã®ããŒãžããã²ãŒã ããŒãžã«å€æŽããã¿ã³ããããã¯ãªãã¯ãããšGamePageããŒãžãéããŸã ã
GamePageã¯åãªãXNAããŒãžã§ãã xamlãã¡ã€ã«ã®å 容ãèŠããšãããŒãžã¬ã€ã¢ãŠãã®ä»£ããã«1è¡ãããããŸããã
<!--No XAML content is required as the page is rendered entirely with the XNA Framework-->
ãããã£ãŠããã®ããŒãžã«ã³ã³ãããŒã«ãè¿œå ããå Žåããã®ã³ã¡ã³ããã³ãŒãã«çœ®ãæããå¿ èŠããããŸãããããè¡ãã«ã¯ã [éå§ ]ãã¿ã³ãé 眮ããŸãã
<Grid Name="Layout" LayoutUpdated="Layout_LayoutUpdated"> <Button Content="Start" Height="71" Name="button1" Width="160" Margin="25,717,295,12" Click="button1_Click" /> </Grid>
ãããžã§ã¯ããéå§ããã ã²ãŒã ããŒãžã«å€æŽ ããã¿ã³ãæŒããŠããã«ãŒã¹ã¯ãªãŒã³ã®ã¿ã衚瀺ããŸãããã¿ã³ã¯ã©ãã«ããããŸããã ããããå¿é ããå¿ èŠã¯ãããŸãããä»ããä¿®æ£ããŸãã
GamePage.xaml.csãã¡ã€ã«ãéããŸãããã®ãã¡ã€ã«ã«ã¯ãXNAã¢ããªã±ãŒã·ã§ã³ã®ãã¹ãŠã®ããžãã¯ãå«ãŸããŠããŸãã SilverlightèŠçŽ ã衚瀺ããã«ã¯ãç¹å¥ãªUIElementRendererãªããžã§ã¯ããäœæããå¿ èŠããããŸãã
UIElementRenderer uiRenderer;
æç»ãµã€ã¯ã«ã§ã¯ãã€ã³ã¿ãŒãã§ã€ã¹ãã¬ã³ããªã³ã°ããŠæç»ããå¿ èŠããããŸãã
private void OnDraw(object sender, GameTimerEventArgs e) { SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.White); uiRenderer.Render(); spriteBatch.Begin(); spriteBatch.Draw(uiRenderer.Texture, Vector2.Zero, Color.White); spriteBatch.End(); // TODO: Add your drawing code here }
ãæ°ã¥ããããããŸããããuiRendererãªããžã§ã¯ããäœæããŸããããã©ãã§ãåæåããŸããã§ããã ãããè¡ãæåã®æ¹æ³ã«ã€ããŠã¯ãããã€ãã®èŠ³ç¹ããããŸãã ã€ã³ã¿ãŒãã§ã€ã¹ã®æŽæ°ããã£ããããŠã¬ã³ãã©ãŒã確èªããååšããªãå Žåãäžè²«æ§ããªãå Žåã¯ãåäœæããŸãã
GamePage.xaml
<Grid Name="Layout" LayoutUpdated="Layout_LayoutUpdated">
GamePage.xaml.cs
private void Layout_LayoutUpdated(object sender, EventArgs e) { int width = (int)ActualWidth; int height = (int)ActualHeight; // Ensure the page size is valid if (width <= 0 || height <= 0) return; // Do we already have a UIElementRenderer of the correct size? if (uiRenderer != null && uiRenderer.Texture != null && uiRenderer.Texture.Width == width && uiRenderer.Texture.Height == height) { return; } // Before constructing a new UIElementRenderer, be sure to Dispose the old one if (uiRenderer != null) uiRenderer.Dispose(); // Create the renderer uiRenderer = new UIElementRenderer(this, width, height); }
ããã§ãããžã§ã¯ããå床å®è¡ããŸãã ã芧ã®ãšããããã¿ã³ãé©åãªå Žæã«è¡šç€ºãããŸãã 競æå ŽãæããŸãããã
2. XNAåºå
ãã£ãŒã«ãã100x100ã»ã«ãåã»ã«ã30x30ãã¯ã»ã«ã«ãªãããšã«åæããŸãããã åèšãã£ãŒã«ãã¯3000x3000ãã¯ã»ã«ã«ãªããŸãã
Areaã¯ã©ã¹ãäœæããŸãã
public class Area { Texture2D point; Rectangle line; public Area(Texture2D point) { this.point = point; } public void Draw(SpriteBatch spriteBatch) { for (int i = 0; i < 100; i++) { line = new Rectangle(i*30, 0, 1, 3000); spriteBatch.Draw(point, line, Color.White); line = new Rectangle( 0, i*30, 3000, 1); spriteBatch.Draw(point, line, Color.White); } } }
XNAã«ã¯ç·ãæç»ããæ¹æ³ã¯ãããŸããããä»»æã®è²ãŸãã¯ãã¯ã¹ãã£ã§å¡ãã€ã¶ãããé·æ¹åœ¢ãæç»ã§ããŸãã ããã§ã¯ã1x1ãµã€ãºã®é»ãç¹ãå«ããã€ã³ããã¯ã¹ãã£ã§å¡ãã€ã¶ããã100æ¬ã®æ°Žå¹³ç·ãš100æ¬ã®åçŽç·ãäœæããŸãã
GamePage.xaml.csã§ãã£ãŒã«ããäœæããŠåæåããŸãã
Texture2D point; Area area;
XNAãšSilverlightãäžç·ã«äœ¿çšããå ŽåãçŽç²ãªXNAã¢ããªã±ãŒã·ã§ã³ïŒLoadContentïŒã®ããã«ãã³ã³ãã³ããããŒãããããã«å¥ã®ã¡ãœããã䜿çšããå¿ èŠã¯ãããŸããã éåžžãã²ãŒã ã³ã³ãã³ãã¯ãã€ã§ãããŠã³ããŒãã§ããŸãããã°ã©ãã£ãã¯ã³ã³ãã³ãã®èªã¿èŸŒã¿äžã«ã¢ããªã±ãŒã·ã§ã³ãXNAã¬ã³ããªã³ã°ã¢ãŒãã«ãªã£ãŠããããšã確èªããå¿ èŠããããŸãïŒSetSharingModeã¡ãœãããåŒã³åºããŠïŒã Silverlightã¬ã³ããªã³ã°äžã«ã°ã©ãã£ãã¯ã³ã³ãã³ããããŒãããããšãããšãäŸå€ãã¹ããŒãããŸãã ContentManagerãäœæãããããã€ã§ãéã°ã©ãã£ãã¯ã³ã³ãã³ããããŠã³ããŒãã§ããŸãã éåžžãOnNavigatedToã¡ãœããã§ã³ã³ãã³ããããŒãããŸãã
protected override void OnNavigatedTo(NavigationEventArgs e) { // Set the sharing mode of the graphics device to turn on XNA rendering SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true); // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice); // TODO: use this.content to load your game content here point = contentManager.Load<Texture2D>("point"); area = new Area(point); // Start the timer timer.Start(); base.OnNavigatedTo(e); }
OnDrawã¡ãœããã§ãã£ãŒã«ããæç»ããŸãã
private void OnDraw(object sender, GameTimerEventArgs e) { SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue); uiRenderer.Render(); spriteBatch.Begin(); area.Draw(spriteBatch) spriteBatch.End(); spriteBatch.Begin(); spriteBatch.Draw(uiRenderer.Texture, Vector2.Zero, Color.White); spriteBatch.End(); // TODO: Add your drawing code here }
ã芧ã®ãšãããå¥ã®spriteBatchã«ãã£ãŒã«ããæç»ããŸãã ãªããããªã®ã-å°ãåŸã§èª¬æããŸãã
ã¢ããªã±ãŒã·ã§ã³ãèµ·åããŸããããã§ããã¿ã³ãã£ãŒã«ãã«æå¿ããããšãã§ããŸãã
è¡šé¢çãªå°ããªå€æŽãè¡ããŸãããããã¿ã³ã®äžã«åéæã®ãããã³ã°ãè¿œå ãããã£ãŒã«ããçœã«ããŸãã
GamePage.xamlïŒ
<Grid Name="Layout" LayoutUpdated="Layout_LayoutUpdated"> <Rectangle Height="100" HorizontalAlignment="Left" Margin="0,700,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="480" Fill="#B1000000" /> <Button Content="Start" Height="71" Name="button1" Width="160" Margin="25,717,295,12" Click="button1_Click" /> <Button Content="1 gen" Height="72" HorizontalAlignment="Left" Margin="254,716,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" /> </Grid>
GamePage.xaml.csïŒ
private void OnDraw(object sender, GameTimerEventArgs e) { SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.White); uiRenderer.Render(); ...
3.ãžã§ã¹ãã£ãŒãæäœãã
XNA Frameworkã¯10çš®é¡ã®ãžã§ã¹ãã£ããµããŒãããŠããŸããããããã«ã€ããŠã®è©±ã¯å¥ã®èšäºã«å€ããŸãïŒå¿ èŠã«å¿ããŠäœæããŸãïŒã ãã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãã¿ããïŒã¯ãªãã¯ïŒãšFreeDragïŒèªç±ç§»åïŒã®2çš®é¡ã®ãžã§ã¹ãã£ã䜿çšããŸãã
ãŸãããã£ãŒã«ãã«ãã€ã³ããè¿œå ããããšããå§ããŸãããã ããã§ã¯Dotsã¯ã©ã¹ã®å®å šãªã³ãŒããæäŸããããããŸãããèšäºã®äžéšã«ããäŸãããŠã³ããŒãããŠãèªåã§ç¢ºèªã§ããŸãã ããã§ã¯ããã€ã³ããè¿œå ããããã«äœ¿çšãããAddDotã¡ãœããã®ã¿ã«é¢å¿ããããŸãã
public void AddDot(int x, int y, Vector2 shift) { DotX = (int)(x / DotSize ); DotY = (int)(y / DotSize ); DotsNow[DotX, DotY] = !DotsNow[DotX, DotY]; }
ãã®æ¹æ³ã§ã¯ãã¿ãããã€ã³ãã®åº§æšã転éããããã«åºã¥ããŠãããªãã¯ã¹å ã®ç®çã®ã»ã«ãèšç®ããŸãã ãã¹ãŠãã·ã³ãã«ã§ãã
ãŸããOnNavigatedToïŒïŒã¡ãœããã§ãžã§ã¹ãã£ãµããŒããæå¹ã«ããå¿ èŠããããŸãã
TouchPanel.EnabledGestures = GestureType.Tap | GestureType.FreeDrag;
OnUpdateã«ãŒãã§ãããã远跡ã§ããããã«ãªããŸããã
private void OnUpdate(object sender, GameTimerEventArgs e) { while (TouchPanel.IsGestureAvailable) { GestureSample gesture = TouchPanel.ReadGesture(); if (gesture.GestureType == GestureType.Tap) { dots.AddDot((int)gesture.Position.X, (int)gesture.Position.Y); } } }
OnDrawïŒïŒã¡ãœããã®ç»é¢ã«ç现èã衚瀺ããããšãå¿ããªãã§ãã ããïŒ
dots.Draw(spriteBatch);
ããã§ã¢ããªã±ãŒã·ã§ã³ãèµ·åããŠãã€ã³ããè¿œå ããããšãããšãããã«æ¶ããŸãã ãã®çç±ã¯ãæŽæ°é床ã§ãã dots.UpdateïŒïŒã®åŒã³åºãã«ã¯å€å°ã®é 延ãå¿ èŠã§ãã ç§ã®äŸã§ã¯ããã®é 延ã®æãç°¡åãªå®è£ ã¯æ¬¡ã®ãšããã§ãã
i++; if (IsGameStarted && Math.IEEERemainder(i, 15) == 0) { i = 0; dots.Update(); }
IsGameStartedã¯ã[ã¹ã¿ãŒã]ãã¿ã³ãã¯ãªãã¯ãããšå€ãå€ãããã©ã°ã§ãã 2çªç®ã®å®¿é¡ã¯ãGameTimeã¯ã©ã¹ã䜿çšããŠé 延ãå®è£ ããããšã§ãã
ããã§å®å šã«æ©èœããã²ãŒã ãã§ããŸãããããã£ãŒã«ãã®ã»ãã®äžéšããèŠããŸããã ãããä¿®æ£ããã«ã¯ããã£ãŒã«ãå ã移åããæ©èœãè¿œå ããŸãã
4.ã«ã¡ã©ã§ã®äœæ¥
æãç°¡åãªè¿äŒŒã§ã¯ãã«ã¡ã©ã¯ã²ãŒã äžçãç»åã®åœ¢ã§ç»é¢ã«æ圱ãããããªãã¯ã¹ã§ãã Matrixã¯ã©ã¹ã«ã¯ãCreateScaleïŒïŒãCreateTranslationïŒïŒãCreateRotationXïŒïŒãªã©ãå€ãã®é¢é£ã¡ãœããããããŸãã CreateTranslationïŒïŒã ããå¿ èŠã§ãã æåã«ãã²ãŒã ã¯ãŒã«ãã移åãããããªãã¯ã¹ãäœæããããã«å¿ èŠãªéãèŠã€ããŸãã
while (TouchPanel.IsGestureAvailable) { GestureSample gesture = TouchPanel.ReadGesture(); if (gesture.GestureType == GestureType.Tap) { dots.AddDot((int)gesture.Position.X, (int)gesture.Position.Y, totalShift); } if (gesture.GestureType == GestureType.FreeDrag) { shift = gesture.Delta; totalShift += shift; } } matrix *= Matrix.CreateTranslation(totalShift.X, totalShift.Y, 0);
次ã«ãæ°ããæ圱ãæãå¿ èŠããããŸãã ãããè¡ãã«ã¯ããããªãã¯ã¹ãspriteBatchã«æž¡ããŸãã
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, matrix); area.Draw(spriteBatch); dots.Draw(spriteBatch); spriteBatch.End();
ãã®ããã2çªç®ã®spriteBatchãå¿ èŠã§ããã1ã€ã§ãã¹ãŠã¬ã³ããªã³ã°ãããšããã¿ã³ããã£ãŒã«ããšãšãã«ç§»åããŸããããã£ãŒã«ãã¯å¿ èŠãããŸããã 宿é¡-ã²ãŒã ãšã€ã³ã¿ãŒãã§ã€ã¹ã®ã¬ã³ããªã³ã°ã1ã€ã®spriteBatchã«é 眮ãããšã©ããªããã確èªããŸãã
1ã€ã®åé¡ãæ®ã£ãŠããŸã-ãã€ã³ããè¿œå ãããšãã«ã«ã¡ã©ã®ã·ãããèæ ®ããŸããã ããã¯åçŽã«è§£æ±ºãããtotalShiftãAddDotïŒïŒã¡ãœããã«æž¡ãå¿ èŠããããŸãã
public void AddDot(int x, int y, Vector2 shift) { DotX = (int)(Math.Abs(x-shift.X) / DotSize ); DotY = (int)(Math.Abs(y-shift.Y) / DotSize ); DotsNow[DotX, DotY] = !DotsNow[DotX, DotY]; }
ããã§ãå¿ èŠãªå Žæã«ãã€ã³ããè¿œå ãããŸãã 宿é¡ïŒãã£ãŒã«ãã®å€åŽã«ç现èãè¿œå ããããšãããšããŸã ééã£ãåå¿ããããŸãïŒãã©ãŒãã€ã³ãã«è¿œå ããããã¢ã¬ã€ã®å¢çãè¶ãããã®ããããã§ãã ãã£ãŒã«ãã®å¢çãè¶ããããšãäžå¯èœã«ããŸãã
ãã§ã·ã£ç«
ãããã«
å®éãããã ãã§ãã Windows Phoneãšå®¿é¡ã®å®å šã«æ©èœããã²ãŒã Lifeãããããã®äžã§æ°æéé ãçããããšãã§ããŸãã çæã«ãŠã³ã¿ãŒãã©ã³ãã å å¡«ãªã©ãããŸããŸãªãã³ãäœæããããšãã§ããŸãã SilverLifeãšããããŒã±ãããã¬ã€ã¹ã§ç§ã®æçµããŒãžã§ã³ãèŠã€ããããšãã§ããŸãã
ã¢ããªã±ãŒã·ã§ã³ãœãŒã¹ãããŠã³ããŒãããŸã ã
è¯ããæ£æããéãããã ããïŒ