Skip to content

Commit edd686c

Browse files
committed
- Fixed makefile. (by zear)
- Fixed globe memory leak. (by Daiky) - Globe cleanup and stuff. git-svn-id: https://openxcom.svn.sourceforge.net/svnroot/openxcom/trunk@186 11c4a5ed-7179-4546-81a6-367b3be9d812
1 parent 9202c46 commit edd686c

5 files changed

Lines changed: 59 additions & 67 deletions

File tree

src/Geoscape/Globe.cpp

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ Globe::~Globe()
214214
delete _mkCrashedUfo;
215215
delete _mkAlienSite;
216216

217-
for (std::vector<Polygon*>::iterator i = _ocean.begin(); i != _ocean.end(); i++)
217+
for (std::list<Polygon*>::iterator i = _ocean.begin(); i != _ocean.end(); i++)
218218
{
219219
delete *i;
220220
}
221-
for (std::vector<Polygon*>::iterator i = _cacheOcean.begin(); i != _cacheOcean.end(); i++)
221+
for (std::list<Polygon*>::iterator i = _cacheOcean.begin(); i != _cacheOcean.end(); i++)
222222
{
223223
delete *i;
224224
}
225-
for (std::vector<Polygon*>::iterator i = _cacheLand.begin(); i != _cacheLand.end(); i++)
225+
for (std::list<Polygon*>::iterator i = _cacheLand.begin(); i != _cacheLand.end(); i++)
226226
{
227227
delete *i;
228228
}
@@ -327,7 +327,7 @@ bool Globe::insidePolygon(double lon, double lat, Polygon *poly) const
327327
* @param polygons Pointer to the polygon set.
328328
* @sa http://www.ufopaedia.org/index.php?title=WORLD.DAT
329329
*/
330-
void Globe::loadDat(const std::string &filename, std::vector<Polygon*> *polygons)
330+
void Globe::loadDat(const std::string &filename, std::list<Polygon*> *polygons)
331331
{
332332
// Load file
333333
std::ifstream mapFile (filename.c_str(), std::ios::in | std::ios::binary);
@@ -513,7 +513,7 @@ void Globe::center(double lon, double lat)
513513
bool Globe::insideLand(double lon, double lat) const
514514
{
515515
bool inside = false;
516-
for (std::vector<Polygon*>::iterator i = _res->getPolygons()->begin(); i < _res->getPolygons()->end() && !inside; i++)
516+
for (std::list<Polygon*>::iterator i = _res->getPolygons()->begin(); i != _res->getPolygons()->end() && !inside; i++)
517517
{
518518
inside = insidePolygon(lon, lat, *i);
519519
}
@@ -610,14 +610,27 @@ std::vector<Target*> Globe::getTargets(int x, int y, bool craft) const
610610
*/
611611
void Globe::cachePolygons()
612612
{
613-
// Cache ocean
614-
for (std::vector<Polygon*>::iterator i = _cacheOcean.begin(); i != _cacheOcean.end(); i++)
613+
cache(&_ocean, &_cacheOcean);
614+
cache(_res->getPolygons(), &_cacheLand);
615+
draw();
616+
}
617+
618+
/**
619+
* Caches a set of polygons.
620+
* @param polygons Pointer to list of polygons.
621+
* @param cache Pointer to cache.
622+
*/
623+
void Globe::cache(std::list<Polygon*> *polygons, std::list<Polygon*> *cache)
624+
{
625+
// Clear existing cache
626+
for (std::list<Polygon*>::iterator i = cache->begin(); i != cache->end(); i++)
615627
{
616628
delete *i;
617629
}
618-
_cacheOcean.clear();
630+
cache->clear();
619631

620-
for (std::vector<Polygon*>::iterator i = _ocean.begin(); i != _ocean.end(); i++)
632+
// Pre-calculate values to cache
633+
for (std::list<Polygon*>::iterator i = polygons->begin(); i != polygons->end(); i++)
621634
{
622635
// Is quad on the back face?
623636
bool backFace = true;
@@ -639,42 +652,8 @@ void Globe::cachePolygons()
639652
p->setY(j, y);
640653
}
641654

642-
_cacheOcean.push_back(p);
643-
}
644-
645-
// Cache land
646-
for (std::vector<Polygon*>::iterator i = _cacheLand.begin(); i != _cacheLand.end(); i++)
647-
{
648-
delete *i;
649-
}
650-
_cacheLand.clear();
651-
652-
for (std::vector<Polygon*>::iterator i = _res->getPolygons()->begin(); i != _res->getPolygons()->end(); i++)
653-
{
654-
// Don't draw if polygon is facing back
655-
bool backFace = true;
656-
for (int j = 0; j < (*i)->getPoints(); j++)
657-
{
658-
backFace = backFace && pointBack((*i)->getLongitude(j), (*i)->getLatitude(j));
659-
}
660-
if (backFace)
661-
continue;
662-
663-
Polygon* p = new Polygon(**i);
664-
665-
// Convert coordinates
666-
for (int j = 0; j < p->getPoints(); j++)
667-
{
668-
Sint16 x, y;
669-
polarToCart(p->getLongitude(j), p->getLatitude(j), &x, &y);
670-
p->setX(j, x);
671-
p->setY(j, y);
672-
}
673-
674-
_cacheLand.push_back(p);
655+
cache->push_back(p);
675656
}
676-
677-
draw();
678657
}
679658

680659
/**
@@ -772,7 +751,7 @@ void Globe::drawOcean()
772751

773752
filledCircleColor(getSurface(), _cenX, _cenY, (Sint16)floor(_radius[_zoom]), Palette::getRGBA(this->getPalette(), Palette::blockOffset(12)+28));
774753

775-
for (std::vector<Polygon*>::iterator i = _cacheOcean.begin(); i != _cacheOcean.end(); i++)
754+
for (std::list<Polygon*>::iterator i = _cacheOcean.begin(); i != _cacheOcean.end(); i++)
776755
{
777756
double tmpLon = (*i)->getLongitude(0);
778757

@@ -806,7 +785,7 @@ void Globe::drawLand()
806785
double minLon = 0.0, maxLon = 0.0, curTime = _save->getTime()->getDaylight();
807786
Sint16 x[4], y[4];
808787

809-
for (std::vector<Polygon*>::iterator i = _cacheLand.begin(); i != _cacheLand.end(); i++)
788+
for (std::list<Polygon*>::iterator i = _cacheLand.begin(); i != _cacheLand.end(); i++)
810789
{
811790
// Convert coordinates
812791
for (int j = 0; j < (*i)->getPoints(); j++)
@@ -847,7 +826,7 @@ void Globe::drawDetail()
847826
// Lock the surface
848827
_countries->lock();
849828

850-
for (std::vector<Polyline*>::iterator i = _res->getPolylines()->begin(); i != _res->getPolylines()->end(); i++)
829+
for (std::list<Polyline*>::iterator i = _res->getPolylines()->begin(); i != _res->getPolylines()->end(); i++)
851830
{
852831
Sint16 x[2], y[2];
853832
for (int j = 0; j < (*i)->getPoints() - 1; j++)
@@ -871,6 +850,11 @@ void Globe::drawDetail()
871850
// Draw the country names
872851
if (_zoom >= 2)
873852
{
853+
Text *label = new Text(_res->getFont("BIGLETS.DAT"), _res->getFont("SMALLSET.DAT"), 80, 9, 0, 0);
854+
label->setPalette(getPalette());
855+
label->setAlign(ALIGN_CENTER);
856+
label->setColor(Palette::blockOffset(15)-1);
857+
874858
Sint16 x, y;
875859
for (std::map<LangString, Country*>::iterator i = _save->getCountries()->begin(); i != _save->getCountries()->end(); i++)
876860
{
@@ -881,18 +865,23 @@ void Globe::drawDetail()
881865
// Convert coordinates
882866
polarToCart(i->second->getLabelLongitude(), i->second->getLabelLatitude(), &x, &y);
883867

884-
Text *label = new Text(_res->getFont("BIGLETS.DAT"), _res->getFont("SMALLSET.DAT"), 80, 9, x - 40, y);
885-
label->setPalette(getPalette());
886-
label->setAlign(ALIGN_CENTER);
868+
label->setX(x - 40);
869+
label->setY(y);
887870
label->setText(_res->getLanguage()->getString(i->first));
888-
label->setColor(Palette::blockOffset(15)-1);
889871
label->blit(_countries);
890872
}
873+
874+
delete label;
891875
}
892876

893877
// Draw the city markers
894878
if (_zoom >= 3)
895879
{
880+
Text *label = new Text(_res->getFont("BIGLETS.DAT"), _res->getFont("SMALLSET.DAT"), 80, 9, 0, 0);
881+
label->setPalette(getPalette());
882+
label->setAlign(ALIGN_CENTER);
883+
label->setColor(Palette::blockOffset(8)+10);
884+
896885
Sint16 x, y;
897886
for (std::map<LangString, Region*>::iterator i = _save->getRegions()->begin(); i != _save->getRegions()->end(); i++)
898887
{
@@ -910,14 +899,14 @@ void Globe::drawDetail()
910899
_mkCity->setPalette(getPalette());
911900
_mkCity->blit(_countries);
912901

913-
Text *label = new Text(_res->getFont("BIGLETS.DAT"), _res->getFont("SMALLSET.DAT"), 80, 9, x - 40, y + 2);
914-
label->setPalette(getPalette());
915-
label->setAlign(ALIGN_CENTER);
902+
label->setX(x - 40);
903+
label->setY(y + 2);
916904
label->setText(_res->getLanguage()->getString((*j)->getName()));
917-
label->setColor(Palette::blockOffset(8)+10);
918905
label->blit(_countries);
919906
}
920907
}
908+
909+
delete label;
921910
}
922911
}
923912

src/Geoscape/Globe.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define OPENXCOM_GLOBE_H
2121

2222
#include <vector>
23+
#include <list>
2324
#include <string>
2425
#include "../Engine/InteractiveSurface.h"
2526

@@ -51,7 +52,7 @@ class Globe : public InteractiveSurface
5152
Surface *_markers, *_countries;
5253
bool _blink, _detail;
5354
Timer *_blinkTimer, *_rotTimer;
54-
std::vector<Polygon*> _ocean, _cacheOcean, _cacheLand;
55+
std::list<Polygon*> _ocean, _cacheOcean, _cacheLand;
5556
Surface *_mkXcomBase, *_mkAlienBase, *_mkCraft, *_mkWaypoint, *_mkCity;
5657
Surface *_mkFlyingUfo, *_mkLandedUfo, *_mkCrashedUfo, *_mkAlienSite;
5758

@@ -61,13 +62,15 @@ class Globe : public InteractiveSurface
6162
bool insidePolygon(double lon, double lat, Polygon *poly) const;
6263
/// Checks if a target is near a point.
6364
bool targetNear(Target* target, int x, int y) const;
65+
/// Caches a set of polygons.
66+
void cache(std::list<Polygon*> *polygons, std::list<Polygon*> *cache);
6467
public:
6568
/// Creates a new globe at the specified position and size.
6669
Globe(int cenX, int cenY, int width, int height, int x = 0, int y = 0);
6770
/// Cleans up the globe.
6871
~Globe();
6972
/// Loads a set of polygons from a DAT file.
70-
static void loadDat(const std::string &filename, std::vector<Polygon*> *polygons);
73+
static void loadDat(const std::string &filename, std::list<Polygon*> *polygons);
7174
/// Converts polar coordinates to cartesian coordinates.
7275
void polarToCart(double lon, double lat, Sint16 *x, Sint16 *y) const;
7376
/// Converts cartesian coordinates to polar coordinates.

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ BIN = openxcom
2020
endif
2121

2222
# Compiler settings
23-
CXXFLAGS = -Wall -O2 `$(SDL-CONFIG) --cflags` $(addprefix -d,$(TARGET))
23+
CXXFLAGS = -Wall -O2 `$(SDL-CONFIG) --cflags` $(addprefix -D,$(TARGET))
2424
LDFLAGS = `$(SDL-CONFIG) --libs` -lSDL_gfx -lSDL_mixer
2525

2626
# Rules

src/Resource/ResourcePack.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ ResourcePack::~ResourcePack()
5858
{
5959
delete i->second;
6060
}
61-
for (std::vector<Polygon*>::iterator i = _polygons.begin(); i != _polygons.end(); i++)
61+
for (std::list<Polygon*>::iterator i = _polygons.begin(); i != _polygons.end(); i++)
6262
{
6363
delete *i;
6464
}
65-
for (std::vector<Polyline*>::iterator i = _polylines.begin(); i != _polylines.end(); i++)
65+
for (std::list<Polyline*>::iterator i = _polylines.begin(); i != _polylines.end(); i++)
6666
{
6767
delete *i;
6868
}
@@ -195,7 +195,7 @@ SurfaceSet *const ResourcePack::getSurfaceSet(const std::string &name)
195195
* Returns the list of polygons in the resource set.
196196
* @return Pointer to the list of polygons.
197197
*/
198-
std::vector<Polygon*> *const ResourcePack::getPolygons()
198+
std::list<Polygon*> *const ResourcePack::getPolygons()
199199
{
200200
return &_polygons;
201201
}
@@ -204,7 +204,7 @@ std::vector<Polygon*> *const ResourcePack::getPolygons()
204204
* Returns the list of polylines in the resource set.
205205
* @return Pointer to the list of polylines.
206206
*/
207-
std::vector<Polyline*> *const ResourcePack::getPolylines()
207+
std::list<Polyline*> *const ResourcePack::getPolylines()
208208
{
209209
return &_polylines;
210210
}

src/Resource/ResourcePack.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include <map>
2323
#include <string>
24-
#include <vector>
24+
#include <list>
2525
#include "SDL.h"
2626

2727
class Language;
@@ -55,8 +55,8 @@ class ResourcePack
5555
std::map<std::string, Surface*> _surfaces;
5656
std::map<std::string, SurfaceSet*> _sets;
5757
std::map<std::string, SoundSet*> _sounds;
58-
std::vector<Polygon*> _polygons;
59-
std::vector<Polyline*> _polylines;
58+
std::list<Polygon*> _polygons;
59+
std::list<Polyline*> _polylines;
6060
std::map<std::string, Music*> _musics;
6161

6262
/// Converts a filename to its existing case-insensitive name.
@@ -79,9 +79,9 @@ class ResourcePack
7979
/// Gets a particular surface set.
8080
SurfaceSet *const getSurfaceSet(const std::string &name);
8181
/// Gets the list of world polygons.
82-
std::vector<Polygon*> *const getPolygons();
82+
std::list<Polygon*> *const getPolygons();
8383
/// Gets the list of world polylines.
84-
std::vector<Polyline*> *const getPolylines();
84+
std::list<Polyline*> *const getPolylines();
8585
/// Gets a particular music.
8686
Music *const getMusic(const std::string &name);
8787
/// Gets a particular sound set.

0 commit comments

Comments
 (0)