00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "map.h"
00021
00022 #include <cmath>
00023 #include <fstream>
00024 using namespace std;
00025
00026 namespace MapGen
00027 {
00028
00029
00030 Map::Map(
00031 const uint w,
00032 const uint h ):
00033 _w(w),
00034 _h(h)
00035 {
00036 MAP_DEBUG( "ctor" );
00037
00038 _map = new float*[_w];
00039 for( uint x=0 ; x<_w ; x++ )
00040 {
00041 _map[x] = new float[_h];
00042 for( uint y=0 ; y<_h ; y++ )
00043 _map[x][y] = 0.0;
00044 }
00045 }
00046
00047
00048
00049 Map::~Map()
00050 {
00051 MAP_DEBUG( "dtor" );
00052
00053 for( uint x=0 ; x<_w ; x++ )
00054 delete [] _map[x];
00055 delete [] _map;
00056 }
00057
00058
00059
00060 void Map::setAt(
00061 int x,
00062 int y,
00063 float value )
00064 {
00065 _map[x%_w][y%_h] = value;
00066 }
00067
00068
00069
00070 float Map::getAt(
00071 int x,
00072 int y ) const
00073 {
00074 return _map[x%_w][y%_h];
00075 }
00076
00077
00078
00079 bool Map::save( const string &file )
00080 {
00081 ofstream f( file.c_str(), ios::out );
00082
00083 if( ! f )
00084 return false;
00085 else
00086 {
00087 f << "P2" << endl;
00088 f << _w << " " << _h << endl;
00089 f << "256" << endl;
00090 for( uint x=0 ; x<_w ; x++ )
00091 for( uint y=0 ; y<_h ; y++ )
00092 f << int(_map[x][y]) << endl;
00093 f.close();
00094 return true;
00095 }
00096 }
00097
00098
00099
00100 Map *Map::crop(
00101 const uint w,
00102 const uint h ) const
00103 {
00104 Map* map = new Map( w, h );
00105
00106 for( uint x=0 ; x<w ; x++ )
00107 for( uint y=0 ; y<h ; y++ )
00108 map->setAt( x, y, getAt( x, y ) );
00109
00110 return map;
00111 }
00112
00113
00114
00115 int *Map::toIntArray() const
00116 {
00117 int* map = new int[_w*_h];
00118 for( uint x=0 ; x<_w ; x++ )
00119 for( uint y=0 ; y<_h ; y++ )
00120 map[x+y*_w] = (int) round( getAt( x, y ) );
00121 return map;
00122 }
00123
00124 }