| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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) { |
| 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) { |
| 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 |
|
| 161 |
|
| 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 |
} |