From 1822cb6232a430cb15a69aceabb91ffb65aa5452 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 1 Sep 2012 08:33:02 +0100 Subject: [PATCH] Add some typedefs for common templates --- include/ITrackSegment.hpp | 30 ++++++++-------- include/Maths.hpp | 3 ++ src/Logger.cpp | 12 ++++--- src/SplineTrack.cpp | 76 +++++++++++++++++++-------------------- src/StraightTrack.cpp | 38 ++++++++++---------- 5 files changed, 83 insertions(+), 76 deletions(-) diff --git a/include/ITrackSegment.hpp b/include/ITrackSegment.hpp index 715fe23..289a507 100644 --- a/include/ITrackSegment.hpp +++ b/include/ITrackSegment.hpp @@ -1,5 +1,5 @@ // -// Copyright (C) 2009-2010 Nick Gasson +// Copyright (C) 2009-2012 Nick Gasson // // 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 @@ -32,7 +32,7 @@ namespace track { // separate layer field - but will do for now typedef Point Position; typedef Vector Direction; - + // Uniquely identifies the location of a train and its orientation // along a piece of track // Used for verifying whether bits of track can join together @@ -69,7 +69,7 @@ namespace track { int num_exits; // Wrappers for the above functions - + void transform(float delta) const { transformer(*this, delta); @@ -91,11 +91,13 @@ namespace axis { struct ITrackSegment; typedef shared_ptr ITrackSegmentPtr; +typedef vector PointList; + // A segment of track which fits over a number of tiles // Each track segment has an origin and one or more exits -struct ITrackSegment : IXMLSerialisable { +struct ITrackSegment : IXMLSerialisable { virtual ~ITrackSegment() {} - + // Render the track with the origin in the centre virtual void render() const = 0; @@ -112,10 +114,10 @@ struct ITrackSegment : IXMLSerialisable { // position and moving in a particular direciton virtual track::TravelToken get_travel_token(track::Position pos, track::Direction dir) const = 0; - + // True if a train can travel in this direction along the track virtual bool is_valid_direction(const track::Direction& dir) const = 0; - + // Return the position of the next segment of track and the // orientation of the train. // Note that this may not actually be a valid track segment! @@ -130,14 +132,14 @@ struct ITrackSegment : IXMLSerialisable { // Note that an endpoint is not the same as what is returned // from `next_position' - e.g. a straight track that takes up // one tile has a single endpoint which is its origin - virtual void get_endpoints(vector >& a_list) const = 0; + virtual void get_endpoints(PointList& list) const = 0; // Similar to endpoints, the `covers' of a track are the tiles // which are not endpoints but are underneath the track - virtual void get_covers(vector >& output) const = 0; + virtual void get_covers(PointList& output) const = 0; // Covers are tile vertices covered by the track segment - virtual void get_height_locked(vector >& output) const = 0; + virtual void get_height_locked(PointList& output) const = 0; // Add an exit to this section of track possibly generating // a new track segment @@ -146,7 +148,7 @@ struct ITrackSegment : IXMLSerialisable { // may be bigger than the origin segment // The track may already have an exit here in which case // a pointer to itself will be returned - virtual ITrackSegmentPtr merge_exit(Point where, + virtual ITrackSegmentPtr merge_exit(PointI where, track::Direction dir) = 0; // Some track segments may have several states - e.g. points @@ -163,9 +165,9 @@ struct ITrackSegment : IXMLSerialisable { ITrackSegmentPtr make_straight_track(const track::Direction& a_direction); ITrackSegmentPtr make_crossover_track(); ITrackSegmentPtr make_points(track::Direction a_direction, bool reflect); -ITrackSegmentPtr make_slope_track(track::Direction axis, Vector slope, - Vector slope_before, Vector slope_after); -ITrackSegmentPtr make_spline_track(Vector delta, +ITrackSegmentPtr make_slope_track(track::Direction axis, VectorF slope, + VectorF slope_before, VectorF slope_after); +ITrackSegmentPtr make_spline_track(VectorI delta, track::Direction entry_dir, track::Direction exit_dir); diff --git a/include/Maths.hpp b/include/Maths.hpp index db6fa1a..3a401ab 100644 --- a/include/Maths.hpp +++ b/include/Maths.hpp @@ -337,6 +337,9 @@ Point make_point(T x, T y) return Point(x, y); } +typedef Point PointI; +typedef Point PointF; + // A frustum struct Frustum { bool point_in_frustum(float x, float y, float z); diff --git a/src/Logger.cpp b/src/Logger.cpp index 7ce0f2f..4a0a0c8 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -1,5 +1,5 @@ // -// Copyright (C) 2009 Nick Gasson +// Copyright (C) 2009, 2012 Nick Gasson // // 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 @@ -24,6 +24,8 @@ #ifdef WIN32 #include // For _isatty +#else +#include #endif using namespace std; @@ -33,7 +35,7 @@ class LoggerImpl : public ILogger { friend struct PrintLine; public: LoggerImpl(); - + PrintLinePtr write_msg(LogMsgType type); }; @@ -61,7 +63,7 @@ PrintLinePtr LoggerImpl::write_msg(LogMsgType type) { if (is_stdoutTTY) cout << "\x1B[1m"; - + switch (type) { case LOG_NORMAL: cout << "[INFO ] "; @@ -88,14 +90,14 @@ PrintLinePtr LoggerImpl::write_msg(LogMsgType type) PrintLine::PrintLine(ostream& a_stream) : stream(a_stream) { - + } PrintLine::~PrintLine() { if (is_stdoutTTY) cout << "\x1B[0m"; - + stream << endl; } diff --git a/src/SplineTrack.cpp b/src/SplineTrack.cpp index bd7ad24..e30ea7b 100644 --- a/src/SplineTrack.cpp +++ b/src/SplineTrack.cpp @@ -1,5 +1,5 @@ // -// Copyright (C) 2010-2011 Nick Gasson +// Copyright (C) 2010-2012 Nick Gasson // // 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 @@ -29,9 +29,9 @@ class SplineTrack : public ITrackSegment, private SleeperHelper, private BezierHelper { public: - SplineTrack(Vector delta, - track::Direction entry_dir, - track::Direction exit_dir); + SplineTrack(VectorI delta, + track::Direction entry_dir, + track::Direction exit_dir); // ITrackSegment interface void render() const; @@ -40,10 +40,10 @@ public: float segment_length(const track::TravelToken& token) const; bool is_valid_direction(const track::Direction& dir) const; track::Connection next_position(const track::TravelToken& token) const; - void get_endpoints(vector >& output) const; - void get_covers(vector >& output) const; - void get_height_locked(vector >& output) const; - ITrackSegmentPtr merge_exit(Point where, track::Direction dir); + void get_endpoints(PointList& output) const; + void get_covers(PointList& output) const; + void get_height_locked(PointList& output) const; + ITrackSegmentPtr merge_exit(PointI where, track::Direction dir); track::TravelToken get_travel_token(track::Position pos, track::Direction dir) const; void next_state() {} @@ -53,37 +53,37 @@ public: // IXMLSerialisable interface xml::element to_xml() const; - + private: typedef vector > Polygon; void bounding_polygon(Polygon& poly) const; static bool point_in_polygon(const Polygon& poly, Point p); - + float extend_from_center(track::Direction dir) const; void ensure_valid_direction(track::Direction dir) const; void transform(const track::TravelToken& token, float delta, bool backwards) const; float rotation_at(float delta) const; - + BezierCurve curve; IMeshBufferPtr rail_buf; - Point origin; + PointI origin; float height; - Vector delta; + VectorI delta; track::Direction entry_dir, exit_dir; Polygon bounds; - typedef tuple, + typedef tuple Parameters; - typedef map MeshCache; + typedef map MeshCache; static MeshCache mesh_cache; }; SplineTrack::MeshCache SplineTrack::mesh_cache; -SplineTrack::SplineTrack(Vector delta, +SplineTrack::SplineTrack(VectorI delta, track::Direction entry_dir, track::Direction exit_dir) : delta(delta), @@ -99,7 +99,7 @@ SplineTrack::SplineTrack(Vector delta, static_cast(entry_dir.x), 0.0f, static_cast(entry_dir.z)).normalise(); - + Vector exit_dir_norm = make_vector( static_cast(exit_dir.x), 0.0f, @@ -116,7 +116,7 @@ SplineTrack::SplineTrack(Vector delta, Vector p4 = delta_f + (exit_dir_norm * exit_extend); curve = make_bezier_curve(p1, p2, p3, p4); - + Parameters parms = make_tuple(delta, entry_dir, exit_dir); MeshCache::iterator it = mesh_cache.find(parms); if (it == mesh_cache.end()) { @@ -130,17 +130,17 @@ SplineTrack::SplineTrack(Vector delta, } float SplineTrack::extend_from_center(track::Direction dir) const -{ +{ // Track must extend from the centre to the edge of a tile const float x_sq = static_cast(dir.x * dir.x); const float y_sq = static_cast(dir.z * dir.z); - + if (abs(dir.x) == abs(dir.z)) return sqrtf(2.0f) * 0.5f; else if (abs(dir.x) < abs(dir.z)) return sqrtf(x_sq / y_sq + 1) * 0.5f; else - return sqrtf(y_sq / x_sq + 1) * 0.5f; + return sqrtf(y_sq / x_sq + 1) * 0.5f; } void SplineTrack::merge(IMeshBufferPtr buf) const @@ -162,7 +162,7 @@ void SplineTrack::merge(IMeshBufferPtr buf) const ++n; delta = ((curve.length - sleeper_sep) / n) - sleeper_sep; } while (delta > sleeper_sep / n); - + for (int i = 0; i <= n; i++) { float pos = (sleeper_sep / 2) + i * (sleeper_sep + delta); @@ -185,7 +185,7 @@ void SplineTrack::render() const glTranslatef(origin.x, 0.0f, origin.y); glColor3f(0.1f, 0.1f, 0.8f); - + glBegin(GL_LINE_LOOP); for (Polygon::const_iterator it = bounds.begin(); it != bounds.end(); @@ -213,7 +213,7 @@ void SplineTrack::render() const glPopAttrib(); glPopMatrix(); #endif -} +} void SplineTrack::set_origin(int x, int y, float h) { @@ -248,17 +248,17 @@ SplineTrack::next_position(const track::TravelToken& token) const ensure_valid_direction(token.direction); if (token.direction == entry_dir) { - Point off = + PointI off = make_point(exit_dir.x, exit_dir.z) + make_point(delta.x, delta.y); return make_pair(origin + off, exit_dir); } else { - Point off = -make_point(entry_dir.x, entry_dir.z); + PointI off = -make_point(entry_dir.x, entry_dir.z); return make_pair(origin + off, -entry_dir); - } + } } -void SplineTrack::get_endpoints(vector >& output) const +void SplineTrack::get_endpoints(PointList& output) const { output.push_back(origin); @@ -267,13 +267,13 @@ void SplineTrack::get_endpoints(vector >& output) const make_point(origin.x + delta.x, origin.y + delta.y)); } -void SplineTrack::get_covers(vector >& output) const +void SplineTrack::get_covers(PointList& output) const { const Point off = make_point(0.5f, 0.5f); - + for (int x = min(0, delta.x); x <= max(0, delta.x) + 1; x++) { for (int y = min(0, delta.y); y <= max(0, delta.y) + 1; y++) { - Point p = make_point(x, y); + PointI p = make_point(x, y); const bool is_origin = p == make_point(0, 0); const bool is_delta = p == make_point(delta.x, delta.y); @@ -311,7 +311,7 @@ void SplineTrack::bounding_polygon(Polygon& poly) const { const float step = 0.01f; const float fudge = 0.8f; - + for (float t = 0.0f; t <= 1.0f; t += step) { Vector v = curve.offset(t, fudge); poly.push_back(make_point(v.x, v.z)); @@ -323,19 +323,19 @@ void SplineTrack::bounding_polygon(Polygon& poly) const } } -void SplineTrack::get_height_locked(vector >& output) const +void SplineTrack::get_height_locked(PointList& output) const { for (int x = min(0, delta.x); x <= max(0, delta.x) + 1; x++) { for (int y = min(0, delta.y); y <= max(0, delta.y) + 1; y++) { - Point p = make_point(x, y); + PointI p = make_point(x, y); if (point_in_polygon(bounds, point_cast(p))) output.push_back(p + origin); } } } -ITrackSegmentPtr SplineTrack::merge_exit(Point where, track::Direction dir) +ITrackSegmentPtr SplineTrack::merge_exit(PointI where, track::Direction dir) { return ITrackSegmentPtr(); } @@ -343,7 +343,7 @@ ITrackSegmentPtr SplineTrack::merge_exit(Point where, track::Direction dir) float SplineTrack::rotation_at(float curve_delta) const { assert(curve_delta >= 0.0f && curve_delta <= 1.0f); - + const Vector deriv = curve.deriv(curve_delta); // Derivation of angle depends on quadrant @@ -378,7 +378,7 @@ void SplineTrack::transform(const track::TravelToken& token, float angle = rotation_at(u_curve_delta); if (backwards) angle += 180.0f; - + glRotatef(-angle, 0.0f, 1.0f, 0.0f); } @@ -412,7 +412,7 @@ xml::element SplineTrack::to_xml() const .add_attribute("exit-dir-y", exit_dir.z); } -ITrackSegmentPtr make_spline_track(Vector delta, +ITrackSegmentPtr make_spline_track(VectorI delta, track::Direction entry_dir, track::Direction exit_dir) { diff --git a/src/StraightTrack.cpp b/src/StraightTrack.cpp index e76def6..477a31f 100644 --- a/src/StraightTrack.cpp +++ b/src/StraightTrack.cpp @@ -1,5 +1,5 @@ // -// Copyright (C) 2009-2010 Nick Gasson +// Copyright (C) 2009-2012 Nick Gasson // // 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 @@ -39,19 +39,19 @@ class StraightTrack : public ITrackSegment, public: StraightTrack(const Direction& a_direction); ~StraightTrack(); - + void render() const {} void merge(IMeshBufferPtr buf) const; - + void set_origin(int x, int y, float h); float segment_length(const track::TravelToken& token) const { return 1.0f; } Connection next_position(const track::TravelToken& a_direction) const; bool is_valid_direction(const Direction& a_direction) const; - void get_endpoints(vector >& a_list) const; - void get_covers(vector >& output) const { } - void get_height_locked(vector >& output) const; - + void get_endpoints(PointList& a_list) const; + void get_covers(PointList& output) const { } + void get_height_locked(PointList& output) const; + ITrackSegmentPtr merge_exit(Point where, track::Direction dir); track::TravelToken get_travel_token(track::Position a_position, track::Direction a_direction) const; @@ -63,11 +63,11 @@ public: // IXMLSerialisable interface xml::element to_xml() const; - + private: void transform(const track::TravelToken& a_token, float delta) const; void ensure_valid_direction(const track::Direction& a_direction) const; - + Point origin; // Absolute position Direction direction; float height; @@ -76,12 +76,12 @@ private: StraightTrack::StraightTrack(const Direction& a_direction) : direction(a_direction), height(0.0f) { - + } StraightTrack::~StraightTrack() { - + } void StraightTrack::set_origin(int x, int y, float h) @@ -125,7 +125,7 @@ void StraightTrack::transform(const track::TravelToken& a_token, glRotated(-90.0, 0.0, 1.0, 0.0); glTranslated(-0.5, 0.0, 0.0); - + if (a_token.direction == -direction) glRotated(-180.0, 0.0, 1.0, 0.0); } @@ -166,7 +166,7 @@ ITrackSegmentPtr StraightTrack::merge_exit(Point where, else if (where == origin + make_point(-1, 2)) return make_points(axis::Y, false); } - + // Not possible to merge return ITrackSegmentPtr(); } @@ -179,12 +179,12 @@ bool StraightTrack::is_valid_direction(const Direction& a_direction) const return a_direction == axis::Y || -a_direction == axis::Y; } -void StraightTrack::get_endpoints(vector >& a_list) const +void StraightTrack::get_endpoints(PointList& a_list) const { a_list.push_back(origin); } -void StraightTrack::get_height_locked(vector >& output) const +void StraightTrack::get_height_locked(PointList& output) const { output.push_back(origin + make_point(0, 0)); output.push_back(origin + make_point(0, 1)); @@ -232,12 +232,12 @@ void StraightTrack::merge(IMeshBufferPtr buf) const y_angle += 90.0f; off += rotate(make_vector(-0.4f, 0.0f, 0.0f), y_angle, 0.0f, 1.0f, 0.0f); - + for (int i = 0; i < 4; i++) { merge_sleeper(buf, off, y_angle); off += rotate(make_vector(0.25f, 0.0f, 0.0f), y_angle, 0.0f, 1.0f, 0.0f); - } + } } xml::element StraightTrack::to_xml() const @@ -249,7 +249,7 @@ xml::element StraightTrack::to_xml() const ITrackSegmentPtr make_straight_track(const Direction& a_direction) { Direction real_dir(a_direction); - + // Direction must either be along axis::X or axis::Y but we // allow the opositite direction here too if (real_dir == -axis::X || real_dir == -axis::Y) @@ -258,6 +258,6 @@ ITrackSegmentPtr make_straight_track(const Direction& a_direction) if (real_dir != axis::X && real_dir != axis::Y) throw runtime_error("Illegal straight track direction: " + lexical_cast(a_direction)); - + return ITrackSegmentPtr(new StraightTrack(real_dir)); } -- 2.39.2