00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "loadpcx.h"
00019 #include "mapdisplay.h"
00020 #include "mapimageexport.h"
00021 #include "dialogs/fileselector.h"
00022 #include "graphics/surface2png.h"
00023 #include "spfst.h"
00024 #include "iconrepository.h"
00025 #include "viewcalculation.h"
00026 #include "graphics/blitter.h"
00027
00028 WholeMapRenderer :: WholeMapRenderer ( GameMap* actmap ) : gamemap ( actmap )
00029 {
00030 int bufsizex = actmap->xsize * fielddistx + 200 ;
00031 int bufsizey = actmap->ysize * fielddisty + 200 ;
00032 surface = Surface::createSurface( bufsizex, bufsizey, 32, Surface::transparent << 24 );
00033 }
00034
00035
00036 void WholeMapRenderer::render()
00037 {
00038 paintTerrain( surface, gamemap, gamemap->getPlayerView(), ViewPort( 0, 0, gamemap->xsize, gamemap->ysize ), MapCoordinate( 0, 0 ) );
00039
00040 }
00041
00042 void WholeMapRenderer::renderVisibility()
00043 {
00044 computeview( gamemap );
00045 ColorMerger_AlphaMerge<4> cmam;
00046
00047 PutPixel<4, ColorMerger_AlphaMerge > pp(surface);
00048
00049 Surface& mask = IconRepository::getIcon("largehex.pcx");
00050 for ( int y = 0; y < gamemap->ysize; ++y )
00051 for ( int x = 0; x < gamemap->xsize; ++x )
00052 if ( fieldvisiblenow( gamemap->getField(x,y), gamemap->getPlayerView() )) {
00053 int view = -1;
00054 int maxview = 0;
00055 for ( int i = 1; i < gamemap->getPlayerCount(); ++i )
00056 if ( gamemap->getField(x,y)->view[i].view > maxview ) {
00057 maxview = gamemap->getField(x,y)->view[i].view;
00058 view = i;
00059 }
00060
00061 if ( view >= 0 )
00062 for ( int yp = 0; yp < fieldsizey; ++yp)
00063 for ( int xp = 0; xp < fieldsizex; ++xp)
00064 if ( mask.GetPixel(xp,yp) != 0xff )
00065 pp.set( getFieldPos(x,y) + SPoint(xp,yp), gamemap->getPlayer( view ).getColor().MapRGBA( surface.getBaseSurface()->format, min(maxview,150)*2/3));
00066 }
00067 }
00068
00069
00070 void WholeMapRenderer::writePCX( const ASCString& filename )
00071 {
00072 writepcx( filename, surface, SDLmm::SRect( SPoint( surfaceBorder, surfaceBorder), (gamemap->xsize-1) * fielddistx + fielddisthalfx + fieldsizex, (gamemap->ysize - 1) * fielddisty + fieldysize ) );
00073 }
00074
00075 void WholeMapRenderer::writePNG( const ASCString& filename )
00076 {
00077 ::writePNG( constructFileName(0,"",filename), surface, SDLmm::SRect( SPoint( surfaceBorder, surfaceBorder), (gamemap->xsize-1) * fielddistx + fielddisthalfx + fieldsizex, (gamemap->ysize - 1) * fielddisty + fieldysize ) );
00078 }
00079
00080 void writemaptopcx ( GameMap* gamemap, bool addview )
00081 {
00082 ASCString name = selectFile( "*.png", false );
00083
00084 StatusMessageWindowHolder smw = MessagingHub::Instance().infoMessageWindow( "writing map to " + name );
00085
00086 if ( !name.empty() ) {
00087 WholeMapRenderer wmr( gamemap );
00088 wmr.render();
00089 if ( addview )
00090 wmr.renderVisibility();
00091
00092 wmr.writePNG( name );
00093 }
00094 }
00095
00096 void writemaptostream ( GameMap* gamemap, int width, int height, tnstream& stream )
00097 {
00098 WholeMapRenderer wmr( gamemap );
00099 wmr.render();
00100
00101 Surface dst = Surface::createSurface(width, height, 32, 0);
00102 MegaBlitter< gamemapPixelSize, gamemapPixelSize,ColorTransform_None,ColorMerger_AlphaOverwrite,SourcePixelSelector_DirectZoom,TargetPixelSelector_Rect> blitter;
00103 blitter.setSize( wmr.surface.w(), wmr.surface.h(), dst.w(), dst.h() );
00104
00105 SDL_Rect clip;
00106 clip.x = 0;
00107 clip.y = 0;
00108 clip.h = height;
00109 clip.w = width;
00110 blitter.setTargetRect( clip );
00111
00112 blitter.blit( wmr.surface, dst, SPoint(0, 0) );
00113
00114 stream.writeInt( 1 );
00115 stream.writeInt( width );
00116 stream.writeInt( height );
00117
00118 for ( int y = 0; y < dst.h(); ++y ) {
00119 for ( int x = 0; x < dst.w(); ++x ) {
00120 stream.writeInt( dst.GetPixel(x, y) );
00121 }
00122 }
00123 }
00124
00125 Surface loadmapfromstream ( tnstream& stream )
00126 {
00127 int version = stream.readInt();
00128 if ( version != 1 )
00129 throw tinvalidversion ( "Embedded map image", 1, version );
00130
00131 int width = stream.readInt();
00132 int height = stream.readInt();
00133 assertOrThrow( width >= 0 && width <= 1000 );
00134 assertOrThrow( height >= 0 && height <= 1000 );
00135
00136 Surface image = Surface::createSurface( width, height , 32, 0);
00137
00138 for ( int y = 0; y < height; ++y )
00139 for ( int x = 0; x < width; ++x ) {
00140 image.SetPixel(x,y, stream.readInt());
00141 }
00142
00143 return image;
00144 }
00145
00146