|

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:
string "MapEditorFile"
{
float [version number]
long [length of Tilesets data block]
{
long [length of tileset data]
{
int [tilesize]
long [length of text]
string [name of tileset]
long [length of image data]
{
}
}
...
}
long [length of Tiles data block]
{
int [width of map]
int [height of map]
{
int [src xpos]
int [src ypos]
bool [solid]
int [tileset index]
}
...
}
}
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.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh) | FirstPrevNext |
|
 |
|
|
hello dude
very good our soft, excellent for my game engine
only problem.
i draw a map and save.
Now the map are cripted
and for use all cordinates in my game engine need to decript file and read the all cordinates.
tiles and position
how to decrypt a map?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
If you use the MapReader DLL, you should be able to get a 2d array of Tile objects that represent your map. If a tile is at [4,6] in the arry, and you have a tilesize of 32 the the tiles position in-game would be [128,192]. You should be able to get all the information you need from the DLL.
Also the data isn't encrypted, its just written sequentially into a file (in binary).
My current favourite word is: Nipple!-SK Genius Game Programming articles start - here[ ^]-
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanx for this article, and the sample source code. It's very similiar to something I was creating for myself, and was wondering if I could ask a couple of questions...
1) Is there a way to select more than 1 tile, and paint with it on the map? Say for example I wanted to select four tiles, and drop them onto the map?
2) Is there any way to re-arrange the tiles on the map once they've been dropped?
3)Is there a way to save the map as strictly an image, and not so much a map, readable by a game...
What I'm wanting to make is a custom tileset maker, where you load an image, select tiles from that image, drop into a new image (like the map), and then create a new custom tileset... This way the user could extract elements from different tilesets they already have, and combine them into a completely new tileset...
Thanx again, for this artivcle though. It certainly answered someof the questions I had.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I actually worked on this a little while ago, but havn't updated the article yet. In my updated version you can select and drop multiple tiles, though you can't move tiles you've already placed, you just have to re-place them.
As for saving as an image, you could use the code for drawing to the screen, but instead of using the graphics object for the screen substitute it for your own graphics object created from an Bitmap. Everything would then be drawn to this bitmap and you can save it to disc. It's probably best to just copy the contents of the OnPaint method into a new method and make the changes there.
I'll send you an e-mail with a link to the latest version I have so you can look at how I'm currently selecting multiple tiles. I really must get 'round to updating this... and continueing with my other articles... one day.
My current favourite word is: I'm starting to run out of fav. words!-SK Genius Game Programming articles start - here[ ^]-
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I would be very interested in seeing your updated versions, that allows for selecting of multiple tiles. Thanx!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm not sure if you ever sent the email you mentioned in your reply, but I never received anything. Are you going to email it?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I sent it using the e-mail link at the bottom of your post, so it should have been sent to whichever e-mail address you're using with CP.
To check what address CP may (or may have not) sent the e-mail to, check your profile here[^]
My current favourite word is: I'm starting to run out of fav. words!-SK Genius Game Programming articles start - here[ ^]-
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I checked my profile, and the listed email address is correct, I dunno whyt I never got it. I checked my spam folder as well, and there was nothing in there either. I don't get any email notifications from the discussion here either, so perhaps it's something with the mail server codeproject uses? Could you re-email it to jsheble AT logicor.com?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I mean the map should contain information about which regions a sprite is movable. By the way, I'd like you go on with Collision Detection and Parallax Scrolling stuff first, don't worry about the map editor too much at this time.
I am happy to work with people doing great projects.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
You can already set if a tile is solid or not, that would give you rectangular regions but i'm not so sure how useful setting up exact regions would be. If your thinking of slopes, you could always add an extra propety to the tile class which you can use to store the gradient of the slope, then when you come accross the tile ingame you can read the gradient and adjust the players position accordingly.
Liu Junfeng wrote: By the way, I'd like you go on with Collision Detection and Parallax Scrolling stuff first, don't worry about the map editor too much at this time.
This isn't really part of the other articles, it was just something else I was working on. I'll get 'round to doing the next article eventually.
My current favourite word is: I'm starting to run out of fav. words!-SK Genius Game Programming articles start - here[ ^]-
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Game programming. After all those grids and controls posted on CP, it's good to see someone is actually doing something different for a change. I'll vote your article with a 5, for the breath of fresh air. It reminds me of when I won a programming contest with a tile-based game like Sokoban. I actually came in 4th, but that's a prize too. There were actually other games that looked better than mine (think Warcraft/Starrcraft look-a-likes). They didn't get anything for being incomplete projects. But my humble (finished) tile game got prized over them.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hello, well I have to tell you that I have been reading your previous "posts" not articles, because are really for dummies, for instance in this article:
1.- Only one tile layer. At least two tiles layers is necessary. 2.- Doesn't exists the posibility of making groups of tiles and you put directly on the layer. 3.- What happens if I want to make a big map, like 400x16, the layout of the controls is not the best, why dont you put the tileset below the tile map or in a "control child" window. 4.- Selections, cut, copy, paste? Delete with right button? Why not a menu with right button? 5.- Exporting as array, like the whole life? 6.- When I pass the cursor over a tile in the tile set and in the tile map, I dont know which tile number is. 7.- Placing a background or two as bg layer below the tile layers.
And then to be able to call it "article" it needs:
6.- Animated tiled layers. 7.- Sprite characters layer. 8.- Window (as screen) scrolling like preview. 9.- Zoom, undo, redo. 10.- Setting opacity between layers.
I hope dont find this reply as offensive, only is constructive criticism, because I am thinking of making a map editor, for programming the NDS and you have to improve a lot.
modified on Monday, May 19, 2008 3:13 AM
|
| Sign In·View Thread·PermaLink | 4.00/5 (1 vote) |
|
|
|
 |
|
|
Well thanks for the comments, this is just V0.1.
1.- Only one tile layer. At least two tiles layers is necessary. I was already planning on adding extra layers, so that'll be up soon
2.- Doesn't exists the posibility of making groups of tiles and you put directly on the layer. Thats a pretty good idea, you mean like selecting 5 tiles in a row and then putting all 5 tiles on in one click?
3.- What happens if I want to make a big map, like 400x16, the layout of the controls is not the best, why dont you put the tileset below the tile map or in a "control child" window. I actually like the layout. Perhaps if i make it so you can drag the TileSelect control out into its own window, then you can have it whichever way you want
4.- Selections, cut, copy, paste? Delete with right button? Why not a menu with right button? Maybe i could add in selections, but only show the context menu when a selection has been made. That way i can keep right-click to remove a tile.
5.- Exporting as array, like the whole life? I'm not sure what you mean
6.- When I pass the cursor over a tile in the tile set and in the tile map, I dont know which tile number is. Thats true, i'll just put another status bar on the bottom of it.
7.- Placing a background or two as bg layer below the tile layers. Now thats something i didn't think of, i should probably do that one.
6.- Animated tiled layers. Animated tile layers, well it seems like it could be a good idea, but i;m not so sure. I would usually add an extra sprite or something if i wanted something animated on a map. If i where to add this in, it wouldn't be for a while.
7.- Sprite characters layer. Sprite characters layer? Do you mean for players an enemies? Becuase i'm not sure we can add that in without turning this into a level designer where you can add player objects and other things, and that would be getting a little specific for a map editor.
8.- Window (as screen) scrolling like preview. A scrolling preview eh? Might be good for 2D-Side scrolling games, but for a rpg style game? Besides you can always turn off the grid and scroll yourself. I'm not sure how much sue this would get.
9.- Zoom, undo, redo. I was thinking about adding a zoom feature, seems you'd like it too. Undo-Redo i should probably do.
10.- Setting opacity between layers. It gets less like a map with every proposal I'll think about that one. It wouldn't be rendered with transparency in the editor though, that would be ridiculously slow, we would need some preview window for that - one that wouldn't need much scrolling.
As for the other articles, you do need to start at the begining.
My current favourite word is: I'm starting to run out of fav. words!-SK Genius Game Programming articles start - here[ ^]-
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
5.- Exporting as array, like the whole life? I'm not sure what you mean
Well, what I mean is to export the tiles maps as arrays with the tiles numbers
Hey, in case you find something difficult to develop, if you want post your questions here, I'll help you if I can
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
As in a text file?
So it would export something like:
0,5 1,5 1,5 0,4
My current favourite word is: I'm starting to run out of fav. words!-SK Genius Game Programming articles start - here[ ^]-
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|