pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] CVS: Games/Pingus/src savegame.cxx,NONE,1.1 savegame.hxx,N


From: grumbel
Subject: [Pingus-CVS] CVS: Games/Pingus/src savegame.cxx,NONE,1.1 savegame.hxx,NONE,1.1 savegame_manager.cxx,NONE,1.1 savegame_manager.hxx,NONE,1.1 Makefile.am,1.139,1.140 game_session.cxx,1.35,1.36 pingus_main.cxx,1.51,1.52 xml_file_reader.hxx,1.3,1.4 xml_file_writer.hxx,1.3,1.4
Date: 26 Mar 2003 12:01:19 -0000

Update of /var/lib/cvs/Games/Pingus/src
In directory dark:/tmp/cvs-serv10395/src

Modified Files:
        Makefile.am game_session.cxx pingus_main.cxx 
        xml_file_reader.hxx xml_file_writer.hxx 
Added Files:
        savegame.cxx savegame.hxx savegame_manager.cxx 
        savegame_manager.hxx 
Log Message:
added savegame support

--- NEW FILE: savegame.cxx ---
//  $Id: savegame.cxx,v 1.1 2003/03/26 12:01:17 grumbel Exp $
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 2002 Ingo Ruhnke <address@hidden>
//
//  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 the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include <iostream>
#include "xml_file_reader.hxx"
#include "xml_file_writer.hxx"
#include "string_converter.hxx"
#include "savegame.hxx"

std::string
Savegame::status_to_string (Status status)
{
  switch (status) 
    {
    case FINISHED:
      return "finished";

    case ACCESSIBLE:
      return "accessible";

    case NONE:
    default:
      return "none";
    }
}

Savegame::Status
Savegame::string_to_status (std::string str)
{
  str = string_downcase(str);
  if (str == "accessible")
    return ACCESSIBLE;
  else if (str == "finished")
    return FINISHED;
  else if (str == "none")
    return NONE;
  else 
    return NONE;
}

Savegame::Savegame(xmlDocPtr doc, xmlNodePtr node)
{
  read_xml(doc, node);
}

Savegame::Savegame(std::string arg_levelname, Status arg_status, int arg_time, 
int arg_saved_pingus)
  : levelname(arg_levelname),
    status(arg_status),
    time(arg_time),
    saved_pingus(arg_saved_pingus)
{
}

void
Savegame::write_xml(std::ostream& xml)
{
  XMLFileWriter writer(xml);
  writer.begin_section("level");
  writer.write_string ("name", levelname);
  writer.write_enum   ("status", status, status_to_string);
  writer.write_int    ("time", time);
  writer.write_int    ("saved-pingus", saved_pingus);
  writer.end_section();
}

void
Savegame::read_xml (xmlDocPtr doc, xmlNodePtr node)
{
  XMLFileReader reader(doc, node);
  reader.read_string ("name", levelname);
  reader.read_enum   ("status", status, string_to_status);
  reader.read_int    ("time", time);
  reader.read_int    ("saved-pingus", saved_pingus);

}

/* EOF */

--- NEW FILE: savegame.hxx ---
//  $Id: savegame.hxx,v 1.1 2003/03/26 12:01:17 grumbel Exp $
// 
//  Pingus - A free Lemmings clone
//  Copyright (C) 2002 Ingo Ruhnke <address@hidden>
//
//  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 the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#ifndef HEADER_PINGUS_SAVEGAME_HXX
#define HEADER_PINGUS_SAVEGAME_HXX

#include <string>
#include "libxmlfwd.hxx"

/** The Savegame class holds savegame informations for a single
    level */
class Savegame
{
private:
public:
  enum Status { FINISHED,   // level is successfully finished
                ACCESSIBLE, // level is accessible and can be played 
                NONE};      // level is not finished and cannot be accessed
  static std::string status_to_string (Status s);
  static Status string_to_status (std::string s);


  std::string levelname;
  Status status;

  /** Time needed to finish the level, only valid if status == FINISHED */
  int time;

  /** Number of Pingus that where saved while playing the level */
  int saved_pingus;

  Savegame() {}
  Savegame(xmlDocPtr doc, xmlNodePtr node);
  Savegame(std::string arg_levelname, Status arg_status, int arg_time, int 
arg_saved_pingus);

  void read_xml (xmlDocPtr doc, xmlNodePtr node);
  void write_xml(std::ostream& xml);
};

#endif

/* EOF */

--- NEW FILE: savegame_manager.cxx ---
//  $Id: savegame_manager.cxx,v 1.1 2003/03/26 12:01:17 grumbel Exp $
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 2002 Ingo Ruhnke <address@hidden>
//
//  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 the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include <iostream>
#include <fstream>
#include "system.hxx"
#include "xml_helper.hxx"
#include "savegame_manager.hxx"

SavegameManager* SavegameManager::instance_;

SavegameManager*
SavegameManager::instance()
{
  if (instance_)
    return instance_; 
  else
    return (instance_ = new SavegameManager(System::get_statdir() + 
"stat/savegame.xml")); 
}

SavegameManager::SavegameManager()
{
}

SavegameManager::SavegameManager(const std::string& arg_filename)
  : filename(arg_filename)
{
  xmlDocPtr doc = xmlParseFile(filename.c_str());
  
  if (!doc)
    {
      std::cout << "SavegameManager: Couldn't find savegame file '" << filename
                << "', starting with a empty one." << std::endl;
    }
  else
    {
      xmlNodePtr cur = doc->ROOT;
  
      cur = XMLhelper::skip_blank(cur);
  
      if (cur && XMLhelper::equal_str(cur->name, "pingus-savegame"))
        {
          cur = XMLhelper::skip_blank(cur);
          cur = cur->children;
          cur = XMLhelper::skip_blank(cur);

          while(cur)
            {
              if (XMLhelper::equal_str(cur->name, "level"))
                {
                  Savegame* savegame = new Savegame(doc, cur);
                  SavegameTable::iterator i = 
savegames.find(savegame->levelname);

                  if (i != savegames.end())
                    {
                      std::cout << "SavegameManager: name collision: " << 
savegame->levelname << std::endl;
                      delete i->second;
                      i->second = savegame;
                    }
                  else
                    {
                      std::cout << "SavegameManager: Loading savegame for: " << 
savegame->levelname << std::endl;
                      savegames[savegame->levelname] = savegame;
                    }
                }
              else
                {
                  std::cout << "SavegameManager: Unknownen tag: " << cur->name 
<< std::endl; 
                }
          
              cur = cur->next;
              cur = XMLhelper::skip_blank(cur);
            }
        }

  
      xmlFreeDoc(doc);
    }
}

Savegame*
SavegameManager::get(const std::string& levelname)
{
  SavegameTable::iterator i = savegames.find(levelname);
  if (i == savegames.end())
    {
      return 0;
    }
  else
    {
      return i->second;
    }
}

void
SavegameManager::store(Savegame& arg_savegame)
{
  Savegame* savegame = new Savegame(arg_savegame);
  SavegameTable::iterator i = savegames.find(savegame->levelname);
  if (i == savegames.end())
    {
      savegames[savegame->levelname] = savegame;
    }
  else
    {
      delete i->second;
      i->second = savegame;
    }
  
  flush();
}

void
SavegameManager::flush()
{
  std::ofstream xml(filename.c_str());
  xml << "<?xml version=\"1.0\"  encoding=\"ISO-8859-1\"?>\n\n"
      << "<pingus-savegame>\n";
    
  for(SavegameTable::iterator i = savegames.begin(); i != savegames.end(); ++i)
    {
      assert(i->second);
      i->second->write_xml(xml);
    }

  xml << "</pingus-savegame>\n" 
      << std::endl;
}

/* EOF */

--- NEW FILE: savegame_manager.hxx ---
//  $Id: savegame_manager.hxx,v 1.1 2003/03/26 12:01:17 grumbel Exp $
// 
//  Pingus - A free Lemmings clone
//  Copyright (C) 2002 Ingo Ruhnke <address@hidden>
//
//  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 the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#ifndef HEADER_SAVEGAME_MANAGER_HXX
#define HEADER_SAVEGAME_MANAGER_HXX

#include <map>
#include "savegame.hxx"

/** */
class SavegameManager
{
private:
  static SavegameManager* instance_; 
  typedef std::map<std::string, Savegame*> SavegameTable;

  std::string filename;
  SavegameTable savegames;
public:
  static SavegameManager* instance();

  SavegameManager();
  SavegameManager(const std::string& filename);
  ~SavegameManager();
  
  Savegame* get(const std::string& levelname);
  void store(Savegame&);
  
  /** Write the current savegames down to file */
  void flush();
private:
  SavegameManager (const SavegameManager&);
  SavegameManager& operator= (const SavegameManager&);
};

#endif

/* EOF */

Index: Makefile.am
===================================================================
RCS file: /var/lib/cvs/Games/Pingus/src/Makefile.am,v
retrieving revision 1.139
retrieving revision 1.140
diff -u -d -r1.139 -r1.140
--- Makefile.am 21 Mar 2003 22:08:06 -0000      1.139
+++ Makefile.am 26 Mar 2003 12:01:17 -0000      1.140
@@ -242,6 +242,10 @@
 result.hxx \
 result_screen.cxx \
 result_screen.hxx \
+savegame.hxx \
+savegame.cxx \
+savegame_manager.hxx \
+savegame_manager.cxx \
 screenshot.cxx \
 screenshot.hxx \
 server.cxx \

Index: game_session.cxx
===================================================================
RCS file: /var/lib/cvs/Games/Pingus/src/game_session.cxx,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -d -r1.35 -r1.36
--- game_session.cxx    25 Mar 2003 00:37:44 -0000      1.35
+++ game_session.cxx    26 Mar 2003 12:01:17 -0000      1.36
@@ -29,6 +29,7 @@
 #include "pingu_holder.hxx"
 #include "world.hxx"
 #include "result_screen.hxx"
+#include "savegame_manager.hxx"
 #include "globals.hxx"
 
 PingusGameSession::PingusGameSession (PLFHandle arg_plf, bool 
arg_show_result_screen)
@@ -112,6 +113,19 @@
 
       result.max_time  = server->get_plf()->get_time();
       result.used_time = server->get_time();
+
+      { // Write the savegame
+        Savegame savegame;
+        savegame.levelname    = result.plf->get_filename();
+        savegame.time         = result.used_time;
+        savegame.saved_pingus = result.saved;
+        if (result.saved >= result.needed)
+          savegame.status     = Savegame::FINISHED;
+        else
+          savegame.status     = Savegame::ACCESSIBLE;
+
+        SavegameManager::instance()->store(savegame);
+      }
 
       if (show_result_screen)
         ScreenManager::instance()->replace_screen(new ResultScreen(result));

Index: pingus_main.cxx
===================================================================
RCS file: /var/lib/cvs/Games/Pingus/src/pingus_main.cxx,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -d -r1.51 -r1.52
--- pingus_main.cxx     21 Mar 2003 22:08:06 -0000      1.51
+++ pingus_main.cxx     26 Mar 2003 12:01:17 -0000      1.52
@@ -73,6 +73,7 @@
 #include "game_session.hxx"
 #include "story_screen.hxx"
 #include "start_screen.hxx"
+#include "savegame_manager.hxx"
 #include "demo_session.hxx"
 #include "debug.hxx"
 #include "editor/editor.hxx"
@@ -893,6 +894,8 @@
 
   fps_counter.init();
   console.init();
+
+  SavegameManager::instance();
  
   // FIXME: See action_data.hxx, a bit ugly
   init_default_actions();

Index: xml_file_reader.hxx
===================================================================
RCS file: /var/lib/cvs/Games/Pingus/src/xml_file_reader.hxx,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- xml_file_reader.hxx 18 Feb 2003 10:14:52 -0000      1.3
+++ xml_file_reader.hxx 26 Mar 2003 12:01:17 -0000      1.4
@@ -54,6 +54,20 @@
   bool read_string(const char* name, std::string&);
   bool read_vector(const char* name, Vector&);
 
+  template<class E, class T>
+  bool read_enum  (const char* name, E& value, T enum2string)
+  {
+    xmlNodePtr node = find_node(name);
+    
+    if (node)
+      {
+        value = enum2string(XMLhelper::parse_string(doc, node));
+        return true;
+      }
+
+    return false;
+  }
+
   bool read_section(const char* name, XMLFileReader&);
 
 private:

Index: xml_file_writer.hxx
===================================================================
RCS file: /var/lib/cvs/Games/Pingus/src/xml_file_writer.hxx,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- xml_file_writer.hxx 18 Feb 2003 01:23:51 -0000      1.3
+++ xml_file_writer.hxx 26 Mar 2003 12:01:17 -0000      1.4
@@ -49,6 +49,12 @@
   void write_color  (const char* name, const Color&);
   void write_string (const char* name, const std::string&);
   void write_vector (const char* name, const Vector&);
+
+  template<class E, class F>
+  void write_enum (const char* name, E value, F enum2string) 
+  {
+    (*out) << "<" << name << ">" << enum2string(value) << "</" << name << 
">\n";
+  }
   
 private:
   XMLFileWriter (const XMLFileWriter&);





reply via email to

[Prev in Thread] Current Thread [Next in Thread]