Sign in to follow this  
TheFlea

source for a simple map-editor wrote in c

Recommended Posts

TheFlea    107
Hi, i'm new in this forum. I want to create a simple map-editor in C, but i need some tutorials or source of other map-editors... thank you.

Share this post


Link to post
Share on other sites
Bob Janova    769
A map editor for what? 2D or 3D? What sort of game?

For some games Notepad is an adequate map editor, so no-one can possible help you without some more specifics.

Share this post


Link to post
Share on other sites
TheFlea    107
Hi ... I understand @Bob Janova you are perfectly right. I want an example for a map-editor (or a tutorial) 2D ,not 3D . I need for a strategy game.
I repeat : A SIMPLE MAP-EDITOR. I find a long time a tutorial, but he included allegro.h ( and I dont know hwo installing this library) I use Visual C++ 6
I know only create problem for highschool(-school:PASCAL :((boring; Home:C/C++-)

P.S. sorry for my bad english (with this problem, I learn English(very well) and i please ,you when i wrong in a senteces with a gramatical problem, tell me!!! )
thank you in advance.


Share this post


Link to post
Share on other sites
DumbBill    133
Map editors can get very complex very quickly. I would advise against trying to get the souce code for a complete editor and modifying it. It would be far better for you to start from the ground up and learn the basics as you go along. Learning the details of tile based engines will allow you to build a much better editor and then go onto developing games with it. If you jump in with someone elses code, you might be able to figure it out, but you'll probably spend a lot of time trying to make it do what you want.


There's a lot of info on this site for tile based games here:

Gamedev tile based stuff

And another site with a lot of useful stuff on it is:

Amit's game programming info

There's loads more too I'm sure.


PS. Most of your English was actually very good.

Share this post


Link to post
Share on other sites
pulpfist    528
This looks like a good tutorial for creating a tile-based map editor in C from scratch.
It uses allegro. You can follow method B to install allegro for Visual C++.

When writing a tile based map editor the biggest challenge is to react on keyboard and mouse clicks. Allegro has functions to do this, as well as functions to do all the drawing of the tiles, so it will make life much easier for you.

Creating the editor with native WIN32 API is possible but more complex.
Here is an example on creating a level editor.

Here is another tutorial based on SDL.

If you have a RAD tool like Delphi (pascal) or Borland C++ Builder (C++) it can be done quite easy. They both have functions for handling graphics, keyboard and mouse.

Share this post


Link to post
Share on other sites
Bob Janova    769
Well, for a simple strategy game with a rectilinear grid I would say use Notepad as your map editor! Each character position represents one tile in the game, and different letters can be used for different types of tile (blank space for an open tile, dots for water, T for trees etc).

If you want to include entities in the map, below the section that defines the terrain you can just list them. I.e.
# Example map file with a river
T TT T ... TT
T T TT ... T T T
TT T ... T
T TT .. . T
T ...
...

# Below a blank line, list entities
# type, team, x, y
building_hq1,team_0,0,1
unit_civilian,team_neutral,16,5


Obviously you need to write a parser for the entity lines if you do this, but it is very easy to do so.

Once you have a game engine, you can re-use parts of it to create a level editor in which tiles are displayed in the same way as they are in the game, entities can be placed and edited and so on. But that can wait until later.

Share this post


Link to post
Share on other sites
DumbBill    133
I agree with Bob Janova that you can leave the map editor until later. Most professional games will probably have the level editor first, but when doing it on your own it can save a lot of time to get the basic game up and running first.

I wrote a platform game similar to Sonic The Hedgehog that had an in-built editor. This was a lot easier, since I could use much of the same code to handle user input, rendering the map etc.

You can also use bitmap images as maps, and have different colours for different objects. Green for trees, blue for water etc. This has an added advantage that you can use the bitmap image as a sort of mini-map in the game itself, which would be handy in an RTS game.

Share this post


Link to post
Share on other sites
TheFlea    107
@pulpfist and @DumbBill thank you for answers! This tutorial i talk and me
http://www.adersoftware.com/src/editor.html This tutorial is not for OOP or something else... (so i dont need to know programming on windows for this tutorial, dont I?)
I choose METHOD B(with instaling allegro.h) -I wait a good internet ,because i use a DIAL-UP, for download this paches.

A question : What means "TILE"

@Bob Janova and @DumbBill: you jocking me ? be serious!

"# Example map file with a river
T TT T ... TT
T T TT ... T T T
TT T ... T
T TT .. . T
T ...
...

# Below a blank line, list entities
# type, team, x, y
building_hq1,team_0,0,1
unit_civilian,team_neutral,16,5"

NOT BAD! :):D But I want to use allegro :P

off-topic
I decided to create a game not for a internet which means like that
www.bitefight.com (he is for the internet) (sorry if I do ads),but only for computer (for DOS)
I used function Sleep for waiting some min or sec.
What you say?(when you know i am a beginner in C\C++ , I know only programming for DOS)

Share this post


Link to post
Share on other sites
pulpfist    528
Quote:

http://www.adersoftware.com/src/editor.html This tutorial is not for OOP or something else... (so i dont need to know programming on windows for this tutorial, dont I?)

Thats right. You dont need to know OOP or Win32 API.
Quote:

A question : What means "TILE"

A tile is usually no more than a small image.
A tileset is a collection of tiles.

Here is a good tileset for free that can be used to make a game level.

The level in a 2d game is created by drawing tiles side by side.

Quote:

off-topic
I decided to create a game not for a internet which means like that
www.bitefight.com (he is for the internet) (sorry if I do ads),but only for computer (for DOS)
I used function Sleep for waiting some min or sec.
What you say?(when you know i am a beginner in C\C++ , I know only programming for DOS)

When you say DOS, do you mean the text console?

Programming with graphics is a bit more difficult than text only.

If you are unexperienced, setting up allegro with Visual Studio can be difficult. I have not done it myself for years so I am not sure.

Good luck =)

[Edited by - pulpfist on March 13, 2006 4:07:48 PM]

Share this post


Link to post
Share on other sites
TheFlea    107
hi, text console ! I have a change?
I decided to don't learn grahics for DOS, what u say ?
"If you are unexperienced, setting up allegro with Visual Studio can be difficult." WHY IS DIFFICULT? if i read Method B and if I installing this lib. I will read tutorials!

"I have not done it myself for years so I am not sure."
I don't understand what mean this sentences...

regards.

Share this post


Link to post
Share on other sites
TheFlea    107
news... I want to download "Allero Source Version (2.7Mb)" to method B but dont work ... The page cannot be displayed for DOS / Windows
Why dont work ?
other link's to installing LIB ALLEGRO ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
LOOK What I can do!


#include <cstdlib>
#include <iostream>

using namespace std;

#define NUM_X 10
#define NUM_Y 10

int board[NUM_X][NUM_Y] = {0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,};

void DrawBoard(void);

int main(int argc, char *argv[])
{
DrawBoard();
system("PAUSE");
return EXIT_SUCCESS;
}

void DrawBoard(void)
{
int n, m;
for (n=0; n < NUM_X; n++)
{
for (m=0; m < NUM_Y; m++)
{
if (board[n][m] == 1) cout << 'X';
else cout << 'O';
}
if (m == NUM_Y) cout << "\n";
}
}

It makes 1's into X's! So far I can't find any use for this. Yay!

Share this post


Link to post
Share on other sites
TheFlea    107
ok...any links for installing Allegro? I need for trying to make a map-editor in there tutorial ! I read other threads in this forum...but...they want to create in MFC, and others;

I have a other question :
I want to return a RANDOM of other char up[126]="---"; (I dont know hwo say in english)
Is it a function for my problem ??



Share this post


Link to post
Share on other sites
TheFlea    107
news... I download to this site http://www.talula.demon.co.uk/allegro/wip.html
first .zip "Source code for all platforms in DOS/Windows friendly format."
oh...I forgot... and download 3 dll's who name's
alld40.dll
alleg40.dll and
allp40.dll

This is only i need to installing this "legend" lib...? "Legend": because it is for me this...
Please tell me how installing this thing ...


P.S. I know that i will irritate you but I am a novice(not a noob) (I now to create programs for highschool-only that- and this is the reason to create other things...i.e. I creating a minigame text mod and try to create a map-editor)

"Thats right. You dont need to know OOP or Win32 API." and what release from this tutorial? (an map-editor on DOS or windows)

See you soon and thank's for everything...


Share this post


Link to post
Share on other sites
TheFlea    107
I have a problem: why write me this
--------------------Configuration: ga - Win32 Debug--------------------
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/ga.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

ga.exe - 2 error(s), 0 warning(s)

I use VC++ and version allegro 4.2.0
#include <stdio.h>
#include <stdlib.h>
#include <allegro.h>


#define NUM_TILES 10 // number of tiles in the datafile
#define TILE_SIZE 32 // tile size
#define MAP_SIZE 14 // size of the map

DATAFILE *datTiles; // tiles graphics
unsigned char map[MAP_SIZE][MAP_SIZE]; // map data
int currentTile; // current tile used to draw

/***********************************************************************************
* Just zero out all the entries in 2D array which holds map values
* Call this function to create new map
*/
void newMap(void)
{
int i, j;
for (i=0; i<MAP_SIZE; i++)
for (j=0; j<MAP_SIZE; j++)
map[i][j] = 0;
}

/***********************************************************************************
* saves a map to file named: filename
*
*/
void saveMap(char* filename)
{
FILE *file;

// open the file in write binary mode
file = fopen(filename, "wb");
if (!file)
return;

// write the map data
fwrite(map, sizeof(unsigned char), MAP_SIZE*MAP_SIZE, file);

// never forget to close the file
fclose(file);
}

/***********************************************************************************
* load a map from file: filename
*
*/
void loadMap(char* filename)
{
FILE *file;

// open file in read binary mode
file = fopen(filename, "rb");
if (!file)
return;

// read map data
fread(map, sizeof(unsigned char), MAP_SIZE*MAP_SIZE, file);
fclose(file);

}

/***********************************************************************************
* Draw a map to the screen
* using map 2D array
*
*/
void drawMap(void)
{
int i, j; // loop counters
int tile;

// hide the mouse
show_mouse(NULL);

// iterate through map data
for (i=0; i<MAP_SIZE; i++) {
for (j=0; j<MAP_SIZE; j++) {
tile = map[i][j];
// if tile is there draw it
if (tile > 0)
draw_sprite(screen, (BITMAP*)datTiles[tile].dat, i*TILE_SIZE, j*TILE_SIZE);
// else just draw black rectangle
else
rectfill(screen, i*TILE_SIZE, j*TILE_SIZE, i*TILE_SIZE+TILE_SIZE, j*TILE_SIZE+TILE_SIZE, 0);
}
}

// show mouse again
show_mouse(screen);
}

/***********************************************************************************
* if map area was clicked put tile there
* using currentTile
*
*/
void mapClicked(void)
{
int tile; // tile to be drawn
int tileX; // tile in x direction to draw onto
int tileY; // tile in y direction to draw onto

// if no mouse button pressed exit function
if (!mouse_b)
return;

// if mouse was clicked outside the map area exit function
if (mouse_x >= TILE_SIZE*MAP_SIZE || mouse_y >= TILE_SIZE*MAP_SIZE)
return;

// if left button pressed draw the tile
if (mouse_b & 1)
tile = currentTile;

// else erase tiles
else
tile = 0;


tileX = mouse_x / TILE_SIZE; // x tile clicked
tileY = mouse_y / TILE_SIZE; // y tile clicked

map[tileX][tileY] = tile; // assign the value of the map to tile

// and redraw the map
drawMap();

}

/***********************************************************************************
* initialize editor. Load datafile with tiles and create new map
*
*/
void initEditor(void)
{
currentTile = 1; // we start with tile #1 being drawn

// now load the datafile
datTiles = load_datafile("eddat.dat");

// if can't load datafile, give error message
if (!datTiles) {
printf("Error loading eddat.dat\n");
exit(-1);
}

newMap(); // create a new map
}

/***********************************************************************************
* Start message loop which checks
* for keypresses that control editor and mouse clicked for drawing
*
*/
void startEditor(void)
{
// draw initial map
drawMap();

while (1) {
mapClicked(); // check if map was clicked

// keys 1 to 0 change the currently used tile
if (key[KEY_1]) currentTile = 1;
else if (key[KEY_2]) currentTile = 2;
else if (key[KEY_3]) currentTile = 3;
else if (key[KEY_4]) currentTile = 4;
else if (key[KEY_5]) currentTile = 5;
else if (key[KEY_6]) currentTile = 6;
else if (key[KEY_7]) currentTile = 7;
else if (key[KEY_8]) currentTile = 8;
else if (key[KEY_9]) currentTile = 9;
else if (key[KEY_0]) currentTile = 10;

// F1 pressed new map
else if (key[KEY_F1]) {
newMap();
drawMap();
}

// F2 pressed save map
else if (key[KEY_F2]) {
saveMap("map1.map");
drawMap();
}
// F3 pressed load map
else if (key[KEY_F3]) {
loadMap("map1.map");
drawMap();
}
// ESC pressed exit program
else if (key[KEY_ESC])
break;
}
}

/***********************************************************************************/
int main(int argc, char** argv)
{
allegro_init();

// initialize editor
initEditor();

// we are going to use 640x480 graphics mode
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);

// install timer, mouse and keyboard
install_timer();
install_mouse();
install_keyboard();

// clear the screen to background color
clear(screen);

// make sure 0th entry in datafile is pallete
set_pallete((RGB*)datTiles[0].dat);

// display the mouse
show_mouse(screen);

// start processing events
startEditor();

exit(0); // succesfully exit
}

Share this post


Link to post
Share on other sites
drreagan    122
Quote:
Original post by TheFlea
Hi ... I understand @Bob Janova you are perfectly right. I want an example for a map-editor (or a tutorial) 2D ,not 3D . I need for a strategy game.
I repeat : A SIMPLE MAP-EDITOR. I find a long time a tutorial, but he included allegro.h ( and I dont know hwo installing this library) I use Visual C++ 6
I know only create problem for highschool(-school:PASCAL :((boring; Home:C/C++-)

P.S. sorry for my bad english (with this problem, I learn English(very well) and i please ,you when i wrong in a senteces with a gramatical problem, tell me!!! )
thank you in advance.


Hi TheFlea, glad people seem to be willing to help you with your coding problems! Though it doesn't look like anyone has so far responded to your request to point out when you use ungrammatical english.

I don't know if this will help, but i'll write your request out in the way that i would write it as a native english speaker, with some comments included:


"Hi...Yes, Bob Janova, you are definitely right". /* i would use "definitely" instead of perfectly, though "perfectly" is grammatically correct */
I'd like an example (or tutorial) of a 2D, not 3D, map-editor. /* i would not use ", not 3D," unless i really wanted to emphasis that point */
I repeat: A SIMPLE MAP-EDITOR. /*probably unnecessary emphasis, although that's a subjective point. */
I have found an old tutorial, but it uses allegro.h (and i'm not sure how to
install this library). I'm using Visual C++ 6. I only know how to program
through working on problems from highschool (at school, i use pascal...it's
boring. at home, c++).

P.S. sorry for my bad english (since i've been working on this problem, i'm
working on learning English (hopefully very well), so when i use ungrammatical
sentences, please tell me!). thank you in advance.


just wanted to say it's cool that people are willing to help someone who is (to me at least) half-way around the world (nearly). good luck, man.

Share this post


Link to post
Share on other sites
vallentin    181
Hey, post here the error when you start the console application
P.S.Read more of this forum and you will be more intelligible(and you could also get a subscription to a TV cable company and watch Discovery, or if you can not afford to do such thing or no TV cable comp.nearby your location, try to listen and reproduce mentally what actors say in English language based films)
What age are you?

(I am from Romania too..)

Hey, console app with an empty project or a simple app at most(no MFC or alike),I am not aware of last MSVC options, as my knowledge is based on an ancient MSVC 6.0, good enough for what I do at home

Share this post


Link to post
Share on other sites
TheFlea    107
In the first time I congratulation drreagan bacause explain this things (i was surprised when I saw) and thz all which help me.
I have 16 years old
From the first time I want a code for a map-editor which work for VC++ (include allegro, don’t metter) but don’t work with console project or win32aplication …


The error is …This error is for both aplication (win32 aplication , console aplication)
--------------------Configuration: ga - Win32 Debug--------------------
Compiling...
ga.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/ga.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

ga.exe - 2 error(s), 0 warning(s)

I set up in lib ! alld ! and I use allegro version 4.2.0
Again…. Any ideas?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Are there such tutorials or articles about 3d map editors, too?

Share this post


Link to post
Share on other sites
Quote:
Original post by Gameteen
Skimming over this post, I'm surprised someone hasn't mentioned mappy you can get the source code, and it has many helpful features! Hope this was helpful!

As I was reading this thread, that occured to me too. Mappy rocks![grin]


@TheFlea: I, using C++, don't use allegro or 'C' too much. From what I can remember, however, you must end you main() function with the following command: END_OF_MAIN();

Example allegro program:

int main(int argc, char** argv)
{
allegro_init();

// initialize editor
initEditor();

// we are going to use 640x480 graphics mode
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);

// install timer, mouse and keyboard
install_timer();
install_mouse();
install_keyboard();

// clear the screen to background color
clear(screen);

// make sure 0th entry in datafile is pallete
set_pallete((RGB*)datTiles[0].dat);

// display the mouse
show_mouse(screen);

// start processing events
startEditor();

exit(0); // succesfully exit
}
END_OF_MAIN();



And maybe add allegro_exit(); instead of exit(0)?
As I've said, I haven't used allegro much, or recently. But hope this helps.

Much luck on you programming quests!

Share this post


Link to post
Share on other sites
vallentin    181
your "main"-containing file has not been compiled and/or included in project.
it may be some allegro specific problem (with which I can't help or simply you didn't provide a file containing function main)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this