Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BloomPostprocess;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using monogame_test_2.Asset;
- namespace monogame_test_2
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class GameRoot : Game
- {
- public static GameRoot Instance { get; private set; }
- public static Viewport Viewport { get { return Instance.GraphicsDevice.Viewport; } }
- public static Vector2 ScreenSize { get { return new Vector2(Viewport.Width, Viewport.Height); } }
- public static GameTime GameTime { get; private set; }
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- BloomComponent bloom;
- public GameRoot()
- {
- Instance = this;
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 600;
- bloom = new BloomComponent(this);
- Components.Add(bloom);
- bloom.Settings = new BloomSettings(null, 0.25f, 4, 2, 1, 1.5f, 1);
- }
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- base.Initialize();
- EntityManager.Add(PlayerShip.Instance);
- MediaPlayer.IsRepeating = true;
- MediaPlayer.Play(Sound.Music);
- }
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- // TODO: use this.Content to load your game content here
- Art.Load(Content);
- Sound.Load(Content);
- }
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- protected override void Update(GameTime gameTime)
- {
- GameTime = gameTime;
- Input.Update();
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- EntityManager.Update();
- EnemySpawner.Update();
- PlayerStatus.Update();
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- //GraphicsDevice.SetRenderTarget(null);
- bloom.BeginDraw();
- GraphicsDevice.Clear(Color.CornflowerBlue);
- // draw entities, sort by texture for better batching
- spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Additive);
- EntityManager.Draw(spriteBatch);
- spriteBatch.End();
- // draw user interface
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
- spriteBatch.DrawString(Art.Font, "Lives: " + PlayerStatus.Lives, new Vector2(5), Color.White);
- DrawRightAlignedString("Score: " + PlayerStatus.Score, 5);
- DrawRightAlignedString("Multiplier: " + PlayerStatus.Multiplier, 35);
- // draw the custom mouse cursor
- spriteBatch.Draw(Art.Pointer, Input.MousePosition, Color.White);
- if (PlayerStatus.IsGameOver)
- {
- string text = "Game Overn" +
- "Your Score: " + PlayerStatus.Score + "n" +
- "High Score: " + PlayerStatus.HighScore;
- Vector2 textSize = Art.Font.MeasureString(text);
- spriteBatch.DrawString(Art.Font, text, ScreenSize / 2 - textSize / 2, Color.White);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- private void DrawRightAlignedString(string text, float y)
- {
- var textWidth = Art.Font.MeasureString(text).X;
- spriteBatch.DrawString(Art.Font, text, new Vector2(ScreenSize.X - textWidth - 5, y), Color.White);
- }
- }
- }
- #region File Description
- //-----------------------------------------------------------------------------
- // BloomSettings.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- namespace BloomPostprocess
- {
- /// <summary>
- /// Class holds all the settings used to tweak the bloom effect.
- /// </summary>
- public class BloomSettings
- {
- #region Fields
- // Name of a preset bloom setting, for display to the user.
- public readonly string Name;
- // Controls how bright a pixel needs to be before it will bloom.
- // Zero makes everything bloom equally, while higher values select
- // only brighter colors. Somewhere between 0.25 and 0.5 is good.
- public readonly float BloomThreshold;
- // Controls how much blurring is applied to the bloom image.
- // The typical range is from 1 up to 10 or so.
- public readonly float BlurAmount;
- // Controls the amount of the bloom and base images that
- // will be mixed into the final scene. Range 0 to 1.
- public readonly float BloomIntensity;
- public readonly float BaseIntensity;
- // Independently control the color saturation of the bloom and
- // base images. Zero is totally desaturated, 1.0 leaves saturation
- // unchanged, while higher values increase the saturation level.
- public readonly float BloomSaturation;
- public readonly float BaseSaturation;
- #endregion
- /// <summary>
- /// Constructs a new bloom settings descriptor.
- /// </summary>
- public BloomSettings(string name, float bloomThreshold, float blurAmount,
- float bloomIntensity, float baseIntensity,
- float bloomSaturation, float baseSaturation)
- {
- Name = name;
- BloomThreshold = bloomThreshold;
- BlurAmount = blurAmount;
- BloomIntensity = bloomIntensity;
- BaseIntensity = baseIntensity;
- BloomSaturation = bloomSaturation;
- BaseSaturation = baseSaturation;
- }
- /// <summary>
- /// Table of preset bloom settings, used by the sample program.
- /// </summary>
- public static BloomSettings[] PresetSettings =
- {
- // Name Thresh Blur Bloom Base BloomSat BaseSat
- new BloomSettings("Default", 0.25f, 4, 1.25f, 1, 1, 1),
- new BloomSettings("Soft", 0, 3, 1, 1, 1, 1),
- new BloomSettings("Desaturated", 0.5f, 8, 2, 1, 0, 1),
- new BloomSettings("Saturated", 0.25f, 4, 2, 1, 2, 0),
- new BloomSettings("Blurry", 0, 2, 1, 0.1f, 1, 1),
- new BloomSettings("Subtle", 0.5f, 2, 1, 1, 1, 1),
- };
- }
- }
- using BloomPostprocess;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using monogame_test_2.Asset;
- namespace monogame_test_2
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class GameRoot : Game
- {
- public static GameRoot Instance { get; private set; }
- public static Viewport Viewport { get { return Instance.GraphicsDevice.Viewport; } }
- public static Vector2 ScreenSize { get { return new Vector2(Viewport.Width, Viewport.Height); } }
- public static GameTime GameTime { get; private set; }
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- BloomComponent bloom;
- public GameRoot()
- {
- Instance = this;
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 600;
- bloom = new BloomComponent(this);
- Components.Add(bloom);
- bloom.Settings = new BloomSettings(null, 0.25f, 4, 2, 1, 1.5f, 1);
- }
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- base.Initialize();
- EntityManager.Add(PlayerShip.Instance);
- MediaPlayer.IsRepeating = true;
- MediaPlayer.Play(Sound.Music);
- }
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- // TODO: use this.Content to load your game content here
- Art.Load(Content);
- Sound.Load(Content);
- }
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- protected override void Update(GameTime gameTime)
- {
- GameTime = gameTime;
- Input.Update();
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- EntityManager.Update();
- EnemySpawner.Update();
- PlayerStatus.Update();
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- //GraphicsDevice.SetRenderTarget(null);
- bloom.BeginDraw();
- GraphicsDevice.Clear(Color.CornflowerBlue);
- // draw entities, sort by texture for better batching
- spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Additive);
- EntityManager.Draw(spriteBatch);
- spriteBatch.End();
- // draw user interface
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
- spriteBatch.DrawString(Art.Font, "Lives: " + PlayerStatus.Lives, new Vector2(5), Color.White);
- DrawRightAlignedString("Score: " + PlayerStatus.Score, 5);
- DrawRightAlignedString("Multiplier: " + PlayerStatus.Multiplier, 35);
- // draw the custom mouse cursor
- spriteBatch.Draw(Art.Pointer, Input.MousePosition, Color.White);
- if (PlayerStatus.IsGameOver)
- {
- string text = "Game Overn" +
- "Your Score: " + PlayerStatus.Score + "n" +
- "High Score: " + PlayerStatus.HighScore;
- Vector2 textSize = Art.Font.MeasureString(text);
- spriteBatch.DrawString(Art.Font, text, ScreenSize / 2 - textSize / 2, Color.White);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- private void DrawRightAlignedString(string text, float y)
- {
- var textWidth = Art.Font.MeasureString(text).X;
- spriteBatch.DrawString(Art.Font, text, new Vector2(ScreenSize.X - textWidth - 5, y), Color.White);
- }
- }
- }
- // Pixel shader combines the bloom image with the original
- // scene, using tweakable intensity levels and saturation.
- // This is the final step in applying a bloom postprocess.
- sampler BloomSampler : register(s0);
- sampler BaseSampler : register(s1);
- float BloomIntensity;
- float BaseIntensity;
- float BloomSaturation;
- float BaseSaturation;
- // Helper for modifying the saturation of a color.
- float4 AdjustSaturation(float4 color, float saturation)
- {
- // The constants 0.3, 0.59, and 0.11 are chosen because the
- // human eye is more sensitive to green light, and less to blue.
- float grey = dot(color, float3(0.3, 0.59, 0.11));
- return lerp(grey, color, saturation);
- }
- float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
- {
- // Look up the bloom and original base image colors.
- float4 bloom = tex2D(BloomSampler, texCoord);
- float4 base = tex2D(BaseSampler, texCoord);
- // Adjust color saturation and intensity.
- bloom = AdjustSaturation(bloom, BloomSaturation) * BloomIntensity;
- base = AdjustSaturation(base, BaseSaturation) * BaseIntensity;
- // Darken down the base image in areas where there is a lot of bloom,
- // to prevent things looking excessively burned-out.
- base *= (1 - saturate(bloom));
- // Combine the two images.
- return base + bloom;
- }
- technique BloomCombine
- {
- pass Pass1
- {
- PixelShader = compile ps_2_0 PixelShaderFunction();
- }
- }
- // Pixel shader extracts the brighter areas of an image.
- // This is the first step in applying a bloom postprocess.
- sampler TextureSampler : register(s0);
- float BloomThreshold;
- float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
- {
- // Look up the original image color.
- float4 c = tex2D(TextureSampler, texCoord);
- // Adjust it to keep only values brighter than the specified threshold.
- return saturate((c - BloomThreshold) / (1 - BloomThreshold));
- }
- technique BloomExtract
- {
- pass Pass1
- {
- PixelShader = compile ps_2_0 PixelShaderFunction();
- }
- }
- // Pixel shader applies a one dimensional gaussian blur filter.
- // This is used twice by the bloom postprocess, first to
- // blur horizontally, and then again to blur vertically.
- sampler TextureSampler : register(s0);
- #define SAMPLE_COUNT 15
- float2 SampleOffsets[SAMPLE_COUNT];
- float SampleWeights[SAMPLE_COUNT];
- float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
- {
- float4 c = 0;
- // Combine a number of weighted image filter taps.
- for (int i = 0; i < SAMPLE_COUNT; i++)
- {
- c += tex2D(TextureSampler, texCoord + SampleOffsets[i]) * SampleWeights[i];
- }
- return c;
- }
- technique GaussianBlur
- {
- pass Pass1
- {
- PixelShader = compile ps_2_0 PixelShaderFunction();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement