e0f2978529c5e3ba48f88894c191b2f52d4da404
[supertux.git] / src / script_manager.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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 #ifdef ENABLE_SQDBG
29 #include <sqdbg/sqrdbg.h>
30 #endif
31
32 class GameObject;
33
34 /**
35  * This class is responsible for managing all running squirrel threads
36  * (they are cooperative threads or coroutines)
37  * It keeps a list of suspended scripts and receives wakeup events for them
38  */
39 class ScriptManager
40 {
41 public:
42   ScriptManager();
43   ScriptManager(ScriptManager* parent);
44   ~ScriptManager();
45
46   void update();
47
48   /**
49    * Creates a new thread and registers it with the script manager
50    * (so it can suspend and register for wakeup events)
51    */
52   HSQUIRRELVM create_thread(bool leave_thread_on_stack = false);
53
54   HSQUIRRELVM get_vm() const
55   {
56     return vm;
57   }
58
59   enum WakeupEvent {
60     NO_EVENT,
61     TIME,
62     SCREEN_SWITCHED,
63     WAKEUP_EVENT_COUNT
64   };
65
66   struct WakeupData {
67     explicit WakeupData() : type(NO_EVENT) {}
68     explicit WakeupData(WakeupEvent type_) : type(type_) {}
69
70     WakeupEvent type;
71     
72     union {
73       // GAMEOBJECT_DONE
74       GameObject* game_object;
75     };
76   };
77
78   void set_wakeup_event(HSQUIRRELVM vm, WakeupEvent event, float timeout = -1);
79   void set_wakeup_event(HSQUIRRELVM vm, WakeupData  event, float timeout = -1);
80   void fire_wakeup_event(WakeupEvent event);
81   void fire_wakeup_event(WakeupData  event);
82
83   // global (root) instance of the ScriptManager
84   static ScriptManager* instance;
85   
86 private:
87   class SquirrelVM
88   {
89   public:
90     SquirrelVM(HSQUIRRELVM arg_vm, HSQOBJECT arg_obj);
91
92     HSQUIRRELVM vm;
93     HSQOBJECT   vm_obj;
94     float       wakeup_time;
95     WakeupData  waiting_for_events;
96   };
97   
98   typedef std::list<SquirrelVM> SquirrelVMs;
99   SquirrelVMs squirrel_vms;
100
101   HSQUIRRELVM vm;
102   ScriptManager* parent;
103   std::vector<ScriptManager*> childs;
104 #ifdef ENABLE_SQDBG
105   HSQREMOTEDBG debugger;
106 #endif
107 };
108
109 #endif
110