[go: up one dir, main page]

Menu

[r127]: / trunk / UI / Program.cs  Maximize  Restore  History

Download this file

153 lines (142 with data), 4.7 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
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ConstellationStation.UI;
using ConstellationStation.Graphics;
using ConstellationStation.Data;
using System.Threading;
namespace ConstellationStation.UI
{
/// <summary>
/// Entry point for the program.
/// </summary>
public class Program
{
// Fields
/// <summary>
/// A reference to the main form UI.
/// </summary>
private MainForm mainForm;
/// <summary>
/// A reference to the keyboard input controller.
/// </summary>
private KeyboardInputController keyboardInput;
/// <summary>
/// A reference to the mouse input controller.
/// </summary>
private MouseInputController mouseInput;
/// <summary>
/// A reference to the graphics controller.
/// </summary>
private GraphicsController graphics;
/// <summary>
/// A reference to the universe data store.
/// </summary>
private Universe universe;
/// <summary>
/// Whether or not the program is paused.
/// </summary>
private bool paused;
// Constructors
/// <summary>
/// Constructs a new instance of the <see cref="T:Program"/> class.
/// </summary>
private Program()
{
universe = new Universe();
mainForm = new MainForm();
keyboardInput = new KeyboardInputController(universe);
mouseInput = new MouseInputController(universe);
graphics = new GraphicsController();
}
// Properties
/// <summary>
/// Gets a value indicating whether or not the program is paused.
/// As long as the program is paused, no calls to the controllers are made.
/// </summary>
public bool IsPaused
{
get
{
return paused;
}
}
// Methods
/// <summary>
/// Initialize the UI, graphics, and data controllers.
/// </summary>
private void Initialize()
{
// Initialize Data
universe.NewUniverse();
// Initialize UI
mainForm.Program = this;
mainForm.KeyboardInput = keyboardInput;
mainForm.MouseInput = mouseInput;
mainForm.Graphics = graphics;
mainForm.Universe = universe;
// Initialize Graphics
graphics.InitializeGraphics(mainForm, universe);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Program program = new Program();
program.Initialize();
program.Run();
}
/// <summary>
/// Pauses the program. See <see cref="P:IsPaused"/>.
/// </summary>
internal void Pause()
{
paused = true;
}
/// <summary>
/// Resumes the program. See <see cref="P:IsPaused"/>.
/// </summary>
internal void Resume()
{
paused = false;
}
/// <summary>
/// The main program loop.
/// </summary>
private void Run()
{
mainForm.Show();
// Main program loop
int ticksPrev = 0;
int ticks = Environment.TickCount;
while (mainForm.Created)
{
if (paused)
{
Thread.Sleep(100);
}
else
{
// Compute time elapsed
ticksPrev = ticks;
ticks = Environment.TickCount;
int ticksElapsed = ticks - ticksPrev;
// Handle input
keyboardInput.HandleInput(ticksElapsed);
mouseInput.HandleInput(ticksElapsed);
if (graphics.IsGraphicsInitialized)
{
// Render the graphics
graphics.RenderScene(ticksElapsed);
}
}
// Process events to keep the app from freezing
Application.DoEvents();
}
}
}
}