DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Grid Tile Render Engine, In .Net
// A Render engine for grid tile maps in .Net
Imports System.Drawing Public Class RenderEngineObj Private gfx As Graphics Private offScreen As Bitmap Private gridLineColor As Color Private dimX As Integer Private dimY As Integer Private gridSize As Size Private tiles As Bitmap(,) Public Sub New(ByVal graphicObj As Graphics, ByVal columnCount As Integer, ByVal rowCount As Integer, ByVal gridColor As Color) gfx = graphicObj Me.offScreen = New Bitmap(gfx.VisibleClipBounds.Width, gfx.VisibleClipBounds.Height, gfx) dimX = columnCount dimY = rowCount gridLineColor = gridColor gridSize = calculateGridSize() ReDim tiles(dimX, dimY) End Sub Public Sub setTileImg(ByVal gridX As Integer, ByVal gridy As Integer, ByVal img As Bitmap) tiles(gridX, gridy) = img End Sub Private Function calculateGridSize() As Size Return New Size((gfx.VisibleClipBounds.Width - 1) / dimX, (gfx.VisibleClipBounds.Height - 1) / dimY) End Function Public Sub renderImageToGrid(ByVal gridX As Integer, ByVal gridY As Integer, ByVal offsetXPerc As Integer, ByVal offsetYPerc As Integer, ByVal img As Bitmap, ByVal renderNow As Boolean) If Not (img Is Nothing) Then Dim offGfx As Graphics = System.Drawing.Graphics.FromImage(Me.offScreen) img.MakeTransparent(img.GetPixel(0, 0)) offGfx.DrawImage(img, (gridX * gridSize.Width) + 1, (gridY * gridSize.Height) + 1, gridSize.Width - 1, gridSize.Height - 1) If renderNow Then Me.gfx.DrawImage(Me.offScreen, 0, 0) End If End If End Sub Private Sub renderGridLines() Dim offGfx As Graphics = System.Drawing.Graphics.FromImage(Me.offScreen) offGfx.DrawRectangle(New Pen(Me.gridLineColor, 1), Me.gfx.VisibleClipBounds.Left, Me.gfx.VisibleClipBounds.Top, Me.gfx.VisibleClipBounds.Right - 1, Me.gfx.VisibleClipBounds.Bottom - 1) For x As Integer = 1 To dimX - 1 offGfx.DrawLine(New Pen(Me.gridLineColor, 1), Me.gridSize.Width * x, 0, Me.gridSize.Width * x, Me.gfx.VisibleClipBounds.Height - 1) Next For y As Integer = 1 To dimY - 1 offGfx.DrawLine(New Pen(Me.gridLineColor, 1), 0, Me.gridSize.Height * y, Me.gfx.VisibleClipBounds.Width - 1, Me.gridSize.Height * y) Next End Sub Private Sub renderTiles() For y As Integer = 0 To dimY - 1 For x As Integer = 0 To dimX - 1 Me.renderImageToGrid(x, y, 0, 0, Me.tiles(x, y), False) Next Next End Sub Public Sub renderScene() If Not (gfx Is Nothing) Then Me.renderTiles() Me.renderGridLines() Me.gfx.DrawImage(Me.offScreen, 0, 0) End If End Sub End Class





