Debian Codegraph @

src/cdogs/draw/drawtools.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 2003-2007 Lucas Martin-King
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 This file incorporates work covered by the following copyright and
21 permission notice:
22
23 Copyright (c) 2013-2014, 2018-2019 Cong Xu
24 All rights reserved.
25
26 Redistribution and use in source and binary forms, with or without
27 modification, are permitted provided that the following conditions are met:
28
29 Redistributions of source code must retain the above copyright notice, this
30 list of conditions and the following disclaimer.
31 Redistributions in binary form must reproduce the above copyright notice,
32 this list of conditions and the following disclaimer in the documentation
33 and/or other materials provided with the distribution.
34
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
39 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 POSSIBILITY OF SUCH DAMAGE.
46 */
47
48 #include <stdio.h>
49
50 #include "algorithms.h"
51 #include "config.h"
52 #include "draw/drawtools.h"
53 #include "log.h"
54 #include "palette.h"
55 #include "pic_manager.h"
56 #include "texture.h"
57 #include "utils.h"
58 #include "blit.h"
59 #include "grafx.h"
60
61
62 void DrawPoint(const struct vec2i pos, const color_t c)
63 {
64 if (SDL_SetRenderDrawBlendMode(
65 gGraphicsDevice.gameWindow.renderer, SDL_BLENDMODE_BLEND) != 0)
66 {
67 LOG(LM_GFX, LL_ERROR, "Failed to set draw blend mode: %s",
68 SDL_GetError());
69 }
70 if (SDL_SetRenderDrawColor(
71 gGraphicsDevice.gameWindow.renderer, c.r, c.g, c.b, c.a) != 0)
72 {
73 LOG(LM_GFX, LL_ERROR, "Failed to set draw color: %s",
74 SDL_GetError());
75 }
76 if (SDL_RenderDrawPoint(
77 gGraphicsDevice.gameWindow.renderer, pos.x, pos.y) != 0)
78 {
79 LOG(LM_GFX, LL_ERROR, "Failed to render point: %s", SDL_GetError());
80 }
81 }
82
83 static
84 void
85 Draw_StraightLine(
86 const int x1, const int y1, const int x2, const int y2, color_t c)
87 {
88 register int i;
89 register int start, end;
90
91 if (x1 == x2) { /* vertical line */
92 if (y2 > y1) {
93 start = y1;
94 end = y2;
95 } else {
96 start = y2;
97 end = y1;
98 }
99
100 for (i = start; i <= end; i++) {
101 DrawPoint(svec2i(x1, i), c);
102 }
103 } else if (y1 == y2) { /* horizontal line */
104 if (x2 > x1) {
105 start = x1;
106 end = x2;
107 } else {
108 start = x2;
109 end = x1;
110 }
111
112 for (i = start; i <= end; i++) {
113 DrawPoint(svec2i(i, y1), c);
114 }
115 }
116 return;
117 }
118
119 static void Draw_DiagonalLine(const int x1, const int x2, color_t c)
120 {
121 register int i;
122 register int start, end;
123
124 if (x1 < x2) {
125 start = x1;
126 end = x2;
127 } else {
128 start = x2;
129 end = x1;
130 }
131
132 for (i = start; i < end; i++) {
133 DrawPoint(svec2i(i, i), c);
134 }
135
136 return;
137 }
138
139 static void DrawPointFunc(void *data, const struct vec2i pos);
140 void DrawLine(const struct vec2i from, const struct vec2i to, color_t c)
141 {
142 AlgoLineDrawData data;
143 data.Draw = DrawPointFunc;
144 data.data = &c;
145 BresenhamLineDraw(from, to, &data);
146 }
147 static void DrawPointFunc(void *data, const struct vec2i pos)
148 {
149 const color_t *c = data;
150 DrawPoint(pos, *c);
151 }
152
153 void Draw_Line(
154 const int x1, const int y1, const int x2, const int y2, color_t c)
155 {
156 if (x1 == x2 || y1 == y2)
157 Draw_StraightLine(x1, y1, x2, y2, c);
158 else if (abs((x2 - x1)) == abs((y1 - y2)))
159 Draw_DiagonalLine(x1, x2, c);
160 /*else
161 Draw_OtherLine(x1, y1, x2, y2, c);*/
162
163 return;
164 }
165
166 void DrawRectangle(
167 GraphicsDevice *g, const struct vec2i pos, const struct vec2i size,
168 const color_t color, const bool filled)
169 {
170 if (SDL_SetRenderDrawBlendMode(
171 g->gameWindow.renderer, SDL_BLENDMODE_BLEND) != 0)
172 {
173 LOG(LM_GFX, LL_ERROR, "Failed to set draw blend mode: %s",
174 SDL_GetError());
175 }
176 if (SDL_SetRenderDrawColor(
177 g->gameWindow.renderer, color.r, color.g, color.b, color.a) != 0)
178 {
179 LOG(LM_GFX, LL_ERROR, "Failed to set draw color: %s",
180 SDL_GetError());
181 }
182 const SDL_Rect rect = { pos.x, pos.y, size.x, size.y };
183 const int result =
184 filled ?
185 SDL_RenderFillRect(g->gameWindow.renderer, &rect) :
186 SDL_RenderDrawRect(g->gameWindow.renderer, &rect);
187 if (result != 0)
188 {
189 LOG(LM_GFX, LL_ERROR, "Failed to render rect: %s", SDL_GetError());
190 }
191 }
192
193 void DrawCross(GraphicsDevice *g, const struct vec2i pos, const color_t c)
194 {
195 if (SDL_SetRenderDrawBlendMode(
196 g->gameWindow.renderer, SDL_BLENDMODE_BLEND) != 0)
197 {
198 LOG(LM_GFX, LL_ERROR, "Failed to set draw blend mode: %s",
199 SDL_GetError());
200 }
201 if (SDL_SetRenderDrawColor(g->gameWindow.renderer, c.r, c.g, c.b, c.a) != 0)
202 {
203 LOG(LM_GFX, LL_ERROR, "Failed to set draw color: %s",
204 SDL_GetError());
205 }
206 if (SDL_RenderDrawLine(
207 g->gameWindow.renderer, pos.x - 1, pos.y, pos.x + 1, pos.y) != 0)
208 {
209 LOG(LM_GFX, LL_ERROR, "Failed to render line: %s", SDL_GetError());
210 }
211 if (SDL_RenderDrawLine(
212 g->gameWindow.renderer, pos.x, pos.y - 1, pos.x, pos.y + 1) != 0)
213 {
214 LOG(LM_GFX, LL_ERROR, "Failed to render line: %s", SDL_GetError());
215 }
216 }
217
218 void DrawShadow(
219 GraphicsDevice *g, const struct vec2i pos, const struct vec2 scale,
220 const color_t mask)
221 {
222 if (!ConfigGetBool(&gConfig, "Graphics.Shadows") ||
223 ColorEquals(mask, colorTransparent))
224 {
225 return;
226 }
227 const Pic *shadow = PicManagerGetPic(&gPicManager, "particles/shadow");
228 const struct vec2 drawScale =
229 svec2_divide(svec2_scale(scale, 2), svec2_assign_vec2i(shadow->size));
230 const struct vec2i drawPos = svec2i_subtract(pos, svec2i_assign_vec2(scale));
231 PicRender(
232 shadow, g->gameWindow.renderer, drawPos, mask, 0,
233 drawScale, SDL_FLIP_NONE, Rect2iZero());
234 }