| 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 |
#include "blit.h" |
| 29 |
|
| 30 |
#include <assert.h> |
| 31 |
#include <math.h> |
| 32 |
#include <stdbool.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#include <SDL.h> |
| 37 |
|
| 38 |
#include "config.h" |
| 39 |
#include "log.h" |
| 40 |
|
| 41 |
color_t *CharColorGetByType(CharColors *c, const CharColorType t) |
| 42 |
{ |
| 43 |
switch (t) |
| 44 |
{ |
| 45 |
case CHAR_COLOR_SKIN: |
| 46 |
return &c->Skin; |
| 47 |
case CHAR_COLOR_ARMS: |
| 48 |
return &c->Arms; |
| 49 |
case CHAR_COLOR_BODY: |
| 50 |
return &c->Body; |
| 51 |
case CHAR_COLOR_LEGS: |
| 52 |
return &c->Legs; |
| 53 |
case CHAR_COLOR_HAIR: |
| 54 |
return &c->Hair; |
| 55 |
case CHAR_COLOR_FEET: |
| 56 |
return &c->Feet; |
| 57 |
case CHAR_COLOR_FACEHAIR: |
| 58 |
return &c->Facehair; |
| 59 |
case CHAR_COLOR_HAT: |
| 60 |
return &c->Hat; |
| 61 |
case CHAR_COLOR_GLASSES: |
| 62 |
return &c->Glasses; |
| 63 |
default: |
| 64 |
CASSERT(false, "Unexpected colour"); |
| 65 |
return &c->Skin; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
uint8_t CharColorTypeAlpha(const CharColorType t) |
| 83 |
{ |
| 84 |
static uint8_t alphas[] = {254, 253, 252, 251, 250, 249, 248, 247, 246}; |
| 85 |
if (t < CHAR_COLOR_COUNT) |
| 86 |
{ |
| 87 |
return alphas[t]; |
| 88 |
} |
| 89 |
return 255; |
| 90 |
} |
| 91 |
#define CHAR_COLOR_THRESHOLD 5 |
| 92 |
CharColorType CharColorTypeFromColor(const color_t c, const CharColorType headPartColor) |
| 93 |
{ |
| 94 |
if (abs((int)c.r - c.g) < CHAR_COLOR_THRESHOLD && |
| 95 |
abs((int)c.g - c.b) < CHAR_COLOR_THRESHOLD && |
| 96 |
abs((int)c.r - c.b) < CHAR_COLOR_THRESHOLD) |
| 97 |
{ |
| 98 |
|
| 99 |
return CHAR_COLOR_COUNT; |
| 100 |
} |
| 101 |
if ((c.g < 5 && c.b < 5) || (abs((int)c.g - c.b) < 5 && c.r > 250)) |
| 102 |
{ |
| 103 |
|
| 104 |
return CHAR_COLOR_SKIN; |
| 105 |
} |
| 106 |
else if ((c.r < 5 && c.b < 5) || (abs((int)c.r - c.b) < 5 && c.g > 250)) |
| 107 |
{ |
| 108 |
|
| 109 |
return headPartColor; |
| 110 |
} |
| 111 |
else if ((c.r < 5 && c.g < 5) || (abs((int)c.r - c.g) < 5 && c.b > 250)) |
| 112 |
{ |
| 113 |
|
| 114 |
return CHAR_COLOR_ARMS; |
| 115 |
} |
| 116 |
else if (c.b < 5 || (c.r > 250 && c.g > 250)) |
| 117 |
{ |
| 118 |
|
| 119 |
return CHAR_COLOR_BODY; |
| 120 |
} |
| 121 |
else if (c.r < 5 || (c.g > 250 && c.b > 250)) |
| 122 |
{ |
| 123 |
|
| 124 |
return CHAR_COLOR_LEGS; |
| 125 |
} |
| 126 |
else if (c.g < 5 || (c.r > 250 && c.b > 250)) |
| 127 |
{ |
| 128 |
|
| 129 |
return CHAR_COLOR_FEET; |
| 130 |
} |
| 131 |
|
| 132 |
return CHAR_COLOR_COUNT; |
| 133 |
} |
| 134 |
CharColors CharColorsFromOneColor(const color_t color) |
| 135 |
{ |
| 136 |
CharColors c = {color, color, color, color, color, color, color, color, color}; |
| 137 |
return c; |
| 138 |
} |
| 139 |
color_t CharColorsGetChannelMask(const CharColors *c, const uint8_t alpha) |
| 140 |
{ |
| 141 |
switch (alpha) |
| 142 |
{ |
| 143 |
case 255: |
| 144 |
return colorWhite; |
| 145 |
case 254: |
| 146 |
return c->Skin; |
| 147 |
case 253: |
| 148 |
return c->Arms; |
| 149 |
case 252: |
| 150 |
return c->Body; |
| 151 |
case 251: |
| 152 |
return c->Legs; |
| 153 |
case 250: |
| 154 |
return c->Hair; |
| 155 |
case 249: |
| 156 |
return c->Feet; |
| 157 |
case 248: |
| 158 |
return c->Facehair; |
| 159 |
case 247: |
| 160 |
return c->Hat; |
| 161 |
case 246: |
| 162 |
return c->Glasses; |
| 163 |
default: |
| 164 |
return colorWhite; |
| 165 |
} |
| 166 |
} |
| 167 |
void CharColorsGetMaskedName(char *buf, const char *base, const CharColors *c) |
| 168 |
{ |
| 169 |
char bufSkin[COLOR_STR_BUF], bufArms[COLOR_STR_BUF], |
| 170 |
bufBody[COLOR_STR_BUF], bufLegs[COLOR_STR_BUF], bufHair[COLOR_STR_BUF], |
| 171 |
bufFeet[COLOR_STR_BUF], bufFacehair[COLOR_STR_BUF], bufHat[COLOR_STR_BUF], |
| 172 |
bufGlasses[COLOR_STR_BUF]; |
| 173 |
ColorStr(bufSkin, c->Skin); |
| 174 |
ColorStr(bufArms, c->Arms); |
| 175 |
ColorStr(bufBody, c->Body); |
| 176 |
ColorStr(bufLegs, c->Legs); |
| 177 |
ColorStr(bufHair, c->Hair); |
| 178 |
ColorStr(bufFeet, c->Feet); |
| 179 |
ColorStr(bufFacehair, c->Facehair); |
| 180 |
ColorStr(bufHat, c->Hat); |
| 181 |
ColorStr(bufGlasses, c->Glasses); |
| 182 |
sprintf( |
| 183 |
buf, "%s/%s/%s/%s/%s/%s/%s/%s/%s/%s", base, bufSkin, bufArms, bufBody, bufLegs, |
| 184 |
bufHair, bufFeet, bufFacehair, bufHat, bufGlasses); |
| 185 |
} |
| 186 |
|
| 187 |
void BlitClearBuf(GraphicsDevice *g) |
| 188 |
{ |
| 189 |
memset(g->buf, 0, GraphicsGetMemSize(&g->cachedConfig)); |
| 190 |
} |
| 191 |
void BlitFillBuf(GraphicsDevice *g, const color_t c) |
| 192 |
{ |
| 193 |
const Uint32 pixel = COLOR2PIXEL(c); |
| 194 |
RECT_FOREACH(Rect2iNew(svec2i_zero(), g->cachedConfig.Res)) |
| 195 |
g->buf[_i] = pixel; |
| 196 |
RECT_FOREACH_END() |
| 197 |
} |
| 198 |
void BlitUpdateFromBuf(GraphicsDevice *g, SDL_Texture *t) |
| 199 |
{ |
| 200 |
if (SDL_UpdateTexture( |
| 201 |
t, NULL, g->buf, g->cachedConfig.Res.x * sizeof(Uint32)) != 0) |
| 202 |
{ |
| 203 |
LOG(LM_GFX, LL_ERROR, "cannot update texture: %s", SDL_GetError()); |
| 204 |
} |
| 205 |
} |