Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace WindowsFormsApplication1
- {
- public partial class MainForm : Form
- {
- Map map = new Map(0,0);
- Bitmap tileMap = (Bitmap)Bitmap.FromFile("tilemap.png");
- int vScrollValue = 0;
- int hScrollValue = 0;
- string fileName = "";
- public MainForm(){
- InitializeComponent();
- }
- public int[][] copyTiles(Map map, int nWidth, int nHeight)
- {
- int[][] nTiles = new int[nWidth][];
- for (int i = 0; i < nWidth; i++)
- {
- nTiles[i] = new int[nHeight];
- for (int j = 0; j < nTiles[i].Length; j++)
- {
- if (i < map.tiles.Length)
- {
- if (j < map.tiles[i].Length)
- nTiles[i][j] = map.tiles[i][j];
- }
- }
- }
- return nTiles;
- }
- private void resizeButton_Click(object sender, EventArgs e)
- {
- map.tiles = copyTiles(map, Int32.Parse(widthBox.Text), Int32.Parse(heightBox.Text));
- MapPanel.Refresh();
- }
- private void MapPanel_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- GraphicsUnit units = GraphicsUnit.Pixel;
- for (int i = 0; i < map.tiles.Length; i++)
- {
- for (int j = 0; j < map.tiles[i].Length; j++)
- {
- Rectangle destRect = new Rectangle((i * map.tileSize) - hScrollValue, (j * map.tileSize) - vScrollValue, 30, 30);
- g.DrawImage(tileMap, destRect, (map.tiles[i][j] * 50), 0, 50, 50, units);
- }
- }
- }
- private void mapScrollV_Scroll(object sender, ScrollEventArgs e)
- {
- vScrollValue = ((map.tiles[0].Length * map.tileSize) / 100) * mapScrollV.Value;
- MapPanel.Refresh();
- }
- private void mapScrollH_Scroll(object sender, ScrollEventArgs e)
- {
- hScrollValue = ((map.tiles.Length * map.tileSize) / 100) * mapScrollH.Value;
- MapPanel.Refresh();
- }
- private void saveButton_Click(object sender, EventArgs e)
- {
- saveFileDialog1.ShowDialog();
- FileStream stream = new FileStream(fileName, FileMode.Create);
- //TextWriter tw = new StreamWriter("test.txt");
- BinaryWriter writer = new BinaryWriter(stream);
- // write a line of text to the file
- writer.Write(map.tiles.Length);
- writer.Write(map.tiles[0].Length);
- for (int i = 0; i < map.tiles.Length; i++)
- {
- for (int j = 0; j < map.tiles[0].Length; j++)
- {
- writer.Write(map.tiles[i][j]);
- }
- }
- // close the stream
- writer.Close();
- }
- private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
- {
- fileName = saveFileDialog1.FileName;
- }
- private void MapPanel_MouseDown(object sender, MouseEventArgs e)
- {
- map.tiles[(int)(System.Windows.Forms.Cursor.Position.X / map.tileSize)][(int)(System.Windows.Forms.Cursor.Position.Y / map.tileSize)] = 1;
- MapPanel.Refresh();
- }
- }
- public class Map
- {
- public int[][] tiles;
- public int tileSize = 30;
- public Map(int width, int height)
- {
- tiles = new int[width][];
- for (int i = 0; i < width; i++)
- {
- tiles[i] = new int[height];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement