InfoMap.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <landscape/InfoMap.h>
00022 #include <landscape/Landscape.h>
00023 #include <landscapemap/LandscapeMaps.h>
00024 #include <console/ConsoleRuleMethodIAdapter.h>
00025 #include <image/ImageFactory.h>
00026 #include <client/ScorchedClient.h>
00027 #include <client/ClientParams.h>
00028 #include <common/OptionsTransient.h>
00029 
00030 InfoMap *InfoMap::instance_ = 0;
00031 
00032 InfoMap *InfoMap::instance()
00033 {
00034         if (!instance_)
00035         {
00036                 instance_ = new InfoMap;
00037         }
00038         return instance_;
00039 }
00040 
00041 InfoMap::InfoMap()
00042 {
00043 }
00044 
00045 InfoMap::~InfoMap()
00046 {
00047 }
00048 
00049 void InfoMap::addAdapters()
00050 {
00051         static ConsoleRuleMethodIAdapter<Landscape> *off = 0;
00052         static ConsoleRuleMethodIAdapter<InfoMap> *bands = 0;
00053         static ConsoleRuleMethodIAdapter<InfoMap> *grid = 0;
00054         
00055         delete off; off = 0;
00056         delete bands; bands = 0;
00057         delete grid; grid = 0;
00058 
00059         if(ScorchedClient::instance()->getOptionsGame().getDebugFeatures() ||
00060                 !ClientParams::instance()->getConnectedToServer())
00061         {
00062                 off = new ConsoleRuleMethodIAdapter<Landscape>(
00063                         Landscape::instance(), &Landscape::restoreLandscapeTexture, "LandscapeInfoOff");
00064                 bands = new ConsoleRuleMethodIAdapter<InfoMap>(
00065                         this, &InfoMap::showHeightBands, "LandscapeInfoHeightBands");
00066                 grid = new ConsoleRuleMethodIAdapter<InfoMap>(
00067                         this, &InfoMap::showGrid, "LandscapeInfoGrid");
00068         }
00069 }
00070 
00071 void InfoMap::showHeightBands()
00072 {
00073         ImageHandle newMap = ImageFactory::createBlank(
00074                 Landscape::instance()->getMainMap().getWidth(),
00075                 Landscape::instance()->getMainMap().getHeight());
00076         float *heights = new float[
00077                 Landscape::instance()->getMainMap().getWidth() *
00078                 Landscape::instance()->getMainMap().getHeight()];
00079 
00080         const float width = (float)
00081                 ScorchedClient::instance()->getLandscapeMaps().getGroundMaps().getLandscapeWidth();
00082         const float height = (float)
00083                 ScorchedClient::instance()->getLandscapeMaps().getGroundMaps().getLandscapeHeight();
00084         const float sqSizeWidth = width / float(newMap.getWidth());
00085         const float sqSizeHeight = height / float(newMap.getHeight());
00086         const float heightSep = 3.0f;
00087         
00088         int y;
00089         for (y=0; y<newMap.getHeight(); y++)
00090         {
00091                 float posY = float(y) * sqSizeHeight;
00092                 for (int x=0; x<newMap.getWidth(); x++)
00093                 {
00094                         float posX = float(x) * sqSizeWidth;
00095                         heights[x + y * newMap.getWidth()] = 
00096                                 ScorchedClient::instance()->getLandscapeMaps().
00097                                         getGroundMaps().getInterpHeight(
00098                                                 fixed::fromFloat(posX), fixed::fromFloat(posY)).asFloat();
00099                 }
00100         }
00101 
00102         GLubyte *dest = newMap.getBits();
00103         GLubyte *src = Landscape::instance()->getMainMap().getBits();
00104         for (y=0; y<newMap.getHeight(); y++)
00105         {
00106                 for (int x=0; x<newMap.getWidth(); x++)
00107                 {
00108                         GLubyte r = src[0];
00109                         GLubyte g = src[1];
00110                         GLubyte b = src[2];
00111 
00112                         if (x!=0 && y!=0 && x<newMap.getWidth()-1 && y<newMap.getHeight()-1)
00113                         {
00114                         float height = heights[x + y * newMap.getWidth()];
00115                         float height2 = heights[x + 1 + y * newMap.getWidth()];
00116                         float height3 = heights[x - 1 + y * newMap.getWidth()];
00117                         float height4 = heights[x + (y +1) * newMap.getWidth()];
00118                         float height5 = heights[x + (y -1) * newMap.getWidth()];
00119                         float baseHeight = float(int(height / heightSep)) * heightSep;
00120                         float baseHeight2 = float(int(height2 / heightSep)) * heightSep;
00121                         float baseHeight3 = float(int(height3 / heightSep)) * heightSep;
00122                         float baseHeight4 = float(int(height4 / heightSep)) * heightSep;
00123                         float baseHeight5 = float(int(height5 / heightSep)) * heightSep;
00124 
00125                         if (baseHeight < baseHeight2 || baseHeight < baseHeight3 ||
00126                                 baseHeight < baseHeight4 || baseHeight < baseHeight5)
00127                         {
00128                                 r = g = b = 0;
00129                         }
00130                         else if (baseHeight > baseHeight2 || baseHeight > baseHeight3 ||
00131                                 baseHeight > baseHeight4 || baseHeight > baseHeight5)
00132                         {
00133                                  r = g = b = 255;
00134                         }
00135                         }
00136 
00137                         dest[0] = r;
00138                         dest[1] = g;
00139                         dest[2] = b;
00140 
00141                         dest+=3;
00142                         src+=3;
00143                 }
00144         }
00145 
00146         Landscape::instance()->getMainTexture().replace(newMap, false);
00147         Landscape::instance()->setTextureType(Landscape::eOther);
00148 
00149         delete [] heights;
00150 }
00151 
00152 void InfoMap::showGrid()
00153 {
00154         ImageHandle newMap = ImageFactory::createBlank(
00155                 Landscape::instance()->getMainMap().getWidth(),
00156                 Landscape::instance()->getMainMap().getHeight());
00157 
00158         GLubyte *dest = newMap.getBits();
00159         GLubyte *src = Landscape::instance()->getMainMap().getBits();
00160         for (int y=0; y<newMap.getHeight(); y++)
00161         {
00162                 for (int x=0; x<newMap.getWidth(); x++)
00163                 {
00164                         GLubyte r = src[0];
00165                         GLubyte g = src[1];
00166                         GLubyte b = src[2];
00167 
00168                         if (x % 15 == 0 || y % 15 == 0)
00169                         {
00170                                 r = g = b = 255;
00171                         }
00172                         else if ((1+x) % 15 == 0 || (1+y) % 15 == 0)
00173                         {
00174                                 r = g = b = 0;
00175                         }
00176         
00177                         dest[0] = r;
00178                         dest[1] = g;
00179                         dest[2] = b;
00180 
00181                         dest+=3;
00182                         src+=3;
00183                 }
00184         }
00185 
00186         Landscape::instance()->getMainTexture().replace(newMap, false);
00187         Landscape::instance()->setTextureType(Landscape::eOther);
00188 }

Generated on Mon Feb 16 15:14:47 2009 for Scorched3D by  doxygen 1.5.3