-
Notifications
You must be signed in to change notification settings - Fork 1
/
Loop.cs
132 lines (103 loc) · 2.64 KB
/
Loop.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using Draught;
using System.Diagnostics;
using tmp;
using graphic;
using graphic.GUI;
namespace ISO {
class Loop {
private Form1 f;
private bool running;
private long delta;
private long lastFrame;
private List<ITickable> updateList = new List<ITickable>();
private Bitmap buffer;
private DrawManager drawManager;
private GuiManager guiManager;
private World world;
private Map map;
private Draught.Control control;
public Loop(Form1 f){
this.f = f;
this.buffer = new Bitmap(f.getPanelWidth(), f.getPanelWidth());
drawManager = new DrawManager();
guiManager = new GuiManager(this, drawManager);
}
public void doLoop() {
this.initialize();
while (running) {
this.calc();
this.repaint();
this.calcDelta();
Thread.Sleep(30);
}
this.close();
}
private void calcDelta() {
long thisFrame = Loop.getCurrentTime();
delta = thisFrame - lastFrame;
lastFrame = thisFrame;
}
private void close() {
Console.WriteLine("Close");
}
private void repaint() {
this.buffer = new Bitmap(f.getPanelWidth(), f.getPanelWidth());
using (Graphics g = Graphics.FromImage(buffer)) {
drawManager.draw(g);
}
f.repaint(buffer);
}
private void calc() {
foreach (var t in updateList) {
t.update(delta);
}
}
private void initialize() {
this.lastFrame = Loop.getCurrentTime();
running = true;
guiManager.showOptionsGui();
}
public DrawManager getDrawManager() {
return drawManager;
}
public GuiManager getGuiManager() {
return guiManager;
}
public void addToUpdateList(ITickable t) {
updateList.Add(t);
}
public void exit() {
this.running = false;
Program.exit();
}
private static long getCurrentTime() {
return DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
}
public Form1 getForm()
{
return f;
}
internal void startGame(int size, Draught.Intelligence p1, Draught.Intelligence p2)
{
map = new Map(size);
control = new Draught.Control(map, p1, p2, this);
world = new World(1, size, control, map);
world.setVisible(true);
drawManager.addDrawable(world);
f.registerMouseListener(world);
guiManager.closeActiveGui();
}
public void showMainMenu()
{
if (world != null) world.setVisible(false);
guiManager.showOptionsGui();
}
}
}