| 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 |
#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 |
|
| 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 |
|
| 55 |
return (pixel & (f->Rmask | f->Gmask | f->Bmask)) | |
| 56 |
((Uint32)color.a << aShift); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 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 |
|
| 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 |
|
| 87 |
|
| 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 |
|
| 95 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |