windstille-devel
[Top][All Lists]
Advanced

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

[Windstille-devel] rev 394 - in trunk/src: . SuperTux scripting


From: Ingo Ruhnke
Subject: [Windstille-devel] rev 394 - in trunk/src: . SuperTux scripting
Date: Sun, 06 Jun 2004 15:10:13 +0200

Author: grumbel
Date: 2004-06-06 15:10:12 +0200 (Sun, 06 Jun 2004)
New Revision: 394

Added:
   trunk/src/ruby_functor.cxx
   trunk/src/ruby_functor.hxx
   trunk/src/ruby_meta_data.cxx
   trunk/src/ruby_meta_data.hxx
   trunk/src/ruby_object.cxx
   trunk/src/ruby_object.hxx
   trunk/src/ruby_sexpr_parser.cxx
   trunk/src/ruby_sexpr_parser.hxx
   trunk/src/sexpr.rb
   trunk/src/supertux.rb
Modified:
   trunk/src/SConstruct
   trunk/src/SuperTux/level.py
   trunk/src/SuperTux/sector.py
   trunk/src/flexlay_wrap.i
   trunk/src/scripting/editor.cxx
   trunk/src/scripting/editor.hxx
   trunk/src/sexpr_parser.hxx
Log:
- converted some stuff to ruby

Modified: trunk/src/SConstruct
===================================================================
--- trunk/src/SConstruct        2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/SConstruct        2004-06-06 13:10:12 UTC (rev 394)
@@ -151,6 +151,10 @@
     target = '_flexlay_ruby_wrap.so',
     source = [
     'flexlay_ruby_wrap.cxx',
+    'ruby_functor.cxx',
+    'ruby_meta_data.cxx',
+    'ruby_sexpr_parser.cxx',
+    'ruby_object.cxx'
     ],
     CPPPATH=['/home/ingo/run/ClanLib-0.7-current//include/ClanLib-0.7/',
             '/usr/lib/ruby/1.8/i386-linux/',

Modified: trunk/src/SuperTux/level.py
===================================================================
--- trunk/src/SuperTux/level.py 2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/SuperTux/level.py 2004-06-06 13:10:12 UTC (rev 394)
@@ -1,3 +1,5 @@
+# translated to ruby
+
 import os
 import sys
 import code

Modified: trunk/src/SuperTux/sector.py
===================================================================
--- trunk/src/SuperTux/sector.py        2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/SuperTux/sector.py        2004-06-06 13:10:12 UTC (rev 394)
@@ -1,3 +1,5 @@
+# translated to ruby
+
 class Sector:
     parent    = None
     name      = None

Modified: trunk/src/flexlay_wrap.i
===================================================================
--- trunk/src/flexlay_wrap.i    2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/flexlay_wrap.i    2004-06-06 13:10:12 UTC (rev 394)
@@ -50,13 +50,19 @@
 #include "objmap_path_node.hxx"
 
 #include "netpanzer.hxx" 
+#include "scripting/editor.hxx"
 
 #ifdef SWIGPYTHON
 #include "sexpr_parser.hxx"
-#include "scripting/editor.hxx"
 #include "python_meta_data.hxx"
 #include "python_functor.hxx"
 #endif
+
+#ifdef SWIGRUBY
+#include "ruby_sexpr_parser.hxx"
+#include "ruby_meta_data.hxx"
+#include "ruby_functor.hxx"
+#endif
 %}
 
 %include "std_string.i"
@@ -105,13 +111,19 @@
 %include "zoom_tool.hxx" 
 %include "graphic_context_state.hxx"
 %include "objmap_path_node.hxx"
+%include "scripting/editor.hxx"
 
 %include "netpanzer.hxx" 
 
 #ifdef SWIGPYTHON
-%include "scripting/editor.hxx"
 %include "python_meta_data.hxx"
 %include "sexpr_parser.hxx"
 #endif
 
+#ifdef SWIGRUBY
+%include "ruby_meta_data.hxx"
+%include "ruby_sexpr_parser.hxx"
+#endif
+
+
 /* EOF */

Added: trunk/src/ruby_functor.cxx
===================================================================
--- trunk/src/ruby_functor.cxx  2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/ruby_functor.cxx  2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,49 @@
+//  $Id$
+//
+//  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 "ruby_functor.hxx"
+
+RubyFunctor::RubyFunctor(const RubyObject& val_)
+  : val(val_)
+{
+}
+
+RubyFunctor::~RubyFunctor()
+{
+}
+
+void
+RubyFunctor::operator()()
+{
+  rb_funcall(val.ptr(), rb_intern("call"), 0);
+}
+
+void
+RubyFunctor::operator()(int i)
+{
+  rb_funcall(val.ptr(), rb_intern("call"), 1, INT2FIX(i));
+}
+
+void
+RubyFunctor::operator()(int x, int y)
+{
+  rb_funcall(val.ptr(), rb_intern("call"), 2, INT2FIX(x), INT2FIX(y));
+}
+
+/* EOF */

Added: trunk/src/ruby_functor.hxx
===================================================================
--- trunk/src/ruby_functor.hxx  2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/ruby_functor.hxx  2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,43 @@
+//  $Id$
+// 
+//  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_RUBY_FUNCTOR_HXX
+#define HEADER_RUBY_FUNCTOR_HXX
+
+#include "ruby.h"
+#include "ruby_object.hxx"
+
+/** */
+class RubyFunctor
+{
+private:
+  RubyObject val;
+
+public:
+  RubyFunctor(const RubyObject& val_);  
+  ~RubyFunctor();
+
+  void operator()();
+  void operator()(int i);
+  void operator()(int x, int y);
+};
+
+#endif
+
+/* EOF */

Added: trunk/src/ruby_meta_data.cxx
===================================================================
--- trunk/src/ruby_meta_data.cxx        2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/ruby_meta_data.cxx        2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,46 @@
+//  $Id$
+//
+//  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 "ruby_object.hxx"
+#include "meta_data_impl.hxx"
+#include "ruby_meta_data.hxx"
+
+typedef MetaDataGeneric<RubyObject> RubyMetaData;
+
+MetaData  make_metadata(VALUE obj)
+{
+  return MetaData(SharedPtr<MetaDataImpl>(new RubyMetaData(RubyObject(obj))));
+}
+
+VALUE get_ruby_object(const MetaData& data_obj)
+{
+  MetaDataImpl* data = data_obj.get_impl().get();
+
+  if (data)
+    {
+      RubyMetaData* rbdata = dynamic_cast<RubyMetaData*>(data);
+      if (rbdata)
+        {
+          return rbdata->data.ptr();
+        }
+    }
+  return Qnil;
+}
+
+/* EOF */

Added: trunk/src/ruby_meta_data.hxx
===================================================================
--- trunk/src/ruby_meta_data.hxx        2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/ruby_meta_data.hxx        2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,31 @@
+//  $Id$
+// 
+//  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_RUBY_META_DATA_HXX
+#define HEADER_RUBY_META_DATA_HXX
+
+#include "ruby.h"
+#include "meta_data.hxx"
+
+MetaData  make_metadata(VALUE obj);
+VALUE get_ruby_object(const MetaData& data);
+
+#endif
+
+/* EOF */

Added: trunk/src/ruby_object.cxx
===================================================================
--- trunk/src/ruby_object.cxx   2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/ruby_object.cxx   2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,57 @@
+//  $Id$
+//
+//  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 "ruby_object.hxx"
+
+RubyObject::RubyObject(VALUE val_)
+  : val(val_)
+{
+  rb_gc_register_address(&val);
+}
+
+RubyObject::RubyObject(const RubyObject& copy)
+  : val(copy.val)
+{
+  rb_gc_register_address(&val);
+}
+
+RubyObject&
+RubyObject::operator= (const RubyObject& copy)
+{
+  if (this != &copy)
+    {
+      rb_gc_unregister_address(&val);
+      val = copy.val;
+      rb_gc_register_address(&val);
+    }
+  return *this;
+}
+
+RubyObject::~RubyObject()
+{
+  rb_gc_unregister_address(&val);
+}
+
+VALUE
+RubyObject::ptr() 
+{
+  return val; 
+}
+
+/* EOF */

Added: trunk/src/ruby_object.hxx
===================================================================
--- trunk/src/ruby_object.hxx   2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/ruby_object.hxx   2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,42 @@
+//  $Id$
+// 
+//  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_RUBY_OBJECT_HXX
+#define HEADER_RUBY_OBJECT_HXX
+
+#include "ruby.h"
+
+/** */
+class RubyObject
+{
+private:
+  VALUE val;
+
+public:
+  RubyObject(VALUE val_);
+  RubyObject(const RubyObject&);
+  RubyObject& operator= (const RubyObject&);
+  ~RubyObject();
+  
+  VALUE ptr();
+};
+
+#endif
+
+/* EOF */

Added: trunk/src/ruby_sexpr_parser.cxx
===================================================================
--- trunk/src/ruby_sexpr_parser.cxx     2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/ruby_sexpr_parser.cxx     2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,83 @@
+//  $Id$
+//
+//  Flexlay - A Generic 2D Game Editor
+//  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 "lispreader.hxx"
+#include "ruby_sexpr_parser.hxx"
+
+VALUE
+build_py_sexpr(lisp_object_t* cur)
+{
+  if (lisp_cons_p(cur))
+    {
+      VALUE lst = rb_ary_new();
+  
+      while (cur)
+        {
+          rb_ary_push(lst, build_py_sexpr(lisp_car(cur)));
+          cur = lisp_cdr(cur);
+        }
+      
+      return lst;
+    }
+  else if (lisp_string_p(cur))
+    {
+      return rb_str_new2(lisp_string(cur));
+    }
+  else if (lisp_symbol_p(cur))
+    {
+      return rb_str_new2(lisp_symbol(cur));
+    }
+  else if (lisp_integer_p(cur))
+    {
+      return INT2NUM(lisp_integer(cur));
+    }
+  else if (lisp_real_p(cur))
+    {
+      return rb_float_new(lisp_real(cur));
+    }
+  else if (lisp_boolean_p(cur))
+    {
+      if (lisp_boolean(cur))
+        return Qtrue;
+      else
+        return Qfalse;
+    }
+  else
+    {
+      return Qnil;
+    }
+}
+
+VALUE sexpr_read_from_file(const char* filename)
+{
+  lisp_object_t* cur = lisp_read_from_file(filename);
+
+  if (cur)
+    {
+      VALUE obj = build_py_sexpr(cur);
+      lisp_free(cur);
+      return obj;
+    }
+  else
+    {
+      return Qnil;
+    }
+}
+
+/* EOF */

Added: trunk/src/ruby_sexpr_parser.hxx
===================================================================
--- trunk/src/ruby_sexpr_parser.hxx     2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/ruby_sexpr_parser.hxx     2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,29 @@
+//  $Id$
+// 
+//  Flexlay - A Generic 2D Game Editor
+//  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_RUBY_SEXPR_PARSER_HXX
+#define HEADER_RUBY_SEXPR_PARSER_HXX
+
+#include "ruby.h"
+
+VALUE sexpr_read_from_file(const char* filename);
+
+#endif
+
+/* EOF */

Modified: trunk/src/scripting/editor.cxx
===================================================================
--- trunk/src/scripting/editor.cxx      2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/scripting/editor.cxx      2004-06-06 13:10:12 UTC (rev 394)
@@ -51,11 +51,12 @@
   return CL_Sprite(desc);
 }
 
+/*
 Tile
 make_tile(const char* filename, 
           unsigned char red, unsigned char green, unsigned char blue, unsigned 
char alpha)
 {
   return Tile(filename, CL_Color(red, green, blue, alpha));
-}
+}*/
 
 /* EOF */

Modified: trunk/src/scripting/editor.hxx
===================================================================
--- trunk/src/scripting/editor.hxx      2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/scripting/editor.hxx      2004-06-06 13:10:12 UTC (rev 394)
@@ -27,15 +27,17 @@
 #include <ClanLib/Signals/signal_v0.h>
 #include "../tile.hxx"
 
+#ifdef SWIGPYTHON
 #include "Python.h"
 
 void connect(CL_Signal_v0& sig, PyObject* obj);
 void connect_v1(CL_Signal_v1<int>& sig, PyObject* obj);
 void connect_v2(CL_Signal_v2<int, int>& sig, PyObject* obj);
+#endif
 
 CL_Sprite make_sprite(const std::string& filename);
-Tile make_tile(const char* filename, 
-               unsigned char red, unsigned char green, unsigned char blue, 
unsigned char alpha);
+/*Tile make_tile(const char* filename, 
+  unsigned char red, unsigned char green, unsigned char blue, unsigned char 
alpha);*/
 
 #endif
 

Added: trunk/src/sexpr.rb
===================================================================
--- trunk/src/sexpr.rb  2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/sexpr.rb  2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,58 @@
+##  $Id$
+## 
+##  Flexlay - A Generic 2D Game Editor
+##  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.
+
+def assoc_ref(lst, str)
+  if lst == []
+    return false
+  elsif lst[0][0] == str
+    return lst[0][1..-1]
+  else
+    return assoc_ref(lst[1..-1], str)
+  end
+end
+
+def sexpr_filter(name, tree)
+  ret = []
+  for i in tree
+    if i[0] == name
+      ret.push(*i[1..-1])
+    end
+  end
+    
+  return ret
+end
+
+def get_value_from_tree(spec, tree, default)
+    if spec == []
+      return tree
+    elsif spec == ['_']
+      return tree[0]
+    elsif tree == []
+      return default
+    else
+      el = assoc_ref(tree, spec[0])
+      if el
+        return get_value_from_tree(spec[1..-1], el, default)
+      else
+        return default
+      end
+    end
+end
+
+# EOF #

Modified: trunk/src/sexpr_parser.hxx
===================================================================
--- trunk/src/sexpr_parser.hxx  2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/sexpr_parser.hxx  2004-06-06 13:10:12 UTC (rev 394)
@@ -20,8 +20,8 @@
 #ifndef HEADER_SEXPR_PARSER_HXX
 #define HEADER_SEXPR_PARSER_HXX
 
+#include "lispreader.hxx"
 #include "Python.h"
-#include "lispreader.hxx"
 
 PyObject* sexpr_read_from_file(const char* filename);
 

Added: trunk/src/supertux.rb
===================================================================
--- trunk/src/supertux.rb       2004-06-05 23:39:25 UTC (rev 393)
+++ trunk/src/supertux.rb       2004-06-06 13:10:12 UTC (rev 394)
@@ -0,0 +1,527 @@
+# bla
+require "flexlay_wrap"
+include Flexlay_wrap
+
+require "sexpr.rb"
+
+#import os
+#import sys
+#import code
+#from flexlay import *
+#from sexpr   import *
+
+flexlay = Flexlay.new()
+flexlay.init()
+
+editor = Editor.new()
+gui = editor.get_gui_manager()
+
+myrect     = CL_Rect.new(CL_Point.new(0, 56), CL_Size.new(665, 488+56))
+editor_map = EditorMapComponent.new(myrect, gui.get_component())
+workspace  = Workspace.new(myrect.get_width(), myrect.get_height())
+editor_map.set_workspace(workspace)
+
+# Tools
+tilemap_paint_tool  = TileMapPaintTool.new()
+tilemap_select_tool = TileMapSelectTool.new()
+zoom_tool           = ZoomTool.new()
+objmap_select_tool  = ObjMapSelectTool.new()
+
+workspace.set_tool(tilemap_paint_tool.to_tool());
+
+#mysprite = make_sprite("../data/images/icons16/stock_paste-16.png")
+
+minimap = Minimap.new(editor_map, CL_Rect.new(CL_Point.new(3, 488+3-14), 
+                                              CL_Size.new(794-134-16, 50)), 
editor_map)
+
+class Config
+  attr_reader :datadir;
+
+  def initialize()
+    @datadir = "/home/ingo/cvs/supertux/supertux/data/"
+    end
+end
+
+$datadir = "/home/ingo/cvs/supertux/supertux/data/"
+
+def Tileset_load(tileset, filename)
+  "Load game tiles from filename into tileset"
+  tree = sexpr_read_from_file(filename)
+  tree = tree[1..-1]
+  for i in tree
+    if i[0] == "tile"
+      data  = i[1..-1]
+      id    = get_value_from_tree(['id', '_'], data, -1)
+      image = get_value_from_tree(['editor-images', '_'], data, false)
+      
+      if not(image)
+        image = get_value_from_tree(['images', '_'], data, "notile.png")
+      end
+      
+      if id != 0 # leave tile 0 transparent
+        tileset.add_tile(id,
+                         Tile.new($datadir + 'images/tilesets/' + image,
+                                  CL_Color.new(255,   0,   0, 128)))
+      end
+    end
+  end
+end
+
+$tileset = Tileset.new(32)
+Tileset_load($tileset, $datadir + "images/tilesets/supertux.stgt")
+
+class Level
+  version = 2
+  filename = nil
+  
+  name   = "no name"
+  author = "no author"
+  theme = "antarctica"
+  time = 999
+  music = "Mortimers_chipdisko.mod"
+  
+  objects = nil
+  camera  = nil
+  
+  sectors = nil
+  current_sector = nil
+  
+  def initialize(*params)
+    if params.length() == 2 then
+      # New Level
+      (width, height) = params
+      
+      @name   = "No Name"
+      @author = "No Author"
+      
+      @width  = width
+      @height = height
+      
+      @current_sector = Sector.new(self)
+      @current_sector.new_from_size(width, height)
+      @sectors = []
+      @sectors.push(@current_sector)
+      
+    elsif params.length() == 1 then
+      # Load Level from file
+      (@filename,) = params
+      
+      tree = sexpr_read_from_file(@filename)
+      if tree == nil
+        raise("Couldn't load level: ", filename)
+      end
+      
+      data = tree[1..-1]
+      
+      @version = get_value_from_tree(["version", "_"], data, 1)
+      
+      if (@version == 1) then
+        parse_v1(data)
+      else
+        parse_v2(data)
+      end
+    else
+      raise "Wrong arguments for SuperTux::___init__"
+    end
+  end
+  
+  def parse_v2(data)
+    @name    = get_value_from_tree(["name", "_"], data, "no name")
+    @author  = get_value_from_tree(["author", "_"], data, "no author")
+    @time    = int(get_value_from_tree(["time", "_"], data, "999"))
+    
+    self.sectors = []
+    for sec in sexpr_filter("sector", data)
+      sector = Sector.new(self)
+      sector.load_v2(sec)
+      self.sectors.append(sector)
+      if sector.name == "main"
+        self.current_sector = sector
+      end
+    end
+    
+    if self.current_sector == nil
+      print "Error: No main sector defined: ", sectors
+    end
+  end
+
+  def parse_v1(data)
+    sector = Sector.new(self)
+    sector.load_v1(data)
+    
+    @sectors = []
+    @sectors.append(sector)
+    @current_sector = sector
+    
+    @name    = get_value_from_tree(["name", "_"], data, "no name")
+    @author  = get_value_from_tree(["author", "_"], data, "no author")
+    @time    = int(get_value_from_tree(["time", "_"], data, "999"))       
+  end
+  
+  def resize(size, pos)
+    @width  = size.width
+    @height = size.height
+    @background.resize(size, pos)
+    @interactive.resize(size, pos)
+    @foreground.resize(size, pos)
+  end
+
+  def save(filename)
+    save_v2(filename)
+  end
+  
+  def save_v2(filename)
+    f = File.new(filename, "w")
+    f.write(";; Generated by Flexlay Editor\n" +
+                                                "(supertux-level\n")
+    f.write("  (version 2)\n")
+    f.write("  (name   \"%s\")\n" % @name)
+    f.write("  (author \"%s\")\n" % @author)
+    f.write("  (width  %s)\n"  % @width)
+    f.write("  (height  %s)\n" % @height)
+    
+    f.write("  (music  \"%s\")\n" % @music)
+    f.write("  (time   \"%s\")\n" % @time)
+    
+    f.write("  (gravity %d)\n" % @gravity)
+    
+    f.write("  (theme \"%s\")\n" % @theme)
+    
+    f.write("  (interactive-tm\n")
+    for i in interactive.get_data()
+      f.write("%d " % i)
+    end
+    f.write("  )\n\n")
+    
+    f.write("  (background-tm\n")
+    for i in background.get_data()
+      f.write("%d " % i)
+    end
+    f.write("  )\n\n")
+
+    f.write("  (foreground-tm\n")
+    for i in foreground.get_data()
+      f.write("%d " % i)
+    end
+    f.write("  )\n\n")
+
+    f.write("  (camera\n")
+    f.write("    (mode \"autoscroll\")\n")
+    f.write("    (path\n")
+    for obj in objects.get_objects()
+      pathnode = get_python_object(obj.get_metadata())
+      if (pathnode.__class__ == PathNode)
+        f.write("     (point (x %d) (y %d) (speed 1))\n" % obj.get_pos().x, 
obj.get_pos().y)
+      end
+    end
+    f.write("  ))\n\n")
+
+    f.write("  (objects\n")
+    for obj in self.objects.get_objects()
+      badguy = get_python_object(obj.get_metadata())
+      if (badguy.__class__ == BadGuy)
+        pos    = obj.get_pos()
+        if (badguy.type != "resetpoint")
+          f.write("     (%s (x %d) (y %d))\n" % badguy.type, int(pos.x), 
int(pos.y))
+        end
+      end
+    end
+    f.write("  )\n\n")
+
+    f.write("  (reset-points\n")
+    for obj in self.objects.get_objects()
+      badguy = get_python_object(obj.get_metadata())
+      if (badguy.__class__ == BadGuy)
+        pos    = obj.get_pos()
+        if (badguy.type == "resetpoint")
+          f.write("     (point (x %d) (y %d))\n" % int(pos.x), int(pos.y))
+        end
+      end
+    end
+    f.write("  )\n\n")
+    
+    f.write(" )\n\n;; EOF ;;\n")
+  end
+  
+  def save_v1(filename)
+    f = File.new(filename, "w")
+    f.write(";; Generated by Flexlay Editor\n" +
+                                                "(supertux-level\n")
+    f.write("  (version 1)\n")
+    f.write("  (name   \"%s\")\n" % @name)
+    f.write("  (author \"%s\")\n" % @author)
+    f.write("  (width  %s)\n"  % @width)
+    f.write("  (height  %s)\n" % @height)
+    
+    f.write("  (music  \"%s\")\n" % @music)
+    f.write("  (time   \"%s\")\n" % @time)
+    
+    f.write("  (gravity %d)\n" % @gravity)
+    
+    f.write("  (theme \"%s\")\n" % @theme)
+    
+    f.write("  (interactive-tm\n")
+    for i in self.interactive.get_data()
+      f.write("%d " % i)
+    end
+    f.write("  )\n\n")
+
+    f.write("  (background-tm\n")
+    for i in self.background.get_data()
+      f.write("%d " % i)
+    end
+    f.write("  )\n\n")
+
+    f.write("  (foreground-tm\n")
+    for i in self.foreground.get_data()
+      f.write("%d " % i)
+    end
+    f.write("  )\n\n")
+
+    f.write("  (camera\n")
+    f.write("    (mode \"autoscroll\")\n")
+    f.write("    (path\n")
+    for obj in self.objects.get_objects()
+      pathnode = get_python_object(obj.get_metadata())
+      if (pathnode.__class__ == PathNode)
+        f.write("     (point (x %d) (y %d) (speed 1))\n" % obj.get_pos().x, 
obj.get_pos().y)
+      end
+    end
+    f.write("  ))\n\n")
+    
+    f.write("  (objects\n")
+    for obj in self.objects.get_objects()
+      badguy = get_python_object(obj.get_metadata())
+      if (badguy.__class__ == BadGuy)
+        pos    = obj.get_pos()
+        if (badguy.type != "resetpoint")
+          f.write("     (%s (x %d) (y %d))\n" % badguy.type, int(pos.x), 
int(pos.y))
+        end
+      end
+    end
+    f.write("  )\n\n")
+    
+    f.write("  (reset-points\n")
+    for obj in self.objects.get_objects()
+      badguy = get_python_object(obj.get_metadata())
+      if (badguy.__class__ == BadGuy)
+        pos    = obj.get_pos()
+        if (badguy.type == "resetpoint")
+          f.write("     (point (x %d) (y %d))\n" % (pos.x.to_i), pos.y.to_i)
+        end
+      end
+    end
+    f.write("  )\n\n")
+    
+    f.write(" )\n\n;; EOF ;;\n")
+  end
+
+  def activate_sector(sector, workspace)
+    for sec in @sectors
+      if sec.name == sector
+        sec.activate(workspace)
+        break
+      end
+    end
+  end
+
+  def add_sector(sector)
+    @sectors.push(sector)
+  end
+
+  def get_sectors()
+    return @sectors.map {|sec| sec.name}
+  end
+
+  def activate(workspace)
+    @current_sector.activate(workspace)
+  end
+end
+
+class Sector
+  parent    = nil
+  name      = nil
+  song      = nil
+  gravity   = 10.0
+  
+  width  = nil
+  height = nil
+  
+  background  = nil
+  interactive = nil
+  foreground  = nil
+  
+  objects   = nil
+  editormap = nil
+  
+  def initialize(parent)
+    @parent = parent
+  end
+
+  def get_level()
+    return @parent
+  end
+
+  def new_from_size(width, height)
+    @name = "<No Name>"
+    @song = "<No Song>"
+    @gravity = 10.0
+    
+    @width  = width
+    @height = height
+    
+    @foreground  = TilemapLayer.new($tileset, @width, @height)
+    @interactive = TilemapLayer.new($tileset, @width, @height)
+    @background  = TilemapLayer.new($tileset, @width, @height)       
+    @objects = ObjectLayer.new()
+
+    @editormap = EditorMap.new()
+    @editormap.add_layer(@background.to_layer())
+    @editormap.add_layer(@interactive.to_layer())
+    @editormap.add_layer(@objects.to_layer())
+    @editormap.add_layer(@foreground.to_layer())
+    # FIXME: Data might not get freed since its 'recursively' refcounted
+    @editormap.set_metadata(make_metadata(self))
+    return self
+  end
+
+  def load_v1(data)
+    @name = "<No Name>"
+    @song = "<No Song>"
+    @gravity = 10.0
+    
+    @width  = get_value_from_tree(["width", "_"], data, 20)
+    @height = get_value_from_tree(["height""_"], data, 15)
+    
+    @foreground  = TilemapLayer($tileset, @width, @height)
+    @foreground.set_data(get_value_from_tree(["foreground-tm"], data, []))
+    
+    @interactive = TilemapLayer($tileset, @width, @height)
+    @interactive.set_data(get_value_from_tree(["interactive-tm"], data, []))
+    
+    @background  = TilemapLayer($tileset, @width, @height)
+    @background.set_data(get_value_from_tree(["background-tm"], data, []))
+    
+    @objects = ObjectLayer.new()
+    for i in get_value_from_tree(["objects"], data, [])
+      type = i[0]
+      x = get_value_from_tree(["x", "_"], i[1..-1], [])
+      y = get_value_from_tree(["y", "_"], i[1..-1], [])
+      object = game_objects.find{|x| x[0] == type}
+      if object != nil
+        @objects.add_object(ObjMapSpriteObject(make_sprite(config.datadir + 
object[1]),
+                                               CL_Point(x, y),
+                                               
make_metadata(BadGuy(object[0]))).to_object())
+      else
+        print "Error: Couldn't resolve object type: ", type
+      end
+    end
+    
+    for i in get_value_from_tree(["reset-points"], data, [])
+      type = i[0]
+      x = get_value_from_tree(["x", "_"], i[1..-1], [])
+      y = get_value_from_tree(["y", "_"], i[1..-1], [])
+      object = find(game_objects, "resetpoint")
+      @objects.add_object(ObjMapSpriteObject(make_sprite(config.datadir + 
object[1]),
+                                             CL_Point(x, y),
+                                             
make_metadata(BadGuy(object[0]))).to_object())
+    end
+
+    @editormap = EditorMap.new()
+    @editormap.add_layer(@background.to_layer())
+    @editormap.add_layer(@interactive.to_layer())
+    @editormap.add_layer(@objects.to_layer())
+    @editormap.add_layer(@foreground.to_layer())
+    # FIXME: Data might not get freed since its 'recursively' refcounted
+    @editormap.set_metadata(make_metadata(self))
+  end
+  
+  def load_v2(data)
+    @name = "<No Name>"
+    @song = "<No Song>"
+    @gravity = 10.0
+    
+    @objects = ObjectLayer.new()
+    for i in data
+      (name,data) = i[0], i[1..-1]
+      if name == "name"
+        @name = data[0]
+      elsif name == "gravity"
+        @gravity = int(data[0])
+      elsif name == "playerspawn"
+        print "playerspawn unhandled"
+      elsif name == "tilemap"
+        width   = get_value_from_tree(["width", "_"], data, 20)
+        height  = get_value_from_tree(["height", "_"], data, 15)
+        solid   = get_value_from_tree(["solid", "_"], data, false)
+
+        tilemap = TilemapLayer($tileset, width, height)
+        tilemap.set_data(get_value_from_tree(["tiles"], data, []))
+        
+        print "Solid: ", solid
+        if solid and @interactive == nil
+          @interactive = tilemap
+          @width       = width
+          @height      = height
+        elsif @background == nil
+          @background = tilemap
+        elsif @foreground == nil
+          @foreground = tilemap
+        else
+          print "Error: Duplicate tilemap in levelfile"
+        end
+      elsif name == "background"
+        print "background unhandled"
+      else
+        object = game_objects.find{|x| x[0] == name}
+        if object != nil
+          (name, image) = object
+          x = get_value_from_tree(["x", "_"], data, [])
+          y = get_value_from_tree(["y", "_"], data, [])
+          @objects.add_object(ObjMapSpriteObject(make_sprite(config.datadir + 
image),
+                                                 CL_Point(x, y),
+                                                 
make_metadata(BadGuy(name))).to_object())
+        else
+          print "Error: Couldn't resolve object type: ", name
+          print "Sector: Unhandled tag: ", name
+        end
+      end
+    end
+    
+    
+    if (@background == nil)
+      @background = TilemapLayer.new($tileset, width, height)
+    end
+
+    if (@interactive == nil)
+      @interactive = TilemapLayer.new($tileset, width, height)
+    end
+    
+    if (@foreground == nil)
+      @foreground = TilemapLayer.new($tileset, width, height)
+    end
+
+    @editormap = EditorMap()
+    @editormap.add_layer(@background.to_layer())
+    @editormap.add_layer(@interactive.to_layer())
+    @editormap.add_layer(@foreground.to_layer())
+    @editormap.add_layer(@objects.to_layer())
+    
+    @editormap.set_metadata(make_metadata(self))
+  end
+
+  def activate(workspace)
+    workspace.set_map(@editormap)
+    TilemapLayer.set_current(@interactive)
+    ObjectLayer.set_current(@objects)
+    #connect(@editormap.sig_change(), on_map_change)
+  end
+end
+
+startlevel = Level.new(100, 50)
+startlevel.activate(workspace)
+
+gui.run()
+flexlay.deinit()
+
+# EOF #





reply via email to

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