public const int ChunkSize = 8; // 8x8 chunks ==> 192x192 bitmaps. Bitmap RenderChunk(int u, int v) { var bitmap = new Bitmap(ChunkSize * TileSetRenderer.TileSize.Width, ChunkSize * TileSetRenderer.TileSize.Height); var data = bitmap.LockBits(bitmap.Bounds(), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); unsafe { int *p = (int *)data.Scan0.ToPointer(); var stride = data.Stride >> 2; for (var i = 0; i < ChunkSize; i++) { for (var j = 0; j < ChunkSize; j++) { var tr = Map.MapTiles.Value[u * ChunkSize + i, v *ChunkSize + j]; var tile = TileSetRenderer.Data(tr.Type); var index = (tr.Index < tile.Count) ? tr.Index : (byte)0; var rawImage = tile[index]; for (var x = 0; x < TileSetRenderer.TileSize.Width; x++) { for (var y = 0; y < TileSetRenderer.TileSize.Height; y++) { p[(j * TileSetRenderer.TileSize.Width + y) * stride + i * TileSetRenderer.TileSize.Width + x] = Palette.GetColor(rawImage[x + TileSetRenderer.TileSize.Width * y]).ToArgb(); } } if (Map.MapResources.Value[u * ChunkSize + i, v *ChunkSize + j].Type != 0) { var resourceImage = ResourceTemplates[Map.MapResources.Value[u * ChunkSize + i, v *ChunkSize + j].Type].Bitmap; var srcdata = resourceImage.LockBits(resourceImage.Bounds(), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int *q = (int *)srcdata.Scan0.ToPointer(); var srcstride = srcdata.Stride >> 2; for (var x = 0; x < TileSetRenderer.TileSize.Width; x++) { for (var y = 0; y < TileSetRenderer.TileSize.Height; y++) { var c = q[y * srcstride + x]; if ((c & 0xff000000) != 0) /* quick & dirty, i cbf doing real alpha */ { p[(j * TileSetRenderer.TileSize.Width + y) * stride + i * TileSetRenderer.TileSize.Width + x] = c; } } } resourceImage.UnlockBits(srcdata); } } } } bitmap.UnlockBits(data); if (ShowGrid) { using (var g = SGraphics.FromImage(bitmap)) { var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); ControlPaint.DrawGrid(g, rect, new Size(2, Game.CellSize), Color.DarkRed); ControlPaint.DrawGrid(g, rect, new Size(Game.CellSize, 2), Color.DarkRed); ControlPaint.DrawGrid(g, rect, new Size(Game.CellSize, Game.CellSize), Color.Red); } } return(bitmap); }
public const int ChunkSize = 8; // 8x8 chunks ==> 192x192 bitmaps. Bitmap RenderChunk(int u, int v) { var bitmap = new Bitmap(ChunkSize * TileSetRenderer.TileSize, ChunkSize * TileSetRenderer.TileSize); var data = bitmap.LockBits(bitmap.Bounds(), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); unsafe { var p = (int *)data.Scan0.ToPointer(); var stride = data.Stride >> 2; for (var i = 0; i < ChunkSize; i++) { for (var j = 0; j < ChunkSize; j++) { var ui = u * ChunkSize + i; var vj = v * ChunkSize + j; var uv = new MPos(ui, vj); if (uv.U >= Map.MapSize.X || uv.V >= Map.MapSize.Y) { for (var x = 0; x < TileSetRenderer.TileSize; x++) { for (var y = 0; y < TileSetRenderer.TileSize; y++) { p[(j * TileSetRenderer.TileSize + y) * stride + i * TileSetRenderer.TileSize + x] = 0; } } continue; } var tr = Map.MapTiles.Value[uv]; var tile = TileSetRenderer.Data(tr.Type); if (tile == null) { continue; } var index = (tr.Index < tile.Length) ? tr.Index : (byte)0; var rawImage = tile[index]; for (var x = 0; x < TileSetRenderer.TileSize; x++) { for (var y = 0; y < TileSetRenderer.TileSize; y++) { p[(j * TileSetRenderer.TileSize + y) * stride + i * TileSetRenderer.TileSize + x] = Palette.GetColor(rawImage[x + TileSetRenderer.TileSize * y]).ToArgb(); } } if (Map.MapResources.Value[uv].Type != 0) { var resourceImage = ResourceTemplates[Map.MapResources.Value[uv].Type].Bitmap; var srcdata = resourceImage.LockBits(resourceImage.Bounds(), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var q = (int *)srcdata.Scan0.ToPointer(); var srcstride = srcdata.Stride >> 2; for (var x = 0; x < TileSetRenderer.TileSize; x++) { for (var y = 0; y < TileSetRenderer.TileSize; y++) { var c = q[y * srcstride + x]; if ((c & 0xff000000) != 0) /* quick & dirty, i cbf doing real alpha */ { p[(j * TileSetRenderer.TileSize + y) * stride + i * TileSetRenderer.TileSize + x] = c; } } } resourceImage.UnlockBits(srcdata); } } } } bitmap.UnlockBits(data); if (ShowGrid) { using (var g = SGraphics.FromImage(bitmap)) { var ts = Game.ModData.Manifest.TileSize; var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); ControlPaint.DrawGrid(g, rect, new Size(2, ts.Height), Color.DarkRed); ControlPaint.DrawGrid(g, rect, new Size(ts.Width, 2), Color.DarkRed); ControlPaint.DrawGrid(g, rect, new Size(ts.Width, ts.Height), Color.Red); } } return(bitmap); }