Scripting scheduler is now paused while pause menu is shown
[supertux.git] / src / scripting / time_scheduler.cpp
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 #include <config.h>
21
22 #include <algorithm>
23
24 #include "time_scheduler.hpp"
25 #include "squirrel_util.hpp"
26 #include "squirrel_error.hpp"
27 #include "log.hpp"
28
29 namespace Scripting
30 {
31
32 TimeScheduler* TimeScheduler::instance = NULL;
33
34 TimeScheduler::TimeScheduler() : paused(false), last_update(0), internal_time(0)
35 {
36 }
37
38 TimeScheduler::~TimeScheduler()
39 {
40 }
41
42 void
43 TimeScheduler::update(float time)
44 {
45   if (!paused) internal_time+=(time - last_update); 
46   last_update = time;
47
48   while(!schedule.empty() && schedule.front().wakeup_time < internal_time) {
49     HSQOBJECT thread_ref = schedule.front().thread_ref;
50
51     sq_pushobject(global_vm, thread_ref);
52     sq_getweakrefval(global_vm, -1);
53
54     HSQUIRRELVM scheduled_vm;
55     if(sq_gettype(global_vm, -1) == OT_THREAD &&
56         SQ_SUCCEEDED(sq_getthread(global_vm, -1, &scheduled_vm))) {
57       if(SQ_FAILED(sq_wakeupvm(scheduled_vm, SQFalse, SQFalse, SQTrue))) {
58         std::ostringstream msg;
59         msg << "Couldn't wakeup scheduled squirrel VM: ";
60         sq_getlasterror(scheduled_vm);
61         if(sq_gettype(scheduled_vm, -1) != OT_STRING) {
62             msg << "(no info)";
63         } else {
64             const char* lasterr;
65             sq_getstring(scheduled_vm, -1, &lasterr);
66             msg << lasterr;
67         }
68         log_warning << msg.str() << std::endl;
69         sq_pop(scheduled_vm, 1);
70       }
71     }
72
73     sq_release(global_vm, &thread_ref);
74     sq_pop(global_vm, 2);
75
76     std::pop_heap(schedule.begin(), schedule.end());
77     schedule.pop_back();
78   }
79 }
80
81 void
82 TimeScheduler::schedule_thread(HSQUIRRELVM scheduled_vm, float time)
83 {
84   // create a weakref to the VM
85   SQObject vm_obj = vm_to_object(scheduled_vm);
86   sq_pushobject(global_vm, vm_obj);
87   sq_weakref(global_vm, -1);
88
89   ScheduleEntry entry;
90   if(SQ_FAILED(sq_getstackobj(global_vm, -1, & entry.thread_ref))) {
91     sq_pop(global_vm, 2);
92     throw SquirrelError(global_vm, "Couldn't get thread weakref from vm");
93   }
94   entry.wakeup_time = time - (last_update - internal_time);
95
96   sq_addref(global_vm, & entry.thread_ref);
97   sq_pop(global_vm, 2);
98
99   schedule.push_back(entry);
100   std::push_heap(schedule.begin(), schedule.end());
101 }
102
103 void
104 TimeScheduler::set_pause(bool paused)
105 {
106   this->paused = paused;
107 }
108
109 }