Debian Codegraph @

src/cdogs/blit.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2020, 2023 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
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 // Character colors are embedded in pixels in two ways:
70 // - From source images, as color keys
71 // - E.g. pure red represents skin. This either means
72 // - G and B channel are near-zero and R channel is not, or
73 // - G and B channel are very close but not max, and R channel is at max
74 // - This is for ease of authoring
75 // - In-game, where colors are greyscale but alpha value is set to a special
76 // value
77 // - E.g. 254 alpha represents skin
78 // When character sprites are masked with character colors, it examines pixels
79 // with special alpha values and masks with the associated character color.
80 // Otherwise a plain mask is performed.
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 // don't convert greyscale colours
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 // Skin (R)
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 // Head parts (G)
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 // Arms (B)
114 return CHAR_COLOR_ARMS;
115 }
116 else if (c.b < 5 || (c.r > 250 && c.g > 250))
117 {
118 // Body (RG)
119 return CHAR_COLOR_BODY;
120 }
121 else if (c.r < 5 || (c.g > 250 && c.b > 250))
122 {
123 // Legs (GB)
124 return CHAR_COLOR_LEGS;
125 }
126 else if (c.g < 5 || (c.r > 250 && c.b > 250))
127 {
128 // Feet (RB)
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 }