From fe67c0e65f9b6c285d2b67bfe8401537b4c9765c Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 1 Aug 2010 21:04:02 +0100 Subject: [PATCH] New Anchor template to generalise TrackNode --- src/Map.cpp | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/Map.cpp b/src/Map.cpp index 39f9fcc..f1f2983 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -42,32 +42,29 @@ #include #include -// A single piece of track may appear multiple times in the map - this -// will be true for all track segments that cover multiple tiles. -// For various map algorithms (e.g. drawing) it is useful to keep track -// of which track segments have already been visited. So track segments -// are wrapped inside TrackNode which is used to store map-specific -// information about the track. -class TrackNode { +// A single piece of track, scenery, etc. may be connected to +// more than one tile. Anchor is the association class +template +class Anchor { public: - TrackNode(ITrackSegmentPtr a_track, int x, int y) - : track(a_track), last_frame(-1), - origin(make_point(x, y)) {} + Anchor(shared_ptr obj, Point origin) + : owned(obj), last_frame(-1), origin_(origin) {} - inline void rendered_on(int f) { last_frame = f; } - inline bool needs_rendering(int f) const { return f != last_frame; } + // These numbers don't necessarily refer to frames + void rendered_on(int f) { last_frame = f; } + bool needs_rendering(int f) const { return f != last_frame; } - inline ITrackSegmentPtr get() { return track; } + const Point& origin() const { return origin_; } - inline int originX() const { return origin.x; } - inline int originY() const { return origin.y; } + inline shared_ptr get() { return owned; } private: - ITrackSegmentPtr track; + shared_ptr owned; int last_frame; - Point origin; + Point origin_; }; -typedef shared_ptr TrackNodePtr; +typedef shared_ptr > TrackAnchor; +typedef shared_ptr > SceneryAnchor; class Map : public IMap, public ISectorRenderable, public enable_shared_from_this { @@ -324,7 +321,7 @@ void Map::set_track_at(const Point& where, ITrackSegmentPtr track) track->set_origin(where.x, where.y, lowest_height); - TrackNodePtr node(new TrackNode(track, where.x, where.y)); + TrackNodePtr node(new Anchor(track, where)); // Attach the track node to every tile it covers vector > covers; @@ -1536,8 +1533,7 @@ void Map::save_to(ostream& of) tile_xml.add_attribute("y", y); if (tile.track - && tile.track->originX() == x - && tile.track->originY() == y) { + && tile.track->origin() == make_point(x, y)) { tile_xml.add_child(tile.track->get()->to_xml()); useful = true; -- 2.39.2