Advanced Strategic Command
mapimageexport.cpp
Go to the documentation of this file.
1 
2 /***************************************************************************
3  mapimageexport.cpp
4  -------------------
5  copyright : (C) 2005 by Martin Bickel
6  email : bickel@asc-hq.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "loadpcx.h"
19 #include "mapdisplay.h"
20 #include "mapimageexport.h"
21 #include "dialogs/fileselector.h"
22 #include "graphics/surface2png.h"
23 #include "spfst.h"
24 #include "iconrepository.h"
25 #include "viewcalculation.h"
26 #include "graphics/blitter.h"
27 
29 {
30  if ( actmap->xsize > (16300 - 200) / fielddistx )
31  xsize = (16300 - 200) / fielddistx;
32  else
33  xsize = actmap->xsize;
34 
35  if ( actmap->ysize > (16300 - 200) / fielddisty )
36  ysize = (16300 - 200) / fielddisty;
37  else
38  ysize = actmap->ysize;
39 
40 
41  int bufsizex = xsize * fielddistx + 200 ;
42  int bufsizey = ysize * fielddisty + 200 ;
43  surface = Surface::createSurface( bufsizex, bufsizey, 32, Surface::transparent << 24 );
44 }
45 
46 
48 {
49  paintTerrain( surface, gamemap, gamemap->getPlayerView(), ViewPort( 0, 0, xsize, ysize ), MapCoordinate( 0, 0 ) );
50  // renderVisibility();
51 }
52 
54 {
55  computeview( gamemap );
57 
59 
60  Surface& mask = IconRepository::getIcon("largehex.pcx");
61  for ( int y = 0; y < ysize; ++y )
62  for ( int x = 0; x < xsize; ++x )
63  if ( fieldvisiblenow( gamemap->getField(x,y), gamemap->getPlayerView() )) {
64  int view = -1;
65  int maxview = 0;
66  for ( int i = 1; i < gamemap->getPlayerCount(); ++i )
67  if ( gamemap->getField(x,y)->view[i].view > maxview ) {
68  maxview = gamemap->getField(x,y)->view[i].view;
69  view = i;
70  }
71 
72  if ( view >= 0 )
73  for ( int yp = 0; yp < fieldsizey; ++yp)
74  for ( int xp = 0; xp < fieldsizex; ++xp)
75  if ( mask.GetPixel(xp,yp) != 0xff )
76  pp.set( getFieldPos(x,y) + SPoint(xp,yp), gamemap->getPlayer( view ).getColor().MapRGBA( surface.getBaseSurface()->format, min(maxview,150)*2/3));
77  }
78 }
79 
80 
81 void WholeMapRenderer::writePCX( const ASCString& filename )
82 {
83  writepcx( filename, surface, SDLmm::SRect( SPoint( surfaceBorder, surfaceBorder), (xsize-1) * fielddistx + fielddisthalfx + fieldsizex, (ysize - 1) * fielddisty + fieldysize ) );
84 }
85 
86 void WholeMapRenderer::writePNG( const ASCString& filename )
87 {
88  ::writePNG( constructFileName(0,"",filename), surface, SDLmm::SRect( SPoint( surfaceBorder, surfaceBorder), (xsize-1) * fielddistx + fielddisthalfx + fieldsizex, (ysize - 1) * fielddisty + fieldysize ) );
89 }
90 
91 void writemaptopcx ( GameMap* gamemap, bool addview )
92 {
93  ASCString name = selectFile( "*.png", false );
94 
95  StatusMessageWindowHolder smw = MessagingHub::Instance().infoMessageWindow( "writing map to " + name );
96 
97  if ( !name.empty() ) {
98  WholeMapRenderer wmr( gamemap );
99  wmr.render();
100  if ( addview )
101  wmr.renderVisibility();
102 
103  wmr.writePNG( name );
104  }
105 }
106 
107 void writemaptostream ( GameMap* gamemap, int width, int height, tnstream& stream )
108 {
109  WholeMapRenderer wmr( gamemap );
110  wmr.render();
111 
112  Surface dst = Surface::createSurface(width, height, 32, 0);
114  blitter.setSize( wmr.surface.w(), wmr.surface.h(), dst.w(), dst.h() );
115 
116  SDL_Rect clip;
117  clip.x = 0;
118  clip.y = 0;
119  clip.h = height;
120  clip.w = width;
121  blitter.setTargetRect( clip );
122 
123  blitter.blit( wmr.surface, dst, SPoint(0, 0) );
124 
125  stream.writeInt( 1 ); // version counter
126  stream.writeInt( width );
127  stream.writeInt( height );
128 
129  for ( int y = 0; y < dst.h(); ++y ) {
130  for ( int x = 0; x < dst.w(); ++x ) {
131  stream.writeInt( dst.GetPixel(x, y) );
132  }
133  }
134 }
135 
137 {
138  int version = stream.readInt();
139  if ( version != 1 )
140  throw tinvalidversion ( "Embedded map image", 1, version );
141 
142  int width = stream.readInt();
143  int height = stream.readInt();
144  assertOrThrow( width >= 0 && width <= 1000 );
145  assertOrThrow( height >= 0 && height <= 1000 );
146 
147  Surface image = Surface::createSurface( width, height , 32, 0);
148 
149  for ( int y = 0; y < height; ++y )
150  for ( int x = 0; x < width; ++x ) {
151  image.SetPixel(x,y, stream.readInt());
152  }
153 
154  return image;
155 }
156 
157 
void writemaptopcx(GameMap *gamemap, bool addview)
WholeMapRenderer(GameMap *actmap)
Interface for loading and writing of PCX images.
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
int xsize
the size of the map
Definition: gamemap.h:201
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
int computeview(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
completely computes the view
map accessing and usage routines used by ASC and the mapeditor
void paintTerrain(Surface &surf, GameMap *actmap, int playerView, const ViewPort &viewPort, const MapCoordinate &offset)
Definition: mapdisplay.cpp:558
void blit(const Surface &src, Surface &dst, SPoint dstPos)
Definition: blitter.h:349
void writePCX(const ASCString &filename)
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
#define assertOrThrow(expr)
Definition: errors.h:69
static const Uint32 transparent
Definition: surface.h:65
void writemaptostream(GameMap *gamemap, int width, int height, tnstream &stream)
#define fieldysize
Definition: typen.h:432
struct MapField::View view[8]
static const int surfaceBorder
Definition: mapdisplay.h:68
static Surface createSurface(int width, int height, SDLmm::Color color=255)
Definition: surface.cpp:387
SPoint getFieldPos(int x, int y)
Definition: mapdisplay.h:89
The interface for all kinds of IO stream.
SDL_Surface * getBaseSurface()
Definition: surface.h:116
Surface loadmapfromstream(tnstream &stream)
Uint32 MapRGBA(SDL_PixelFormat *format, Uint8 a) const
Definition: surface.h:47
A class that hols a MessageWindow. This class ensures that the window is removed if the last copy of ...
Definition: messaginghub.h:34
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
int getPlayerView() const
the player which is currently viewing the map.
Definition: gamemap.cpp:1007
#define fieldsizey
Definition: typen.h:441
void writepcx(const ASCString &name, int x1, int y1, int x2, int y2, dacpalette256 pal)
Definition: loadpcxc.cpp:277
bool fieldvisiblenow(const MapField *pe, Vehicle *veh, int player)
{@
Definition: spfst.cpp:399
#define fielddisthalfx
Definition: typen.h:435
Coordinate on the twodimensional map.
Definition: typen.h:202
char * constructFileName(char *buf, int directoryLevel, const char *path, const char *filename)
Definition: basestrm.cpp:873
DI_Color getColor() const
Definition: player.cpp:249
#define fieldsizex
Definition: typen.h:440
SDLmm::SPoint SPoint
Definition: surface.h:27
int ysize
Definition: gamemap.h:201
ASCString selectFile(const ASCString &ext, bool load, bool overwriteMessage)
#define fielddistx
Definition: typen.h:433
static Surface & getIcon(const ASCString &name)
void writePNG(const ASCString &filename)
GameMap * actmap
Definition: spfst.cpp:64
const T & min(const T &a, const T &b, const T &c)
Definition: misc.h:80
int getPlayerCount() const
Definition: gamemap.h:255
void set(SPoint pos, PixelType src)
Definition: drawing.h:144
#define fielddisty
Definition: typen.h:434
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465