-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.cpp
289 lines (228 loc) · 9.38 KB
/
sprites.cpp
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
/** Copyright (C) 2008 Josh Ventura
*** Copyright (C) 2013 Robert B. Colton
*** Copyright (C) 2015 Harijs Grinbergs
***
*** This file is a part of the ENIGMA Development Environment.
***
*** ENIGMA 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, version 3 of the license or any later version.
***
*** This application and its source code is distributed AS-IS, 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 code. If not, see <http://www.gnu.org/licenses/>
**/
#include "sprites_internal.h"
#include "Universal_System/image_formats.h"
#include "Graphics_Systems/graphics_mandatory.h"
#include "Graphics_Systems/General/GStextures.h"
#include "Graphics_Systems/General/GScolor_macros.h"
#include "Widget_Systems/widgets_mandatory.h"
#include <string>
using enigma::Sprite;
using enigma::sprites;
using enigma::TexRect;
using enigma::Color;
using enigma::RawImage;
namespace {
Sprite sprite_add_helper(std::string filename, int imgnumb, bool precise, bool transparent, bool smooth, bool preload, int x_offset, int y_offset, bool mipmap) {
std::vector<RawImage> imgs = enigma::image_load(filename);
if (imgs.empty()) {
DEBUG_MESSAGE("ERROR - Failed to append sprite to index!", MESSAGE_TYPE::M_ERROR);
return Sprite();
}
unsigned cellwidth = ((imgs.size() > 1) ? imgs[0].w : imgs[0].w / imgnumb);
Sprite ns(cellwidth, imgs[0].h, x_offset, y_offset);
ns.SetBBox(0, 0, cellwidth, imgs[0].h);
if (imgs.size() == 1 && imgnumb > 1) {
if (transparent) enigma::image_remove_color(imgs[0]);
std::vector<RawImage> rawSubimages = enigma::image_split(imgs[0], imgnumb);
for (const RawImage& i : rawSubimages) {
ns.AddSubimage(i, ((precise) ? enigma::ct_precise : enigma::ct_bbox), i.pxdata, mipmap);
}
} else {
for (RawImage& i : imgs) {
if (transparent) enigma::image_remove_color(i);
ns.AddSubimage(i, ((precise) ? enigma::ct_precise : enigma::ct_bbox), i.pxdata, mipmap);
}
}
return ns;
}
}
namespace enigma_user {
int sprite_get_width(int sprid) {
return sprites.get(sprid).width;
}
int sprite_get_height(int sprid) {
return sprites.get(sprid).height;
}
gs_scalar sprite_get_texture_width_factor(int sprid, int subimg) {
const auto& spr2d = sprites.get(sprid);
return spr2d.GetTextureRect(spr2d.ModSubimage(subimg)).w;
}
gs_scalar sprite_get_texture_height_factor(int sprid, int subimg) {
const auto& spr2d = sprites.get(sprid);
return spr2d.GetTextureRect(spr2d.ModSubimage(subimg)).h;
}
int sprite_get_bbox_bottom(int sprid) {
return sprites.get(sprid).bbox.bottom();
}
int sprite_get_bbox_left(int sprid) {
return sprites.get(sprid).bbox.left();
}
int sprite_get_bbox_right(int sprid) {
return sprites.get(sprid).bbox.right();
}
int sprite_get_bbox_top(int sprid) {
return sprites.get(sprid).bbox.top();
}
int sprite_get_bbox_mode(int sprid) {
return sprites.get(sprid).bbox_mode;
}
int sprite_get_bbox_bottom_relative(int sprid) {
const Sprite& spr = sprites.get(sprid);
return spr.bbox.bottom() - spr.yoffset;
}
int sprite_get_bbox_left_relative(int sprid) {
const Sprite& spr = sprites.get(sprid);
return spr.bbox.left() - spr.xoffset;
}
int sprite_get_bbox_right_relative(int sprid) {
const Sprite& spr = sprites.get(sprid);
return spr.bbox.right() - spr.xoffset;
}
int sprite_get_bbox_top_relative(int sprid) {
const Sprite& spr = sprites.get(sprid);
return spr.bbox.top() - spr.yoffset;
}
int sprite_get_number(int sprid) {
return sprites.get(sprid).SubimageCount();
}
int sprite_get_texture(int sprid, int subimage) {
const auto& spr2d = sprites.get(sprid);
return spr2d.GetTexture(spr2d.ModSubimage(subimage));
}
int sprite_get_xoffset(int sprid) {
return sprites.get(sprid).xoffset;
}
int sprite_get_yoffset(int sprid) {
return sprites.get(sprid).yoffset;
}
int sprite_add(std::string filename, int imgnumb, bool precise, bool transparent, bool smooth, bool preload, int x_offset, int y_offset, bool mipmap) {
return sprites.add(sprite_add_helper(filename, imgnumb, precise, transparent, smooth, preload, x_offset, y_offset, mipmap));
}
int sprite_add(std::string filename, int imgnumb, bool transparent, bool smooth, int x_offset, int y_offset, bool mipmap) {
return sprite_add(filename, imgnumb, false, transparent, smooth, false, x_offset, y_offset, mipmap);
}
bool sprite_replace(int ind, std::string filename, int imgnumb, bool precise, bool transparent, bool smooth, bool preload,
int x_offset, int y_offset, bool free_texture, bool mipmap) {
if (free_texture) sprites.get(ind).FreeTextures();
return (sprites.replace(ind, sprite_add_helper(filename, imgnumb, precise, transparent, smooth, preload, x_offset, y_offset, mipmap)) != -1);
}
bool sprite_replace(int ind, std::string filename, int imgnumb, bool transparent, bool smooth, int x_offset, int y_offset,
bool free_texture, bool mipmap) {
return sprite_replace(ind, filename, imgnumb, false, transparent, smooth, false, x_offset, y_offset, free_texture, mipmap);
}
bool sprite_exists(int spr) {
return sprites.exists(spr);
}
void sprite_delete(int ind, bool free_texture) {
if (free_texture) sprites.get(ind).FreeTextures();
sprites.destroy(ind);
}
int sprite_duplicate(int ind) {
return sprites.duplicate(ind);
}
void sprite_assign(int ind, int copy_sprite, bool free_texture) {
if (free_texture) sprites.get(ind).FreeTextures();
Sprite copy = sprites.get(copy_sprite);
sprites.assign(ind, std::move(copy));
}
// NOTE: I doubt this method actually works properly on sprites of differing sizes
// testing needed
void sprite_merge(int ind, int copy_sprite) {
Sprite& spr1 = sprites.get(ind);
const Sprite& spr2 = sprites.get(copy_sprite);
// FIXME: this will break when we add functionality for removing subimages
for (size_t i = 0; i < spr2.SubimageCount(); ++i) {
spr1.AddSubimage(spr2.GetSubimage(i));
}
spr1.width = std::max(spr1.width, spr2.width);
spr1.height = std::max(spr1.height, spr2.height);
}
void sprite_set_offset(int ind, int xoff, int yoff) {
Sprite& spr = sprites.get(ind);
spr.xoffset = xoff;
spr.yoffset = yoff;
}
// FIXME: free_texture unused
void sprite_set_alpha_from_sprite(int ind, int copy_sprite, bool free_texture) {
Sprite& spr = sprites.get(ind);
const Sprite& spr_copy = sprites.get(copy_sprite);
// FIXME: this will break when we add functionality for removing subimages
for (size_t i = 0; i < spr.SubimageCount(); i++)
enigma::graphics_replace_texture_alpha_from_texture(spr.GetTexture(i), spr_copy.GetTexture(i % spr_copy.SubimageCount()));
}
// FIXME: This sets the mode but the mode is currently meaningless
void sprite_set_bbox_mode(int ind, int mode) {
sprites.get(ind).bbox_mode = mode;
}
void sprite_set_bbox(int ind, int left, int top, int right, int bottom) {
enigma::BoundingBox& bb = sprites.get(ind).bbox;
bb.x = left;
bb.y = top;
bb.w = right - left;
bb.h = bottom - top;
}
//FIXME: This only updates the bbox currently
void sprite_collision_mask(int ind, bool sepmasks, int mode, int left, int top, int right, int bottom, int kind,
unsigned char tolerance) {
sprite_set_bbox(ind, left, top, right, bottom);
}
var sprite_get_uvs(int ind, int subimg) {
var uvs;
uvs[4] = 0;
const enigma::Subimage& s = sprites.get(ind).GetSubimage(subimg);
uvs[0] = s.textureBounds.left();
uvs[1] = s.textureBounds.top();
uvs[2] = s.textureBounds.right();
uvs[3] = s.textureBounds.bottom();
return uvs;
}
void sprite_save(int ind, unsigned subimg, std::string fname) {
const Sprite& spr = sprites.get(ind);
if (spr.SubimageCount() <= subimg) {
DEBUG_MESSAGE("Requested subimage: " + std::to_string(subimg) + " out of range. Sprite: "
+ std::to_string(ind) + " only has " + std::to_string(spr.SubimageCount())
+ " subimages.", MESSAGE_TYPE::M_USER_ERROR);
return;
}
unsigned w, h;
unsigned char* rgbdata =
enigma::graphics_copy_texture_pixels(spr.GetSubimage(subimg).textureID, &w, &h);
enigma::image_save(fname, rgbdata, spr.width, spr.height, w, h, false);
delete[] rgbdata;
}
//void sprite_set_precise(int ind, bool precise); //FIXME: We don't support this yet
//void sprite_save_strip(int ind, std::string fname); //FIXME: We don't support this yet
int sprite_create_color(unsigned w, unsigned h, int col) {
RawImage img(new unsigned char[w * h * 4], w, h);
std::fill((unsigned*)(img.pxdata), (unsigned*)(img.pxdata) + w * h, (COL_GET_R(col) | (COL_GET_G(col) << 8) | (COL_GET_B(col) << 16) | 255 << 24));
Sprite s(w, h, 0, 0);
s.SetBBox(0, 0, w, h);
s.AddSubimage(img, enigma::ct_bbox, img.pxdata, false);
return sprites.add(std::move(s));
}
bool sprite_textures_equal(int id1, int subimg1, int id2, int subimg2) {
// Note: this will need to be amended to use textures_regions_equal when atlas support is implemented
return textures_equal(sprite_get_texture(id1, subimg1), sprite_get_texture(id2, subimg2));
}
uint32_t sprite_get_pixel(int id, int subimg, unsigned x, unsigned y) {
// Note: this will need to be amended when atlas support is implemented
return texture_get_pixel(sprite_get_texture(id, subimg), x, y);
}
}