Making a C++ map editor (need help)
Moderator: Coders of Rage
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Making a C++ map editor (need help)
Well, I've been wanting to make a map editor in C++ for a long time now, (let's ignore the fact that I'm using DirectX for a second)
and I just can't understand how to split the map to 64x64 or any kind of "blocks".
If you can send me some code or tutorial I can look at it would be very appreciated (I don't really care what language, just want to get a hold of the concept)
P.S.
I don't have problem blitting tiles, but when it comes to the user placing them in a "tile" order like in the ES map editor I just get lost.
will post my progress here.
Modulus FTW%!
Thank you in advance.
and I just can't understand how to split the map to 64x64 or any kind of "blocks".
If you can send me some code or tutorial I can look at it would be very appreciated (I don't really care what language, just want to get a hold of the concept)
P.S.
I don't have problem blitting tiles, but when it comes to the user placing them in a "tile" order like in the ES map editor I just get lost.
will post my progress here.
Modulus FTW%!
Thank you in advance.
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Re: Making a C++ map editor (need help)
Instead of letting the user browse files, I will use an algorithm to bring all image files from a folder, you can then switch tiles (added later) using the directional keys (left and right) on the keyboard or some other method I haven't quite decided yet. (move back and forth in the vector, or should I make this a list? don't have anything done so I chose the option that fits anything, i.e. vector)
This is what I got so far
The purpose of this algorithm is to get the full path to all the image files in the specific folder, then link them to a LPDIRECT3DTEXTURE(again... DirectX) the user controls
You can also use this algorithm for tons of other stuff, tell me what you think.
This is what I got so far
Code: Select all
//Test version(debug) to be moved to ListImg.cpp
#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;
char* locjpg = "C:\\UNIMPICS\\*.jpg"; //any file; *.jpg- any filename with .jpg extension
char* locbmp = "C:\\UNIMPICS\\*.bmp"; //any file; *.bmp- any filename with .bmp extension
char* locpos = "C:\\UNIMPICS\\";
char* nfilen;
unsigned f=1;
int main()
{
char desOfile[80];
vector<string> victor;
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile((LPCSTR)locjpg, &findFileData);
cout <<"Picture files in dir:" <<"\n"<<locjpg<<"\n"<<locbmp<<"\n\n"<<endl;
cout <<findFileData.cFileName<<"\n"<<endl;
nfilen = findFileData.cFileName;
strcpy(desOfile,"C:\\UNIMPICS\\");
strcat(desOfile,nfilen);
victor.push_back(desOfile);
for(f=1/*FindFirstFile is the first file*/;FindNextFile(hFind,&findFileData);++f/*count file for w/e use*/)
{
strcpy(desOfile,"C:\\UNIMPICS\\");
strcat(desOfile,nfilen);
cout<<desOfile<<"\n\n"<<endl; //I had to go and wanted to flush...
victor.push_back(desOfile);
}
for(unsigned v=0;v<f;v++)
{
cout<<victor[v].c_str()<<"\n";
}
system("PAUSE");
return 0;
}
You can also use this algorithm for tons of other stuff, tell me what you think.
Last edited by Netwatcher on Mon Jul 06, 2009 2:32 pm, edited 1 time in total.
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Making a C++ map editor (need help)
Basically the map is just an array (whether it be 1D or 2D) of tiles. Tiles can be as simple as integers, or they can actually be their own class with all sorts of other attributes.
Drawing the map to the screen involved iterating through that array, or vector if you wish, and adding tile width to the drawing position each time until you reach the end of the row where you would add tile height to the drawing position (ie. 0,0 ; 32,0 ; 64, 0 ; 96,0 for 32x32 tiles).
Saving the map can also be done by simple writing integers out to a text file, and reading them back into an array to load the map. More complex methods would have to be created if you plan to store more information than just the image of that particular tile.
Did this help at all?
Drawing the map to the screen involved iterating through that array, or vector if you wish, and adding tile width to the drawing position each time until you reach the end of the row where you would add tile height to the drawing position (ie. 0,0 ; 32,0 ; 64, 0 ; 96,0 for 32x32 tiles).
Saving the map can also be done by simple writing integers out to a text file, and reading them back into an array to load the map. More complex methods would have to be created if you plan to store more information than just the image of that particular tile.
Did this help at all?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Re: Making a C++ map editor (need help)
I know how to draw tiles on a 2D plane(modulus ftw) and the mapping method (why would I use vector where I can use an array?)dandymcgee wrote:Basically the map is just an array (whether it be 1D or 2D) of tiles. Tiles can be as simple as integers, or they can actually be their own class with all sorts of other attributes.
Drawing the map to the screen involved iterating through that array, or vector if you wish, and adding tile width to the drawing position each time until you reach the end of the row where you would add tile height to the drawing position (ie. 0,0 ; 32,0 ; 64, 0 ; 96,0 for 32x32 tiles).
Saving the map can also be done by simple writing integers out to a text file, and reading them back into an array to load the map. More complex methods would have to be created if you plan to store more information than just the image of that particular tile.
Did this help at all?
I use a multi-dimensional array for multiple levels or for special attributes(non-passable,Z-order etc..)
My problem is with the UI
I don't see how I can let the user replace tiles visually;clicking on the tile and changing it to a different one, or how can I detect when the mouse is over a certain tile.
Do you say I need to first make an integer-based tile-editor and then bind the integers to the bitmaps/visual tiles?
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- Bakkon
- Chaos Rift Junior
- Posts: 384
- Joined: Wed May 20, 2009 2:38 pm
- Programming Language of Choice: C++
- Location: Indiana
Re: Making a C++ map editor (need help)
Basically use point inside rectangle collision detection.Netwatcher wrote: My problem is with the UI
I don't see how I can let the user replace tiles visually;clicking on the tile and changing it to a different one, or how can I detect when the mouse is over a certain tile.
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Re: Making a C++ map editor (need help)
Wow, the simplest solution and I didn't even think about it >,<.Bakkon wrote:Basically use point inside rectangle collision detection.Netwatcher wrote: My problem is with the UI
I don't see how I can let the user replace tiles visually;clicking on the tile and changing it to a different one, or how can I detect when the mouse is over a certain tile.
The more tiles I get the more rectangles I need to make...
Is there a way to fasten this process of creating rects for each tile?
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: Making a C++ map editor (need help)
I just use this to determine the x of the tile which the mouse is inside
and the same for the y.
Code: Select all
((mousex + camera.x) - ((mousex + camera.x) % OBJECTWIDTH)) / OBJECTWIDTH;
I'll make your software hardware.
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Re: Making a C++ map editor (need help)
Code: Select all
((mousex + camera.x) - ((mousex + camera.x) % OBJECTWIDTH)) / OBJECTWIDTH;
created a sprite "mouse" object who's position is determined by mickies (if I spelled it right ) so it's easier to locate the mouse and check collision like that(since i've already created my bounding box) ;D
What is the camera object for??? i didn't say i was scrolling :O
#IF nothing_here_makes_sense
#define ME tired
#endif
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Making a C++ map editor (need help)
Code: Select all
mapArray[ layer ][ mouse_x/tile_width ][ mouse_y/tile_height ];
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: Making a C++ map editor (need help)
sorry. That code is just what is used in mine. It should work exactly the same without the camera.xNetwatcher wrote:What is the camera object for??? i didn't say i was scrolling :O
That's pretty much mine but with yours it would round it to the nearest integer, this means that even if your mouse was to the right or bottom of the tile's center it would place the tile in the tile to the right or below.dandymcgee wrote:Or something like that..Code: Select all
mapArray[ layer ][ mouse_x/tile_width ][ mouse_y/tile_height ];
I'll make your software hardware.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Making a C++ map editor (need help)
Fix'dthejahooli wrote: That's pretty much mine but with yours it would round it to the nearest integer, this means that even if your mouse was to the right or bottom of the tile's center it would place the tile in the tile to the right or below.
Code: Select all
mapArray[ layer ][ (mouse_x - (mouse_x % tile_width)) / tile_width ][ (mouse_y - (mouse_y % tile_height)) / tile_height ];

Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Re: Making a C++ map editor (need help)
dandymcgee wrote:Fix'dthejahooli wrote: That's pretty much mine but with yours it would round it to the nearest integer, this means that even if your mouse was to the right or bottom of the tile's center it would place the tile in the tile to the right or below.What do ya know, it's EXACTLY the same as your code.Code: Select all
mapArray[ layer ][ (mouse_x - (mouse_x % tile_width)) / tile_width ][ (mouse_y - (mouse_y % tile_height)) / tile_height ];
Looks like the code I use to blit tiles O_o just with mouse input.(well not completely like it)
Thank you guys.
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: Making a C++ map editor (need help)
Take the position of the mouse ( add and subtract here if you want to use a camera object)
Then divide that by the size of the tile to get a number representing the position of the mouse in tile widths
Then floor this number to get the tile that the user is on.
(If you're using a 1d array, the position in the array will be the x tile position, add the y tile position multiplied by how many tiles wide the screen is)
I know others have said this, but just said it in idiot proof steps incase the problem isnt fixed :P
To the OP; i think you need to put [Solved] in the title if your problem is solved
Then divide that by the size of the tile to get a number representing the position of the mouse in tile widths
Then floor this number to get the tile that the user is on.
(If you're using a 1d array, the position in the array will be the x tile position, add the y tile position multiplied by how many tiles wide the screen is)
I know others have said this, but just said it in idiot proof steps incase the problem isnt fixed :P
To the OP; i think you need to put [Solved] in the title if your problem is solved
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Re: Making a C++ map editor (need help)
I need to take the position of the mouse position to the window right? (If i'm using abs mouse pos)RyanPridgeon wrote:Take the position of the mouse ( add and subtract here if you want to use a camera object)
Then divide that by the size of the tile to get a number representing the position of the mouse in tile widths
Then floor this number to get the tile that the user is on.
(If you're using a 1d array, the position in the array will be the x tile position, add the y tile position multiplied by how many tiles wide the screen is)
I know others have said this, but just said it in idiot proof steps incase the problem isnt fixed :P
To the OP; i think you need to put [Solved] in the title if your problem is solved
Won't it only work in full-screen mode?
Think I just might learn SDL instead of fighting with DirectX for the map editor

"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Making a C++ map editor (need help)
This isn't necessary. Simple division will do the trick. Much easier on memory, too.Bakkon wrote:Basically use point inside rectangle collision detection.
No. C doesn't round. It truncates.thejahooli wrote:That's pretty much mine but with yours it would round it to the nearest integer, this means that even if your mouse was to the right or bottom of the tile's center it would place the tile in the tile to the right or below.
Take the mouse coordinates relative to the control. Which really just means, take the mouse coordinates. It doesn't matter whether or not the screen is maximized. If you want to "snap" the mouse to a position, for instance, (0, 0) - (31, 31) is one tile, which means TILE_HEIGHT = 32 and TILE_WIDTH = 32, then you can do:Netwatcher wrote:I need to take the position of the mouse position to the window right? (If i'm using abs mouse pos)
myMapsTiles[mouse.y / TILE_HEIGHT][mouse.x / TILE_WIDTH]
Thanks to the wonders of integer math, this works just fine. No need to floor() anything.
mouse.y / TILE_HEIGHT
0/32 = 0.
1/32 = 0.0322580645 -> which is truncated to 0
31/32 = 0.98675 -> 0
32/32 = 1, which means you're now modifying the next tile down.
To be able to select a tile, you do the same thing, only in your tile selection tool. If all your tiles are output in a straight line, you do
selectedTile = mouse.x / TILE_WIDTH;
And then when they click on the map, you do
myMapsTiles[mouse.y / TILE_HEIGHT][mouse.x / TILE_WIDTH] = selectedTile;
Keep in mind the mouse coordinates I've used are relative to the control that has been clicked. If you have application-wide mouse coordinates, you can do this:
selectedTile = (mouse.x - myTool.x) / TILE_WIDTH;
myMapsTiles[(mouse.y - mapViewport.y) / TILE_HEIGHT][(mouse.x - mapViewport.x) / TILE_WIDTH];
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.