Skip to content

Commit 37b4b74

Browse files
committed
Render Target example
1 parent 285cee8 commit 37b4b74

4 files changed

Lines changed: 188 additions & 140 deletions

File tree

BasicUI_MonoGame/BasicUI_MonoGame_Win_Desktop/Game1.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using EmptyKeys.UserInterface.Generated;
77
using EmptyKeys.UserInterface.Input;
88
using EmptyKeys.UserInterface.Media;
9+
using EmptyKeys.UserInterface.Media.Imaging;
910
using GameUILibrary;
1011
using Microsoft.Xna.Framework;
1112
using Microsoft.Xna.Framework.Content;
@@ -20,10 +21,13 @@ namespace BasicUI_MonoGame_Win_Desktop
2021
/// </summary>
2122
public class Game1 : Game
2223
{
24+
private readonly Random random = new Random();
25+
2326
GraphicsDeviceManager graphics;
2427

2528
private int nativeScreenWidth;
2629
private int nativeScreenHeight;
30+
private RenderTarget2D renderTarget;
2731

2832
private BasicUI basicUI;
2933
private DebugViewModel debug;
@@ -125,6 +129,17 @@ protected override void LoadContent()
125129
RelayCommand resizeCommand = new RelayCommand(new Action<object>(OnResize));
126130
KeyBinding resizeBinding = new KeyBinding(resizeCommand, KeyCode.R, ModifierKeys.Control);
127131
basicUI.InputBindings.Add(resizeBinding);
132+
133+
renderTarget = new RenderTarget2D(GraphicsDevice,
134+
100,
135+
100,
136+
false,
137+
GraphicsDevice.PresentationParameters.BackBufferFormat,
138+
DepthFormat.Depth24Stencil8);
139+
140+
viewModel.RenderTargetSource = new BitmapImage();
141+
var texture = Engine.Instance.Renderer.CreateTexture(renderTarget);
142+
viewModel.RenderTargetSource.Texture = texture;
128143
}
129144

130145
private void OnResize(object obj)
@@ -192,6 +207,15 @@ protected override void Update(GameTime gameTime)
192207
/// <param name="gameTime">Provides a snapshot of timing values.</param>
193208
protected override void Draw(GameTime gameTime)
194209
{
210+
GraphicsDevice.SetRenderTarget(renderTarget);
211+
Color color = new Color();
212+
color.A = 255;
213+
color.R = (byte)random.Next(0, 255);
214+
color.G = (byte)random.Next(0, 255);
215+
color.B = (byte)random.Next(0, 255);
216+
GraphicsDevice.Clear(color);
217+
GraphicsDevice.SetRenderTarget(null);
218+
195219
GraphicsDevice.Clear(Color.CornflowerBlue);
196220

197221
basicUI.Draw(gameTime.ElapsedGameTime.TotalMilliseconds);

BasicUI_MonoGame/GameUILibrary/BasicUIViewModel.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using EmptyKeys.UserInterface;
88
using EmptyKeys.UserInterface.Input;
9+
using EmptyKeys.UserInterface.Media.Imaging;
910
using EmptyKeys.UserInterface.Mvvm;
1011
using GameData;
1112

@@ -30,6 +31,7 @@ public class BasicUIViewModel : ViewModelBase
3031
private List<PointF> chartData;
3132
private ObservableCollection<DragDropItem> dataOne;
3233
private ObservableCollection<DragDropItem> dataTwo;
34+
private BitmapImage renderTargetSource;
3335

3436
/// <summary>
3537
/// Gets or sets the tetris.
@@ -211,6 +213,18 @@ public ObservableCollection<DragDropItem> DataTwo
211213
set { SetProperty(ref dataTwo, value); }
212214
}
213215

216+
/// <summary>
217+
/// Gets or sets the render target source.
218+
/// </summary>
219+
/// <value>
220+
/// The render target source.
221+
/// </value>
222+
public BitmapImage RenderTargetSource
223+
{
224+
get { return renderTargetSource; }
225+
set { SetProperty(ref renderTargetSource, value); }
226+
}
227+
214228
/// <summary>
215229
/// Initializes a new instance of the <see cref="BasicUIViewModel"/> class.
216230
/// </summary>

0 commit comments

Comments
 (0)