My first map editor and map loader!
Posted: Sat Mar 13, 2010 11:50 pm
It's amazing what you can do when you put your mind to it . 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
Map Loader:
Main.cpp:
Here is the tile sheet that has to be in the directories of the map file and the 2 executables
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/
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();
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();
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/