Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Main
- Dim logicarray(64, 48, 1) As Decimal '0=type 1=value
- Dim q_exit As Boolean = False
- Dim display As New Bitmap(640, 480)
- Dim displaybuffer As Graphics = Graphics.FromImage(display)
- Dim q_mousedown As Boolean = False
- Dim mp As Point
- Dim chronometer As New Stopwatch
- Dim chronometer2 As New Stopwatch
- Dim last As Integer
- Dim ttr As Integer
- Dim count As Integer
- Private Sub Main_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
- q_exit = True
- End Sub
- Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- End Sub
- Private Sub Main_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
- chronometer.Start()
- Do Until q_exit = True
- logic()
- draw()
- Application.DoEvents()
- GC.Collect()
- Loop
- End Sub
- Sub logic()
- If q_mousedown = True Then
- If rb_base.Checked = True Then
- logicarray(mp.X, mp.Y, 0) = 2
- logicarray(mp.X, mp.Y, 1) = 0
- End If
- If rb_metal.Checked = True Then
- logicarray(mp.X, mp.Y, 0) = 1
- logicarray(mp.X, mp.Y, 1) = 0
- End If
- If rb_eraser.Checked = True Then
- logicarray(mp.X, mp.Y, 0) = 0
- logicarray(mp.X, mp.Y, 1) = 0
- End If
- End If
- If chronometer.ElapsedMilliseconds > last + 20 Then
- If cb_processing.Checked = True Then
- count = 0
- chronometer2.Start()
- If cb_step.Checked = True Then cb_processing.Checked = False
- 'DO PROCESSING HERE
- For x As Integer = 1 To 62
- For y As Integer = 1 To 46
- If logicarray(x, y, 0) = 1 Then 'is metal?
- count += 1
- 'Spread stress
- Dim totalsplit As Integer = 1
- If logicarray(x + 1, y, 0) = 1 Then totalsplit += 1
- If logicarray(x, y - 1, 0) = 1 Then totalsplit += 1
- If logicarray(x - 1, y, 0) = 1 Then totalsplit += 1
- If logicarray(x, y + 1, 0) = 1 Then totalsplit += 2
- Dim valuesplit = logicarray(x, y, 1) / totalsplit
- logicarray(x + 1, y, 1) += valuesplit
- logicarray(x, y - 1, 1) += valuesplit
- logicarray(x - 1, y, 1) += valuesplit
- logicarray(x, y + 1, 1) += (valuesplit * 2)
- logicarray(x, y, 1) = valuesplit
- End If
- Next
- Next
- For x As Integer = 1 To 62
- For y As Integer = 1 To 46
- If logicarray(x, y, 0) = 2 Then 'is base?
- If logicarray(x, y - 1, 0) = 1 Then
- logicarray(x, y - 1, 1) -= 50 'minus 5
- If logicarray(x, y - 1, 1) <= 0 Then 'Below minimum?
- logicarray(x, y - 1, 1) = 0
- End If
- End If
- End If
- Next
- Next
- For x As Integer = 1 To 62
- For y As Integer = 1 To 46
- If logicarray(x, y, 0) = 1 Then 'is metal?
- logicarray(x, y, 1) += 1 'Add one
- If logicarray(x, y, 1) >= 255 Then 'Above maximum?
- Dim totalsplit As Integer = 0
- If logicarray(x + 1, y, 0) = 1 Then totalsplit += 1
- If logicarray(x, y - 1, 0) = 1 Then totalsplit += 1
- If logicarray(x - 1, y, 0) = 1 Then totalsplit += 1
- If logicarray(x, y + 1, 0) = 1 Then totalsplit += 2
- If Not totalsplit = 0 Then
- Dim valuesplit = logicarray(x, y, 1) / totalsplit
- logicarray(x + 1, y, 1) += valuesplit
- logicarray(x, y - 1, 1) += valuesplit
- logicarray(x - 1, y, 1) += valuesplit
- logicarray(x, y + 1, 1) += (valuesplit * 2)
- End If
- logicarray(x, y, 0) = 0
- logicarray(x, y, 1) = 0
- End If
- End If
- Next
- Next
- End If
- ttr = chronometer2.ElapsedMilliseconds
- last = chronometer.ElapsedMilliseconds
- chronometer2.Stop()
- chronometer2.Reset()
- End If
- End Sub
- Sub draw()
- displaybuffer.Clear(Color.Black)
- For x As Integer = 0 To 63
- For y As Integer = 0 To 47
- Select Case logicarray(x, y, 0)
- Case 0 'Nothing
- Case 1 'Metal
- displaybuffer.FillRectangle(Brushes.Gray, x * 10, y * 10, 10, 10)
- Dim alphavar As Integer = logicarray(x, y, 1)
- If alphavar > 255 Then alphavar = 255
- Dim mybrush As New SolidBrush(Color.FromArgb(alphavar, Color.Red))
- displaybuffer.FillRectangle(mybrush, x * 10, y * 10, 10, 10)
- Case 2 'Base
- displaybuffer.FillRectangle(Brushes.Blue, x * 10, y * 10, 10, 10)
- End Select
- Next
- Next
- displaybuffer.DrawString("Logic: " & ttr, New Font("Arial", 12), Brushes.Yellow, 4, 4)
- displaybuffer.DrawString("Draw: " & count, New Font("Arial", 12), Brushes.Yellow, 4, 24)
- If Me.IsDisposed = False Then Canvas.CreateGraphics.DrawImage(display, 0, 0, 640, 480)
- End Sub
- Private Sub Canvas_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Canvas.MouseDown
- q_mousedown = True
- End Sub
- Private Sub Canvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Canvas.MouseMove
- mp.X = (e.X - 5) / 10
- mp.Y = (e.Y - 5) / 10
- End Sub
- Private Sub Canvas_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Canvas.MouseUp
- q_mousedown = False
- End Sub
- Private Sub btn_resetstress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_resetstress.Click
- For x As Integer = 0 To 63
- For y As Integer = 0 To 47
- logicarray(x, y, 1) = 0
- Next
- Next
- End Sub
- Private Sub btn_reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reset.Click
- For x As Integer = 0 To 63
- For y As Integer = 0 To 47
- logicarray(x, y, 1) = 0
- logicarray(x, y, 0) = 0
- Next
- Next
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment