5,660,782 members and growing! (14,377 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Game Development » General     Intermediate License: The Code Project Open License (CPOL)

2D Map Editor

By SK Genius

Create and edit 2D maps using tiles.
C#, Windows, .NET, GDI, GDI+, WinForms

Posted: 18 May 2008
Updated: 18 May 2008
Views: 16,676
Bookmarked: 26 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 4.31 Rating: 4.52 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 11.1%
3
2 votes, 22.2%
4
6 votes, 66.7%
5

MapReader_dll

Introduction

This is a somewhat simple application to make and edit 2D maps for your games. It's not full of fancy features, but it does get the job done. You can place tiles from a tile set, and define if they are solid or not, change the size of each tile, and add/remove tile sets. Oh, and of course, it can save and load to your hard disk - it wouldn't be much use if you couldn't save your map. The application also includes some settings (available through the menu/F7) so that you can change the colour of the grid and other information, change the font used in the controls, etc.

Background

A while ago, I made a very quick, very unstable map editor for a project. It wasn't very good, but now that I have the time, I have completely rebuilt it from scratch. So, this time, it actually works without crashing, the UI is simpler, and the resulting file is much neater.

Building the Application

The program is made up of three main things:

  • A way to select tiles
  • Somewhere to put the tiles
  • A way to save and load the data

So, there are two main controls, the TileSelect, and the TiledMap. The TileSelect control is made of a PictureBox to display the image, a ComboBox to change the image, and a NumericUpDown to change the TileSize. The OnPaint event of the PictureBox is used to draw a grid over it so we can see where each tile is. The TiledMap is similar, but we need to put tiles onto it. To store the tiles, we use a 2D array of the class Tile; each instance of the Tile class has a reference to the image it is using and the XY position of the tile in that image.

When we click the TiledMap, an event is fired with information on which tile was clicked. The main form uses this event, and gets the currently selected tile from the TileSelect and sends this tile to the TiledMap.

The TileSelect and TiledMap do not interact with each other directly, they each have events that are fired when their properties change or they are clicked etc. The main form listens for these events and passes information between the two controls.

Drawing the Map

So, you have a map, 40*25 tiles that fills your screen. In the OnPaint event, you'd have to draw 1000 tiles. If you're using Graphics.DrawImage(...), then this is very, very slow. So, we have to take a step backwards and use the good old BitBlt from GDI. When the Tile Select control loads a new tileset, it also creates a HBitmap and stores it. That way, when we come to render the map, we can use this HBitmap and BitBlt to quickly draw it onto the screen. The only problem with using BitBlt is that it doesn't support transparency, but its a small price to pay for the application to work.

Saving Your Map

The file format is like this:

//FILE FORMAT

string "MapEditorFile"
{
  float [version number]
 
  //Tilesets (including images)
  long [length of Tilesets data block]
  {
    //Tileset
    long [length of tileset data]
    {
      int [tilesize]
      long [length of text]
      string [name of tileset]
      long [length of image data]
      {
        //Image data
      }
    }
    ...
  }

  //Tiles
  long [length of Tiles data block]
  {
    int [width of map]
    int [height of map]

    //Tile
    {
      int [src xpos]
      int [src ypos]
      bool [solid]
      int [tileset index]
    }
    ...
  }

  //Aditional data can be added without affecting
  // earlier versions of the application
}

As you can see, the images are saved within the file; this way, we don't need to remember to copy the image files with the map, and the user of the map can't just go around swapping/deleting the images.

Using the Code

Once you've made the map and saved it, if you want to use it in some game or other application, I've included a DLL with the MapReader class and other necessary classes. If you use the DLL to load your map, you will get a List<> of type TileSet. Each TileSet contains the image and its tile size, as well as a 2D array of Tiles, which contains a reference to the tile set it uses, its state (solid or not), and the X and Y positions of the tile in the image.

Points of Interest

The source code contains a few classes you might find useful elsewhere:

  • ColourPicker - A simple combobox colour picker
  • ReusableMessageBox - More of a cheat so you don't need to reproduce the same message box over and over

OK, a couple of classes you might find useful. Also, did you know that if you set a shortcut key for a menu item, and the MenuStrip has focus, the shortcut doesn't work? I don't know if it was just me, but there it is.

Well, I hope somebody finds this useful, and I hope I didn't go over the top on events. Well, until next time.

History

  • 18th May - Article posted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SK Genius


*blip*
Occupation: Software Developer
Location: United Kingdom United Kingdom

Other popular Game Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
QuestionDecripting Mapmemberthejuster3:19 10 Sep '08  
AnswerRe: Decripting MapmemberSK Genius9:11 10 Sep '08  
GeneralCurious about additional functionalitymemberJSheble14:33 26 Jun '08  
GeneralRe: Curious about additional functionalitymemberSK Genius14:40 26 Jun '08  
GeneralRe: Curious about additional functionalitymemberJSheble7:43 27 Jun '08  
GeneralRe: Curious about additional functionalitymemberJSheble6:09 1 Jul '08  
GeneralRe: Curious about additional functionalitymemberSK Genius6:12 1 Jul '08  
GeneralRe: Curious about additional functionalitymemberJSheble9:08 1 Jul '08  
GeneralEdit enterable non rectangular regions of a mapmemberLiu Junfeng0:18 23 May '08  
GeneralRe: Edit enterable non rectangular regions of a mapmemberSK Genius0:58 23 May '08  
RantBreath of fresh air :)membersk8er_boy2872:07 19 May '08  
GeneralRe: Breath of fresh air :)memberMarkBrock16:22 24 May '08  
GeneralToo much beginner article [modified]mvpJuan Pablo G.C.22:05 18 May '08  
GeneralRe: Too much beginner articlememberSK Genius0:32 19 May '08  
GeneralRe: Too much beginner articlemvpJuan Pablo G.C.1:53 19 May '08  
GeneralRe: Too much beginner articlememberSK Genius1:56 19 May '08  
GeneralAnimated Tile LayersmemberReelix21:14 19 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 May 2008
Editor: Smitha Vijayan
Copyright 2008 by SK Genius
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project