adonthell-commits
[Top][All Lists]
Advanced

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

[Adonthell-commits] CVS: adonthell/src event.cc,1.16,1.17 gamedata.cc,1.


From: Kai Sterker <address@hidden>
Subject: [Adonthell-commits] CVS: adonthell/src event.cc,1.16,1.17 gamedata.cc,1.24,1.25 mapcharacter.cc,1.45,1.46 mapcharacter.h,1.55,1.56 py_callback.cc,1.7,1.8 py_callback.h,1.3,1.4
Date: Mon, 19 Aug 2002 15:57:31 -0400

Update of /cvsroot/adonthell/adonthell/src
In directory subversions:/tmp/cvs-serv19307

Modified Files:
        event.cc gamedata.cc mapcharacter.cc mapcharacter.h 
        py_callback.cc py_callback.h 
Log Message:
ADDED saving/restoring of events w/ python callbacks
ADDED mapcharacter::time_callback* methods


Index: event.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event.cc,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -r1.16 -r1.17
*** event.cc    18 Aug 2002 19:53:16 -0000      1.16
--- event.cc    19 Aug 2002 19:57:28 -0000      1.17
***************
*** 141,154 ****
      Action >> file;
      
!     if (Action != ACTION_SCRIPT) return;
!         
!     Script->object_file () >> file;
!     
!     if (Args)
      {
!         true >> file;
!         python::put_tuple (Args, file);
      }
-     else false >> file;
  }
  
--- 141,170 ----
      Action >> file;
      
!     switch (Action)
      {
!         // save script
!         case ACTION_SCRIPT:
!         {
!             Script->object_file () >> file;
!     
!             if (Args)
!             {
!                 true >> file;
!                 python::put_tuple (Args, file);
!             }
!             else false >> file;
!             
!             return;
!         }
!         
!         // save python callback
!         case ACTION_PYFUNC:
!         {
!             PyFunc->put_state (file);
!             return;
!         }
!         
!         default: return;
      }
  }
  
***************
*** 156,163 ****
  bool event::get_state (igzstream & file) 
  {
-     std::string name;
-     bool has_args;
-     PyObject * args = NULL;
- 
      // Note that »Type« is already read by event_list::load to
      // determine what event subclass to instanciate
--- 172,175 ----
***************
*** 165,179 ****
      Action << file;
      
!     if (Action != ACTION_SCRIPT) return true;
!         
!     name << file;
!     has_args << file;
      
!     if (has_args) args = python::get_tuple (file);
      
!     set_script (name, args);
!     Py_XDECREF (args);
      
!     return true;
  }
  
--- 177,211 ----
      Action << file;
      
!     switch (Action)
!     {
!         // load script from file
!         case ACTION_SCRIPT:
!         {
!             std::string name;
!             bool has_args;
!             PyObject * args = NULL;
! 
!             name << file;
!             has_args << file;
!     
!             if (has_args) args = python::get_tuple (file);
      
!             set_script (name, args);
!             Py_XDECREF (args);
      
!             return true;
!         }
!         
!         // load python callback from file
!         case ACTION_PYFUNC:
!         {
!             PyFunc = new py_callback;
!             return PyFunc->get_state (file); 
!         }
!         
!         default: return true;
!     }
      
!     return false;
  }
  

Index: gamedata.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/gamedata.cc,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -r1.24 -r1.25
*** gamedata.cc 18 Aug 2002 19:53:16 -0000      1.24
--- gamedata.cc 19 Aug 2002 19:57:28 -0000      1.25
***************
*** 38,42 ****
  // File format versions of the various data files
  // *** Increase when changing file format! ***
! #define ENGINE_DAT_VER  3
  #define AUDIO_DAT_VER   2
  #define CHAR_DAT_VER    4
--- 38,42 ----
  // File format versions of the various data files
  // *** Increase when changing file format! ***
! #define ENGINE_DAT_VER  4
  #define AUDIO_DAT_VER   2
  #define CHAR_DAT_VER    4

Index: mapcharacter.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/mapcharacter.cc,v
retrieving revision 1.45
retrieving revision 1.46
diff -C2 -r1.45 -r1.46
*** mapcharacter.cc     18 Aug 2002 19:53:17 -0000      1.45
--- mapcharacter.cc     19 Aug 2002 19:57:28 -0000      1.46
***************
*** 22,25 ****
--- 22,26 ----
  #include "mapcharacter.h"
  #include "map_event.h"
+ #include "time_event.h"
  #include "event_handler.h"
  #include "landmap.h"
***************
*** 206,210 ****
      set_action_active (b);
  
!     return 0;
  }
  
--- 207,213 ----
      set_action_active (b);
  
!     // get the events
!     py_callback::instance = schedule.get_instance ();
!     return event_list::get_state (file);
  }
  
***************
*** 248,251 ****
--- 251,257 ----
      is_action_activated () >> file; 
      
+     // save the events
+     event_list::put_state (file);
+     
      return 0;
  }
***************
*** 554,557 ****
--- 560,597 ----
      if (callback) delete callback;
      callback = new py_callback (cb, args);
+ }
+ 
+ void mapcharacter::time_callback (string delay, PyObject *cb, PyObject *args)
+ {
+     time_event *ev = new time_event (delay);
+     ev->set_callback (cb, args);
+     add_event (ev);
+ }
+ 
+ void mapcharacter::time_callback_string (string delay, string cb, PyObject 
*args)
+ {
+     PyObject *instance = schedule.get_instance ();
+ 
+     // check that we have a valid instance that contains our callback
+     if (instance == NULL)
+     {
+         fprintf (stderr, "*** error: mapcharacter::time_callback: Invalid 
instance!");
+         return;
+     }
+      
+     PyObject *callback = PyObject_GetAttrString (instance, (char *) cb.c_str 
());
+ 
+     if (!PyCallable_Check (callback))
+     {
+         fprintf (stderr, "*** error: mapcharacter::time_callback: Setting 
callback ' %s' failed!", cb.c_str ());
+     }
+     else
+     {
+         time_event *ev = new time_event (delay);
+         ev->set_callback (callback, args);
+         add_event (ev);
+     }
+     
+     Py_XDECREF (callback);
  }
  

Index: mapcharacter.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/mapcharacter.h,v
retrieving revision 1.55
retrieving revision 1.56
diff -C2 -r1.55 -r1.56
*** mapcharacter.h      18 Aug 2002 19:53:17 -0000      1.55
--- mapcharacter.h      19 Aug 2002 19:57:28 -0000      1.56
***************
*** 598,601 ****
--- 598,603 ----
      void stop_moving ();
      
+     void time_callback (string delay, PyObject *cb, PyObject *args = NULL);
+     void time_callback_string (string delay, string cb, PyObject *args = 
NULL);
      //@}
      

Index: py_callback.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_callback.cc,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** py_callback.cc      23 Sep 2001 14:05:45 -0000      1.7
--- py_callback.cc      19 Aug 2002 19:57:29 -0000      1.8
***************
*** 2,6 ****
     $Id$
  
!    Copyright (C) 2001 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
--- 2,6 ----
     $Id$
  
!    Copyright (C) 2001/2002 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
***************
*** 26,31 ****
  #include "py_callback.h"
  #include "python_class.h"
- #include <iostream.h>
  
  py_callback::py_callback (PyObject *func, PyObject *args)
  {
--- 26,41 ----
  #include "py_callback.h"
  #include "python_class.h"
  
+ // 'hack' to aid restoring of callbacks from file
+ PyObject *py_callback::instance = NULL;
+ 
+ // default constructor
+ py_callback::py_callback ()
+ {
+     function = NULL;
+     arguments = NULL;
+ }
+ 
+ // preferred constructor
  py_callback::py_callback (PyObject *func, PyObject *args)
  {
***************
*** 36,39 ****
--- 46,50 ----
  }
  
+ // dtor
  py_callback::~py_callback ()
  {
***************
*** 76,83 ****
  }
  
! PyObject *py_callback::make_call (PyObject *arg)
  {
!     PyObject * val = PyObject_CallObject (function, arg);
!     Py_XDECREF (arg);
  
  #ifdef PY_DEBUG
--- 87,152 ----
  }
  
! // save callback to a file
! void py_callback::put_state (ogzstream & file) const
  {
!     std::string name = "";
!     
!     // get name of callback function
!     PyObject *p_name = PyObject_GetAttrString (function, "__name__");
!     if (PyString_Check (p_name)) name = PyString_AsString (p_name);
!     else fprintf (stderr, "*** error: py_callback::put_state: Failed to 
retrieve callback name!");
!     
!     name >> file;
! 
!     // NOTE: extra arguments need to be a tuple containing only ints or 
strings.
!     if (arguments != NULL)
!     {
!         true >> file; 
!         python::put_tuple (arguments, file);
!     }
!     else false >> file; 
!      
!     // cleanup
!     Py_XDECREF (p_name);
! }
! 
! // restore callback from a file
! bool py_callback::get_state (igzstream & file)
! {
!     std::string name;
!     bool has_args;
!     
!     name << file;
!     has_args << file;
!     
!     // load arguments. No need to INCREF as get_tuple returns new instance.
!     if (has_args) arguments = python::get_tuple (file); 
! 
!     // check that we have a valid instance that contains our callback
!     if (instance == NULL)
!     {
!         fprintf (stderr, "*** error: py_callback::get_state: Invalid 
instance!");
!         return false;
!     }
!      
!     // get our callback from the class or module. No need to INCREF
!     // as GetAttrString returns a new instance.
!     function = PyObject_GetAttrString (instance, (char *) name.c_str ());
!     
!     // sanity check
!     if (!PyCallable_Check (function))
!     {
!         fprintf (stderr, "*** error: py_callback::get_state: Setting callback 
' %s' failed!", name.c_str ());
!         return false;
!     }
!     
!     return true;
! }
! 
! // the actual python callback call
! PyObject *py_callback::make_call (PyObject *args)
! {
!     PyObject * val = PyObject_CallObject (function, args);
!     Py_XDECREF (args);
  
  #ifdef PY_DEBUG
***************
*** 86,88 ****
  
      return val;
! }
\ No newline at end of file
--- 155,157 ----
  
      return val;
! }

Index: py_callback.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_callback.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** py_callback.h       23 Sep 2001 14:05:45 -0000      1.3
--- py_callback.h       19 Aug 2002 19:57:29 -0000      1.4
***************
*** 2,6 ****
     $Id$
  
!    Copyright (C) 2001 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
--- 2,6 ----
     $Id$
  
!    Copyright (C) 2001/2002 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
***************
*** 28,31 ****
--- 28,32 ----
   
  #include "Python.h"
+ #include "fileops.h"
  
  /**
***************
*** 36,41 ****
  {
  public:
      /** 
!      * Constructor that assigns a function and it's arguments to the callback.
       * 
       * @param func function assigned to this callback.
--- 37,48 ----
  {
  public:
+ 
+     /**
+      * Default ctor,
+      */
+     py_callback ();
+ 
      /** 
!      * Constructor that assigns a function and its arguments to the callback.
       * 
       * @param func function assigned to this callback.
***************
*** 46,56 ****
      /** 
       * Destructor.
-      * 
       */
      ~py_callback ();
  
      /** 
       * Calls the python function without arguments.
-      * 
       */
      void callback_func0 ();
--- 53,67 ----
      /** 
       * Destructor.
       */
      ~py_callback ();
  
+     
+     /**
+      * @name Executing the callback
+      */
+     //@{
+     
      /** 
       * Calls the python function without arguments.
       */
      void callback_func0 ();
***************
*** 58,83 ****
      /**
       * Calls the python function and returns bool.
-      * 
       */
- 
      bool callback_func0ret (); 
  
      /**
       * Calls the python function with an integer.
!      * 
       */ 
!     void callback_func1 (int);
  
  private:
  
      /**
!      * TThe actual function call
       *
       */
!     PyObject *make_call (PyObject*);
  
      /**
       * The function to be called.
-      * 
       */ 
      PyObject *function;
--- 69,132 ----
      /**
       * Calls the python function and returns bool.
       */
      bool callback_func0ret (); 
  
      /**
       * Calls the python function with an integer.
!      *
!      * @param arg Integer value to pass to the callback
       */ 
!     void callback_func1 (int arg);
!     
!     //@}
!     
!     
!     /**
!      * @name Loading / Saving
!      */
!     //@{
! 
!     /** 
!      * Saves the callback and it's arguments to file.
!      * @note Currently, arguments have to be a tuple containing only
!      *      integers and/or strings.
!      *
!      * @param out file where to save the callback.
!      */ 
!     void put_state (ogzstream& out) const;
!     
!     /** 
!      * Restores the callback from a file. For that to work, the static
!      * py_callback::instance member has to point to the python instance 
!      * containing the callback.
!      * 
!      * @param in file to load the callback from.
!      *
!      * @return \e true if the callback could be restored, \e false otherwise
!      *
!      * @sa instance
!      */
!     bool get_state (igzstream& in);
! 
!     /**
!      * When restoring a callback from file, instance has to point to the
!      * python instance (module or class) containing the callback.
!      *
!      * @sa get_state
!      */
!     static PyObject *instance;
!     //@}
  
  private:
  
      /**
!      * The actual function call.
       *
+      * @param args The arguments passed to the callback.
       */
!     PyObject *make_call (PyObject *args);
  
      /**
       * The function to be called.
       */ 
      PyObject *function;
***************
*** 85,89 ****
      /**
       * Additional arguments passed to the function.
-      * 
       */ 
      PyObject *arguments;
--- 134,137 ----





reply via email to

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