From ba2a548e59439e95ff13bb451c5c1207a3e8a32c Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 8 Sep 2012 15:10:54 +0100 Subject: [PATCH] Fix one case of drawing constrained track --- src/Editor.cpp | 46 +++++++++++++++++++++++++--------------------- src/Game.cpp | 46 +++++++++++++++++++++++----------------------- 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/src/Editor.cpp b/src/Editor.cpp index e89ec48..50d2149 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -45,11 +45,11 @@ public: void on_key_down(SDLKey a_key); void on_key_up(SDLKey a_key); void on_mouse_move(IPickBufferPtr pick_buffer, - int x, int y, int xrel, int yrel); + int x, int y, int xrel, int yrel); void on_mouse_click(IPickBufferPtr pick_buffer, int x, int y, - MouseButton a_button); + MouseButton a_button); void on_mouse_release(IPickBufferPtr pick_buffer, int x, int y, - MouseButton a_button); + MouseButton a_button); // Different tools the user can be using enum Tool { @@ -65,7 +65,7 @@ public: private: void build_gui(); void draw_dragged_track(); - bool draw_track_tile(Point where, track::Direction axis); + bool draw_track_tile(PointI where, track::Direction axis); void draw_dragged_straight(const track::Direction& axis, int length); void draw_diagonal_straight(const track::Direction& axis, int length); void draw_initial_track(); @@ -75,10 +75,10 @@ private: void draw_curve(const track::Direction& entry_dir, const track::Direction& exit_dir); void draw_s_bend(const track::Direction& dir); - bool can_connect(const Point& a_first_point, - const Point& a_second_point) const; + bool can_connect(const PointI& a_first_point, + const PointI& a_second_point) const; bool can_place_track(ITrackSegmentPtr track); - bool guess_track_dir(const Point& p, track::Direction& d) const; + bool guess_track_dir(const PointI& p, track::Direction& d) const; void drag_box_bounds(int& x_min, int& x_max, int &y_min, int& y_max) const; void drag_box_size(int& xlen, int& ylen) const; void delete_objects(); @@ -95,7 +95,7 @@ private: bool am_scrolling; // Variables for dragging track segments - Point drag_begin, drag_end; + PointI drag_begin, drag_end; bool am_dragging, is_shift_down; // GUI elements @@ -228,8 +228,8 @@ void Editor::update(IPickBufferPtr pick_buffer, int a_delta) // True if the `a_first_point' is a valid track segment and it can // connect to `a_second_point' -bool Editor::can_connect(const Point& a_first_point, - const Point& a_second_point) const +bool Editor::can_connect(const PointI& a_first_point, + const PointI& a_second_point) const { if (!map->is_valid_track(a_first_point)) return false; @@ -247,7 +247,7 @@ bool Editor::can_connect(const Point& a_first_point, // Try to guess the direction of a track endpoint by looking at the // surrounding tiles -bool Editor::guess_track_dir(const Point& p, track::Direction& d) const +bool Editor::guess_track_dir(const PointI& p, track::Direction& d) const { if (can_connect(p.left(), p)) { d = axis::X; @@ -281,7 +281,7 @@ bool Editor::guess_track_dir(const Point& p, track::Direction& d) const // Draw a single tile of straight track and check for collisions // Returns `false' if track cannot be placed here -bool Editor::draw_track_tile(Point where, track::Direction axis) +bool Editor::draw_track_tile(PointI where, track::Direction axis) { // Ensure axis is only in the positive direction if (axis == -axis::X) @@ -347,7 +347,7 @@ bool Editor::draw_track_tile(Point where, track::Direction axis) // This just draws straight track along the rectangle void Editor::draw_dragged_straight(const track::Direction& axis, int length) { - Point where = drag_begin; + PointI where = drag_begin; for (int i = 0; i < length; i++) { draw_track_tile(where, axis); @@ -361,7 +361,7 @@ void Editor::draw_dragged_straight(const track::Direction& axis, int length) // track segment void Editor::draw_diagonal_straight(const track::Direction& axis, int length) { - Point where = drag_begin; + PointI where = drag_begin; for (int i = 0; i < length; i++) { VectorI delta = make_vector(axis.x, axis.z, 0); @@ -376,11 +376,11 @@ void Editor::draw_diagonal_straight(const track::Direction& axis, int length) // True if a track segment could be placed in its present location bool Editor::can_place_track(ITrackSegmentPtr track) { - vector > covered; + PointList covered; track->get_endpoints(covered); track->get_covers(covered); - for (vector >::iterator it = covered.begin(); + for (PointList::iterator it = covered.begin(); it != covered.end(); ++it) { if (map->is_valid_track(*it)) { warn() << "Cannot place track here"; @@ -563,7 +563,11 @@ void Editor::draw_constrained_track(const track::Direction& start_dir, draw_diagonal_straight(start_dir, xlen); } else { - warn() << "TODO"; + VectorI delta = make_vector(drag_end.x - drag_begin.x, + drag_end.y - drag_begin.y, + 0); + ITrackSegmentPtr curve = make_spline_track(delta, start_dir, -end_dir); + map->set_track_at(drag_begin, curve); } } @@ -694,7 +698,7 @@ void Editor::draw_dragged_track() xlen); } else { - Point delta = drag_end - drag_begin; + PointI delta = drag_end - drag_begin; VectorI off = make_vector(delta.x, delta.y, 0); @@ -702,7 +706,7 @@ void Editor::draw_dragged_track() start_dir, end_dir); - Point where = drag_begin; + PointI where = drag_begin; track->set_origin(where.x, where.y, map->height_at(where)); if (can_place_track(track)) @@ -736,7 +740,7 @@ void Editor::plant_trees() for (int x = xmin; x <= xmax; x++) { for (int y = ymin; y <= ymax; y++) { - const Point p = make_point(x, y); + const PointI p = make_point(x, y); if ((is_single_tile || tree_rand() > threshold) && map->empty_tile(p)) map->add_scenery(p, tree_picker->get()); @@ -789,7 +793,7 @@ void Editor::on_mouse_click(IPickBufferPtr pick_buffer, int x, int y, if (id > 0) { // Begin dragging a selection rectangle - Point where = map->pick_position(id); + PointI where = map->pick_position(id); drag_begin = drag_end = where; am_dragging = true; diff --git a/src/Game.cpp b/src/Game.cpp index 14c92e7..a6cb352 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -39,7 +39,7 @@ class Game : public IScreen { public: Game(IMapPtr a_map); ~Game(); - + void display(IGraphicsPtr a_context) const; void overlay() const; void update(IPickBufferPtr a_pick_buffer, int a_delta); @@ -61,7 +61,7 @@ private: enum TrackStateReq { NEXT, PREV }; void alter_track_state(TrackStateReq req); - + IMapPtr map; ITrainPtr train; ILightPtr sun; @@ -79,7 +79,7 @@ private: enum CameraMode { CAMERA_FLOATING, CAMERA_BIRD }; CameraMode camera_mode; - + gui::ILayoutPtr layout; IMessageAreaPtr message_area; IRenderStatsPtr render_stats; @@ -109,7 +109,7 @@ Game::Game(IMapPtr a_map) Game::~Game() { - + } Vector Game::camera_position(float a_radius) const @@ -140,12 +140,12 @@ void Game::display(IGraphicsPtr a_context) const Vector train_pos = train->front(); Vector position = camera_position(view_radius); - + a_context->look_at(position, train_pos); set_billboard_cameraOrigin(position); - + sun->apply(); - + map->render(a_context); train->render(); @@ -167,13 +167,13 @@ void Game::update(IPickBufferPtr a_pick_buffer, int a_delta) { message_area->update(a_delta); render_stats->update(a_delta); - + train->update(a_delta); // Update the GUI elements layout->cast("/throttle_meter").value( train->controller()->throttle()); - + const double ms_toMPH = 2.237; layout->cast("/speed_label").format( "Speed: %.1lfmph", abs(train->speed()) * ms_toMPH); @@ -181,7 +181,7 @@ void Game::update(IPickBufferPtr a_pick_buffer, int a_delta) IControllerPtr ctrl = train->controller(); layout->get("/brake_label").visible(ctrl->brake_on()); layout->get("/reverse_label").visible(ctrl->reverse_on()); - + look_ahead(); // Move the camera vertically if it's currently underground @@ -194,12 +194,12 @@ void Game::update(IPickBufferPtr a_pick_buffer, int a_delta) const float MIN_HEIGHT = 0.25f; float h = map->height_at(clip_position.x, clip_position.z); - if (h + MIN_HEIGHT > clip_position.y) { + if (h + MIN_HEIGHT > clip_position.y) { cameraVTarget -= 0.001f * static_cast(a_delta); camera_speed = 200.0f; } #endif - + // Bounce the camera if we need to vert_angle -= (vert_angle - cameraVTarget) / camera_speed; horiz_angle -= (horiz_angle - cameraHTarget) / camera_speed; @@ -246,7 +246,7 @@ void Game::look_ahead() stopped_at_station(); else message_area->post("Stop here for station " + it.station->name()); - + return; } @@ -256,7 +256,7 @@ void Game::look_ahead() if (it.status != TRACK_OK) { bool clear_station = true; - + switch (it.status) { case TRACK_STATION: message_area->post("Approaching station " + it.station->name()); @@ -279,7 +279,7 @@ void Game::look_ahead() return; } } - + // We're not approaching any station left_station(); } @@ -297,7 +297,7 @@ void Game::alter_track_state(TrackStateReq req) // Skip over the first section of track which may be some // points - we don't want to alter the track we're on! it = it.next(); - + if (it.status == TRACK_CHOICE) { switch (req) { case NEXT: @@ -307,7 +307,7 @@ void Game::alter_track_state(TrackStateReq req) it.track->prev_state(); break; } - + return; } } @@ -316,7 +316,7 @@ void Game::alter_track_state(TrackStateReq req) } void Game::on_key_down(SDLKey a_key) -{ +{ switch (a_key) { case SDLK_PAGEUP: view_radius = max(view_radius - 0.2f, 0.1f); @@ -363,8 +363,8 @@ void Game::on_key_down(SDLKey a_key) } void Game::on_key_up(SDLKey a_key) -{ - +{ + } void Game::on_mouse_click(IPickBufferPtr a_pick_buffer, int x, int y, @@ -396,19 +396,19 @@ void Game::on_mouse_release(IPickBufferPtr pick_buffer, int x, int y, break; } } - + void Game::on_mouse_move(IPickBufferPtr a_pick_buffer, int x, int y, int xrel, int yrel) { if (camera_mode == CAMERA_FLOATING && panning) { cameraHTarget -= xrel / 100.0f; cameraVTarget += yrel / 100.0f; - + // Don't allow the camera to go under the ground const float ground = (M_PI / 2.0f) - 0.01f; if (cameraVTarget > ground) cameraVTarget = ground; - + // Don't let the camera flip over the top const float top = 0.01f; if (cameraVTarget < top) -- 2.39.2