]> git.lizzy.rs Git - dragonfireclient.git/blob - src/minimap.h
Remove busy polling inside minimap thread
[dragonfireclient.git] / src / minimap.h
1 /*
2 Minetest
3 Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
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 MINIMAP_HEADER
21 #define MINIMAP_HEADER
22
23 #include "irrlichttypes_extrabloated.h"
24 #include "client.h"
25 #include "voxel.h"
26 #include "jthread/jmutex.h"
27 #include "jthread/jsemaphore.h"
28 #include <map>
29 #include <string>
30 #include <vector>
31
32 enum MinimapMode {
33         MINIMAP_MODE_OFF,
34         MINIMAP_MODE_SURFACEx1,
35         MINIMAP_MODE_SURFACEx2,
36         MINIMAP_MODE_SURFACEx4,
37         MINIMAP_MODE_RADARx1,
38         MINIMAP_MODE_RADARx2,
39         MINIMAP_MODE_RADARx4
40 };
41
42 struct MinimapPixel
43 {
44         u16 id;
45         u16 height;
46         u16 air_count;
47         u16 light;
48 };
49
50 struct MinimapMapblock
51 {
52         MinimapPixel data[MAP_BLOCKSIZE * MAP_BLOCKSIZE];
53 };
54
55 struct MinimapData
56 {
57         bool radar;
58         MinimapMode mode;
59         v3s16 pos;
60         v3s16 old_pos;
61         u16 scan_height;
62         u16 map_size;
63         MinimapPixel minimap_scan[512 * 512];
64         bool map_invalidated;
65         bool minimap_shape_round;
66         video::IImage *minimap_image;
67         video::IImage *heightmap_image;
68         video::IImage *minimap_mask_round;
69         video::IImage *minimap_mask_square;
70         video::ITexture *texture;
71         video::ITexture *heightmap_texture;
72         video::ITexture *minimap_overlay_round;
73         video::ITexture *minimap_overlay_square;
74         video::ITexture *player_marker;
75 };
76
77 struct QueuedMinimapUpdate
78 {
79         v3s16 pos;
80         MinimapMapblock *data;
81
82         QueuedMinimapUpdate();
83         ~QueuedMinimapUpdate();
84 };
85
86 /*
87         A thread-safe queue of minimap mapblocks update tasks
88 */
89
90 class MinimapUpdateQueue
91 {
92 public:
93         MinimapUpdateQueue();
94
95         ~MinimapUpdateQueue();
96
97         bool addBlock(v3s16 pos, MinimapMapblock *data);
98
99         // blocking!!
100         QueuedMinimapUpdate *pop();
101
102         u32 size()
103         {
104                 JMutexAutoLock lock(m_mutex);
105                 return m_queue.size();
106         }
107
108 private:
109         std::list<QueuedMinimapUpdate*> m_queue;
110         JMutex m_mutex;
111 };
112
113 class MinimapUpdateThread : public JThread
114 {
115 private:
116         JSemaphore m_queue_sem;
117         MinimapUpdateQueue m_queue;
118
119 public:
120         MinimapUpdateThread(IrrlichtDevice *device, Client *client)
121         {
122                 this->device = device;
123                 this->client = client;
124                 this->driver = device->getVideoDriver();
125                 this->tsrc = client->getTextureSource();
126         }
127         void getMap (v3s16 pos, s16 size, s16 height, bool radar);
128         MinimapPixel *getMinimapPixel (v3s16 pos, s16 height, s16 &pixel_height);
129         s16 getAirCount (v3s16 pos, s16 height);
130         video::SColor getColorFromId(u16 id);
131
132         void enqueue_Block(v3s16 pos, MinimapMapblock *data);
133
134         IrrlichtDevice *device;
135         Client *client;
136         video::IVideoDriver *driver;
137         ITextureSource *tsrc;
138         void Stop();
139         void *Thread();
140         MinimapData *data;
141         std::map<v3s16, MinimapMapblock *> m_blocks_cache;
142 };
143
144 class Mapper
145 {
146 private:
147         MinimapUpdateThread *m_minimap_update_thread;
148         video::ITexture *minimap_texture;
149         scene::SMeshBuffer *m_meshbuffer;
150         bool m_enable_shaders;
151         JMutex m_mutex;
152
153 public:
154         Mapper(IrrlichtDevice *device, Client *client);
155         ~Mapper();
156
157         void addBlock(v3s16 pos, MinimapMapblock *data);
158         void setPos(v3s16 pos);
159         video::ITexture* getMinimapTexture();
160         v3f getYawVec();
161         MinimapMode getMinimapMode();
162         void setMinimapMode(MinimapMode mode);
163         void toggleMinimapShape();
164         scene::SMeshBuffer *getMinimapMeshBuffer();
165         void drawMinimap();
166         IrrlichtDevice *device;
167         Client *client;
168         video::IVideoDriver *driver;
169         LocalPlayer *player;
170         ITextureSource *tsrc;
171         IShaderSource *shdrsrc;
172         MinimapData *data;
173 };
174
175 #endif