Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

Commit

Permalink
[VisualStudio] Add SharpDX VisualStudio package with project template…
Browse files Browse the repository at this point in the history
…s for the Toolkit
  • Loading branch information
xoofx committed Aug 8, 2013
1 parent 4ef3758 commit 4700eff
Show file tree
Hide file tree
Showing 32 changed files with 5,381 additions and 0 deletions.
221 changes: 221 additions & 0 deletions Source/VisualStudio/DesktopGame/$safeclassname$.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
using System;
using SharpDX;
using SharpDX.Toolkit;
using SharpDX.Toolkit.Graphics;

namespace $safeprojectname$
{
/// <summary>
/// Simple $safeclassname$ game using SharpDX.Toolkit.
/// </summary>
public class $safeclassname$ : Game
{
private GraphicsDeviceManager graphicsDeviceManager;
$if$ ($sharpdx_feature_spritebatch$ == true) private SpriteBatch spriteBatch;
$endif$$if$ ($sharpdx_feature_spritetexture$ == true) private Texture2D ballsTexture;
$endif$$if$ ($sharpdx_feature_spritefont$ == true) private SpriteFont arial16Font;
$endif$$if$ ($sharpdx_feature_3d$ == true)
private Matrix view;
private Matrix projection;
$endif$$if$ ($sharpdx_feature_model3d$ == true)
private Model model;
$endif$$if$ ($sharpdx_feature_bloomeffect$ == true)
private Effect bloomEffect;
private RenderTarget2D renderTargetOffScreen;
private RenderTarget2D[] renderTargetDownScales;
private RenderTarget2D renderTargetBlurTemp;
$endif$$if$ ($sharpdx_feature_primitive3d$ == true)
private BasicEffect basicEffect;
private GeometricPrimitive primitive;
$endif$
/// <summary>
/// Initializes a new instance of the <see cref="$safeclassname$" /> class.
/// </summary>
public $safeclassname$()
{
// Creates a graphics manager. This is mandatory.
graphicsDeviceManager = new GraphicsDeviceManager(this);

// Setup the relative directory to the executable directory
// for loading contents with the ContentManager
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
// Modify the title of the window
Window.Title = "$safeclassname$";

base.Initialize();
}

protected override void LoadContent()
{
$if$ ($sharpdx_feature_spritebatch$ == true) // Instantiate a SpriteBatch
spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));
$endif$$if$ ($sharpdx_feature_spritetexture$ == true)
// Loads the balls texture (32 textures (32x32) stored vertically => 32 x 1024 ).
ballsTexture = Content.Load<Texture2D>("Balls");
$endif$$if$ ($sharpdx_feature_spritefont$ == true)
// Loads a sprite font
arial16Font = Content.Load<SpriteFont>("Arial16");
$endif$$if$ ($sharpdx_feature_model3d$ == true)
// Load a 3D model
model = Content.Load<Model>("Ship");

// Enable default lighting on model.
BasicEffect.EnableDefaultLighting(model, true);
$endif$$if$ ($sharpdx_feature_bloomeffect$ == true)
// Bloom Effect
bloomEffect = Content.Load<Effect>("Bloom");

// Creates render targets for bloom effect
renderTargetDownScales = new RenderTarget2D[5];
var backDesc = GraphicsDevice.BackBuffer.Description;
renderTargetOffScreen = ToDisposeContent(RenderTarget2D.New(GraphicsDevice, backDesc.Width, backDesc.Height, 1, backDesc.Format));
for (int i = 0; i < renderTargetDownScales.Length; i++)
{
renderTargetDownScales[i] = ToDisposeContent(RenderTarget2D.New(GraphicsDevice, backDesc.Width >> i, backDesc.Height >> i, 1, backDesc.Format));
}
renderTargetBlurTemp = ToDisposeContent((RenderTarget2D)renderTargetDownScales[renderTargetDownScales.Length - 1].Clone());
$endif$$if$ ($sharpdx_feature_primitive3d$ == true)
// Creates a basic effect
basicEffect = ToDisposeContent(new BasicEffect(GraphicsDevice));
basicEffect.PreferPerPixelLighting = true;
basicEffect.EnableDefaultLighting();

// Creates torus primitive
primitive = ToDisposeContent(GeometricPrimitive.Torus.New(GraphicsDevice));
$endif$
base.LoadContent();
}

protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
$if$ ($sharpdx_feature_3d$ == true)
// Calculates the world and the view based on the model size
view = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, -7.0f), new Vector3(0, 0.0f, 0), Vector3.UnitY);
projection = Matrix.PerspectiveFovLH(0.9f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, 100.0f);
$endif$
$if$ ($sharpdx_feature_primitive3d$ == true) // Update basic effect for rendering the Primitive
basicEffect.View = view;
basicEffect.Projection = projection;
$endif$}

protected override void Draw(GameTime gameTime)
{
// Use time in seconds directly
var time = (float)gameTime.TotalGameTime.TotalSeconds;
$if$ ($sharpdx_feature_bloomeffect$ == true)
// Make offline rendering
GraphicsDevice.SetRenderTargets(GraphicsDevice.DepthStencilBuffer, renderTargetOffScreen);
$endif$
// Clears the screen with the Color.CornflowerBlue
GraphicsDevice.Clear(Color.CornflowerBlue);
$if$ ($sharpdx_feature_3d$ == true)
// Constant used to translate 3d models
float translateX = 0.0f;
$endif$$if$ ($sharpdx_feature_model3d$ == true)
// ------------------------------------------------------------------------
// Draw the 3d model
// ------------------------------------------------------------------------
var world = Matrix.Scaling(0.003f) *
Matrix.RotationY(time) *
Matrix.Translation(0, -1.5f, 2.0f);
model.Draw(GraphicsDevice, world, view, projection);
translateX += 3.5f;
$endif$$if$ ($sharpdx_feature_primitive3d$ == true)
// ------------------------------------------------------------------------
// Draw the 3d primitive using BasicEffect
// ------------------------------------------------------------------------
basicEffect.World = Matrix.Scaling(2.0f, 2.0f, 2.0f) *
Matrix.RotationX(0.8f * (float)Math.Sin(time * 1.45)) *
Matrix.RotationY(time * 2.0f) *
Matrix.RotationZ(0) *
Matrix.Translation(translateX, -1.0f, 0);
primitive.Draw(basicEffect);
$endif$$if$ ($sharpdx_feature_spritefont$ == true)
// ------------------------------------------------------------------------
// Draw the some 2d text
// ------------------------------------------------------------------------
spriteBatch.Begin();
spriteBatch.DrawString(arial16Font, "This text is displayed with SpriteBatch", new Vector2(16, 16), Color.White);
spriteBatch.End();
$endif$$if$ ($sharpdx_feature_spritetexture$ == true)
// ------------------------------------------------------------------------
// Use SpriteBatch to draw some balls on the screen using NonPremultiplied mode
// as the sprite texture used is not premultiplied
// ------------------------------------------------------------------------
spriteBatch.Begin(SpriteSortMode.Deferred, GraphicsDevice.BlendStates.NonPremultiplied);
for (int i = 0; i < 40; i++)
{
var posX = (float)Math.Cos(time * 4.5f + i * 0.1f) * 60.0f + 136.0f;
var posY = GraphicsDevice.BackBuffer.Height * 2.0f / 3.0f + 100.0f * (float)Math.Sin(time * 10.0f + i * 0.4f);

spriteBatch.Draw(
ballsTexture,
new Vector2(posX, posY),
new Rectangle(0, 0, 32, 32),
Color.White,
0.0f,
new Vector2(16, 16),
Vector2.One,
SpriteEffects.None,
0f);
}
spriteBatch.End();
$endif$$if$ ($sharpdx_feature_bloomeffect$ == true)
// ------------------------------------------------------------------------
// Cheap bloom post effect
// Blur applied only on latest downscale render target
// ------------------------------------------------------------------------

// Setup states for posteffect
GraphicsDevice.SetRasterizerState(GraphicsDevice.RasterizerStates.Default);
GraphicsDevice.SetBlendState(GraphicsDevice.BlendStates.Default);
GraphicsDevice.SetDepthStencilState(GraphicsDevice.DepthStencilStates.None);

// Apply BrightPass
const float brightPassThreshold = 0.5f;
GraphicsDevice.SetRenderTargets(renderTargetDownScales[0]);
bloomEffect.CurrentTechnique = bloomEffect.Techniques["BrightPassTechnique"];
bloomEffect.Parameters["Texture"].SetResource(renderTargetOffScreen);
bloomEffect.Parameters["PointSampler"].SetResource(GraphicsDevice.SamplerStates.PointClamp);
bloomEffect.Parameters["BrightPassThreshold"].SetValue(brightPassThreshold);
GraphicsDevice.DrawQuad(bloomEffect.CurrentTechnique.Passes[0]);

// Down scale passes
for (int i = 1; i < renderTargetDownScales.Length; i++)
{
GraphicsDevice.SetRenderTargets(renderTargetDownScales[i]);
GraphicsDevice.DrawQuad(renderTargetDownScales[0]);
}

// Horizontal blur pass
var renderTargetBlur = renderTargetDownScales[renderTargetDownScales.Length - 1];
GraphicsDevice.SetRenderTargets(renderTargetBlurTemp);
bloomEffect.CurrentTechnique = bloomEffect.Techniques["BlurPassTechnique"];
bloomEffect.Parameters["Texture"].SetResource(renderTargetBlur);
bloomEffect.Parameters["LinearSampler"].SetResource(GraphicsDevice.SamplerStates.LinearClamp);
bloomEffect.Parameters["TextureTexelSize"].SetValue(new Vector2(1.0f / renderTargetBlurTemp.Width, 1.0f / renderTargetBlurTemp.Height));
GraphicsDevice.DrawQuad(bloomEffect.CurrentTechnique.Passes[0]);

// Vertical blur pass
GraphicsDevice.SetRenderTargets(renderTargetBlur);
bloomEffect.Parameters["Texture"].SetResource(renderTargetBlurTemp);
GraphicsDevice.DrawQuad(bloomEffect.CurrentTechnique.Passes[1]);

// Render to screen
GraphicsDevice.SetRenderTargets(GraphicsDevice.BackBuffer);
GraphicsDevice.DrawQuad(renderTargetOffScreen);

// Add bloom on top of it
GraphicsDevice.SetBlendState(GraphicsDevice.BlendStates.Additive);
GraphicsDevice.DrawQuad(renderTargetBlur);
GraphicsDevice.SetBlendState(GraphicsDevice.BlendStates.Default);
$endif$
base.Draw(gameTime);
}
}
}
18 changes: 18 additions & 0 deletions Source/VisualStudio/DesktopGame/Arial16.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<TkFont>
<FontName>Arial</FontName>
<Size>16</Size>
<Spacing>0</Spacing>
<LineSpacing>0</LineSpacing>
<UseKerning>false</UseKerning>
<Format>Auto</Format>
<CharacterRegions>
<CharacterRegion>
<Start>32</Start>
<End>127</End>
</CharacterRegion>
</CharacterRegions>
<DefaultCharacter>32</DefaultCharacter>
<Style>Regular</Style>
<NoPremultiply>false</NoPremultiply>
</TkFont>
36 changes: 36 additions & 0 deletions Source/VisualStudio/DesktopGame/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("$projectname$")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("$registeredorganization$")]
[assembly: AssemblyProduct("$projectname$")]
[assembly: AssemblyCopyright("Copyright © $registeredorganization$ $year$")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("$guid1$")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file added Source/VisualStudio/DesktopGame/Balls.dds
Binary file not shown.
84 changes: 84 additions & 0 deletions Source/VisualStudio/DesktopGame/Bloom.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Shared parameter
Texture2D<float4> Texture;

// -----------------------------------------------------
// BrightPass filter
// -----------------------------------------------------
SamplerState PointSampler;
float BrightPassThreshold;

float4 PSBrightPassFilter(float2 tex : TEXCOORD) : SV_TARGET
{
float3 color = Texture.Sample(PointSampler, tex).rgb;

// Calculate perceptive luminance
float luminance = dot(color, float3(0.299, 0.587, 0.114));

// Apply threshold
color *= max(luminance - BrightPassThreshold, 0.0f) / luminance;

return float4(color, 1.0);
}

// -----------------------------------------------------
// Blur5x5 filter
// -----------------------------------------------------
float2 TextureTexelSize;
SamplerState LinearSampler;

// Blur5x5 code generated automatically
float4 Blur5x5Core(float2 tex, float2 texScale)
{
// Filter size 5x5
static const float offsets[2] = { 0, 1.217873 };
static const float weights[2] = { 0.3745258, 0.3127371 };

float3 value = 0;

// Use linear sampling to perform a 5x5 kernel by only sampling 3 positions
value += Texture.Sample(LinearSampler, tex - texScale * offsets[1]).rgb * weights[1];
value += Texture.Sample(LinearSampler, tex).rgb * weights[0];
value += Texture.Sample(LinearSampler, tex + texScale * offsets[1]).rgb * weights[1];

return float4(value, 1.0);
}

// Horizontal gaussian blur
float4 PSBlur5x5H(float2 tex : TEXCOORD) : SV_Target
{
return Blur5x5Core(tex, float2(1, 0) * TextureTexelSize);
}

// Vertical gaussian blur
float4 PSBlur5x5V(float2 tex : TEXCOORD) : SV_Target
{
return Blur5x5Core(tex, float2(0, 1) * TextureTexelSize);
}

// -----------------------------------------------------
// Techniques
// -----------------------------------------------------
technique BrightPassTechnique
{
pass
{
Profile = 9.1;
PixelShader = PSBrightPassFilter;
}
}

technique BlurPassTechnique
{
pass
{
// Horizontal pass
Profile = 9.1;
PixelShader = PSBlur5x5H;
}
pass
{
// Vertical pass
Profile = 9.1;
PixelShader = PSBlur5x5V;
}
}
Loading

0 comments on commit 4700eff

Please sign in to comment.