Debian Codegraph @

src/cdogs/pic.c

raw symbols SCIP
1 /*
2 Copyright (c) 2013-2016, 2018-2019, 2024 Cong Xu
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 POSSIBILITY OF SUCH DAMAGE.
25 */
26 #include "pic.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "c_hashmap/hashmap.h"
32 #include "defs.h"
33 #include "grafx.h"
34 #include "log.h"
35 #include "texture.h"
36 #include "utils.h"
37
38 map_t textureDebugger = NULL;
39
40
41 color_t PixelToColor(
42 const SDL_PixelFormat *f, const Uint8 aShift, const Uint32 pixel)
43 {
44 color_t c;
45 SDL_GetRGBA(pixel, f, &c.r, &c.g, &c.b, &c.a);
46 // Manually apply the alpha as SDL seems to always set it to 0
47 c.a = (Uint8)((pixel & ~(f->Rmask | f->Gmask | f->Bmask)) >> aShift);
48 return c;
49 }
50 Uint32 ColorToPixel(
51 const SDL_PixelFormat *f, const Uint8 aShift, const color_t color)
52 {
53 const Uint32 pixel = SDL_MapRGBA(f, color.r, color.g, color.b, color.a);
54 // Manually apply the alpha as SDL seems to always set it to 0
55 return (pixel & (f->Rmask | f->Gmask | f->Bmask)) |
56 ((Uint32)color.a << aShift);
57 }
58
59 // Get the true pixel size of the pic
60 static struct vec2i PicPixelSize(const Pic *p)
61 {
62 if (p->isHD)
63 {
64 return svec2i_scale(p->size, 2);
65 }
66 return p->size;
67 }
68
69 void PicLoad(
70 Pic *p, const struct vec2i size, const struct vec2i offset, const SDL_Surface *image, const bool isHD)
71 {
72 memset(p, 0, sizeof *p);
73 p->size = size;
74 // Pretend to be half the size for HD pics
75 p->isHD = isHD;
76 if (isHD)
77 {
78 p->size = svec2i_scale_divide(p->size, 2);
79 }
80 p->offset = svec2i_zero();
81 CMALLOC(p->Data, size.x * size.y * sizeof *((Pic *)0)->Data);
82 if (p->Data == NULL)
83 {
84 return;
85 }
86 // Manually copy the pixels and replace the alpha component,
87 // since our gfx device format has no alpha
88 int srcI = offset.y*image->w + offset.x;
89 for (int i = 0; i < size.x * size.y; i++, srcI++)
90 {
91 const Uint32 pixel = ((Uint32 *)image->pixels)[srcI];
92 color_t c;
93 SDL_GetRGBA(pixel, image->format, &c.r, &c.g, &c.b, &c.a);
94 // If completely transparent, replace rgb with black (0) too
95 // This is because transparency blitting checks entire pixel
96 if (c.a == 0)
97 {
98 p->Data[i] = 0;
99 }
100 else
101 {
102 p->Data[i] = COLOR2PIXEL(c);
103 }
104 if ((i + 1) % size.x == 0)
105 {
106 srcI += image->w - size.x;
107 }
108 }
109
110 if (!PicTryMakeTex(p))
111 {
112 goto bail;
113 }
114 return;
115
116 bail:
117 PicFree(p);
118 }
119 bool PicTryMakeTex(Pic *p)
120 {
121 CASSERT(!PicIsNone(p), "cannot make tex of none pic");
122 if (textureDebugger == NULL)
123 {
124 textureDebugger = hashmap_new();
125 }
126 if (p->Tex != NULL)
127 {
128 LOG(LM_GFX, LL_TRACE, "destroying texture %p data(%p)", p->Tex, p->Data);
129 SDL_DestroyTexture(p->Tex);
130 if (LL_TRACE >= LogModuleGetLevel(LM_GFX))
131 {
132 char key[32];
133 sprintf(key, "%p", p->Tex);
134 if (hashmap_get(textureDebugger, key, NULL) == MAP_OK)
135 {
136 if (hashmap_remove(textureDebugger, key) != MAP_OK)
137 {
138 LOG(LM_GFX, LL_TRACE, "Error: cannot remove tex from debugger");
139 }
140 else
141 {
142 LOG(LM_GFX, LL_TRACE, "Texture count: %d",
143 hashmap_length(textureDebugger));
144 }
145 }
146 else
147 {
148 LOG(LM_GFX, LL_TRACE, "Error: destroying unknown texture");
149 }
150 }
151 }
152 const struct vec2i size = PicPixelSize(p);
153 p->Tex = TextureCreate(
154 gGraphicsDevice.gameWindow.renderer, SDL_TEXTUREACCESS_STATIC,
155 size, SDL_BLENDMODE_NONE, 255);
156 if (p->Tex == NULL)
157 {
158 LOG(LM_GFX, LL_ERROR, "cannot create texture: %s", SDL_GetError());
159 return false;
160 }
161 if (SDL_UpdateTexture(
162 p->Tex, NULL, p->Data, size.x * sizeof(Uint32)) != 0)
163 {
164 LOG(LM_GFX, LL_ERROR, "cannot update texture: %s", SDL_GetError());
165 return false;
166 }
167 if (SDL_SetTextureBlendMode(p->Tex, SDL_BLENDMODE_BLEND) != 0)
168 {
169 LOG(LM_GFX, LL_ERROR, "cannot set texture blend mode: %s",
170 SDL_GetError());
171 return false;
172 }
173 LOG(LM_GFX, LL_TRACE, "made texture %p data(%p) count(%d)",
174 p->Tex, p->Data, hashmap_length(textureDebugger));
175 if (LL_TRACE >= LogModuleGetLevel(LM_GFX))
176 {
177 char key[32];
178 sprintf(key, "%p", p->Tex);
179 if (hashmap_get(textureDebugger, key, NULL) != MAP_MISSING)
180 {
181 LOG(LM_GFX, LL_TRACE, "Error: repeated texture loc");
182 }
183 if (hashmap_put(textureDebugger, key, (any_t)0) != MAP_OK)
184 {
185 LOG(LM_GFX, LL_TRACE, "Error: cannot add texture to debugger");
186 }
187 }
188 return true;
189 }
190
191 // Note: does not copy the texture
192 Pic PicCopy(const Pic *src)
193 {
194 Pic p = *src;
195 const struct vec2i psize = PicPixelSize(src);
196 const size_t size = psize.x * psize.y * sizeof *p.Data;
197 CMALLOC(p.Data, size);
198 memcpy(p.Data, src->Data, size);
199 p.Tex = NULL;
200 p.isHD = src->isHD;
201 return p;
202 }
203
204 void PicFree(Pic *pic)
205 {
206 if (pic->Tex != NULL)
207 {
208 LOG(LM_GFX, LL_TRACE, "freeing texture %p data(%p)", pic->Tex, pic->Data);
209 SDL_DestroyTexture(pic->Tex);
210 if (LL_TRACE >= LogModuleGetLevel(LM_GFX))
211 {
212 char key[32];
213 sprintf(key, "%p", pic->Tex);
214 if (hashmap_get(textureDebugger, key, NULL) == MAP_OK)
215 {
216 if (hashmap_remove(textureDebugger, key) != MAP_OK)
217 {
218 LOG(LM_GFX, LL_TRACE, "Error: cannot remove tex from debugger");
219 }
220 else
221 {
222 LOG(LM_GFX, LL_TRACE, "Texture count: %d",
223 hashmap_length(textureDebugger));
224 }
225 }
226 else
227 {
228 LOG(LM_GFX, LL_TRACE, "Error: destroying unknown texture");
229 }
230 }
231 }
232 pic->size = svec2i_zero();
233 CFREE(pic->Data);
234 pic->Data = NULL;
235 }
236
237 bool PicIsNone(const Pic *pic)
238 {
239 return pic->size.x == 0 || pic->size.y == 0 || pic->Data == NULL;
240 }
241
242 void PicTrim(Pic *pic, const bool xTrim, const bool yTrim)
243 {
244 // Scan all pixels looking for the min/max of x and y
245 const struct vec2i size = PicPixelSize(pic);
246 struct vec2i min = size;
247 struct vec2i max = svec2i_zero();
248 for (struct vec2i pos = svec2i_zero(); pos.y < size.y; pos.y++)
249 {
250 for (pos.x = 0; pos.x < size.x; pos.x++)
251 {
252 const Uint32 pixel = *(pic->Data + pos.x + pos.y * size.x);
253 if (pixel > 0)
254 {
255 min.x = MIN(min.x, pos.x);
256 min.y = MIN(min.y, pos.y);
257 max.x = MAX(max.x, pos.x);
258 max.y = MAX(max.y, pos.y);
259 }
260 }
261 }
262 // If no opaque pixels found, don't trim
263 struct vec2i newSize = size;
264 struct vec2i offset = svec2i_zero();
265 if (min.x < max.x && min.y < max.y)
266 {
267 if (xTrim)
268 {
269 newSize.x = max.x - min.x + 1;
270 offset.x = min.x;
271 }
272 if (yTrim)
273 {
274 newSize.y = max.y - min.y + 1;
275 offset.y = min.y;
276 }
277 }
278 PicShrink(pic, newSize, offset);
279 }
280 void PicShrink(Pic *pic, const struct vec2i size, const struct vec2i offset)
281 {
282 // Trim by copying pixels
283 Uint32 *newData;
284 CMALLOC(newData, size.x * size.y * sizeof *newData);
285 if (newData == NULL)
286 {
287 return;
288 }
289 for (struct vec2i pos = svec2i_zero(); pos.y < size.y; pos.y++)
290 {
291 for (pos.x = 0; pos.x < size.x; pos.x++)
292 {
293 Uint32 *target = newData + pos.x + pos.y * size.x;
294 const int srcIdx =
295 pos.x + offset.x + (pos.y + offset.y) * pic->size.x;
296 *target = *(pic->Data + srcIdx);
297 }
298 }
299 // Replace the old data
300 CFREE(pic->Data);
301 pic->Data = newData;
302 pic->size = size;
303 if (pic->isHD)
304 {
305 pic->size = svec2i_scale_divide(pic->size, 2);
306 }
307 pic->offset = svec2i_zero();
308 PicTryMakeTex(pic);
309 }
310
311 color_t PicGetRandomColor(const Pic *p)
312 {
313 // Get a random non-transparent pixel from the pic
314 const struct vec2i size = PicPixelSize(p);
315 for (;;)
316 {
317 const uint32_t px = p->Data[rand() % (size.x * size.y)];
318 const color_t c = PIXEL2COLOR(px);
319 if (c.a > 0)
320 {
321 return c;
322 }
323 }
324 }
325
326 void PicRender(
327 const Pic *p, SDL_Renderer *r, const struct vec2i pos, const color_t mask,
328 const double radians, const struct vec2 scale, const SDL_RendererFlip flip,
329 const Rect2i srcRect)
330 {
331 Rect2i src = Rect2iNew(
332 svec2i_max(srcRect.Pos, svec2i_zero()), svec2i_zero()
333 );
334 const struct vec2i srcSize = PicPixelSize(p);
335 src.Size = svec2i_is_zero(srcRect.Size) ? srcSize :
336 svec2i_min(svec2i_subtract(srcRect.Size, src.Pos), srcSize);
337 Rect2i dest = Rect2iNew(pos, src.Size);
338 // Apply scale to render dest
339 const bool unscaled = svec2_is_equal(scale, svec2_one());
340 const struct vec2 destScale = svec2_scale(scale, p->isHD ? 0.5f : 1);
341 if (!unscaled)
342 {
343 dest.Pos.x -= (mint_t)MROUND((destScale.x - 1) * src.Size.x / 2);
344 dest.Pos.y -= (mint_t)MROUND((destScale.y - 1) * src.Size.y / 2);
345 dest.Size.x = (mint_t)MROUND(src.Size.x * destScale.x);
346 dest.Size.y = (mint_t)MROUND(src.Size.y * destScale.y);
347 }
348 else if (p->isHD)
349 {
350 dest.Size.x = (mint_t)MROUND(src.Size.x * destScale.x);
351 dest.Size.y = (mint_t)MROUND(src.Size.y * destScale.y);
352 }
353 const double angle = ToDegrees(radians);
354 TextureRender(p->Tex, r, src, dest, mask, angle, flip);
355 }