File tree Expand file tree Collapse file tree 13 files changed +681
-0
lines changed
tutorials/building_2d_games Expand file tree Collapse file tree 13 files changed +681
-0
lines changed Original file line number Diff line number Diff line change 158
158
href : tutorials/building_2d_games/22_snake_game_mechanics/
159
159
- name : " 23: Completing the Game"
160
160
href : tutorials/building_2d_games/23_completing_the_game/
161
+ - name : " 24: Shaders"
162
+ href : tutorials/building_2d_games/24_shaders/
161
163
- name : Console Access
162
164
href : console_access.md
163
165
- name : Help and Support
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change
1
+ #if OPENGL
2
+ #define SV_POSITION POSITION
3
+ #define VS_SHADERMODEL vs_3_0
4
+ #define PS_SHADERMODEL ps_3_0
5
+ #else
6
+ #define VS_SHADERMODEL vs_4_0_level_9_1
7
+ #define PS_SHADERMODEL ps_4_0_level_9_1
8
+ #endif
9
+
10
+ Texture2D SpriteTexture;
11
+
12
+ sampler2D SpriteTextureSampler = sampler_state
13
+ {
14
+ Texture = <SpriteTexture>;
15
+ };
16
+
17
+ struct VertexShaderOutput
18
+ {
19
+ float4 Position : SV_POSITION ;
20
+ float4 Color : COLOR0 ;
21
+ float2 TextureCoordinates : TEXCOORD0 ;
22
+ };
23
+
24
+ float4 MainPS (VertexShaderOutput input) : COLOR
25
+ {
26
+ return tex2D (SpriteTextureSampler,input.TextureCoordinates) * input.Color;
27
+ }
28
+
29
+ technique SpriteDrawing
30
+ {
31
+ pass P0
32
+ {
33
+ PixelShader = compile PS_SHADERMODEL MainPS ();
34
+ }
35
+ };
Original file line number Diff line number Diff line change
1
+ public override void Draw ( GameTime gameTime )
2
+ {
3
+ // Clear the back buffer.
4
+ Core . GraphicsDevice . Clear ( Color . CornflowerBlue ) ;
5
+
6
+ if ( _state != GameState . Playing )
7
+ {
8
+ // We are in a game over state, so apply the saturation parameter.
9
+ _grayscaleEffect . Parameters [ "Saturation" ] . SetValue ( _saturation ) ;
10
+
11
+ // And begin the sprite batch using the grayscale effect.
12
+ Core . SpriteBatch . Begin ( samplerState : SamplerState . PointClamp , effect : _grayscaleEffect ) ;
13
+ }
14
+ else
15
+ {
16
+ // Otherwise, just begin the sprite batch as normal.
17
+ Core . SpriteBatch . Begin ( samplerState : SamplerState . PointClamp ) ;
18
+ }
19
+
20
+ // Draw the tilemap
21
+ _tilemap . Draw ( Core . SpriteBatch ) ;
22
+
23
+ // Draw the slime.
24
+ _slime . Draw ( ) ;
25
+
26
+ // Draw the bat.
27
+ _bat . Draw ( ) ;
28
+
29
+ // Always end the sprite batch when finished.
30
+ Core . SpriteBatch . End ( ) ;
31
+
32
+ // Draw the UI
33
+ _ui . Draw ( ) ;
34
+ }
Original file line number Diff line number Diff line change
1
+ // The grayscale shader effect.
2
+ private Effect _grayscaleEffect ;
3
+
4
+ // The amount of saturation to provide the grayscale shader effect
5
+ private float _saturation = 1.0f ;
6
+
7
+ // The speed of the fade to grayscale effect.
8
+ private const float FADE_SPEED = 0.02f ;
Original file line number Diff line number Diff line change
1
+ private void GameOver ( )
2
+ {
3
+ // Show the game over panel
4
+ _ui . ShowGameOverPanel ( ) ;
5
+
6
+ // Set the game state to game over
7
+ _state = GameState . GameOver ;
8
+
9
+ // Set the grayscale effect saturation to 1.0f;
10
+ _saturation = 1.0f ;
11
+ }
Original file line number Diff line number Diff line change
1
+ public override void LoadContent ( )
2
+ {
3
+ // Create the texture atlas from the XML configuration file
4
+ TextureAtlas atlas = TextureAtlas . FromFile ( Core . Content , "images/atlas-definition.xml" ) ;
5
+
6
+ // Create the tilemap from the XML configuration file.
7
+ _tilemap = Tilemap . FromFile ( Content , "images/tilemap-definition.xml" ) ;
8
+ _tilemap . Scale = new Vector2 ( 4.0f , 4.0f ) ;
9
+
10
+ // Create the animated sprite for the slime from the atlas.
11
+ AnimatedSprite slimeAnimation = atlas . CreateAnimatedSprite ( "slime-animation" ) ;
12
+ slimeAnimation . Scale = new Vector2 ( 4.0f , 4.0f ) ;
13
+
14
+ // Create the slime
15
+ _slime = new Slime ( slimeAnimation ) ;
16
+
17
+ // Create the animated sprite for the bat from the atlas.
18
+ AnimatedSprite batAnimation = atlas . CreateAnimatedSprite ( "bat-animation" ) ;
19
+ batAnimation . Scale = new Vector2 ( 4.0f , 4.0f ) ;
20
+
21
+ // Load the bounce sound effect for the bat
22
+ SoundEffect bounceSoundEffect = Content . Load < SoundEffect > ( "audio/bounce" ) ;
23
+
24
+ // Create the bat
25
+ _bat = new Bat ( batAnimation , bounceSoundEffect ) ;
26
+
27
+ // Load the collect sound effect
28
+ _collectSoundEffect = Content . Load < SoundEffect > ( "audio/collect" ) ;
29
+
30
+ // Load the grayscale effect
31
+ _grayscaleEffect = Content . Load < Effect > ( "effects/grayscaleEffect" ) ;
32
+ }
Original file line number Diff line number Diff line change
1
+ private void TogglePause ( )
2
+ {
3
+ if ( _state == GameState . Paused )
4
+ {
5
+ // We're now unpausing the game, so hide the pause panel
6
+ _ui . HidePausePanel ( ) ;
7
+
8
+ // And set the state back to playing
9
+ _state = GameState . Playing ;
10
+ }
11
+ else
12
+ {
13
+ // We're now pausing the game, so show the pause panel
14
+ _ui . ShowPausePanel ( ) ;
15
+
16
+ // And set the state to paused
17
+ _state = GameState . Paused ;
18
+
19
+ // Set the grayscale effect saturation to 1.0f;
20
+ _saturation = 1.0f ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ public override void Update ( GameTime gameTime )
2
+ {
3
+ // Ensure the UI is always updated
4
+ _ui . Update ( gameTime ) ;
5
+
6
+ if ( _state != GameState . Playing )
7
+ {
8
+ // The game is in either a paused or game over state, so
9
+ // gradually decrease the saturation to create the fading grayscale.
10
+ _saturation = Math . Max ( 0.0f , _saturation - FADE_SPEED ) ;
11
+
12
+ // If its just a game over state, return back
13
+ if ( _state == GameState . GameOver )
14
+ {
15
+ return ;
16
+ }
17
+ }
18
+
19
+ // If the pause button is pressed, toggle the pause state
20
+ if ( GameController . Pause ( ) )
21
+ {
22
+ TogglePause ( ) ;
23
+ }
24
+
25
+ // At this point, if the game is paused, just return back early
26
+ if ( _state == GameState . Paused )
27
+ {
28
+ return ;
29
+ }
30
+
31
+ // Update the slime;
32
+ _slime . Update ( gameTime ) ;
33
+
34
+ // Update the bat;
35
+ _bat . Update ( gameTime ) ;
36
+
37
+ // Perform collision checks
38
+ CollisionChecks ( ) ;
39
+ }
You can’t perform that action at this time.
0 commit comments