/
Game.cs
289 lines (220 loc) · 9.65 KB
/
Game.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Timers;
using SFML.Graphics;
using SFML.Window;
using Tao.OpenGl;
using GenieLib;
namespace OpenEmpires
{
public static class Game
{
public static RenderWindow Window;
public static View DefaultView
{
get
{
var view = Window.DefaultView;
view.Size = size;
return view;
}
}
public static View GameView;
private static readonly bool[] KeyStates;
private static Vector2f size;
private static Map map;
private static Sprite selected;
private static Sprite selected2;
public static EntityManager EntManager
{
get;
private set;
}
static Game()
{
KeyStates = new bool[(int)Keyboard.Key.KeyCount];
}
public static int Clamp(int value, int min, int max)
{
return (value < min) ? min : (value > max) ? max : value;
}
public static void Initialize()
{
var style = Styles.Titlebar | Styles.Close;
size = new Vector2f(Configuration.Width, Configuration.Height);
Window = new RenderWindow(new VideoMode(Configuration.Width, Configuration.Height), "Open Empires", style);
Window.SetFramerateLimit(Configuration.Framerate);
Window.SetVerticalSyncEnabled(Configuration.VSync);
Window.Closed += (sender, args) => Window.Close();
Window.Resized += (sender, args) => Resize(new Vector2f(args.Width, args.Height));
/*Window.MouseButtonPressed += (sender, args) => DispatchEvent(new MouseButtonInputArgs(args.Button, true, args.X, args.Y));
Window.MouseButtonReleased += (sender, args) => DispatchEvent(new MouseButtonInputArgs(args.Button, false, args.X, args.Y));
Window.MouseWheelMoved += (sender, args) => DispatchEvent(new MouseWheelInputArgs(args.Delta, args.X, args.Y));
Window.MouseMoved += (sender, args) => DispatchEvent(new MouseMoveInputArgs(args.X, args.Y));*/
Window.MouseMoved += new EventHandler<MouseMoveEventArgs>(Window_MouseMoved);
Window.MouseWheelMoved += (sender, args) =>
{
/* Mouse wheel zoom implementation where the view will zoom
* towards the area the cursor is pointing to
*/
var mousePos = Mouse.GetPosition(Window);
float relX = (float) mousePos.X / Window.Size.X - 0.5f;
float relY = (float) mousePos.Y / Window.Size.Y - 0.5f;
GameView.Move(new Vector2f {
X = relX * GameView.Size.X,
Y = relY * GameView.Size.Y
});
GameView.Zoom(1 - (args.Delta * 0.1f));
GameView.Move(new Vector2f {
X = -relX * GameView.Size.X,
Y = -relY * GameView.Size.Y
});
};
Window.KeyPressed += (sender, args) =>
{
KeyStates[(int)args.Code] = true;
};
Window.KeyReleased += (sender, args) =>
{
KeyStates[(int)args.Code] = false;
};
Blendomatic.ReadBlendomatic(new System.IO.FileStream("blendomatic.dat", System.IO.FileMode.Open));
map = new Map();
GameView = new View(DefaultView);
GameView.Center = new Vector2f((map.Width/2)*96, 0);
var blend = Blendomatic.BlendingModes[2];
var tile = blend.Tiles[12];
var img = new Image(97, 49);
for (var y = 0; y < img.Size.Y; y++)
for (var x = 0; x < img.Size.X; x++)
img.SetPixel((uint)x, (uint)y, new Color(255, 0, 0, 0));
var i = 0;
for (var y = 0; y < img.Size.X; y++)
{
var bytesPerRow = y < 24 ? 1 + (4 * y) : 97 - (4 * (y - 24));
var startX = 48 - (bytesPerRow / 2);
for (var x = 0; x < bytesPerRow; x++)
img.SetPixel((uint)(startX + x), (uint)y, new Color(0, 0, 0, tile[i++]));
}
var slpFile = new SLPFile();
slpFile.LoadFile("textures/ter15002.slp");
var spritewidth = slpFile.GetFrame(0).m_Width;
var spriteheight = slpFile.GetFrame(0).m_Height;
var imgg = new Image((uint)spritewidth, (uint)(spriteheight ), slpFile.GetFrame(0).GetRGBAArray());
for (var y = 0; y < imgg.Size.Y; y++)
for (var x = 0; x < imgg.Size.X; x++)
{
var col = imgg.GetPixel((uint)x, (uint)y);
col.A = (byte)(Math.Min(255, (128-img.GetPixel((uint)x, (uint)y).A)*2));
if (img.GetPixel((uint)x, (uint)y).R == 255)
col.A = 0;
imgg.SetPixel((uint)x, (uint)y, col);
}
selected = new Sprite(new Texture(imgg));
/* blend sand to forest now */
var blend2 = Blendomatic.BlendingModes[2];
var tile2 = blend2.Tiles[12];
var img2 = new Image(97, 49);
for (var y = 0; y < img2.Size.Y; y++)
for (var x = 0; x < img2.Size.X; x++)
img2.SetPixel((uint)x, (uint)y, new Color(255, 0, 0, 0));
var i2 = 0;
for (var y = 0; y < img.Size.X; y++)
{
var bytesPerRow = y < 24 ? 1 + (4 * y) : 97 - (4 * (y - 24));
var startX = 48 - (bytesPerRow / 2);
for (var x = 0; x < bytesPerRow; x++)
img2.SetPixel((uint)(startX + x), (uint)y, new Color(0, 0, 0, tile[i2++]));
}
var slpFile2 = new SLPFile();
slpFile2.LoadFile("textures/ter15017.slp");
var spritewidth2 = slpFile2.GetFrame(0).m_Width;
var spriteheight2 = slpFile2.GetFrame(0).m_Height;
var imgg2 = new Image((uint)spritewidth2, (uint)(spriteheight2), slpFile2.GetFrame(0).GetRGBAArray());
for (var y = 0; y < imgg2.Size.Y; y++)
for (var x = 0; x < imgg2.Size.X; x++)
{
var col = imgg2.GetPixel((uint)x, (uint)y);
col.A = (byte)(Math.Min(255, (128 - img.GetPixel((uint)x, (uint)y).A) * 2));
if (img2.GetPixel((uint)x, (uint)y).R == 255)
col.A = 0;
imgg2.SetPixel((uint)x, (uint)y, col);
}
selected2 = new Sprite(new Texture(imgg2));
EntManager = new EntityManager();
}
static void Window_MouseMoved(object sender, MouseMoveEventArgs e)
{
}
public static void Run()
{
var timer = new Stopwatch();
double accumulator = 0;
while (Window.IsOpen())
{
var time = timer.Elapsed.TotalSeconds;
timer.Restart();
accumulator += time;
while (accumulator >= Configuration.Timestep)
{
Window.DispatchEvents();
Update(time);
accumulator -= Configuration.Timestep;
}
Window.Clear(Color.Black);
Draw(time);
Window.Display();
}
}
public static void Exit()
{
Window.Close();
}
private static void Resize(Vector2f newSize)
{
size = newSize;
}
private static void Update(double time)
{
var movex = 0.0f;
var movey = 0.0f;
var multiplier = 1f;
if (KeyStates[(int)Keyboard.Key.LShift])
multiplier = 8f;
if (KeyStates[(int)Keyboard.Key.Up])
movey -= (float)time * 250 * multiplier;
if (KeyStates[(int)Keyboard.Key.Down])
movey += (float)time * 250 * multiplier;
if (KeyStates[(int)Keyboard.Key.Left])
movex -= (float)time * 250 * multiplier;
if (KeyStates[(int)Keyboard.Key.Right])
movex += (float)time * 250 * multiplier;
movex *= GameView.Size.X / Window.Size.X;
movey *= GameView.Size.Y / Window.Size.Y;
GameView.Move(new Vector2f(movex, movey));
}
private static void Draw(double time)
{
Window.SetView(GameView);
Window.Draw(map);
Window.Draw(EntManager);
selected.Position = new Vector2f((map.Width / 2) * 96 - 96 - 96 - 48, 24 * -48 + 24);
Window.Draw(selected);
selected.Position = new Vector2f((map.Width / 2) * 96 - 96 - 96, 24 * -48);
Window.Draw(selected);
selected.Position = new Vector2f((map.Width / 2) * 96 - 96 - 48, 24 * -48 - 24);
Window.Draw(selected);
selected.Position = new Vector2f((map.Width / 2) * 96 - 96, 25 * -48);
Window.Draw(selected);
selected2.Position = new Vector2f((map.Width / 2) * 96 - 96 - 96 - 48 + 48, 24 * -48 + 24 + 24);
Window.Draw(selected2);
selected2.Position = new Vector2f((map.Width / 2) * 96 - 96 - 96 + 48, 24 * -48 + 24);
Window.Draw(selected2);
selected2.Position = new Vector2f((map.Width / 2) * 96 - 96 - 48 + 48, 24 * -48 - 24 + 24);
Window.Draw(selected2);
selected2.Position = new Vector2f((map.Width / 2) * 96 - 96 + 48, 25 * -48 + 24);
Window.Draw(selected2);
}
}
}