-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomm3map.h
183 lines (129 loc) · 3.87 KB
/
homm3map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* homm3-wallpaper, live HOMM3 wallpaper
* Copyright (C) 2022-2024 i.Dark_Templar <[email protected]>
*
* Subject to terms and condition provided in LICENSE.txt
*
*/
#pragma once
#include <memory>
#include <set>
#include <tuple>
#include <QtCore/QFuture>
#include <QtCore/QFutureWatcher>
#include <QtCore/QMetaType>
#include <QtCore/QMutex>
#include <QtCore/QObject>
#include <QtCore/QStringList>
#include <QtCore/QThread>
#include <QtCore/QTimer>
#include <QtGui/QOpenGLFunctions>
#include <QtOpenGL/QOpenGLShaderProgram>
#include <QtQuick/QQuickFramebufferObject>
#include "vcmi/CMap.h"
#include "globals.h"
#include "texture_atlas.h"
class Homm3MapRenderer;
struct AnimatedItem
{
std::string name;
int group = 0;
int special = -1;
size_t total_frames = 1;
bool is_terrain = false;
bool operator<(const AnimatedItem &other) const;
};
struct MapData
{
std::shared_ptr<CMap> m_map;
QString m_name;
int m_level = 0;
std::vector<QVector3D> m_vertices;
std::vector<QVector2D> m_texcoords;
TextureAtlas m_texture_atlas;
std::map<size_t, size_t> m_current_frames;
std::map<AnimatedItem, std::map<int, std::set<size_t> > > m_animated_items;
std::vector<uint8_t> m_texture_data;
};
class Homm3MapLoader: public QObject
{
Q_OBJECT
public:
explicit Homm3MapLoader(QObject *parent = nullptr);
Q_SIGNALS:
void mapLoaded(std::shared_ptr<MapData> data);
public Q_SLOTS:
void loadMapData(QString map_name, std::shared_ptr<CMap> map, int level);
};
class Homm3Map: public QQuickFramebufferObject
{
Q_OBJECT
Q_PROPERTY(double scale READ scale WRITE setScale NOTIFY scaleUpdated);
public:
explicit Homm3Map(QQuickItem *parent = nullptr);
~Homm3Map();
virtual QQuickFramebufferObject::Renderer* createRenderer() const override;
Q_INVOKABLE void loadMap(const QString &filename, int level);
Q_INVOKABLE void toggleLevel();
Q_INVOKABLE void setDataArchives(const QStringList &files);
Q_INVOKABLE bool isMapLoaded() const;
Q_INVOKABLE QString currentMapName() const;
Q_INVOKABLE int mapLevel() const;
double scale() const;
void setScale(double value);
Q_SIGNALS:
void loadingFinished(QString map_name, int level);
void scaleUpdated(double);
void startLoadingMap(QString map_name, std::shared_ptr<CMap> map, int level);
private Q_SLOTS:
void mapLoaded(std::shared_ptr<MapData> data);
private:
QThread m_worker_thread;
double m_scale;
mutable QMutex m_data_mutex;
std::shared_ptr<CMap> m_map;
QString m_current_map;
int m_map_level;
std::vector<QVector3D> m_vertices;
std::vector<QVector2D> m_texcoords;
TextureAtlas m_texture_atlas;
std::map<size_t, size_t> m_current_frames;
std::map<AnimatedItem, std::map<int, std::set<size_t> > > m_animated_items;
std::vector<uint8_t> m_texture_data;
friend class Homm3MapRenderer;
};
class Homm3MapRenderer: public QObject, public QQuickFramebufferObject::Renderer, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit Homm3MapRenderer();
~Homm3MapRenderer();
virtual QOpenGLFramebufferObject* createFramebufferObject(const QSize &size) override;
void initialize();
virtual void render() override;
void prepareRenderData();
private Q_SLOTS:
void updateFrames();
protected:
virtual void synchronize(QQuickFramebufferObject *item) override;
private:
QOpenGLShaderProgram m_program;
int m_vertexAttr = 0;
int m_textureAttr = 0;
int m_matrixUniform = 0;
int m_shaderTexture = 0;
GLuint m_texture_id = 0;
std::shared_ptr<CMap> m_map;
std::vector<QVector3D> m_vertices;
std::vector<QVector2D> m_texcoords;
TextureAtlas m_texture_atlas;
std::map<size_t, size_t> m_current_frames;
std::map<AnimatedItem, std::map<int, std::set<size_t> > > m_animated_items;
std::vector<uint8_t> m_texture_data;
QTimer m_frame_timer;
bool m_need_update_animation;
bool m_need_update_map;
void updateAnimatedItems();
};
Q_DECLARE_METATYPE(std::shared_ptr<MapData>);
Q_DECLARE_METATYPE(std::shared_ptr<CMap>);