- moved levelsubset into an own file
authorIngo Ruhnke <grumbel@gmx.de>
Tue, 15 Jun 2004 22:56:42 +0000 (22:56 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Tue, 15 Jun 2004 22:56:42 +0000 (22:56 +0000)
SVN-Revision: 1501

src/level_subset.cpp [new file with mode: 0644]
src/level_subset.h [new file with mode: 0644]

diff --git a/src/level_subset.cpp b/src/level_subset.cpp
new file mode 100644 (file)
index 0000000..f401121
--- /dev/null
@@ -0,0 +1,205 @@
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
+//
+//  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 "setup.h"
+#include "level.h"
+#include "globals.h"
+#include "screen/surface.h"
+#include "level_subset.h"
+
+LevelSubset::LevelSubset()
+    : image(0), levels(0)
+{
+}
+
+LevelSubset::~LevelSubset()
+{
+  delete image;
+}
+
+void LevelSubset::create(const std::string& subset_name)
+{
+  Level new_lev;
+  LevelSubset new_subset;
+  new_subset.name = subset_name;
+  new_subset.title = "Unknown Title";
+  new_subset.description = "No description so far.";
+  new_subset.save();
+  //new_lev.save(subset_name, 1, 0);
+}
+
+void LevelSubset::parse (lisp_object_t* cursor)
+{
+  while(!lisp_nil_p(cursor))
+    {
+      lisp_object_t* cur = lisp_car(cursor);
+      char *s;
+
+      if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
+        {
+          printf("Not good");
+        }
+      else
+        {
+          if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
+            {
+              if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
+                {
+                  title = s;
+                }
+            }
+          else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
+            {
+              if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
+                {
+                  description = s;
+                }
+            }
+        }
+      cursor = lisp_cdr (cursor);
+    }
+}
+
+void LevelSubset::load(const char* subset)
+{
+  FILE* fi;
+  char filename[1024];
+  char str[1024];
+  int i;
+  lisp_object_t* root_obj = 0;
+
+  name = subset;
+
+  snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
+  if(!faccessible(filename))
+    snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
+  if(faccessible(filename))
+    {
+      fi = fopen(filename, "r");
+      if (fi == NULL)
+        {
+          perror(filename);
+        }
+      lisp_stream_t stream;
+      lisp_stream_init_file (&stream, fi);
+      root_obj = lisp_read (&stream);
+
+      if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
+        {
+          printf("World: Parse Error in file %s", filename);
+        }
+
+      lisp_object_t* cur = lisp_car(root_obj);
+
+      if (!lisp_symbol_p (cur))
+        {
+          printf("World: Read error in %s",filename);
+        }
+
+      if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
+        {
+          parse(lisp_cdr(root_obj));
+
+        }
+
+      lisp_free(root_obj);
+      fclose(fi);
+
+      snprintf(str, 1024, "%s.png", filename);
+      if(faccessible(str))
+        {
+          delete image;
+          image = new Surface(str,IGNORE_ALPHA);
+        }
+      else
+        {
+          snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
+          delete image;
+          image = new Surface(filename,IGNORE_ALPHA);
+        }
+    }
+
+  for(i=1; i != -1; ++i)
+    {
+      /* Get the number of levels in this subset */
+      snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
+      if(!faccessible(filename))
+        {
+          snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
+          if(!faccessible(filename))
+            break;
+        }
+    }
+  levels = --i;
+}
+
+void
+LevelSubset::save()
+{
+  FILE* fi;
+  std::string filename;
+
+  /* Save data file: */
+  filename = "/levels/" + name + "/";
+
+  fcreatedir(filename.c_str());
+  filename = std::string(st_dir) + "/levels/" + name + "/info";
+  if(!fwriteable(filename.c_str()))
+    filename = datadir + "/levels/" + name + "/info";
+  if(fwriteable(filename.c_str()))
+    {
+      fi = fopen(filename.c_str(), "w");
+      if (fi == NULL)
+        {
+          perror(filename.c_str());
+        }
+
+      /* Write header: */
+      fprintf(fi,";SuperTux-Level-Subset\n");
+      fprintf(fi,"(supertux-level-subset\n");
+
+      /* Save title info: */
+      fprintf(fi,"  (title \"%s\")\n", title.c_str());
+
+      /* Save the description: */
+      fprintf(fi,"  (description \"%s\")\n", description.c_str());
+
+      fprintf( fi,")");
+      fclose(fi);
+    }
+}
+
+std::string
+LevelSubset::get_level_filename(unsigned int num)
+{
+  char filename[1024];
+                                                                                
+  // Load data file:
+  snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir,
+      name.c_str(), num);
+  if(!faccessible(filename))
+    snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(),
+        name.c_str(), num);
+
+  return std::string(filename);
+}
+
+/* EOF */
+
diff --git a/src/level_subset.h b/src/level_subset.h
new file mode 100644 (file)
index 0000000..45be7ae
--- /dev/null
@@ -0,0 +1,57 @@
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
+//
+//  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 SUPERTUX_LEVEL_SUBSET_H
+#define SUPERTUX_LEVEL_SUBSET_H
+
+#include <string>
+#include "lispreader.h"
+
+class Surface;
+
+/** This type holds meta-information about a level-subset. 
+    It could be extended to handle manipulation of subsets. */
+class LevelSubset
+{
+public:
+  LevelSubset();
+  ~LevelSubset();
+
+  static void create(const std::string& subset_name);
+  void load(const char* subset);
+  void save();
+
+  std::string get_level_filename(unsigned int i);
+
+  std::string name;
+  std::string title;
+  std::string description;
+  Surface* image;
+  int levels;
+private:
+  void parse(lisp_object_t* cursor);
+};
+
+#endif
+
+/* Local Variables: */
+/* mode:c++ */
+/* End: */