Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. ''' <summary>
  2. ''' The class that handles the Tilesets.
  3. ''' </summary>
  4. ''' <remarks></remarks>
  5. Public Class TileSet
  6.  
  7. Private _img As Image = Nothing
  8.  
  9. Private _tilelist As New List(Of Tiles)
  10. Private _threadlist As New List(Of Threading.Thread)
  11. Private _canfinish As Boolean = False
  12. Private _calculator As New Stopwatch
  13. Public Event TilesetFinished()
  14.  
  15. ''' <summary>
  16. ''' Stores a tile, with the coordinate on the tileset and the image it is.
  17. ''' </summary>
  18. ''' <remarks></remarks>
  19. Public Structure Tiles
  20. Public Property X As Integer
  21. Public Property Y As Integer
  22. Public Property Img As Image
  23. End Structure
  24.  
  25. ''' <summary>
  26. ''' Creates a new Tileset Class.
  27. ''' </summary>
  28. ''' <param name="tileset">The Image to load.</param>
  29. ''' <remarks></remarks>
  30. Public Sub New(ByRef tileset As Image)
  31. 'Check dimensions
  32. If ValidDimensions(tileset) Then _img = tileset Else Throw New ArgumentException("Invalid Image Dimensions!")
  33. 'Now create the tiles
  34. Me.CreateTiles(_img)
  35. End Sub
  36.  
  37. ''' <summary>
  38. ''' Creats a new Tileset Class.
  39. ''' </summary>
  40. ''' <param name="filepath">The filepath to the image.</param>
  41. ''' <remarks></remarks>
  42. Public Sub New(ByVal filepath As String)
  43. Me.New(Image.FromFile(filepath))
  44. End Sub
  45.  
  46. ''' <summary>
  47. ''' Checks the Image for valid base 2 dimensions.
  48. ''' </summary>
  49. ''' <param name="img">The image to check for.</param>
  50. ''' <returns></returns>
  51. ''' <remarks></remarks>
  52. Private Function ValidDimensions(ByVal img As Image) As Boolean
  53. Return img.Width Mod 16 = 0 AndAlso img.Height Mod 16 = 0
  54. End Function
  55.  
  56. ''' <summary>
  57. ''' Creates the individual pieces for the tileset in a 16 x 16 mini square.
  58. ''' </summary>
  59. ''' <param name="tileset">The tileset image to use.</param>
  60. ''' <remarks></remarks>
  61. Private Sub CreateTiles(ByRef tileset As Image)
  62. 'Get the number of horizontal squares
  63. Dim _horizontal As Integer = CInt(tileset.Width / 16)
  64. Dim _vertical As Integer = CInt(tileset.Height / 16)
  65. Dim graphics As Graphics = graphics.FromImage(tileset)
  66. _calculator.Start()
  67. 'x number of columns.
  68. For hori As Integer = 0 To _horizontal
  69. Dim imgcopy As Image = CopyImage(tileset)
  70. 'Spin off a thread.
  71. Dim tr As New Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf CalculateColumn))
  72. _threadlist.Add(tr)
  73. tr.Start(New ThreadData With {.HorizontalValue = hori, .img = imgcopy, .graphics = graphics, .ID = tr.ManagedThreadId})
  74. Next
  75. _canfinish = True
  76. End Sub
  77.  
  78. ''' <summary>
  79. ''' Copies an image.
  80. ''' </summary>
  81. ''' <param name="img"></param>
  82. ''' <returns></returns>
  83. ''' <remarks></remarks>
  84. Private Function CopyImage(ByRef img As Image) As Image
  85. Dim MS As New MemoryStream
  86. img.Save(MS, Imaging.ImageFormat.Png)
  87. Return Image.FromStream(MS)
  88. End Function
  89.  
  90. Private Structure ThreadData
  91. Public img As Image
  92. Public HorizontalValue As Integer
  93. Public graphics As Graphics
  94. Public ID As Integer
  95. End Structure
  96.  
  97. ''' <summary>
  98. ''' Calculuates a series of columns for a tileset.
  99. ''' </summary>
  100. ''' <param name="TD2"></param>
  101. ''' <remarks></remarks>
  102. Private Sub CalculateColumn(ByVal TD2 As Object)
  103. Dim TD As ThreadData = CType(TD2, ThreadData)
  104. Dim _horizontal As Integer = CInt(TD.img.Width / 16)
  105. Dim _vertical As Integer = CInt(TD.img.Height / 16)
  106. Dim graphics As Graphics = graphics.FromImage(TD.img)
  107. Dim bmp As New Bitmap(16I, 16I)
  108. 'x number of rows.
  109. For verti As Integer = 0 To _vertical
  110.  
  111. 'Get current square.
  112. Dim topleft As Point = New Point(TD.HorizontalValue * 16, verti * 16)
  113. Dim bottomright As Point = New Point(topleft.X + 15, topleft.Y + 15)
  114.  
  115. 'Create the tile.
  116. Dim tile As New Tiles
  117. tile.X = TD.HorizontalValue
  118. tile.Y = verti
  119.  
  120. 'Create the bitmap.
  121. graphics.DrawImage(bmp, New Rectangle(topleft, New Size(bottomright)))
  122.  
  123. 'Assign it.
  124. tile.Img = bmp
  125.  
  126. _tilelist.Add(tile)
  127.  
  128. Next
  129. Me.ThreadFinished(TD)
  130. End Sub
  131.  
  132. Public ReadOnly Property TileData As List(Of Tiles)
  133. Get
  134. Return _tilelist
  135. End Get
  136. End Property
  137.  
  138. ''' <summary>
  139. ''' Called when a thread is finished.
  140. ''' </summary>
  141. ''' <param name="td">The thread data.</param>
  142. ''' <remarks></remarks>
  143. Private Sub ThreadFinished(ByRef td As ThreadData)
  144. SyncLock _threadlist
  145. For i As Integer = _threadlist.Count - 1 To 0 Step -1
  146. If _threadlist(i).ManagedThreadId = td.ID Then _threadlist.RemoveAt(i) : Exit For
  147. Next
  148. End SyncLock
  149. If _threadlist.Count = 0 AndAlso _canfinish Then
  150. _calculator.Stop()
  151. System.Windows.Forms.MessageBox.Show(_calculator.Elapsed.TotalSeconds.ToString)
  152. RaiseEvent TilesetFinished()
  153. End If
  154. End Sub
  155.  
  156. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement