File: mapimageexport.cpp

package info (click to toggle)
asc 2.6.1.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 81,740 kB
  • sloc: cpp: 158,704; sh: 11,544; ansic: 6,736; makefile: 604; perl: 138
file content (146 lines) | stat: -rw-r--r-- 5,199 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

/***************************************************************************
                             mapimageexport.cpp
                             -------------------
    copyright            : (C) 2005 by Martin Bickel
    email                : bickel@asc-hq.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "loadpcx.h"
#include "mapdisplay.h"
#include "mapimageexport.h"
#include "dialogs/fileselector.h"
#include "graphics/surface2png.h"
#include "spfst.h"
#include "iconrepository.h"
#include "viewcalculation.h"
#include "graphics/blitter.h"

WholeMapRenderer :: WholeMapRenderer ( GameMap* actmap ) : gamemap ( actmap )
{
   int bufsizex = actmap->xsize * fielddistx + 200 ;
   int bufsizey = actmap->ysize * fielddisty + 200 ;
   surface = Surface::createSurface( bufsizex, bufsizey, 32, Surface::transparent << 24 );
}


void WholeMapRenderer::render()
{
   paintTerrain( surface, gamemap, gamemap->getPlayerView(), ViewPort( 0, 0, gamemap->xsize, gamemap->ysize ), MapCoordinate( 0, 0 ) );
   // renderVisibility();
}

void WholeMapRenderer::renderVisibility()
{
   computeview( gamemap );
   ColorMerger_AlphaMerge<4> cmam;

   PutPixel<4, ColorMerger_AlphaMerge > pp(surface);

   Surface& mask = IconRepository::getIcon("largehex.pcx");
   for ( int y = 0; y < gamemap->ysize; ++y )
      for ( int x = 0; x < gamemap->xsize; ++x )
         if ( fieldvisiblenow( gamemap->getField(x,y), gamemap->getPlayerView() )) {
            int view = -1;
            int maxview = 0;
            for ( int i = 1; i < gamemap->getPlayerCount(); ++i )
               if ( gamemap->getField(x,y)->view[i].view > maxview ) {
                  maxview = gamemap->getField(x,y)->view[i].view;
                  view = i;
               }

            if ( view >= 0 )
               for ( int yp = 0; yp < fieldsizey; ++yp)
                  for ( int xp = 0; xp < fieldsizex; ++xp)
                     if ( mask.GetPixel(xp,yp) != 0xff )
                        pp.set( getFieldPos(x,y) + SPoint(xp,yp), gamemap->getPlayer( view ).getColor().MapRGBA( surface.getBaseSurface()->format, min(maxview,150)*2/3));
         }
}


void WholeMapRenderer::writePCX( const ASCString& filename )
{
   writepcx( filename, surface, SDLmm::SRect( SPoint( surfaceBorder, surfaceBorder), (gamemap->xsize-1) * fielddistx + fielddisthalfx + fieldsizex, (gamemap->ysize - 1) * fielddisty + fieldysize ) );
}

void WholeMapRenderer::writePNG( const ASCString& filename )
{
   ::writePNG( constructFileName(0,"",filename), surface, SDLmm::SRect( SPoint( surfaceBorder, surfaceBorder), (gamemap->xsize-1) * fielddistx + fielddisthalfx + fieldsizex, (gamemap->ysize - 1) * fielddisty + fieldysize ) );
}

void writemaptopcx ( GameMap* gamemap, bool addview )
{
   ASCString name = selectFile( "*.png", false );

   StatusMessageWindowHolder smw = MessagingHub::Instance().infoMessageWindow( "writing map to " + name );

   if ( !name.empty() ) {
      WholeMapRenderer wmr( gamemap );
      wmr.render();
      if ( addview )
         wmr.renderVisibility();

      wmr.writePNG( name );
   }
}

void writemaptostream ( GameMap* gamemap, int width, int height, tnstream& stream  )
{
   WholeMapRenderer wmr( gamemap );
   wmr.render();

   Surface dst = Surface::createSurface(width, height, 32, 0);
   MegaBlitter< gamemapPixelSize, gamemapPixelSize,ColorTransform_None,ColorMerger_AlphaOverwrite,SourcePixelSelector_DirectZoom,TargetPixelSelector_Rect> blitter;
   blitter.setSize( wmr.surface.w(), wmr.surface.h(), dst.w(), dst.h() );

   SDL_Rect clip;
   clip.x = 0;
   clip.y = 0;
   clip.h = height;
   clip.w = width;
   blitter.setTargetRect( clip );

   blitter.blit( wmr.surface, dst, SPoint(0, 0) );

   stream.writeInt( 1 ); // version counter 
   stream.writeInt( width );
   stream.writeInt( height );
   
   for ( int y = 0; y < dst.h(); ++y ) {
      for ( int x = 0; x < dst.w(); ++x ) {
         stream.writeInt( dst.GetPixel(x, y) );
      }
   }
}

Surface loadmapfromstream ( tnstream& stream  )
{
   int version = stream.readInt();
   if ( version != 1 )
      throw tinvalidversion ( "Embedded map image", 1, version );
   
   int width = stream.readInt();
   int height = stream.readInt();
   assertOrThrow( width >= 0 && width <= 1000 );
   assertOrThrow( height >= 0 && height <= 1000 );
   
   Surface image = Surface::createSurface( width, height , 32, 0);
   
   for ( int y = 0; y < height; ++y )
      for ( int x = 0; x < width; ++x ) {
         image.SetPixel(x,y, stream.readInt());
      }
      
   return image; 
}