-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
345 lines (294 loc) · 9.51 KB
/
MainForm.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#region Header
// Copyright© 2003-2023 https://github.com/ArdeshirV, Licensed under MIT
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
#endregion
//-----------------------------------------------------------------------------
namespace Life
{
/// <summary>
/// Startup form.
/// </summary>
public class MainForm: System.Windows.Forms.Form
{
#region Variables
private int m_intCounter = 0;
private int m_intActiveSprite = 1;
private const int m_intMaxArr = 100;
private int[,] m_intArr = new int[m_intMaxArr, m_intMaxArr];
#region designer variable
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
#endregion
#endregion
//---------------------------------------------------------------------
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
/// <param name="args">Command line parameter</param>
public MainForm(string[] args)
{
InitializeComponent();
int l_intIndexerI, l_intIndexerJ, l_intIndexer = 1;
Random l_rnd = new Random((int)DateTime.Now.Second);
for(l_intIndexerI = 0; l_intIndexerI < m_intMaxArr; l_intIndexerI++)
for(l_intIndexerJ = 0; l_intIndexerJ < m_intMaxArr; l_intIndexerJ++)
m_intArr[l_intIndexerI, l_intIndexerJ] = 0;
//for(l_intIndexer = 1; l_intIndexer < 4; l_intIndexer++)
//{
l_intIndexerI = l_rnd.Next(10, 89);
l_intIndexerJ = l_rnd.Next(10, 89);
m_intArr[l_intIndexerI, l_intIndexerJ] = l_intIndexer;
m_intArr[l_intIndexerI - 1, l_intIndexerJ] = l_intIndexer;
m_intArr[l_intIndexerI + 1, l_intIndexerJ] = l_intIndexer;
m_intArr[l_intIndexerI, l_intIndexerJ - 1] = l_intIndexer;
m_intArr[l_intIndexerI, l_intIndexerJ + 1] = l_intIndexer;
m_intArr[l_intIndexerI - 2, l_intIndexerJ] = l_intIndexer;
m_intArr[l_intIndexerI + 2, l_intIndexerJ] = l_intIndexer;
m_intArr[l_intIndexerI, l_intIndexerJ - 2] = l_intIndexer;
m_intArr[l_intIndexerI, l_intIndexerJ + 2] = l_intIndexer;
m_intArr[l_intIndexerI - 1, l_intIndexerJ - 1] = l_intIndexer;
m_intArr[l_intIndexerI + 1, l_intIndexerJ + 1] = l_intIndexer;
//}
Visible = false;
Timer m_timTimer = new Timer();
m_timTimer.Interval = 100;
m_timTimer.Enabled = true;
m_timTimer.Tick += new EventHandler(m_timTimer_Ticker);
}
#endregion
//---------------------------------------------------------------------
#region Event handlers
/// <summary>
/// Occured whenever main form has been painted.
/// </summary>
/// <param name="sender">Main form</param>
/// <param name="e">Event argument</param>
private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Bitmap l_bmpOffScreen = new Bitmap(ClientSize.Width, ClientSize.Height, e.Graphics);
Graphics l_grpOffScreen = Graphics.FromImage(l_bmpOffScreen);
OnPaint(l_grpOffScreen);
e.Graphics.DrawImageUnscaled(l_bmpOffScreen, 0, 0);
}
/// <summary>
/// Occured whenever .
/// </summary>
/// <param name="sender">Timer</param>
/// <param name="e">Event argument</param>
private void m_timTimer_Ticker(object sender, EventArgs e)
{
for(int l_intIndexer = 0; l_intIndexer < 9; l_intIndexer++)
FrameMove(ref m_intArr);
Invalidate();
}
/// <summary>
/// Occured whenever main form has been resized.
/// </summary>
/// <param name="sender">Main form</param>
/// <param name="e">Event argument</param>
private void MainForm_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
#endregion
//---------------------------------------------------------------------
#region Overrided functions
/// <summary>
/// Occured when background of main form need to redraw.
/// </summary>
/// <param name="e"></param>
protected override void OnPaintBackground(PaintEventArgs e)
{
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if(disposing)
if(components != null)
components.Dispose();
base.Dispose(disposing);
}
#endregion
//---------------------------------------------------------------------
#region Utility functions
private int m_intMode = 1;
private bool m_blnFirst = true;
/// <summary>
/// Move current frame.
/// </summary>
/// <param name="arr">Array of boolean values</param>
private void FrameMove(ref int[,] arr)
{
int l_intSpriteCounter;
for(int l_intIndexerI = 0; l_intIndexerI < m_intMaxArr; l_intIndexerI++)
{
for(int l_intIndexerJ = 0; l_intIndexerJ < m_intMaxArr; l_intIndexerJ++)
{
l_intSpriteCounter = 0;
if(l_intIndexerI < m_intMaxArr - 1 && l_intIndexerJ > 0)
{
if(arr[l_intIndexerI + 1, l_intIndexerJ - 1] == m_intActiveSprite)
l_intSpriteCounter++;
}
if(l_intIndexerI > 0 && l_intIndexerJ < m_intMaxArr - 1)
{
if(arr[l_intIndexerI - 1, l_intIndexerJ + 1] == m_intActiveSprite)
l_intSpriteCounter++;
}
if(l_intIndexerI < m_intMaxArr - 1 && l_intIndexerJ < m_intMaxArr - 1)
{
if(arr[l_intIndexerI + 1, l_intIndexerJ + 1] == m_intActiveSprite)
l_intSpriteCounter++;
}
if(l_intIndexerI > 0)
{
if(arr[l_intIndexerI - 1, l_intIndexerJ] == m_intActiveSprite)
l_intSpriteCounter++;
}
if(l_intIndexerI > 0 && l_intIndexerJ > 0)
{
if(arr[l_intIndexerI - 1, l_intIndexerJ - 1] == m_intActiveSprite)
l_intSpriteCounter++;
}
if(l_intIndexerJ > 0)
{
if(arr[l_intIndexerI, l_intIndexerJ - 1] == m_intActiveSprite)
l_intSpriteCounter++;
}
if(l_intIndexerI < m_intMaxArr - 1)
{
if(arr[l_intIndexerI + 1, l_intIndexerJ] == m_intActiveSprite)
l_intSpriteCounter++;
}
if(l_intIndexerJ < m_intMaxArr - 1)
{
if(arr[l_intIndexerI, l_intIndexerJ + 1] == m_intActiveSprite)
l_intSpriteCounter++;
}
switch(m_intMode)
{
case 1:
if(l_intSpriteCounter == 3)
arr[l_intIndexerI, l_intIndexerJ] = m_intActiveSprite;
break;
case 2:
if(l_intSpriteCounter < 2)
arr[l_intIndexerI, l_intIndexerJ] = 0;
break;
case 3:
if(l_intSpriteCounter > 3)
arr[l_intIndexerI, l_intIndexerJ] = 0;
break;
}
}
}
if(++m_intMode > 3)
m_intMode = 1;
}
/// <summary>
/// Main drawing method.
/// </summary>
/// <param name="g">Off Screen</param>
private void OnPaint(Graphics g)
{
Brush l_brsNew = Brushes.Red;
float l_fltW = (float)ClientSize.Width / (float)m_intMaxArr,
l_fltH = (float)ClientSize.Height / (float)m_intMaxArr;
switch(m_intCounter++ % 4)
{
case 1:
l_brsNew = Brushes.Blue;
break;
case 2:
l_brsNew = Brushes.Green;
break;
case 3:
l_brsNew = Brushes.Yellow;
break;
default:
m_intActiveSprite = 1;
l_brsNew = Brushes.Red;
break;
}
if(m_blnFirst)
{
// TODO: Text out...
//Font f = new Font("MS COMIC SANSSERIF", 100);
//g.DrawString("Hi I am virus!!", f, Brushes.Firebrick, 200, 200);
//Font f = new Font("MS COMIC SANSSERIF", 100, FontStyle.Bold);
//g.DrawString(Copyright(), this.Font, Brushes.Black, 40, 40);
m_blnFirst = false;
}
for(int l_intIndexerI = 0; l_intIndexerI < m_intMaxArr; l_intIndexerI++)
{
for(int l_intIndexerJ = 0; l_intIndexerJ < m_intMaxArr; l_intIndexerJ++)
{
if(m_intArr[l_intIndexerI, l_intIndexerJ] > 0)
{
g.FillRectangle(l_brsNew, l_intIndexerI * l_fltW,
l_intIndexerJ * l_fltH, l_fltW, l_fltH);
g.DrawRectangle(Pens.Gray,
l_intIndexerI * l_fltW, l_intIndexerJ * l_fltH, l_fltW, l_fltH);
}
}
}
}
#endregion
//---------------------------------------------------------------------
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.SuspendLayout();
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(412, 339);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "MainForm";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Life";
this.TopMost = true;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
this.Resize += new System.EventHandler(this.MainForm_Resize);
this.ResumeLayout(false);
}
#endregion
//---------------------------------------------------------------------
#region Entry point
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
Application.Run(new Life.MainForm(args));
}
catch(Exception exp)
{
MessageBox.Show(exp.Message, "Error!",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
}
}