Performance fix + SAO factorization
[oweals/minetest.git] / src / guiEngine.h
1 /*
2 Minetest
3 Copyright (C) 2013 sapier
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef GUI_ENGINE_H_
21 #define GUI_ENGINE_H_
22
23 /******************************************************************************/
24 /* Includes                                                                   */
25 /******************************************************************************/
26 #include "irrlichttypes.h"
27 #include "modalMenu.h"
28 #include "guiFormSpecMenu.h"
29 #include "sound.h"
30 #include "client/tile.h"
31 #include "util/enriched_string.h"
32
33 /******************************************************************************/
34 /* Typedefs and macros                                                        */
35 /******************************************************************************/
36 /** texture layer ids */
37 typedef enum {
38         TEX_LAYER_BACKGROUND = 0,
39         TEX_LAYER_OVERLAY,
40         TEX_LAYER_HEADER,
41         TEX_LAYER_FOOTER,
42         TEX_LAYER_MAX
43 } texture_layer;
44
45 typedef struct {
46         video::ITexture* texture;
47         bool             tile;
48         unsigned int     minsize;
49 } image_definition;
50
51 /******************************************************************************/
52 /* forward declarations                                                       */
53 /******************************************************************************/
54 class GUIEngine;
55 class MainMenuScripting;
56 class Clouds;
57 struct MainMenuData;
58
59 /******************************************************************************/
60 /* declarations                                                               */
61 /******************************************************************************/
62
63 /** GUIEngine specific implementation of TextDest used within guiFormSpecMenu */
64 class TextDestGuiEngine : public TextDest
65 {
66 public:
67         /**
68          * default constructor
69          * @param engine the engine data is transmitted for further processing
70          */
71         TextDestGuiEngine(GUIEngine* engine);
72
73         /**
74          * receive fields transmitted by guiFormSpecMenu
75          * @param fields map containing formspec field elements currently active
76          */
77         void gotText(const StringMap &fields);
78
79         /**
80          * receive text/events transmitted by guiFormSpecMenu
81          * @param text textual representation of event
82          */
83         void gotText(std::wstring text);
84
85 private:
86         /** target to transmit data to */
87         GUIEngine* m_engine;
88 };
89
90 /** GUIEngine specific implementation of ISimpleTextureSource */
91 class MenuTextureSource : public ISimpleTextureSource
92 {
93 public:
94         /**
95          * default constructor
96          * @param driver the video driver to load textures from
97          */
98         MenuTextureSource(video::IVideoDriver *driver);
99
100         /**
101          * destructor, removes all loaded textures
102          */
103         virtual ~MenuTextureSource();
104
105         /**
106          * get a texture, loading it if required
107          * @param name path to the texture
108          * @param id receives the texture ID, always 0 in this implementation
109          */
110         video::ITexture* getTexture(const std::string &name, u32 *id = NULL);
111
112 private:
113         /** driver to get textures from */
114         video::IVideoDriver *m_driver;
115         /** set of texture names to delete */
116         std::set<std::string> m_to_delete;
117 };
118
119 /** GUIEngine specific implementation of OnDemandSoundFetcher */
120 class MenuMusicFetcher: public OnDemandSoundFetcher
121 {
122 public:
123         /**
124          * get sound file paths according to sound name
125          * @param name sound name
126          * @param dst_paths receives possible paths to sound files
127          * @param dst_datas receives binary sound data (not used here)
128          */
129         void fetchSounds(const std::string &name,
130                         std::set<std::string> &dst_paths,
131                         std::set<std::string> &dst_datas);
132
133 private:
134         /** set of fetched sound names */
135         std::set<std::string> m_fetched;
136 };
137
138 /** implementation of main menu based uppon formspecs */
139 class GUIEngine {
140         /** grant ModApiMainMenu access to private members */
141         friend class ModApiMainMenu;
142
143 public:
144         /**
145          * default constructor
146          * @param dev device to draw at
147          * @param parent parent gui element
148          * @param menumgr manager to add menus to
149          * @param smgr scene manager to add scene elements to
150          * @param data struct to transfer data to main game handling
151          */
152         GUIEngine(irr::IrrlichtDevice* dev,
153                         JoystickController *joystick,
154                         gui::IGUIElement* parent,
155                         IMenuManager *menumgr,
156                         scene::ISceneManager* smgr,
157                         MainMenuData* data,
158                         bool& kill);
159
160         /** default destructor */
161         virtual ~GUIEngine();
162
163         /**
164          * return MainMenuScripting interface
165          */
166         MainMenuScripting* getScriptIface()
167         {
168                 return m_script;
169         }
170
171         /**
172          * return dir of current menuscript
173          */
174         std::string getScriptDir()
175         {
176                 return m_scriptdir;
177         }
178
179         /** pass async callback to scriptengine **/
180         unsigned int queueAsync(std::string serialized_fct,std::string serialized_params);
181
182 private:
183
184         /** find and run the main menu script */
185         bool loadMainMenuScript();
186
187         /** run main menu loop */
188         void run();
189
190         /** handler to limit frame rate within main menu */
191         void limitFrameRate();
192
193         /** update size of topleftext element */
194         void updateTopLeftTextSize();
195
196         /** device to draw at */
197         irr::IrrlichtDevice*     m_device;
198         /** parent gui element */
199         gui::IGUIElement*        m_parent;
200         /** manager to add menus to */
201         IMenuManager*            m_menumanager;
202         /** scene manager to add scene elements to */
203         scene::ISceneManager*    m_smgr;
204         /** pointer to data beeing transfered back to main game handling */
205         MainMenuData*            m_data;
206         /** pointer to texture source */
207         ISimpleTextureSource*    m_texture_source;
208         /** pointer to soundmanager*/
209         ISoundManager*           m_sound_manager;
210
211         /** representation of form source to be used in mainmenu formspec */
212         FormspecFormSource*      m_formspecgui;
213         /** formspec input receiver */
214         TextDestGuiEngine*       m_buttonhandler;
215         /** the formspec menu */
216         GUIFormSpecMenu*         m_menu;
217
218         /** reference to kill variable managed by SIGINT handler */
219         bool&                    m_kill;
220
221         /** variable used to abort menu and return back to main game handling */
222         bool                     m_startgame;
223
224         /** scripting interface */
225         MainMenuScripting*       m_script;
226
227         /** script basefolder */
228         std::string              m_scriptdir;
229
230         /**
231          * draw background layer
232          * @param driver to use for drawing
233          */
234         void drawBackground(video::IVideoDriver* driver);
235         /**
236          * draw overlay layer
237          * @param driver to use for drawing
238          */
239         void drawOverlay(video::IVideoDriver* driver);
240         /**
241          * draw header layer
242          * @param driver to use for drawing
243          */
244         void drawHeader(video::IVideoDriver* driver);
245         /**
246          * draw footer layer
247          * @param driver to use for drawing
248          */
249         void drawFooter(video::IVideoDriver* driver);
250
251         /**
252          * load a texture for a specified layer
253          * @param layer draw layer to specify texture
254          * @param texturepath full path of texture to load
255          */
256         bool setTexture(texture_layer layer, std::string texturepath,
257                         bool tile_image, unsigned int minsize);
258
259         /**
260          * download a file using curl
261          * @param url url to download
262          * @param target file to store to
263          */
264         static bool downloadFile(std::string url,std::string target);
265
266         /** array containing pointers to current specified texture layers */
267         image_definition m_textures[TEX_LAYER_MAX];
268
269         /** draw version string in topleft corner */
270         void drawVersion();
271
272         /**
273          * specify text to appear as top left string
274          * @param text to set
275          */
276         void setTopleftText(const std::string &text);
277
278         /** pointer to gui element shown at topleft corner */
279         irr::gui::IGUIStaticText*       m_irr_toplefttext;
280         /** and text that is in it */
281         EnrichedString m_toplefttext;
282
283         /** initialize cloud subsystem */
284         void cloudInit();
285         /** do preprocessing for cloud subsystem */
286         void cloudPreProcess();
287         /** do postprocessing for cloud subsystem */
288         void cloudPostProcess();
289
290         /** internam data required for drawing clouds */
291         struct clouddata {
292                 /** delta time since last cloud processing */
293                 f32     dtime;
294                 /** absolute time of last cloud processing */
295                 u32     lasttime;
296                 /** pointer to cloud class */
297                 Clouds* clouds;
298                 /** camera required for drawing clouds */
299                 scene::ICameraSceneNode* camera;
300         };
301
302         /** is drawing of clouds enabled atm */
303         bool        m_clouds_enabled;
304         /** data used to draw clouds */
305         clouddata   m_cloud;
306
307         /** start playing a sound and return handle */
308         s32 playSound(SimpleSoundSpec spec, bool looped);
309         /** stop playing a sound started with playSound() */
310         void stopSound(s32 handle);
311
312
313 };
314
315
316
317 #endif /* GUI_ENGINE_H_ */