NetworkStateManagementGame.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //-----------------------------------------------------------------------------
  2. // Game.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.GamerServices;
  11. using Microsoft.Xna.Framework.Net;
  12. namespace NetworkStateManagement
  13. {
  14. /// <summary>
  15. /// Sample showing how to manage the different game states involved in
  16. /// implementing a networked game, with menus for creating, searching,
  17. /// and joining sessions, a lobby screen, and the game itself. This main
  18. /// game class is extremely simple: all the interesting stuff happens
  19. /// in the ScreenManager component.
  20. /// </summary>
  21. public class NetworkStateManagementGame : Game
  22. {
  23. GraphicsDeviceManager graphicsDeviceManager;
  24. ScreenManager screenManager;
  25. public ScreenManager ScreenManager { get => screenManager; set => screenManager = value; }
  26. // By preloading any assets used by UI rendering, we avoid framerate glitches
  27. // when they suddenly need to be loaded in the middle of a menu transition.
  28. static readonly string[] preloadAssets =
  29. {
  30. "gradient",
  31. "cat",
  32. "chat_ready",
  33. "chat_able",
  34. "chat_talking",
  35. "chat_mute",
  36. };
  37. /// <summary>
  38. /// The main game constructor.
  39. /// </summary>
  40. public NetworkStateManagementGame()
  41. {
  42. Content.RootDirectory = "Content";
  43. graphicsDeviceManager = new GraphicsDeviceManager(this);
  44. graphicsDeviceManager.PreferredBackBufferWidth = ScreenManager.BASE_BUFFER_WIDTH;
  45. graphicsDeviceManager.PreferredBackBufferHeight = ScreenManager.BASE_BUFFER_HEIGHT;
  46. if (UIUtility.IsMobile)
  47. {
  48. graphicsDeviceManager.IsFullScreen = true;
  49. IsMouseVisible = false;
  50. }
  51. else if (UIUtility.IsDesktop)
  52. {
  53. graphicsDeviceManager.IsFullScreen = false;
  54. IsMouseVisible = true;
  55. }
  56. else
  57. {
  58. throw new PlatformNotSupportedException();
  59. }
  60. // Create components.
  61. screenManager = new ScreenManager(this);
  62. Components.Add(screenManager);
  63. Components.Add(new MessageDisplayComponent(this));
  64. Components.Add(new GamerServicesComponent(this));
  65. // Activate the first screens.
  66. screenManager.AddScreen(new BackgroundScreen(), null);
  67. screenManager.AddScreen(new MainMenuScreen(), null);
  68. // Listen for invite notification events.
  69. NetworkSession.InviteAccepted += (sender, e) => NetworkSessionComponent.InviteAccepted(screenManager, e);
  70. // To test the trial mode behavior while developing your game,
  71. // uncomment this line:
  72. // Guide.SimulateTrialMode = true;
  73. }
  74. /// <summary>
  75. /// Loads graphics content.
  76. /// </summary>
  77. protected override void LoadContent()
  78. {
  79. foreach (string asset in preloadAssets)
  80. {
  81. Content.Load<object>(asset);
  82. }
  83. }
  84. /// <summary>
  85. /// This is called when the game should draw itself.
  86. /// </summary>
  87. protected override void Draw(GameTime gameTime)
  88. {
  89. graphicsDeviceManager.GraphicsDevice.Clear(Color.Black);
  90. // The real drawing happens inside the screen manager component.
  91. base.Draw(gameTime);
  92. }
  93. }
  94. }