Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.70 KB | None | 0 0
  1. using BloomPostprocess;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using Microsoft.Xna.Framework.Media;
  6. using monogame_test_2.Asset;
  7.  
  8. namespace monogame_test_2
  9. {
  10. /// <summary>
  11. /// This is the main type for your game.
  12. /// </summary>
  13. public class GameRoot : Game
  14. {
  15. public static GameRoot Instance { get; private set; }
  16. public static Viewport Viewport { get { return Instance.GraphicsDevice.Viewport; } }
  17. public static Vector2 ScreenSize { get { return new Vector2(Viewport.Width, Viewport.Height); } }
  18. public static GameTime GameTime { get; private set; }
  19.  
  20. GraphicsDeviceManager graphics;
  21. SpriteBatch spriteBatch;
  22.  
  23. BloomComponent bloom;
  24.  
  25. public GameRoot()
  26. {
  27. Instance = this;
  28. graphics = new GraphicsDeviceManager(this);
  29. Content.RootDirectory = "Content";
  30.  
  31. graphics.PreferredBackBufferWidth = 800;
  32. graphics.PreferredBackBufferHeight = 600;
  33.  
  34. bloom = new BloomComponent(this);
  35. Components.Add(bloom);
  36. bloom.Settings = new BloomSettings(null, 0.25f, 4, 2, 1, 1.5f, 1);
  37. }
  38.  
  39. protected override void Initialize()
  40. {
  41. // TODO: Add your initialization logic here
  42.  
  43. base.Initialize();
  44.  
  45. EntityManager.Add(PlayerShip.Instance);
  46. MediaPlayer.IsRepeating = true;
  47. MediaPlayer.Play(Sound.Music);
  48. }
  49.  
  50. protected override void LoadContent()
  51. {
  52. // Create a new SpriteBatch, which can be used to draw textures.
  53. spriteBatch = new SpriteBatch(GraphicsDevice);
  54.  
  55. // TODO: use this.Content to load your game content here
  56. Art.Load(Content);
  57. Sound.Load(Content);
  58. }
  59.  
  60. protected override void UnloadContent()
  61. {
  62. // TODO: Unload any non ContentManager content here
  63. }
  64.  
  65. protected override void Update(GameTime gameTime)
  66. {
  67. GameTime = gameTime;
  68. Input.Update();
  69.  
  70. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  71. Exit();
  72.  
  73. EntityManager.Update();
  74. EnemySpawner.Update();
  75. PlayerStatus.Update();
  76.  
  77. base.Update(gameTime);
  78. }
  79.  
  80. protected override void Draw(GameTime gameTime)
  81. {
  82. //GraphicsDevice.SetRenderTarget(null);
  83. bloom.BeginDraw();
  84. GraphicsDevice.Clear(Color.CornflowerBlue);
  85.  
  86.  
  87. // draw entities, sort by texture for better batching
  88. spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Additive);
  89. EntityManager.Draw(spriteBatch);
  90. spriteBatch.End();
  91.  
  92.  
  93. // draw user interface
  94. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
  95. spriteBatch.DrawString(Art.Font, "Lives: " + PlayerStatus.Lives, new Vector2(5), Color.White);
  96. DrawRightAlignedString("Score: " + PlayerStatus.Score, 5);
  97. DrawRightAlignedString("Multiplier: " + PlayerStatus.Multiplier, 35);
  98.  
  99. // draw the custom mouse cursor
  100. spriteBatch.Draw(Art.Pointer, Input.MousePosition, Color.White);
  101.  
  102. if (PlayerStatus.IsGameOver)
  103. {
  104. string text = "Game Overn" +
  105. "Your Score: " + PlayerStatus.Score + "n" +
  106. "High Score: " + PlayerStatus.HighScore;
  107.  
  108. Vector2 textSize = Art.Font.MeasureString(text);
  109. spriteBatch.DrawString(Art.Font, text, ScreenSize / 2 - textSize / 2, Color.White);
  110. }
  111.  
  112. spriteBatch.End();
  113.  
  114. base.Draw(gameTime);
  115. }
  116.  
  117. private void DrawRightAlignedString(string text, float y)
  118. {
  119. var textWidth = Art.Font.MeasureString(text).X;
  120. spriteBatch.DrawString(Art.Font, text, new Vector2(ScreenSize.X - textWidth - 5, y), Color.White);
  121. }
  122. }
  123. }
  124.  
  125. #region File Description
  126. //-----------------------------------------------------------------------------
  127. // BloomSettings.cs
  128. //
  129. // Microsoft XNA Community Game Platform
  130. // Copyright (C) Microsoft Corporation. All rights reserved.
  131. //-----------------------------------------------------------------------------
  132. #endregion
  133.  
  134. namespace BloomPostprocess
  135. {
  136. /// <summary>
  137. /// Class holds all the settings used to tweak the bloom effect.
  138. /// </summary>
  139. public class BloomSettings
  140. {
  141. #region Fields
  142.  
  143.  
  144. // Name of a preset bloom setting, for display to the user.
  145. public readonly string Name;
  146.  
  147.  
  148. // Controls how bright a pixel needs to be before it will bloom.
  149. // Zero makes everything bloom equally, while higher values select
  150. // only brighter colors. Somewhere between 0.25 and 0.5 is good.
  151. public readonly float BloomThreshold;
  152.  
  153.  
  154. // Controls how much blurring is applied to the bloom image.
  155. // The typical range is from 1 up to 10 or so.
  156. public readonly float BlurAmount;
  157.  
  158.  
  159. // Controls the amount of the bloom and base images that
  160. // will be mixed into the final scene. Range 0 to 1.
  161. public readonly float BloomIntensity;
  162. public readonly float BaseIntensity;
  163.  
  164.  
  165. // Independently control the color saturation of the bloom and
  166. // base images. Zero is totally desaturated, 1.0 leaves saturation
  167. // unchanged, while higher values increase the saturation level.
  168. public readonly float BloomSaturation;
  169. public readonly float BaseSaturation;
  170.  
  171.  
  172. #endregion
  173.  
  174.  
  175. /// <summary>
  176. /// Constructs a new bloom settings descriptor.
  177. /// </summary>
  178. public BloomSettings(string name, float bloomThreshold, float blurAmount,
  179. float bloomIntensity, float baseIntensity,
  180. float bloomSaturation, float baseSaturation)
  181. {
  182. Name = name;
  183. BloomThreshold = bloomThreshold;
  184. BlurAmount = blurAmount;
  185. BloomIntensity = bloomIntensity;
  186. BaseIntensity = baseIntensity;
  187. BloomSaturation = bloomSaturation;
  188. BaseSaturation = baseSaturation;
  189. }
  190.  
  191.  
  192. /// <summary>
  193. /// Table of preset bloom settings, used by the sample program.
  194. /// </summary>
  195. public static BloomSettings[] PresetSettings =
  196. {
  197. // Name Thresh Blur Bloom Base BloomSat BaseSat
  198. new BloomSettings("Default", 0.25f, 4, 1.25f, 1, 1, 1),
  199. new BloomSettings("Soft", 0, 3, 1, 1, 1, 1),
  200. new BloomSettings("Desaturated", 0.5f, 8, 2, 1, 0, 1),
  201. new BloomSettings("Saturated", 0.25f, 4, 2, 1, 2, 0),
  202. new BloomSettings("Blurry", 0, 2, 1, 0.1f, 1, 1),
  203. new BloomSettings("Subtle", 0.5f, 2, 1, 1, 1, 1),
  204. };
  205. }
  206. }
  207.  
  208. using BloomPostprocess;
  209. using Microsoft.Xna.Framework;
  210. using Microsoft.Xna.Framework.Graphics;
  211. using Microsoft.Xna.Framework.Input;
  212. using Microsoft.Xna.Framework.Media;
  213. using monogame_test_2.Asset;
  214.  
  215. namespace monogame_test_2
  216. {
  217. /// <summary>
  218. /// This is the main type for your game.
  219. /// </summary>
  220. public class GameRoot : Game
  221. {
  222. public static GameRoot Instance { get; private set; }
  223. public static Viewport Viewport { get { return Instance.GraphicsDevice.Viewport; } }
  224. public static Vector2 ScreenSize { get { return new Vector2(Viewport.Width, Viewport.Height); } }
  225. public static GameTime GameTime { get; private set; }
  226.  
  227. GraphicsDeviceManager graphics;
  228. SpriteBatch spriteBatch;
  229.  
  230. BloomComponent bloom;
  231.  
  232. public GameRoot()
  233. {
  234. Instance = this;
  235. graphics = new GraphicsDeviceManager(this);
  236. Content.RootDirectory = "Content";
  237.  
  238. graphics.PreferredBackBufferWidth = 800;
  239. graphics.PreferredBackBufferHeight = 600;
  240.  
  241. bloom = new BloomComponent(this);
  242. Components.Add(bloom);
  243. bloom.Settings = new BloomSettings(null, 0.25f, 4, 2, 1, 1.5f, 1);
  244. }
  245.  
  246. protected override void Initialize()
  247. {
  248. // TODO: Add your initialization logic here
  249.  
  250. base.Initialize();
  251.  
  252. EntityManager.Add(PlayerShip.Instance);
  253. MediaPlayer.IsRepeating = true;
  254. MediaPlayer.Play(Sound.Music);
  255. }
  256.  
  257. protected override void LoadContent()
  258. {
  259. // Create a new SpriteBatch, which can be used to draw textures.
  260. spriteBatch = new SpriteBatch(GraphicsDevice);
  261.  
  262. // TODO: use this.Content to load your game content here
  263. Art.Load(Content);
  264. Sound.Load(Content);
  265. }
  266.  
  267. protected override void UnloadContent()
  268. {
  269. // TODO: Unload any non ContentManager content here
  270. }
  271.  
  272. protected override void Update(GameTime gameTime)
  273. {
  274. GameTime = gameTime;
  275. Input.Update();
  276.  
  277. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  278. Exit();
  279.  
  280. EntityManager.Update();
  281. EnemySpawner.Update();
  282. PlayerStatus.Update();
  283.  
  284. base.Update(gameTime);
  285. }
  286.  
  287. protected override void Draw(GameTime gameTime)
  288. {
  289. //GraphicsDevice.SetRenderTarget(null);
  290. bloom.BeginDraw();
  291. GraphicsDevice.Clear(Color.CornflowerBlue);
  292.  
  293.  
  294. // draw entities, sort by texture for better batching
  295. spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Additive);
  296. EntityManager.Draw(spriteBatch);
  297. spriteBatch.End();
  298.  
  299.  
  300. // draw user interface
  301. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
  302. spriteBatch.DrawString(Art.Font, "Lives: " + PlayerStatus.Lives, new Vector2(5), Color.White);
  303. DrawRightAlignedString("Score: " + PlayerStatus.Score, 5);
  304. DrawRightAlignedString("Multiplier: " + PlayerStatus.Multiplier, 35);
  305.  
  306. // draw the custom mouse cursor
  307. spriteBatch.Draw(Art.Pointer, Input.MousePosition, Color.White);
  308.  
  309. if (PlayerStatus.IsGameOver)
  310. {
  311. string text = "Game Overn" +
  312. "Your Score: " + PlayerStatus.Score + "n" +
  313. "High Score: " + PlayerStatus.HighScore;
  314.  
  315. Vector2 textSize = Art.Font.MeasureString(text);
  316. spriteBatch.DrawString(Art.Font, text, ScreenSize / 2 - textSize / 2, Color.White);
  317. }
  318.  
  319. spriteBatch.End();
  320.  
  321. base.Draw(gameTime);
  322. }
  323.  
  324. private void DrawRightAlignedString(string text, float y)
  325. {
  326. var textWidth = Art.Font.MeasureString(text).X;
  327. spriteBatch.DrawString(Art.Font, text, new Vector2(ScreenSize.X - textWidth - 5, y), Color.White);
  328. }
  329. }
  330. }
  331.  
  332. // Pixel shader combines the bloom image with the original
  333. // scene, using tweakable intensity levels and saturation.
  334. // This is the final step in applying a bloom postprocess.
  335.  
  336. sampler BloomSampler : register(s0);
  337. sampler BaseSampler : register(s1);
  338.  
  339. float BloomIntensity;
  340. float BaseIntensity;
  341.  
  342. float BloomSaturation;
  343. float BaseSaturation;
  344.  
  345.  
  346. // Helper for modifying the saturation of a color.
  347. float4 AdjustSaturation(float4 color, float saturation)
  348. {
  349. // The constants 0.3, 0.59, and 0.11 are chosen because the
  350. // human eye is more sensitive to green light, and less to blue.
  351. float grey = dot(color, float3(0.3, 0.59, 0.11));
  352.  
  353. return lerp(grey, color, saturation);
  354. }
  355.  
  356.  
  357. float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
  358. {
  359. // Look up the bloom and original base image colors.
  360. float4 bloom = tex2D(BloomSampler, texCoord);
  361. float4 base = tex2D(BaseSampler, texCoord);
  362.  
  363. // Adjust color saturation and intensity.
  364. bloom = AdjustSaturation(bloom, BloomSaturation) * BloomIntensity;
  365. base = AdjustSaturation(base, BaseSaturation) * BaseIntensity;
  366.  
  367. // Darken down the base image in areas where there is a lot of bloom,
  368. // to prevent things looking excessively burned-out.
  369. base *= (1 - saturate(bloom));
  370.  
  371. // Combine the two images.
  372. return base + bloom;
  373. }
  374.  
  375.  
  376. technique BloomCombine
  377. {
  378. pass Pass1
  379. {
  380. PixelShader = compile ps_2_0 PixelShaderFunction();
  381. }
  382. }
  383.  
  384. // Pixel shader extracts the brighter areas of an image.
  385. // This is the first step in applying a bloom postprocess.
  386.  
  387. sampler TextureSampler : register(s0);
  388.  
  389. float BloomThreshold;
  390.  
  391.  
  392. float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
  393. {
  394. // Look up the original image color.
  395. float4 c = tex2D(TextureSampler, texCoord);
  396.  
  397. // Adjust it to keep only values brighter than the specified threshold.
  398. return saturate((c - BloomThreshold) / (1 - BloomThreshold));
  399. }
  400.  
  401.  
  402. technique BloomExtract
  403. {
  404. pass Pass1
  405. {
  406. PixelShader = compile ps_2_0 PixelShaderFunction();
  407. }
  408. }
  409.  
  410. // Pixel shader applies a one dimensional gaussian blur filter.
  411. // This is used twice by the bloom postprocess, first to
  412. // blur horizontally, and then again to blur vertically.
  413.  
  414. sampler TextureSampler : register(s0);
  415.  
  416. #define SAMPLE_COUNT 15
  417.  
  418. float2 SampleOffsets[SAMPLE_COUNT];
  419. float SampleWeights[SAMPLE_COUNT];
  420.  
  421.  
  422. float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
  423. {
  424. float4 c = 0;
  425.  
  426. // Combine a number of weighted image filter taps.
  427. for (int i = 0; i < SAMPLE_COUNT; i++)
  428. {
  429. c += tex2D(TextureSampler, texCoord + SampleOffsets[i]) * SampleWeights[i];
  430. }
  431.  
  432. return c;
  433. }
  434.  
  435.  
  436. technique GaussianBlur
  437. {
  438. pass Pass1
  439. {
  440. PixelShader = compile ps_2_0 PixelShaderFunction();
  441. }
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement