mail[Wesnoth-commits] r34361 - in /trunk/src: menu_events.cpp menu_events.hpp playsingle_controller.cpp savegame.cpp savegame.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by joerg . hinrichs on March 31, 2009 - 23:19:
Author: jhinrichs
Date: Tue Mar 31 23:19:53 2009
New Revision: 34361

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34361&view=rev
Log:
Savegame reorganization Step 1: Providing a simpler interface to saving and 
loading.
Providing a separate interface method for autosaves and adding the file 
savegame.cpp i missed the last time.

Added:
    trunk/src/savegame.cpp   (with props)
    trunk/src/savegame.hpp   (with props)
Modified:
    trunk/src/menu_events.cpp
    trunk/src/menu_events.hpp
    trunk/src/playsingle_controller.cpp

Modified: trunk/src/menu_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/menu_events.cpp?rev=34361&r1=34360&r2=34361&view=diff
==============================================================================
--- trunk/src/menu_events.cpp (original)
+++ trunk/src/menu_events.cpp Tue Mar 31 23:19:53 2009
@@ -729,7 +729,7 @@
                gui_->labels().write(start);
        }
 
-       void menu_handler::autosave(const std::string &label, unsigned turn, 
const config &starting_pos) const
+       void menu_handler::autosave(unsigned turn) const
        {
                if(game_config::disable_autosave)
                        return;
@@ -737,21 +737,18 @@
                Uint32 start, end;
                start = SDL_GetTicks();
                config snapshot;
-               std::string savename;
-               if (label.empty())
-                       savename = _("Auto-Save");
-               else
-                       savename = label + "-" + _("Auto-Save") + 
lexical_cast<std::string>(turn);
                write_game_snapshot(snapshot);
+
                try {
-                       ::save_game(savename, snapshot, gamestate_);
+                       ::save_autosave(turn, snapshot, gamestate_);
                } catch(game::save_game_failed&) {
                        gui::message_dialog(*gui_,"",_("Could not auto save 
the game. Please save the game manually.")).show();
                        //do not bother retrying, since the user can just 
save the game
                        //maybe show a yes-no dialog for "disable autosaves 
now"?
                }
                end = SDL_GetTicks();
-               LOG_NG << "Milliseconds to save " << savename << ": " << end 
- start << "\n";
+               LOG_NG << "Milliseconds to save Autosave: " << end - start << 
"\n";
+               //LOG_NG << "Milliseconds to save " << savename << ": " << 
end - start << "\n";
 
                remove_old_auto_saves();
        }

Modified: trunk/src/menu_events.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/menu_events.hpp?rev=34361&r1=34360&r2=34361&view=diff
==============================================================================
--- trunk/src/menu_events.hpp (original)
+++ trunk/src/menu_events.hpp Tue Mar 31 23:19:53 2009
@@ -117,7 +117,7 @@
        void do_command(const std::string& str, const unsigned int team_num, 
mouse_handler& mousehandler);
        void do_ai_formula(const std::string& str, const unsigned int 
team_num, mouse_handler& mousehandler);
        void clear_undo_stack(const unsigned int team_num);
-       void autosave(const std::string &label, unsigned turn, const config 
&starting_pos) const;
+       void autosave(unsigned turn) const;
        bool has_team() const;
 protected:
        void add_chat_message(const time_t& time, const std::string& speaker,

Modified: trunk/src/playsingle_controller.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/playsingle_controller.cpp?rev=34361&r1=34360&r2=34361&view=diff
==============================================================================
--- trunk/src/playsingle_controller.cpp (original)
+++ trunk/src/playsingle_controller.cpp Tue Mar 31 23:19:53 2009
@@ -664,7 +664,7 @@
        gui_->draw(true,true);
 
        if (save) {
-               menu_handler_.autosave(gamestate_.label, status_.turn(), 
gamestate_.starting_pos);
+               menu_handler_.autosave(status_.turn());
        }
 
        if(preferences::turn_bell()) {

Added: trunk/src/savegame.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/savegame.cpp?rev=34361&view=auto
==============================================================================
--- trunk/src/savegame.cpp (added)
+++ trunk/src/savegame.cpp Tue Mar 31 23:19:53 2009
@@ -1,0 +1,101 @@
+/* $Id$ */
+/*
+   Copyright (C) 2003 - 2009 by Jörg Hinrichs, refactored from various
+   places formerly created by David White <dave@xxxxxxxxxxxxx>
+   Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License version 2
+   or at your option any later version.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY.
+
+   See the COPYING file for more details.
+*/
+
+#include "savegame.hpp"
+
+#include "gettext.hpp"
+#include "log.hpp"
+#include "preferences_display.hpp"
+#include "serialization/binary_or_text.hpp"
+
+#define LOG_SAVE LOG_STREAM(info, engine)
+
+// Prototypes
+
+// Interactive replay save
+void save_replay(std::string& filename, game_state& gamestate)
+{
+       // Create an empty snapshot
+       config snapshot;
+
+       // FIXME
+       // Doesn't make too much sense at the moment but will, once
+       // including the savegame dialog here is done
+       save_game(filename, snapshot, gamestate);
+}
+
+// Automatic replay save at the end of the scenario
+void save_replay(game_state& gamestate)
+{
+       // FIXME:
+       // We create a copy here in order to avoid having to change the 
original
+       // gamestate. Once we made sure the player information is within
+       // [replay_start] only, this whole stuff is no longer needed.
+       game_state saveInfo = gamestate;
+
+       // If the starting position contains player information, use this for
+       // the replay savegame (this originally comes from the gamestate 
constructor,
+       // where the player stuff is added to the starting position to be 
used here.
+       config::child_itors player_list = 
gamestate.starting_pos.child_range("player");
+       if (player_list.first != player_list.second) {
+               saveInfo.players.clear();
+               saveInfo.load_recall_list(player_list);
+       }
+
+       // Create an empty snapshot
+       config snapshot;
+
+       std::string filename = gamestate.label + _(" replay");
+       save_game(filename, snapshot, saveInfo);
+
+       // clear replay data and snapshot for the next scenario
+       gamestate.replay_data = config();
+       gamestate.snapshot = config();
+}
+
+/** Autosave */
+void save_autosave(unsigned turn, const config& snapshot, game_state& 
gamestate)
+{
+       std::string filename;
+       if (gamestate.label.empty())
+               filename = _("Auto-Save");
+       else
+               filename = gamestate.label + "-" + _("Auto-Save") + 
lexical_cast<std::string>(turn);
+
+       save_game(filename, snapshot, gamestate);
+}
+
+void save_game(std::string& filename, const config& snapshot, game_state& 
gamestate)
+{
+       LOG_SAVE << "savegame::save_game";
+
+       if(preferences::compress_saves()) {
+               filename += ".gz";
+       }
+
+       std::stringstream ss;
+       {
+               config_writer out(ss, preferences::compress_saves());
+               ::write_game(out, snapshot, gamestate);
+               finish_save_game(out, gamestate, gamestate.label);
+       }
+       scoped_ostream os(open_save_game(filename));
+       (*os) << ss.str();
+
+       if (!os->good()) {
+               throw game::save_game_failed(_("Could not write to file"));
+       }
+}
+

Propchange: trunk/src/savegame.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/src/savegame.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: trunk/src/savegame.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/savegame.hpp?rev=34361&view=auto
==============================================================================
--- trunk/src/savegame.hpp (added)
+++ trunk/src/savegame.hpp Tue Mar 31 23:19:53 2009
@@ -1,0 +1,36 @@
+/* $Id$ */
+/*
+   Copyright (C) 2003 - 2009 by Jörg Hinrichs, refactored from various
+   places formerly created by David White <dave@xxxxxxxxxxxxx>
+   Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License version 2
+   or at your option any later version.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY.
+
+   See the COPYING file for more details.
+*/
+
+#ifndef SAVEGAME_H_INCLUDED
+#define SAVEGAME_H_INCLUDED
+
+#include "global.hpp"
+#include "gamestatus.hpp"
+
+#include <string>
+
+/** Autosave */
+void save_autosave(unsigned turn, const config& snapshot, game_state& 
gamestate);
+
+/** Normal midgame save */
+void save_game(std::string& filename, const config& snapshot, game_state& 
gamestate);
+
+/** Replay save created by player interaction */
+void save_replay(std::string& filename, game_state& gamestate);
+
+/** Replay save, created automatically at the end of a scenario */
+void save_replay(game_state& gamestate);
+
+#endif

Propchange: trunk/src/savegame.hpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/src/savegame.hpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision




Related Messages


Powered by MHonArc, Updated Tue Mar 31 23:40:36 2009