From ab57903f9f5ba21c2eff25e85f66d6d5d21733e3 Mon Sep 17 00:00:00 2001 From: hiker Date: Mon, 14 Jul 2014 23:08:46 +1000 Subject: [PATCH] Try to convert m_gp from a GPData pointer to a GP object, compiles, but crashes. --- src/race/grand_prix_data.cpp | 8 ++++---- src/race/grand_prix_data.hpp | 13 +++++++++--- src/states_screens/dialogs/gp_info_dialog.cpp | 20 +++++++++---------- src/states_screens/dialogs/gp_info_dialog.hpp | 6 ++++-- .../dialogs/random_gp_dialog.cpp | 16 +++++++-------- .../dialogs/random_gp_dialog.hpp | 9 +-------- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/race/grand_prix_data.cpp b/src/race/grand_prix_data.cpp index 1405e01c8..46b60794e 100644 --- a/src/race/grand_prix_data.cpp +++ b/src/race/grand_prix_data.cpp @@ -49,7 +49,7 @@ GrandPrixData::GrandPrixData(const std::string& filename) // ---------------------------------------------------------------------------- GrandPrixData::GrandPrixData(const unsigned int number_of_tracks, const std::string& track_group, - const RandomGPInfoDialog::REVERSED use_reverse) + const GrandPrixData::GP_Reversed use_reverse) { m_filename = "Random GP - Not loaded from a file!"; m_id = "random"; @@ -118,13 +118,13 @@ void GrandPrixData::changeTrackNumber(const unsigned int number_of_tracks, // ---------------------------------------------------------------------------- -void GrandPrixData::changeReverse(const RandomGPInfoDialog::REVERSED use_reverse) +void GrandPrixData::changeReverse(const GrandPrixData::GP_Reversed use_reverse) { for (unsigned int i = 0; i < m_tracks.size(); i++) { - if (use_reverse == RandomGPInfoDialog::NO_REVERSE) + if (use_reverse == NO_REVERSE) m_reversed[i] = false; - else if (use_reverse == RandomGPInfoDialog::MIXED) + else if (use_reverse == MIXED) if (track_manager->getTrack(m_tracks[i])->reverseAvailable()) m_reversed[i] = (rand() % 2 != 0); else diff --git a/src/race/grand_prix_data.hpp b/src/race/grand_prix_data.hpp index 742ec15f7..04c05249d 100644 --- a/src/race/grand_prix_data.hpp +++ b/src/race/grand_prix_data.hpp @@ -24,7 +24,6 @@ #include #include -#include "states_screens/dialogs/random_gp_dialog.hpp" #include "utils/translation.hpp" using irr::core::stringw; @@ -69,6 +68,14 @@ private: */ bool isTrackAvailable(const std::string &id, bool includeLocked) const; +public: + enum GP_Reversed + { + NO_REVERSE = 0, + ALL_REVERSE = 1, + MIXED = 2 + }; + public: #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__) # pragma warning(disable:4290) @@ -80,10 +87,10 @@ public: /** Creates a new random GP */ GrandPrixData(const unsigned int number_of_tracks, const std::string& track_group, - const RandomGPInfoDialog::REVERSED use_reverse); + const GP_Reversed use_reverse); void changeTrackNumber(const unsigned int number_of_tracks, const std::string& track_group); - void changeReverse(const RandomGPInfoDialog::REVERSED use_reverse); + void changeReverse(const GP_Reversed use_reverse); // Methods for the GP editor void setId(const std::string& id); diff --git a/src/states_screens/dialogs/gp_info_dialog.cpp b/src/states_screens/dialogs/gp_info_dialog.cpp index 36fc61646..8072c875b 100644 --- a/src/states_screens/dialogs/gp_info_dialog.cpp +++ b/src/states_screens/dialogs/gp_info_dialog.cpp @@ -53,8 +53,8 @@ GPInfoDialog::GPInfoDialog(const std::string& gp_ident) doInit(); m_curr_time = 0.0f; - m_gp = grand_prix_manager->getGrandPrix(gp_ident); - m_gp->checkConsistency(); + m_gp = *grand_prix_manager->getGrandPrix(gp_ident); + m_gp.checkConsistency(); m_under_title = m_area.getHeight()/7; m_over_body = m_area.getHeight()/7; @@ -72,7 +72,7 @@ GPInfoDialog::~GPInfoDialog() { GUIEngine::Screen* curr_screen = GUIEngine::getCurrentScreen(); if (curr_screen->getName() == "tracks.stkgui") - static_cast(curr_screen)->setFocusOnGP(m_gp->getId()); + static_cast(curr_screen)->setFocusOnGP(m_gp.getId()); } // ---------------------------------------------------------------------------- @@ -81,7 +81,7 @@ void GPInfoDialog::addTitle() { core::rect< s32 > area_top(0, 0, m_area.getWidth(), m_under_title); IGUIStaticText* title = GUIEngine::getGUIEnv()->addStaticText( - translations->fribidize(m_gp->getName()), + translations->fribidize(m_gp.getName()), area_top, false, true, // border, word wrap m_irrlicht_window); title->setTabStop(false); @@ -92,7 +92,7 @@ void GPInfoDialog::addTitle() void GPInfoDialog::addTracks() { - const std::vector tracks = m_gp->getTrackNames(); + const std::vector tracks = m_gp.getTrackNames(); const unsigned int track_amount = tracks.size(); int height_of_one_line = std::min((m_lower_bound - m_over_body)/(track_amount+1), @@ -185,7 +185,7 @@ void GPInfoDialog::addScreenshot() m_screenshot_widget->m_h = m_area.getWidth()*3/8; // *(3/4)*(1/2) } - Track* track = track_manager->getTrack(m_gp->getTrackNames()[0]); + Track* track = track_manager->getTrack(m_gp.getTrackNames()[0]); m_screenshot_widget->m_properties[GUIEngine::PROP_ICON] = (track->getScreenshotFile().c_str()); m_screenshot_widget->setParent(m_irrlicht_window); m_screenshot_widget->add(); @@ -203,7 +203,7 @@ void GPInfoDialog::addButtons() SavedGrandPrix* saved_gp = SavedGrandPrix::getSavedGP( StateManager::get() ->getActivePlayerProfile(0) ->getUniqueID(), - m_gp->getId(), + m_gp.getId(), race_manager->getDifficulty(), race_manager->getNumberOfKarts(), race_manager->getNumLocalPlayers()); @@ -250,7 +250,7 @@ void GPInfoDialog::addButtons() void GPInfoDialog::onEnterPressedInternal() { // Save the GP id because dismiss() will destroy this instance - std::string gp_id = m_gp->getId(); + std::string gp_id = m_gp.getId(); ModalDialog::dismiss(); // Disable accidentally unlocking of a challenge PlayerManager::getCurrentPlayer()->setCurrentChallenge(""); @@ -264,7 +264,7 @@ GUIEngine::EventPropagation GPInfoDialog::processEvent(const std::string& eventS if (eventSource == "start" || eventSource == "continue") { // Save GP identifier, since dismiss will delete this object. - std::string gp_id = m_gp->getId(); + std::string gp_id = m_gp.getId(); ModalDialog::dismiss(); race_manager->startGP(grand_prix_manager->getGrandPrix(gp_id), false, (eventSource == "continue")); @@ -284,7 +284,7 @@ void GPInfoDialog::onUpdate(float dt) m_curr_time += dt; int frameAfter = (int)(m_curr_time / 1.5f); - const std::vector tracks = m_gp->getTrackNames(); + const std::vector tracks = m_gp.getTrackNames(); if (frameAfter >= (int)tracks.size()) { frameAfter = 0; diff --git a/src/states_screens/dialogs/gp_info_dialog.hpp b/src/states_screens/dialogs/gp_info_dialog.hpp index d221d44d9..93158162e 100644 --- a/src/states_screens/dialogs/gp_info_dialog.hpp +++ b/src/states_screens/dialogs/gp_info_dialog.hpp @@ -20,7 +20,7 @@ #define HEADER_GP_INFO_DIALOG_HPP #include "guiengine/modaldialog.hpp" -// Don't include grand_prix_data.hpp here or the compilation will fail +#include "race/grand_prix_data.hpp" class GrandPrixData; @@ -39,7 +39,9 @@ class GPInfoDialog : public GUIEngine::ModalDialog protected: // Necessary for RandomGPInfoDialog GUIEngine::IconButtonWidget* m_screenshot_widget; float m_curr_time; - GrandPrixData* m_gp; + + /** The grand prix data. */ + GrandPrixData m_gp; /** height of the separator over the body */ int m_over_body; diff --git a/src/states_screens/dialogs/random_gp_dialog.cpp b/src/states_screens/dialogs/random_gp_dialog.cpp index dffe5221f..1d9b32dc1 100644 --- a/src/states_screens/dialogs/random_gp_dialog.cpp +++ b/src/states_screens/dialogs/random_gp_dialog.cpp @@ -37,7 +37,7 @@ RandomGPInfoDialog::RandomGPInfoDialog() // Defaults - loading selection from last time frrom a file would be better m_number_of_tracks = 2; // We can assume that there are at least 2 standard tracks m_trackgroup = "standard"; - m_use_reverse = NO_REVERSE; + m_use_reverse = GrandPrixData::NO_REVERSE; doInit(); m_curr_time = 0.0f; @@ -46,7 +46,7 @@ RandomGPInfoDialog::RandomGPInfoDialog() m_over_body = m_area.getHeight()/7 + SPINNER_HEIGHT + 10; // 10px space m_lower_bound = m_area.getHeight()*6/7; - m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse); + m_gp = GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse); addTitle(); addSpinners(); @@ -135,14 +135,14 @@ GUIEngine::EventPropagation RandomGPInfoDialog::processEvent( if (eventSource == "start") { ModalDialog::dismiss(); - race_manager->startGP(m_gp, false, false); + race_manager->startGP(&m_gp, false, false); return GUIEngine::EVENT_BLOCK; } else if (eventSource == "Number of tracks") { // The old gp can be reused because there's only track deletion/adding m_number_of_tracks = getWidget("Number of tracks")->getValue(); - m_gp->changeTrackNumber(m_number_of_tracks, m_trackgroup); + m_gp.changeTrackNumber(m_number_of_tracks, m_trackgroup); addTracks(); } else if (eventSource == "Trackgroup") @@ -164,18 +164,18 @@ GUIEngine::EventPropagation RandomGPInfoDialog::processEvent( if (s->getValue() > (signed)max) s->setValue(max); - new (m_gp) GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse); + m_gp = GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse); addTracks(); } else if (eventSource == "reverse") { Spinner* r = getWidget("reverse"); - m_use_reverse = static_cast(r->getValue()); - m_gp->changeReverse(m_use_reverse); + m_use_reverse = static_cast(r->getValue()); + m_gp.changeReverse(m_use_reverse); } else if (eventSource == "reload") { - new (m_gp) GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse); + m_gp = GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse); addTracks(); } diff --git a/src/states_screens/dialogs/random_gp_dialog.hpp b/src/states_screens/dialogs/random_gp_dialog.hpp index 698217eec..508153ad6 100644 --- a/src/states_screens/dialogs/random_gp_dialog.hpp +++ b/src/states_screens/dialogs/random_gp_dialog.hpp @@ -24,17 +24,10 @@ class RandomGPInfoDialog : public GPInfoDialog { -public: - enum REVERSED - { - NO_REVERSE = 0, - ALL_REVERSE = 1, - MIXED = 2 - }; private: unsigned int m_number_of_tracks; std::string m_trackgroup; - REVERSED m_use_reverse; + GrandPrixData::GP_Reversed m_use_reverse; public: static const int SPINNER_HEIGHT = 40;