Advertisement
Guest User

Untitled

a guest
Mar 31st, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.  
  14.  
  15. public partial class MainForm : Form
  16. {
  17. Map map = new Map(0,0);
  18. Bitmap tileMap = (Bitmap)Bitmap.FromFile("tilemap.png");
  19. int vScrollValue = 0;
  20. int hScrollValue = 0;
  21. string fileName = "";
  22. public MainForm(){
  23.  
  24.  
  25.  
  26. InitializeComponent();
  27. }
  28.  
  29. public int[][] copyTiles(Map map, int nWidth, int nHeight)
  30. {
  31. int[][] nTiles = new int[nWidth][];
  32. for (int i = 0; i < nWidth; i++)
  33. {
  34. nTiles[i] = new int[nHeight];
  35.  
  36.  
  37. for (int j = 0; j < nTiles[i].Length; j++)
  38. {
  39. if (i < map.tiles.Length)
  40. {
  41. if (j < map.tiles[i].Length)
  42. nTiles[i][j] = map.tiles[i][j];
  43. }
  44. }
  45. }
  46.  
  47. return nTiles;
  48. }
  49.  
  50. private void resizeButton_Click(object sender, EventArgs e)
  51. {
  52.  
  53. map.tiles = copyTiles(map, Int32.Parse(widthBox.Text), Int32.Parse(heightBox.Text));
  54. MapPanel.Refresh();
  55. }
  56.  
  57. private void MapPanel_Paint(object sender, PaintEventArgs e)
  58. {
  59. Graphics g = e.Graphics;
  60. GraphicsUnit units = GraphicsUnit.Pixel;
  61.  
  62. for (int i = 0; i < map.tiles.Length; i++)
  63. {
  64. for (int j = 0; j < map.tiles[i].Length; j++)
  65. {
  66. Rectangle destRect = new Rectangle((i * map.tileSize) - hScrollValue, (j * map.tileSize) - vScrollValue, 30, 30);
  67. g.DrawImage(tileMap, destRect, (map.tiles[i][j] * 50), 0, 50, 50, units);
  68. }
  69. }
  70. }
  71.  
  72. private void mapScrollV_Scroll(object sender, ScrollEventArgs e)
  73. {
  74. vScrollValue = ((map.tiles[0].Length * map.tileSize) / 100) * mapScrollV.Value;
  75. MapPanel.Refresh();
  76. }
  77.  
  78. private void mapScrollH_Scroll(object sender, ScrollEventArgs e)
  79. {
  80. hScrollValue = ((map.tiles.Length * map.tileSize) / 100) * mapScrollH.Value;
  81. MapPanel.Refresh();
  82. }
  83.  
  84. private void saveButton_Click(object sender, EventArgs e)
  85. {
  86. saveFileDialog1.ShowDialog();
  87.  
  88. FileStream stream = new FileStream(fileName, FileMode.Create);
  89. //TextWriter tw = new StreamWriter("test.txt");
  90. BinaryWriter writer = new BinaryWriter(stream);
  91. // write a line of text to the file
  92.  
  93. writer.Write(map.tiles.Length);
  94. writer.Write(map.tiles[0].Length);
  95.  
  96. for (int i = 0; i < map.tiles.Length; i++)
  97. {
  98. for (int j = 0; j < map.tiles[0].Length; j++)
  99. {
  100. writer.Write(map.tiles[i][j]);
  101. }
  102. }
  103.  
  104. // close the stream
  105. writer.Close();
  106. }
  107.  
  108. private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
  109. {
  110.  
  111. fileName = saveFileDialog1.FileName;
  112. }
  113.  
  114. private void MapPanel_MouseDown(object sender, MouseEventArgs e)
  115. {
  116. map.tiles[(int)(System.Windows.Forms.Cursor.Position.X / map.tileSize)][(int)(System.Windows.Forms.Cursor.Position.Y / map.tileSize)] = 1;
  117. MapPanel.Refresh();
  118. }
  119. }
  120.  
  121. public class Map
  122. {
  123. public int[][] tiles;
  124. public int tileSize = 30;
  125.  
  126. public Map(int width, int height)
  127. {
  128. tiles = new int[width][];
  129. for (int i = 0; i < width; i++)
  130. {
  131. tiles[i] = new int[height];
  132. }
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement