Advanced Strategic Command
graphicset.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  graphicset.cpp - description
3  -------------------
4  begin : Sun Jan 28 2001
5  copyright : (C) 2001 by Martin Bickel
6  email : bickel@asc-hq.org
7  ***************************************************************************/
8 
14 /***************************************************************************
15  * *
16  * This program is free software; you can redistribute it and/or modify *
17  * it under the terms of the GNU General Public License as published by *
18  * the Free Software Foundation; either version 2 of the License, or *
19  * (at your option) any later version. *
20  * *
21  ***************************************************************************/
22 
23 #include <map>
24 
25 #include "basestrm.h"
26 #include "misc.h"
27 #include "graphicset.h"
28 // #include "basegfx.h"
29 #include "errors.h"
30 #include "typen.h"
31 #include "sgstream.h"
32 
33 #include "itemrepository.h"
34 #include "iconrepository.h"
35 
36 
37 GraphicSetManager_Base::GraphicSetManager_Base() : activeSet(NULL)
38 {
39 
40 }
41 
42 
43 bool GraphicSetManager_Base :: picAvail ( int num ) const
44 {
45  if ( activeSet )
46  return activeSet->picAvail(num);
47  else
48  return false;
49 }
50 
51 
52 int GraphicSetManager_Base :: getMode ( int num ) const
53 {
54  if ( activeSet && activeSet->picmode.size() > num )
55  return activeSet->picmode[num];
56  else
57  return 0;
58 }
59 
60 
61 
63 {
64  if( num >= activeSet->image.size() )
65  return IconRepository::getIcon("emptyfld.raw");
66  else
67  return activeSet->image[num];
68 }
69 
71 {
72  activeSet->image[num] = pic;
73 }
74 
75 
76 
78 {
79  if ( activeSet && id == activeSet->id )
80  return id;
81 
82  for ( GraphicSets::iterator i = graphicSets.begin(); i != graphicSets.end(); ++i )
83  if ( (*i)->id == id ) {
84  activeSet = *i;
85  return id;
86  }
87 
88  if ( graphicSets.size() < 1 )
89  fatalError( "no graphic sets (*.gfx) found! Check that asc.gfx is in your search path" );
90  activeSet = *graphicSets.begin();
91  return 0;
92 }
93 
95 {
96  if ( picAvail ( id )) {
97  map<int,OverviewMapImage>::iterator qv = activeSet->quickViewImages.find ( id );
98  if ( qv == activeSet->quickViewImages.end()) {
99  OverviewMapImage* fqv = new OverviewMapImage ( getPic( id ) );
100  activeSet->quickViewImages[id] = *fqv;
101  delete fqv;
102  return &activeSet->quickViewImages[id];
103  } else
104  return &qv->second;
105 
106  } else {
107  static OverviewMapImage* emptyFieldQuickView = NULL;
108  if ( !emptyFieldQuickView )
109  emptyFieldQuickView = new OverviewMapImage();
110 
111  return emptyFieldQuickView;
112  }
113 }
114 
115 
116 
117 int getGraphicSetIdFromFilename ( const ASCString& filename )
118 {
119  tnfilestream stream ( filename, tnstream::reading );
120 
121  int magic = stream.readInt();
122  if ( magic == -1 ) {
123  return stream.readInt();
124  } else
125  return 0;
126 }
127 
128 
129 
131 {
132  if ( activeSet )
133  return;
134 
135  #ifdef logging
136  logtofile("loadbi3graphics");
137  #endif
138 
139  loadpalette();
140 
141 
142 
143  /*
144 
145  #ifdef genimg
146  void* mask;
147  {
148  int i ;
149  tnfilestream s ( "largehex.raw", tnstream::reading );
150  s.readrlepict ( &mask, false, & i );
151  }
152  #endif
153 */
154  IconRepository::getIcon("emptyfld.raw");
155 
156  ASCString location;
157  tfindfile ff ( "*.gfx" );
158  ASCString filename = ff.getnextname( NULL, NULL, &location);
159  while ( !filename.empty() ) {
160 
161  tnfilestream s ( filename, tnstream::reading );
162 
163  displayLogMessage ( 5, "loading graphic set " + location + filename + "\n" );
164 
165  int magic = s.readInt();
166  if ( magic == -1 ) {
167 
168  GraphicSet* gs = new GraphicSet;
169 
170  gs->id = s.readInt();
171  int picnum = s.readInt();
172  s.readInt(); // maxPicSize
173 
174  int* picmode = new int[picnum];
175  for ( int i = 0; i < picnum; ++i )
176  picmode[i] = s.readInt();
177 
178  gs->image.resize ( picnum );
179  gs->picmode.resize ( picnum );
180  for ( int i = 0; i < picnum; i++ ) {
181  if ( picmode[i] >= 1 ) {
182  Surface& surf = gs->image[i];
183  surf.read ( s );
184  if ( surf.w() != fieldsizex || surf.h() != fieldsizey )
185  surf.strech ( fieldsizex, fieldsizey );
186  gs->picmode[i] = picmode[i];
187  } else {
188  gs->picmode[i] = 256 + 2;
189  gs->image[i] = IconRepository::getIcon("emptyfld.raw");
190  }
191  gs->image[i].assignDefaultPalette();
192 
194 
195  }
196 
197  delete[] picmode;
198 
199  graphicSets.push_back ( gs );
200  }
201 
202  filename = ff.getnextname();
203  }
204 
205  setActive ( 0 );
206 }
207 
208 
210 {
211  for ( GraphicSets::iterator i = graphicSets.begin(); i != graphicSets.end(); ++i)
212  delete *i;
213 }
214 
The (base-) classes which are thrown as exceptions.
miscellaneous functions which are used by ASC and all its tools
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
ASCString getnextname(int *loc=NULL, bool *inContainer=NULL, ASCString *location=NULL)
Returns the next entry of the internal file list.
Definition: basestrm.cpp:2211
sigc::signal< void > dataLoaderTicker
A system that provides a set of images for vehicles, buildings, etc.
void strech(int width, int height)
Definition: surface.cpp:457
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
void read(tnstream &stream)
Definition: surface.cpp:265
#define fieldsizey
Definition: typen.h:441
const int magic
Definition: stack.cpp:36
void displayLogMessage(int msgVerbosity, const char *message,...)
const OverviewMapImage * getQuickView(int id)
Definition: graphicset.cpp:94
Searching for files.
Definition: basestrm.h:484
The interface for the various streams that ASC offers, like file and memory streams.
The IO for many basic classes and structurs of ACS.
#define fieldsizex
Definition: typen.h:440
bool picAvail(int num) const
Definition: graphicset.cpp:43
void loadpalette(void)
Definition: sgstream.cpp:67
int getMode(int num) const
Definition: graphicset.cpp:52
static Surface & getIcon(const ASCString &name)
void setPic(int num, Surface pic)
Definition: graphicset.cpp:70
the image for a TerrainType that is shown on the small map
void fatalError(const ASCString &string)
Surface & getPic(int num)
Definition: graphicset.cpp:62
int getGraphicSetIdFromFilename(const ASCString &filename)
Definition: graphicset.cpp:117