My first map editor and map loader!

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

My first map editor and map loader!

Post by mv2112 »

It's amazing what you can do when you put your mind to it :shock2: . I just made a simple map editor, it only has 3 tiles right now and it outputs to a text file when you press the s key.
The map loader translates the text file and loads the map with the tile sheet. My code is messy and it has some memory leaks but its because i was experimenting, anyways here it is!

Map Editor:
Main.cpp:
Keys 1,2 and 3 switch tiles, p erases all ,left click to place a tile and s saves to map.txt

Code: Select all


#define USE_CONSOLE
#include <allegro.h>
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
	int x;
	int y;
	int pos;
	int w;
	int h;
	int filex=0;
	int filey=0;
	string title="tile_sheet.bmp";
	allegro_init();
	install_keyboard();
	install_mouse();
	set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);
	set_color_depth(16);
	int frame=0;
	int xs=0;
	int ys=0;
	vector<int> xx;
	vector<int> yy;
	vector<int> fx;
	vector<int> fy;
	BITMAP* buffer=create_bitmap(640,480);
	BITMAP* tile;
	tile=load_bitmap(title.c_str(),NULL);
	clear_to_color(buffer,makecol(0,0,0));
	show_mouse(screen);
	char xout[100];
	char yout[100];
	while(!key[KEY_ESC])
	{
		xs=xx.size();
		ys=yy.size();
		itoa(xs,xout,10);
		itoa(ys,yout,10);

		if(key[KEY_S])
		{
			ofstream file;
			file.open("map.txt");
			file<<title.c_str()<<endl;
			for(int i=0;i<xx.size();i++)
			{
				int filetemp=xx[i];
				file<<filetemp<<" ";
			}
			file<<"#"<<endl;
			for(int i=0;i<yy.size();i++)
			{
				int filetemp=yy[i];
				file<<filetemp<<" ";
			}
			file<<"#"<<endl;
			for(int i=0;i<fx.size();i++)
			{
				int filetemp=fx[i];
				file<<filetemp<<" ";
			}
			file<<"#"<<endl;
			for(int i=0;i<fy.size();i++)
			{
				int filetemp=fy[i];
				file<<filetemp<<" ";
			}
			file<<"#"<<endl;
			file.close();
		}

		if(key[KEY_3])
		{
			filex=100;
			filey=0;
		}
		if(key[KEY_2])
		{
			filex=50;
			filey=0;
		}
		if(key[KEY_1])
		{
			filex=0;
			filey=0;
		}
		if(key[KEY_P])
		{
			xx.clear();
			yy.clear();
			fx.clear();
			fy.clear();
		}
		if(mouse_b & 1)
		{
			xx.push_back(x);
			yy.push_back(y);
			fx.push_back(filex);
			fy.push_back(filey);
		}
		clear(buffer);
		pos=mouse_pos;
		poll_mouse();
		x=(25-(pos >> 16))*-1;
	    y=(25-(pos & 0x0000ffff))*-1;
	    w=x+50;
	    h=y+50;

		cout<<x<<endl;
		cout<<y<<endl;
		if(xx.size()>0)
		{
			for(int i=0;i<xx.size();i++)
			{
				blit(tile,buffer,fx[i],fy[i],xx[i],yy[i],50,50);
			}
		}
		textout_ex(buffer,font,xout,20,3,makecol(255,255,255),makecol(0,0,0));
		textout_ex(buffer,font,yout,50,3,makecol(255,255,255),makecol(0,0,0));

		blit(tile,buffer,filex,filey,x,y,50,50);
		blit(buffer,screen,0,0,0,0,640,480);
	}

	destroy_bitmap(buffer);
	destroy_bitmap(tile);
	return 0;
}
END_OF_MAIN();

Map Loader:
Main.cpp:

Code: Select all


#define USE_CONSOLE
#include <allegro.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

class map
{
public:
	void LoadMap(string file_name,int ww,int hh);
	void RenderMap();
	BITMAP* buffer;
private:
	vector<int> x;
	vector<int> y;
	int w;
	int h;
	vector<int> fx;
	vector<int> fy;
 	BITMAP* sprite_sheet;
};

void map::LoadMap(string file_name, int ww,int hh)
{
	buffer=create_bitmap(640,480);
	clear_to_color(buffer,makecol(0,0,0));
	w=ww;
	h=hh;
	int pos=0;
	string t;
	string temp;
	ifstream file;
	file.open(file_name.c_str());
	if(!file.is_open())
	{
		cout<<"ERROR: File Not Found"<<endl;
		return;
	}
	getline(file,temp);
	if(temp[0]==' ')
	{
		cout<<"ERROR: Sprite Sheet Not Defined"<<endl;
		file.close();
		return;
	}
	sprite_sheet=load_bitmap(temp.c_str(),NULL);
	cout<<temp<<endl;
	getline(file,temp); //load x coordinates
	cout<<temp<<endl;
	while(temp[pos]!='#')
	{
		if(temp[pos]!=' ')
		{
			t+=temp[pos];
			pos++;
			continue;
		}
		else
		{
			pos++;
			x.push_back(atoi(t.c_str()));
			t.clear();
		}
	}
	pos=0;
	getline(file,temp); //load y coordinates
	cout<<temp<<endl;
	while(temp[pos]!='#')
	{
		if(temp[pos]!=' ')
		{
			t+=temp[pos];
			pos++;
			continue;
		}
		else
		{
			pos++;
			y.push_back(atoi(t.c_str()));
			t.clear();
		}
	}
	pos=0;
	getline(file,temp);
	cout<<temp<<endl;
	while(temp[pos]!='#')
	{
		if(temp[pos]!=' ')
		{
			t+=temp[pos];
			pos++;
			continue;
		}
		else
		{
			pos++;
			fx.push_back(atoi(t.c_str()));
			t.clear();
		}
	}
	pos=0;
	getline(file,temp);
	cout<<temp<<endl;
	while(temp[pos]!='#')
	{
		if(temp[pos]!=' ')
		{
			t+=temp[pos];
			pos++;
			continue;
		}
		else
		{
			pos++;
			fy.push_back(atoi(t.c_str()));
			t.clear();
		}
	}
}

void map::RenderMap()
{
	for(int i=0;i<x.size();i++)
	{
		blit(sprite_sheet,buffer,fx[i],fy[i],x[i],y[i],w,h);
	}
	blit(buffer,screen,0,0,0,0,640,480);
}

int main()
{
	allegro_init();
	install_keyboard();
	install_mouse();
	set_color_depth(16);
	set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);
	map test;
	test.LoadMap("map.txt",50,50);
	test.RenderMap();

	while(!key[KEY_ESC])
	{
	}
	return 0;
}
END_OF_MAIN();

Here is the tile sheet that has to be in the directories of the map file and the 2 executables
Image

EDIT:
O ya, if you compile, you will probably need the allegro dll, im not sure which one of the 3 it is lol. Here is a link to all of em http://home.roadrunner.com/~mvonline/
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: My first map editor and map loader!

Post by ibly31 »

Nice! I remember when I made the first level editor for the RPG I was making. It just feels so cool once the level is up and running in the engine without any text file altering from you. Good job!
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: My first map editor and map loader!

Post by GroundUpEngine »

+1 for Good Job! Editor's Rule! :P
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: My first map editor and map loader!

Post by mv2112 »

I added in a sprite system that loads a sprite from a config file (sprite.txt) and includes a collision system with the map and obviously movement, also added in support for special tiles you can walk through in the map editor. :shock2:
Now all i need to do is make a program to create the sprite config files. :mrgreen:
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Re: My first map editor and map loader!

Post by Maevik »

Pretty cool dude, keep up the good work!
My love is like a Haddoken, it's downright fierce!
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: My first map editor and map loader!

Post by MrDeathNote »

Well done man, keep it up.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
Post Reply