-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
224 lines (184 loc) · 6.5 KB
/
Program.cs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Numerics;
using DingusEngine.Actors;
using DingusEngine.GameActor;
using DingusEngine.StandardComponents;
using DingusEngine.Rendering;
using Timer = System.Windows.Forms.Timer;
using Microsoft.VisualBasic.Devices;
using System.Runtime.InteropServices;
using WriteLine = System.Diagnostics.Debug;
using System.Security.Policy;
using DingusEngine.Actors;
using DingusEngine.GameActor;
using DingusEngine.Input;
using System.Threading;
using System.Windows.Threading;
using System.Windows.Input;
using GameEngine.Actors;
namespace DingusEngine
{
public class EGameEngine : Form
{
public static EGameEngine Engine = null;
// Timer to control the game loop
//private Timer gameTimer;
private DispatcherTimer gameTimer;
// The current game state
private GameState gameState;
// The current frame rate
private int frameRate;
// The time elapsed since the last frame
public float DeltaTime => _deltaTime;
private float _deltaTime;
public EActorManager ActorManager;
public EInputManager InputManager;
Graphics g;
private Image _bufferImage;
public ERenderHandler RenderHandler;
public EGameEngine()
{
Engine = this;
ActorManager = new EActorManager();
InputManager = new EInputManager();
// Set the size and title of the window
this.ClientSize = new Size(800, 600);
this.Text = "Dingus Engine";
this.Resize += new EventHandler(OnResize);
// Init Graphics
g = this.CreateGraphics();
RenderHandler = new ERenderHandler();
// Create a buffer image with the same size as the form
_bufferImage = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
// Enable double buffering for the form
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
// Initialize the game state and frame rate
gameState = GameState.Running;
frameRate = 60;
// Initialize the timer
//gameTimer = new Timer();
gameTimer = new DispatcherTimer();
//gameTimer.Interval = 1000 / frameRate;
gameTimer.Interval = new TimeSpan(1000 / frameRate);
gameTimer.Tick += new EventHandler(OnTick);
gameTimer.Start();
Start();
}
// Before game starts
private void Start()
{
#region Test Actors
TestActor ta = ActorManager.CreateActor<TestActor>();
MovingActor ma = ActorManager.CreateActor<MovingActor>();
TextActor txa = ActorManager.CreateActor<TextActor>();
Player player = ActorManager.CreateActor<Player>();
ASprite s = ta.GetComponent<ASprite>();
ATransform tf = ta.GetComponent<ATransform>();
//InputManager.OnKeyDown(Key.Space, delegate
InputManager.OnKeyDown(MouseButtons.Left, delegate
{
//s.Visible = !s.Visible;
s.SetScale(1f);
//tf.Position = new Vector3(tf.Position.X, tf.Position.Y, -10);
});
//InputManager.OnKeyUp(Key.Space, delegate
InputManager.OnKeyUp(MouseButtons.Left, delegate
{
s.SetScale(0.3f);
//tf.Position = new Vector3(tf.Position.X, tf.Position.Y, 20);
});
#endregion
}
// The game loop
private void OnTick(object sender, EventArgs e)
{
// Update the elapsed time
_deltaTime = (float) gameTimer.Interval.TotalMilliseconds;
// Update the game state
Update(DeltaTime);
// Render the game
Render();
// Check for input events
ProcessInput();
}
// Update the game state
private void Update(float elapsedTime)
{
// Update the game logic here
foreach(Actor actor in ActorManager.Actors)
{
actor.Update();
foreach(IComponent component in actor.Components)
{
component.Update();
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
// Paint the buffer image when reloaded
base.OnPaint(e);
e.Graphics.DrawImage(_bufferImage, 0, 0, _bufferImage.Width, _bufferImage.Height);
}
private void OnResize(object sender, EventArgs e)
{
// Form has been resized
// TODO Crashes on minimize
_bufferImage = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
}
// Render the game
private void Render()
{
// Render the game graphics here
// Create a Graphics object from the form's handle
if(g == null)
{
g = this.CreateGraphics();
}
// Clear the screen.
// Using the buffer?? Not sure why, but it works.
using (Graphics g = Graphics.FromImage(_bufferImage))
{
g.Clear(Color.White);
}
// Draw on the buffer image
using (Graphics g = Graphics.FromImage(_bufferImage))
{
// Render each assigned task
foreach(IRenderTask task in RenderHandler.Tasks)
{
task.Action(g);
}
}
// Invalidate the form to trigger a repaint
this.Invalidate();
}
// Process input events
private void ProcessInput()
{
// Check for input events here
Dispatcher.CurrentDispatcher.Invoke((Action)delegate
{
InputManager.Update();
});
//DispatcherHelper.CheckBeginInvokeOnUI(Action action)
}
// The main entry point for the application
[STAThread]
static void Main()
{
Application.Run(new EGameEngine());
}
}
// TODO handle this? IDK put it in it's own file or something?
public enum GameState
{
Running,
Paused,
GameOver,
// Add additional game states here
}
}