hopefully fixed the crash on exit, keep sectors script bundled in the sector and...
[supertux.git] / src / script_manager.hpp
1 //  $Id: main.cpp 3275 2006-04-09 00:32:34Z sommer $
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #ifndef __SCRIPT_MANAGER_H__
21 #define __SCRIPT_MANAGER_H__
22
23 #include <vector>
24 #include <list>
25 #include <squirrel.h>
26 #include <iostream>
27 #include "timer.hpp"
28
29 class GameObject;
30
31 /**
32  * This class is responsible for managing all running squirrel threads
33  * (they are cooperative threads or coroutines)
34  * It keeps a list of suspended scripts and receives wakeup events for them
35  */
36 class ScriptManager
37 {
38 public:
39   ScriptManager();
40   ScriptManager(ScriptManager* parent);
41   ~ScriptManager();
42
43   void update();
44
45   /**
46    * Creates a new thread and registers it with the script manager
47    * (so it can suspend and register for wakeup events)
48    */
49   HSQUIRRELVM create_thread(bool leave_thread_on_stack = false);
50
51   HSQUIRRELVM get_vm() const
52   {
53     return vm;
54   }
55
56   enum WakeupEvent {
57     NO_EVENT,
58     TIME,
59     SCREEN_SWITCHED,
60     WAKEUP_EVENT_COUNT
61   };
62
63   struct WakeupData {
64     explicit WakeupData() : type(NO_EVENT) {}
65     explicit WakeupData(WakeupEvent type_) : type(type_) {}
66
67     WakeupEvent type;
68     
69     union {
70       // GAMEOBJECT_DONE
71       GameObject* game_object;
72     };
73   };
74
75   void set_wakeup_event(HSQUIRRELVM vm, WakeupEvent event, float timeout = -1);
76   void set_wakeup_event(HSQUIRRELVM vm, WakeupData  event, float timeout = -1);
77   void fire_wakeup_event(WakeupEvent event);
78   void fire_wakeup_event(WakeupData  event);
79
80   // global (root) instance of the ScriptManager
81   static ScriptManager* instance;
82   
83 private:
84   class SquirrelVM
85   {
86   public:
87     SquirrelVM(HSQUIRRELVM arg_vm, HSQOBJECT arg_obj);
88
89     HSQUIRRELVM vm;
90     HSQOBJECT   vm_obj;
91     float       wakeup_time;
92     WakeupData  waiting_for_events;
93   };
94   
95   typedef std::list<SquirrelVM> SquirrelVMs;
96   SquirrelVMs squirrel_vms;
97
98   HSQUIRRELVM vm;
99   ScriptManager* parent;
100   std::vector<ScriptManager*> childs;
101 };
102
103 #endif
104