-
Notifications
You must be signed in to change notification settings - Fork 224
/
graphics.h
357 lines (281 loc) · 8.59 KB
/
graphics.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#ifndef RME_GRAPHICS_H_
#define RME_GRAPHICS_H_
#include "outfit.h"
#include "common.h"
#include <deque>
#include "client_version.h"
#include <wx/artprov.h>
enum SpriteSize {
SPRITE_SIZE_16x16,
//SPRITE_SIZE_24x24,
SPRITE_SIZE_32x32,
SPRITE_SIZE_COUNT
};
enum AnimationDirection {
ANIMATION_FORWARD = 0,
ANIMATION_BACKWARD = 1
};
enum ItemAnimationDuration {
ITEM_FRAME_DURATION = 500
};
class MapCanvas;
class GraphicManager;
class FileReadHandle;
class Animator;
struct SpriteLight {
uint8_t intensity = 0;
uint8_t color = 0;
};
class Sprite
{
public:
Sprite() {}
virtual void DrawTo(wxDC* dc, SpriteSize sz, int start_x, int start_y, int width = -1, int height = -1) = 0;
virtual void unloadDC() = 0;
private:
Sprite(const Sprite&);
};
class EditorSprite : public Sprite
{
public:
EditorSprite(wxBitmap* b16x16, wxBitmap* b32x32);
virtual ~EditorSprite();
virtual void DrawTo(wxDC* dc, SpriteSize sz, int start_x, int start_y, int width = -1, int height = -1);
virtual void unloadDC();
protected:
wxBitmap* bm[SPRITE_SIZE_COUNT];
};
class GameSprite : public Sprite
{
public:
GameSprite();
virtual ~GameSprite();
int getIndex(int width, int height, int layer, int pattern_x, int pattern_y, int pattern_z, int frame) const;
GLuint getHardwareID(int _x, int _y, int _layer, int _subtype, int _pattern_x, int _pattern_y, int _pattern_z, int _frame);
GLuint getHardwareID(int _x, int _y, int _dir, int _addon, int _pattern_z, const Outfit& _outfit, int _frame); // CreatureDatabase
virtual void DrawTo(wxDC* dc, SpriteSize sz, int start_x, int start_y, int width = -1, int height = -1);
void DrawTo(wxDC* context, const wxRect& rect, const Outfit& outfit);
virtual void unloadDC();
void clean(int time);
uint16_t getDrawHeight() const noexcept { return draw_height; }
const wxPoint& getDrawOffset() const noexcept { return draw_offset; }
uint8_t getMiniMapColor() const noexcept { return minimap_color; }
bool hasLight() const noexcept { return has_light; }
const SpriteLight& getLight() const noexcept { return light; }
static GameSprite* createFromBitmap(const wxArtID& bitmapId);
protected:
class Image;
class NormalImage;
class TemplateImage;
wxMemoryDC* getDC(SpriteSize size);
wxMemoryDC* getDC(const Outfit& outfit);
TemplateImage* getTemplateImage(int sprite_index, const Outfit& outfit);
class Image {
public:
Image();
virtual ~Image();
bool isGLLoaded;
int lastaccess;
void visit();
virtual void clean(int time);
virtual GLuint getHardwareID() = 0;
virtual uint8_t* getRGBData() = 0;
virtual uint8_t* getRGBAData() = 0;
protected:
virtual void createGLTexture(GLuint textureId);
virtual void unloadGLTexture(GLuint textureId);
};
class NormalImage : public Image {
public:
NormalImage();
virtual ~NormalImage();
// We use the sprite id as GL texture id
uint32_t id;
// This contains the pixel data
uint16_t size;
uint8_t* dump;
virtual void clean(int time);
virtual GLuint getHardwareID();
virtual uint8_t* getRGBData();
virtual uint8_t* getRGBAData();
protected:
virtual void createGLTexture(GLuint textureId = 0);
virtual void unloadGLTexture(GLuint textureId = 0);
};
class EditorImage : public NormalImage {
public:
EditorImage(const wxArtID& bitmapId);
protected:
void createGLTexture(GLuint textureId) override;
void unloadGLTexture(GLuint textureId) override;
private:
wxArtID bitmapId;
};
class TemplateImage : public Image {
public:
TemplateImage(GameSprite* parent, int v, const Outfit& outfit);
virtual ~TemplateImage();
virtual GLuint getHardwareID();
virtual uint8_t* getRGBData();
virtual uint8_t* getRGBAData();
GLuint gl_tid;
GameSprite* parent;
int sprite_index;
uint8_t lookHead;
uint8_t lookBody;
uint8_t lookLegs;
uint8_t lookFeet;
protected:
void colorizePixel(uint8_t color, uint8_t &r, uint8_t &b, uint8_t &g);
virtual void createGLTexture(GLuint ignored = 0);
virtual void unloadGLTexture(GLuint ignored = 0);
};
uint32_t id;
wxMemoryDC* dc[SPRITE_SIZE_COUNT];
public:
// GameSprite info
uint8_t height;
uint8_t width;
uint8_t layers;
uint8_t pattern_x;
uint8_t pattern_y;
uint8_t pattern_z;
uint8_t frames;
uint32_t numsprites;
Animator* animator;
uint16_t ground_speed;
uint16_t draw_height;
wxPoint draw_offset;
uint16_t minimap_color;
bool has_light = false;
SpriteLight light;
std::vector<NormalImage*> spriteList;
std::list<TemplateImage*> instanced_templates; // Templates that use this sprite
friend class GraphicManager;
};
struct FrameDuration
{
int min;
int max;
FrameDuration(int min, int max) : min(min), max(max)
{
ASSERT(min <= max);
}
int getDuration() const
{
if(min == max)
return min;
return uniform_random(min, max);
};
void setValues(int min, int max)
{
ASSERT(min <= max);
this->min = min;
this->max = max;
}
};
class Animator
{
public:
Animator(int frames, int start_frame, int loop_count, bool async);
~Animator();
int getStartFrame() const;
FrameDuration* getFrameDuration(int frame);
int getFrame();
void setFrame(int frame);
void reset();
private:
int getDuration(int frame) const;
int getPingPongFrame();
int getLoopFrame();
void calculateSynchronous();
int frame_count;
int start_frame;
int loop_count;
bool async;
std::vector<FrameDuration*> durations;
int current_frame;
int current_loop;
int current_duration;
int total_duration;
AnimationDirection direction;
long last_time;
bool is_complete;
};
class GraphicManager
{
public:
GraphicManager();
~GraphicManager();
void clear();
void cleanSoftwareSprites();
Sprite* getSprite(int id);
GameSprite* getCreatureSprite(int id);
GameSprite* getEditorSprite(int id);
long getElapsedTime() const { return (animation_timer->TimeInMicro() / 1000).ToLong(); }
uint16_t getItemSpriteMinID() const noexcept { return 100; }
uint16_t getItemSpriteMaxID() const noexcept { return item_count; }
uint16_t getCreatureSpriteMaxID() const noexcept { return creature_count; }
// Get an unused texture id (this is acquired by simply increasing a value starting from 0x10000000)
GLuint getFreeTextureID();
// This is part of the binary
bool loadEditorSprites();
// Metadata should be loaded first
// This fills the item / creature adress space
bool loadOTFI(const FileName& filename, wxString& error, wxArrayString& warnings);
bool loadSpriteMetadata(const FileName& datafile, wxString& error, wxArrayString& warnings);
bool loadSpriteMetadataFlags(FileReadHandle& file, GameSprite* sType, wxString& error, wxArrayString& warnings);
bool loadSpriteData(const FileName& datafile, wxString& error, wxArrayString& warnings);
// Cleans old & unused textures according to config settings
void garbageCollection();
void addSpriteToCleanup(GameSprite* spr);
wxFileName getMetadataFileName() const { return metadata_file; }
wxFileName getSpritesFileName() const { return sprites_file; }
bool hasTransparency() const;
bool isUnloaded() const;
ClientVersion *client_version;
private:
bool unloaded;
// This is used if memcaching is NOT on
std::string spritefile;
bool loadSpriteDump(uint8_t*& target, uint16_t& size, int sprite_id);
typedef std::map<int, Sprite*> SpriteMap;
SpriteMap sprite_space;
typedef std::map<int, GameSprite::Image*> ImageMap;
ImageMap image_space;
std::deque<GameSprite*> cleanup_list;
DatFormat dat_format;
uint16_t item_count;
uint16_t creature_count;
bool otfi_found;
bool is_extended;
bool has_transparency;
bool has_frame_durations;
bool has_frame_groups;
wxFileName metadata_file;
wxFileName sprites_file;
int loaded_textures;
int lastclean;
wxStopWatch* animation_timer;
friend class GameSprite::Image;
friend class GameSprite::NormalImage;
friend class GameSprite::EditorImage;
friend class GameSprite::TemplateImage;
};
#endif