@@ -48,14 +48,18 @@ public class Game1 : Game
4848
4949 private readonly GraphicsDeviceManager graphics ;
5050 private SpriteBatch spriteBatch ;
51+ private InputManager Input { get ; } = new InputManager ( ) ;
5152
5253 private SpriteFont Font { get ; set ; }
5354 private Texture2D Image { get ; set ; }
5455 private Point Resolution { get ; } = new Point ( 1280 , 720 ) ;
5556 private Renderer Renderer { get ; } = new Renderer ( ) ;
57+
5658 private bool IsBloom { get ; set ; } = true ;
59+ private bool IsDebug { get ; set ; } = true ;
5760 private int SettingIndex { get ; set ; }
58- private InputManager Input { get ; } = new InputManager ( ) ;
61+ private RenderTarget2D DebugTarget { get ; set ; }
62+ private GameTime GameTime { get ; set ; }
5963
6064 public Game1 ( )
6165 {
@@ -82,6 +86,14 @@ protected override void Initialize()
8286 {
8387 base . Initialize ( ) ;
8488 Renderer . Initialize ( graphics . GraphicsDevice , Resolution ) ;
89+ DebugTarget = new RenderTarget2D ( graphicsDevice : graphics . GraphicsDevice ,
90+ width : Resolution . X ,
91+ height : Resolution . Y ,
92+ mipMap : false ,
93+ preferredFormat : SurfaceFormat . Color ,
94+ preferredDepthFormat : DepthFormat . None ,
95+ preferredMultiSampleCount : 1 ,
96+ usage : RenderTargetUsage . PreserveContents ) ;
8597 }
8698
8799 /// <summary>
@@ -125,6 +137,9 @@ private void HandleInput()
125137 {
126138 if ( Input . Key . Is . Press ( Keys . Space ) )
127139 IsBloom = ! IsBloom ;
140+ if ( Input . Key . Is . Press ( Keys . Tab ) )
141+ IsDebug = ! IsDebug ;
142+
128143 if ( Input . Key . Is . Press ( Keys . OemPlus ) || Input . Key . Is . Press ( Keys . Add ) )
129144 {
130145 SettingIndex ++ ;
@@ -150,7 +165,7 @@ private void HandleInput()
150165
151166 private void HandleFloatInput ( Keys down , Keys up , float step , Fader f , ref bool isModified , bool repeat = false )
152167 {
153- f . Value = HandleFloatInput ( down , up , step , ( float ) f . Value , ref isModified , repeat ) ;
168+ f . Value = HandleFloatInput ( down , up , step , ( float ) f . Value , ref isModified , repeat ) ;
154169 }
155170
156171 private float HandleFloatInput ( Keys down , Keys up , float step , float value , ref bool isModified ,
@@ -176,8 +191,14 @@ private float HandleFloatInput(Keys down, Keys up, float step, float value, ref
176191 /// <param name="gameTime">Provides a snapshot of timing values.</param>
177192 protected override void Draw ( GameTime gameTime )
178193 {
194+ GraphicsDevice . SetRenderTarget ( DebugTarget ) ;
195+ GraphicsDevice . Clear ( Color . TransparentBlack ) ;
196+ GraphicsDevice . SetRenderTarget ( null ) ;
179197 GraphicsDevice . Clear ( Color . CornflowerBlue ) ;
198+ // Persist gameTime to pass it to the debug-renderer for font-lerping.
199+ GameTime = gameTime ;
180200 DrawImage ( ) ;
201+ DrawDebugTarget ( ) ;
181202 DrawText ( gameTime ) ;
182203 base . Draw ( gameTime ) ;
183204 }
@@ -191,7 +212,8 @@ private void DrawImage()
191212 "image" ,
192213 Image ,
193214 null ,
194- Setting . PRESET_SETTING [ SettingIndex ] ) ;
215+ Setting . PRESET_SETTING [ SettingIndex ] ,
216+ DebugDel ) ;
195217 }
196218 else
197219 {
@@ -201,17 +223,53 @@ private void DrawImage()
201223 }
202224 }
203225
204- private void DrawText ( GameTime gameTime )
226+ private void DebugDel ( string name , RenderTarget2D t , RenderPhase phase )
227+ {
228+ if ( t != null )
229+ {
230+ // The constantly switching to and from the debug-rendertarget only works if it's set to 'preserveContents' which is something
231+ // I wouldn't recommend doing in a real game-environment.
232+ GraphicsDevice . SetRenderTarget ( DebugTarget ) ;
233+ spriteBatch . Begin ( SpriteSortMode . Deferred , BlendState . AlphaBlend ) ;
234+ float f = 7 ;
235+ spriteBatch . Draw ( t ,
236+ new Rectangle ( ( ( int ) phase + 1 ) * Resolution . X / ( int ) f ,
237+ 5 * ( Resolution . Y / ( int ) f ) ,
238+ ( int ) ( Resolution . X / f ) ,
239+ ( int ) ( Resolution . Y / f ) ) ,
240+ Color . White ) ;
241+ spriteBatch . DrawString ( Font ,
242+ phase + ":" ,
243+ new Vector2 ( ( ( int ) phase + 1f ) * Resolution . X / ( int ) f , 5 * ( Resolution . Y / ( int ) f ) - 10 ) ,
244+ GetLerpColor ( GameTime ) ) ;
245+ spriteBatch . End ( ) ;
246+ }
247+ }
248+
249+ private void DrawDebugTarget ( )
250+ {
251+ if ( IsDebug )
252+ {
253+ spriteBatch . Begin ( SpriteSortMode . Deferred , BlendState . AlphaBlend ) ;
254+ spriteBatch . Draw ( DebugTarget , new Rectangle ( 0 , 0 , Resolution . X , Resolution . Y ) , Color . White ) ;
255+ spriteBatch . End ( ) ;
256+ }
257+ }
258+
259+ private Color GetLerpColor ( GameTime gameTime )
205260 {
206261 var t = .5f + .5f * ( float ) Math . Sin ( 5 * gameTime . TotalGameTime . TotalSeconds ) ;
207- spriteBatch . Begin ( SpriteSortMode . Deferred , BlendState . AlphaBlend ) ;
208- Color c = Color . Lerp ( Color . White , Color . Gray , t ) ;
262+ return Color . Lerp ( Color . White , Color . Gray , t ) ;
263+ }
209264
210- spriteBatch . DrawString ( Font , BuildText ( ) , new Vector2 ( 10 , 10 ) , c ) ;
265+ private void DrawText ( GameTime gameTime )
266+ {
267+ Color c = GetLerpColor ( gameTime ) ;
211268
269+ spriteBatch . Begin ( SpriteSortMode . Deferred , BlendState . AlphaBlend ) ;
270+ spriteBatch . DrawString ( Font , BuildText ( ) , new Vector2 ( 10 , 10 ) , c ) ;
212271 Vector2 s = Font . MeasureString ( IMAGE_COPYRIGHT ) ;
213272 spriteBatch . DrawString ( Font , IMAGE_COPYRIGHT , new Vector2 ( 30f , Resolution . Y - s . Y - 30f ) , c ) ;
214-
215273 spriteBatch . End ( ) ;
216274 }
217275
@@ -220,8 +278,10 @@ private string BuildText()
220278 Setting s = Setting . PRESET_SETTING [ SettingIndex ] ;
221279 StringBuilder sb = new StringBuilder ( ) ;
222280 string bloom = IsBloom ? "ON" : "OFF" ;
223- sb . Append ( $ "Blur Effect: { bloom } (space)\n \n ") ;
224- sb . Append ( $ "Setting: [{ SettingIndex + 1 } /{ Setting . PRESET_SETTING . Length } ] { s . Name } >(+), <(-)\n ") ;
281+ string debug = IsDebug ? "ON" : "OFF" ;
282+ sb . Append ( $ "Blur Effect: { bloom } (SPACE)\n ") ;
283+ sb . Append ( $ "Debug View: { debug } (TAB)\n \n ") ;
284+ sb . Append ( $ "Setting: [{ SettingIndex + 1 } /{ Setting . PRESET_SETTING . Length } ] { s . Name } >(+), <(-)\n ") ;
225285 sb . Append ( $ " BloomThreshold : { s . BloomThreshold . Value : 0.###} >(q), <(w)\n ") ;
226286 sb . Append ( $ " BlurAmount : { s . BlurAmount . Value : 0.###} >(a), <(s)\n ") ;
227287 sb . Append ( $ " BloomIntensity : { s . BloomIntensity . Value : 0.###} >(y), <(x)\n ") ;
0 commit comments