Uh today I created a map editor, well not a great map editor, but it works nonetheless. Since I dont know how to insert graphics into C++, the map editor below allows you to input any symbol you want into it. To use it though you need to include these header files and source files I think...apvector.cpp, apvector.h, apmatrix.cpp, and apmatrix.h.
I zipped the files together in a zip file.
Code:
#include <stdio.h>
#include <conio.h>
#include "apvector.h"
#include "apmatrix.h"
#include "apstring.h"
#include <time.h>
#include <fstream.h>
#include <windows.h>
enum
{
KEY_ESC = 27,
ARROW_UP = 256 + 72,
ARROW_DOWN = 256 + 80,
ARROW_LEFT = 256 + 75,
ARROW_RIGHT = 256 + 77
};
static int get_code ()
{
int ch = getch();
if ( ch == 0 || ch == 224 )
ch = 256 + getch();
return ch;
}
struct pospass
{
char terrain;
bool passable;
};
bool Valid(int a, int b);
void Display(apmatrix<pospass> A);
void sleep_ticks ( long ticks );
void LoadFile (apmatrix<pospass> & B);
void clrscr();
int main ()
{
cout<<"------------------------------------------------"<<endl;
cout<<"Welcome to Terrain Editor Verson 1.0"<<endl;
cout<<"This program was created by Jaymin Patel"<<endl;
cout<<"Any type of terrain can be added to the editor."<<endl;
cout<<"The keys used in this program are..."<<endl;
cout<<"The Arrow Keys: To move the cursor # around."<<endl;
cout<<"/: To place impassable terrain."<<endl;
cout<<"*: To place passable terrain."<<endl;
cout<<"C: To insert an empty space."<<endl;
cout<<"L: To load a file."<<endl;
cout<<"Esc: To stop and save progress."<<endl;
cout<<"------------------------------------------------"<<endl;
getch();
apmatrix<pospass> B(23, 79);
int i, k;
for (i=0; i<B.numrows(); i++)
{
for (k=0; k<B.numcols(); k++)
{
B[i][k].terrain = ' ';
}
cout<<endl;
}
for (i=0; i<B.numcols(); i++)
{
B[0][i].terrain = 'X';
B[0][i].passable = false;
}
for (i=0; i<B.numrows(); i++)
{
B[i][0].terrain = 'X';
B[i][0].passable = false;
}
for (i=0; i<B.numrows(); i++)
{
B[i][78].terrain = 'X';
B[i][78].passable = false;
}
for (i=0; i<B.numcols(); i++)
{
B[22][i].terrain = 'X';
B[22][i].passable = false;
}
for (i=1; i<B.numrows()-1; i++)
{
for (k=1; k<B.numcols()-1; k++)
B[i][k].terrain = '.';
}
i = 0;
k = 0;
B[i][k].terrain = '#';
int ch;
char temp;
temp = 'X';
char tterrain;
tterrain = ' ';
Display(B);
while ( ( ch = get_code() ) != KEY_ESC )
{
switch ( ch )
{
case ARROW_UP:
{
clrscr();
if (Valid((i-1), k) == true)
{
B[i][k].terrain = temp;
i=i-1;
temp = B[i][k].terrain;
B[i][k].terrain = '#';
}
Display(B);
sleep_ticks (100);
break;
}
case ARROW_DOWN:
{
clrscr();
if (Valid((i+1), k) == true)
{
B[i][k].terrain = temp;
i=i+1;
temp = B[i][k].terrain;
B[i][k].terrain = '#';
}
Display(B);
sleep_ticks (100);
break;
}
case ARROW_LEFT:
{
clrscr();
if (Valid(i, (k-1)) == true)
{
B[i][k].terrain = temp;
k=k-1;
temp = B[i][k].terrain;
B[i][k].terrain = '#';
}
Display(B);
sleep_ticks (100);
break;
}
case ARROW_RIGHT:
{
clrscr();
if (Valid(i, (k+1)) == true)
{
B[i][k].terrain = temp;
k=k+1;
temp = B[i][k].terrain;
B[i][k].terrain = '#';
}
Display(B);
sleep_ticks (100);
break;
}
case '*':
{
cout<<"Enter your terrain: ";
cin>>tterrain;
temp = tterrain;
B[i][k].passable = true;
break;
}
case '/':
{
cout<<"Enter your terrain: ";
cin>>tterrain;
temp = tterrain;
B[i][k].passable = false;
break;
}
case 'C':
case 'c':
{
tterrain = 'ƒ';
temp = tterrain;
B[i][k].passable = true;
break;
}
case 'L':
case 'l':
{
cout<<"You have chosen to load a file..."<<endl;
LoadFile(B);
cout<<"Your file has been loaded."<<endl;
getch();
clrscr();
Display(B);
break;
}
}
}
B[i][k].terrain = temp;
cout<<"------------------------------------------------"<<endl;
cout<<"You have quit the editor, you may now save your file."<<endl;
cout<<"Your file will be saved in two data files with similar names."<<endl;
cout<<"One file will record terrain and and one file will record"<<endl;
cout<<"passable related data in matrix format."<<endl;
cout<<"It is suggested that you do not edit the data files"<<endl;
cout<<"Unless you understand the program."<<endl;
cout<<"------------------------------------------------"<<endl;
getch();
cout<<"Once your file is saved copy the data files that"<<endl;
cout<<"have been created and paste them in another folder."<<endl;
cout<<"If you dont you will write over old maps."<<endl;
cout<<"------------------------------------------------"<<endl;
getch();
cout<<"Your file has been saved...later."<<endl;
cout<<"------------------------------------------------"<<endl;
ofstream output;
ofstream outputa;
int l, j;
output.open("map_terrain.txt");
for (j=0; j<B.numrows(); j++)
{
for (l=0; l<B.numcols(); l++)
output<<B[j][l].terrain;
output<<endl;
}
outputa.open("map_passable.txt");
for (j=0; j<B.numrows(); j++)
{
for (l=0; l<B.numcols(); l++)
{
if (B[j][l].passable == true)
outputa<<"1"<<" ";
else
outputa<<"0"<<" ";
}
outputa<<endl;
}
return 0;
}
void sleep_ticks ( long ticks )
{
clock_t limit, now = clock();
limit = now + ticks;
while ( limit > now )
now = clock();
}
void Display(apmatrix<pospass> A)
{
for (int i=0; i<A.numrows(); i++)
{
for (int k=0; k<A.numcols(); k++)
{
cout<<A[i][k].terrain;
}
cout<<endl;
}
return;
}
bool Valid(int a, int b)
{
if ((a > -1) && (a < 22) && (b > -1) && (b < 79))
return true;
else
return false;
}
void LoadFile (apmatrix<pospass> & B)
{
ifstream input;
ifstream inputa;
int l, j;
apmatrix<int> M(23, 79);
input.open("map_terrain.txt");
for (j=0; j<B.numrows(); j++)
{
for (l=0; l<B.numcols(); l++)
input>>B[j][l].terrain;
}
for (j=0; j<M.numrows(); j++)
{
for (l=0; l<M.numcols(); l++)
input>>M[j][l];
}
inputa.open("map_passable.txt");
for (j=0; j<B.numrows(); j++)
{
for (l=0; l<B.numcols(); l++)
{
if (M[j][l] == 1)
B[j][l].passable = true;
else
B[j][l].passable = false;
}
}
return;
}
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '),
dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}