-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathSampleGame.cs
More file actions
146 lines (119 loc) · 5.08 KB
/
SampleGame.cs
File metadata and controls
146 lines (119 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.IO;
using Num = System.Numerics;
namespace ImGuiNET.SampleProgram.XNA
{
/// <summary>
/// Simple FNA + ImGui example
/// </summary>
public class SampleGame : Game
{
private GraphicsDeviceManager _graphics;
private ImGuiRenderer _imGuiRenderer;
private Texture2D _xnaTexture;
private IntPtr _imGuiTexture;
public SampleGame()
{
_graphics = new GraphicsDeviceManager(this);
_graphics.PreferredBackBufferWidth = 1024;
_graphics.PreferredBackBufferHeight = 768;
_graphics.PreferMultiSampling = false;
IsMouseVisible = true;
}
protected override void Initialize()
{
_imGuiRenderer = new ImGuiRenderer(this);
_imGuiRenderer.RebuildFontAtlas();
base.Initialize();
}
protected override void LoadContent()
{
// Texture loading example
// First, load the texture as a Texture2D (can also be done using the XNA/FNA content pipeline)
_xnaTexture = CreateTexture(GraphicsDevice, 300, 150, pixel =>
{
var red = (pixel % 300) / 2;
return new Color(red, 1, 1);
});
// Then, bind it to an ImGui-friendly pointer, that we can use during regular ImGui.** calls (see below)
_imGuiTexture = _imGuiRenderer.BindTexture(_xnaTexture);
base.LoadContent();
}
protected override void UnloadContent()
{
_imGuiRenderer.Dispose();
Content.Unload();
base.UnloadContent();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(new Color(clear_color.X, clear_color.Y, clear_color.Z));
// Only draw the UI when we have a valid delta time. Otherwise ImGui will give an assertion failure.
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (dt > 0f)
{
ImGui.GetIO().Framerate = 1f / dt;
// Call BeforeLayout first to set things up
_imGuiRenderer.BeforeLayout(gameTime);
// Draw our UI
ImGuiLayout();
// Call AfterLayout now to finish up and draw all the things
_imGuiRenderer.AfterLayout();
}
base.Draw(gameTime);
}
// Direct port of the example at https://github.com/ocornut/imgui/blob/master/examples/sdl_opengl2_example/main.cpp
private float f = 0.0f;
private bool show_test_window = false;
private bool show_another_window = false;
private Num.Vector3 clear_color = new Num.Vector3(114f / 255f, 144f / 255f, 154f / 255f);
private byte[] _textBuffer = new byte[100];
protected virtual void ImGuiLayout()
{
// 1. Show a simple window
// Tip: if we don't call ImGui.Begin()/ImGui.End() the widgets appears in a window automatically called "Debug"
{
ImGui.Text("Hello, world!");
ImGui.SliderFloat("float", ref f, 0.0f, 1.0f, string.Empty);
ImGui.ColorEdit3("clear color", ref clear_color);
if (ImGui.Button("Test Window")) show_test_window = !show_test_window;
if (ImGui.Button("Another Window")) show_another_window = !show_another_window;
ImGui.Text(string.Format("Application average {0:F3} ms/frame ({1:F1} FPS)", 1000f / ImGui.GetIO().Framerate, ImGui.GetIO().Framerate));
ImGui.InputText("Text input", _textBuffer, 100);
ImGui.Text("Texture sample");
ImGui.Image(_imGuiTexture, new Num.Vector2(300, 150), Num.Vector2.Zero, Num.Vector2.One, Num.Vector4.One, Num.Vector4.One); // Here, the previously loaded texture is used
}
// 2. Show another simple window, this time using an explicit Begin/End pair
if (show_another_window)
{
ImGui.SetNextWindowSize(new Num.Vector2(200, 100), ImGuiCond.FirstUseEver);
ImGui.Begin("Another Window", ref show_another_window);
ImGui.Text("Hello");
ImGui.End();
}
// 3. Show the ImGui test window. Most of the sample code is in ImGui.ShowTestWindow()
if (show_test_window)
{
ImGui.SetNextWindowPos(new Num.Vector2(650, 20), ImGuiCond.FirstUseEver);
ImGui.ShowDemoWindow(ref show_test_window);
}
}
public static Texture2D CreateTexture(GraphicsDevice device, int width, int height, Func<int, Color> paint)
{
//initialize a texture
var texture = new Texture2D(device, width, height);
//the array holds the color for each pixel in the texture
Color[] data = new Color[width * height];
for(var pixel = 0; pixel < data.Length; pixel++)
{
//the function applies the color according to the specified pixel
data[pixel] = paint( pixel );
}
//set the color
texture.SetData( data );
return texture;
}
}
}