fade out console
[supertux.git] / src / scripting / functions.cpp
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 #include <config.h>
21
22 #include <memory>
23 #include <stdio.h>
24 #include <string>
25 #include <squirrel.h>
26 #include <sqstdio.h>
27 #include "textscroller.hpp"
28 #include "functions.hpp"
29 #include "game_session.hpp"
30 #include "tinygettext/tinygettext.hpp"
31 #include "physfs/physfs_stream.hpp"
32 #include "script_manager.hpp"
33 #include "resources.hpp"
34 #include "gettext.hpp"
35 #include "log.hpp"
36 #include "mainloop.hpp"
37 #include "worldmap.hpp"
38 #include "world.hpp"
39
40 #include "squirrel_error.hpp"
41 #include "wrapper_util.hpp"
42
43 namespace Scripting
44 {
45
46 int display(HSQUIRRELVM vm)
47 {
48   Console::output << squirrel2string(vm, -1) << std::endl;
49   return 0;
50 }
51
52 void wait(HSQUIRRELVM vm, float seconds)
53 {
54   SQUserPointer ptr = sq_getforeignptr(vm);
55   ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
56   script_manager->set_wakeup_event(vm, ScriptManager::TIME, seconds);
57 }
58
59 void wait_for_screenswitch(HSQUIRRELVM vm)
60 {
61   SQUserPointer ptr = sq_getforeignptr(vm);
62   ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
63   script_manager->set_wakeup_event(vm, ScriptManager::SCREEN_SWITCHED);
64 }
65
66 std::string translate(const std::string& text)
67 {
68   return dictionary_manager.get_dictionary().translate(text);
69 }
70
71 void display_text_file(const std::string& filename)
72 {
73   main_loop->push_screen(new TextScroller(filename));
74 }
75
76 void load_worldmap(const std::string& filename)
77 {
78   using namespace WorldMapNS;
79
80   std::auto_ptr<WorldMap> worldmap(new WorldMap());
81   worldmap->loadmap(filename);
82   main_loop->push_screen(worldmap.release());
83 }
84
85 void load_level(const std::string& filename)
86 {
87   main_loop->push_screen(new GameSession(filename, ST_GL_PLAY));
88 }
89
90 static SQInteger squirrel_read_char(SQUserPointer file)
91 {
92   std::istream* in = reinterpret_cast<std::istream*> (file);
93   char c = in->get();
94   if(in->eof())
95     return 0;
96
97   return c;
98 }
99
100
101 void import(HSQUIRRELVM vm, const std::string& filename)
102 {
103   IFileStream in(filename);
104     
105   if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
106           filename.c_str(), SQTrue)))
107     throw SquirrelError(vm, "Couldn't parse script");
108     
109   sq_pushroottable(vm);
110   if(SQ_FAILED(sq_call(vm, 1, SQFalse))) {
111     sq_pop(vm, 1);
112     throw SquirrelError(vm, "Couldn't execute script");
113   }
114   sq_pop(vm, 1);
115 }
116
117 void add_key(int new_key)
118 {
119   player_status->set_keys(new_key);
120 }
121
122 void save_state()
123 {
124   if(World::current() == NULL)
125     throw std::runtime_error("Can't save state without active World");
126
127   World::current()->save_state();
128 }
129
130 }
131