Atlas - SDL_draw.h
Home / ext / SDL / src / render / software Lines: 1 | Size: 36870 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22#include "SDL_internal.h" 23 24#include "../../video/SDL_surface_c.h" 25 26/* This code assumes that r, g, b, a are the source color, 27 * and in the blend and add case, the RGB values are premultiplied by a. 28 */ 29 30#define DRAW_MUL(_a, _b) (((unsigned)(_a) * (_b)) / 255) 31 32#define DRAW_FASTSETPIXEL(type) \ 33 *pixels = (type)color 34 35#define DRAW_FASTSETPIXEL1 DRAW_FASTSETPIXEL(Uint8) 36#define DRAW_FASTSETPIXEL2 DRAW_FASTSETPIXEL(Uint16) 37#define DRAW_FASTSETPIXEL4 DRAW_FASTSETPIXEL(Uint32) 38 39#define DRAW_FASTSETPIXELXY(x, y, type, bpp, color) \ 40 *(type *)((Uint8 *)dst->pixels + (y)*dst->pitch + (x)*bpp) = (type)color 41 42#define DRAW_FASTSETPIXELXY1(x, y) DRAW_FASTSETPIXELXY(x, y, Uint8, 1, color) 43#define DRAW_FASTSETPIXELXY2(x, y) DRAW_FASTSETPIXELXY(x, y, Uint16, 2, color) 44#define DRAW_FASTSETPIXELXY4(x, y) DRAW_FASTSETPIXELXY(x, y, Uint32, 4, color) 45 46#define DRAW_SETPIXEL(setpixel) \ 47 do { \ 48 unsigned sr = r, sg = g, sb = b, sa = a; \ 49 (void)sa; \ 50 setpixel; \ 51 } while (0) 52 53#define DRAW_SETPIXEL_BLEND(getpixel, setpixel) \ 54 do { \ 55 unsigned sr, sg, sb, sa = 0xFF; \ 56 getpixel; \ 57 sr = DRAW_MUL(inva, sr) + r; \ 58 sg = DRAW_MUL(inva, sg) + g; \ 59 sb = DRAW_MUL(inva, sb) + b; \ 60 sa = DRAW_MUL(inva, sa) + a; \ 61 setpixel; \ 62 } while (0) 63 64#define DRAW_SETPIXEL_BLEND_CLAMPED(getpixel, setpixel) \ 65 do { \ 66 unsigned sr, sg, sb, sa = 0xFF; \ 67 getpixel; \ 68 sr = DRAW_MUL(inva, sr) + r; \ 69 if (sr > 0xff) \ 70 sr = 0xff; \ 71 sg = DRAW_MUL(inva, sg) + g; \ 72 if (sg > 0xff) \ 73 sg = 0xff; \ 74 sb = DRAW_MUL(inva, sb) + b; \ 75 if (sb > 0xff) \ 76 sb = 0xff; \ 77 sa = DRAW_MUL(inva, sa) + a; \ 78 if (sa > 0xff) \ 79 sa = 0xff; \ 80 setpixel; \ 81 } while (0) 82 83#define DRAW_SETPIXEL_ADD(getpixel, setpixel) \ 84 do { \ 85 unsigned sr, sg, sb, sa; \ 86 (void)sa; \ 87 getpixel; \ 88 sr += r; \ 89 if (sr > 0xff) \ 90 sr = 0xff; \ 91 sg += g; \ 92 if (sg > 0xff) \ 93 sg = 0xff; \ 94 sb += b; \ 95 if (sb > 0xff) \ 96 sb = 0xff; \ 97 setpixel; \ 98 } while (0) 99 100#define DRAW_SETPIXEL_MOD(getpixel, setpixel) \ 101 do { \ 102 unsigned sr, sg, sb, sa; \ 103 (void)sa; \ 104 getpixel; \ 105 sr = DRAW_MUL(sr, r); \ 106 sg = DRAW_MUL(sg, g); \ 107 sb = DRAW_MUL(sb, b); \ 108 setpixel; \ 109 } while (0) 110 111#define DRAW_SETPIXEL_MUL(getpixel, setpixel) \ 112 do { \ 113 unsigned sr, sg, sb, sa; \ 114 (void)sa; \ 115 getpixel; \ 116 sr = DRAW_MUL(sr, r) + DRAW_MUL(inva, sr); \ 117 if (sr > 0xff) \ 118 sr = 0xff; \ 119 sg = DRAW_MUL(sg, g) + DRAW_MUL(inva, sg); \ 120 if (sg > 0xff) \ 121 sg = 0xff; \ 122 sb = DRAW_MUL(sb, b) + DRAW_MUL(inva, sb); \ 123 if (sb > 0xff) \ 124 sb = 0xff; \ 125 setpixel; \ 126 } while (0) 127 128#define DRAW_SETPIXELXY(x, y, type, bpp, op) \ 129 do { \ 130 type *pixels = (type *)((Uint8 *)dst->pixels + (y)*dst->pitch + (x)*bpp);\ 131 op; \ 132 } while (0) 133 134/* 135 * Define draw operators for RGB555 136 */ 137 138#define DRAW_SETPIXEL_RGB555 \ 139 DRAW_SETPIXEL(RGB555_FROM_RGB(*pixels, sr, sg, sb)) 140 141#define DRAW_SETPIXEL_BLEND_RGB555 \ 142 DRAW_SETPIXEL_BLEND(RGB_FROM_RGB555(*pixels, sr, sg, sb),\ 143 RGB555_FROM_RGB(*pixels, sr, sg, sb)) 144 145#define DRAW_SETPIXEL_BLEND_CLAMPED_RGB555 \ 146 DRAW_SETPIXEL_BLEND_CLAMPED(RGB_FROM_RGB555(*pixels, sr, sg, sb),\ 147 RGB555_FROM_RGB(*pixels, sr, sg, sb)) 148 149#define DRAW_SETPIXEL_ADD_RGB555 \ 150 DRAW_SETPIXEL_ADD(RGB_FROM_RGB555(*pixels, sr, sg, sb),\ 151 RGB555_FROM_RGB(*pixels, sr, sg, sb)) 152 153#define DRAW_SETPIXEL_MOD_RGB555 \ 154 DRAW_SETPIXEL_MOD(RGB_FROM_RGB555(*pixels, sr, sg, sb),\ 155 RGB555_FROM_RGB(*pixels, sr, sg, sb)) 156 157#define DRAW_SETPIXEL_MUL_RGB555 \ 158 DRAW_SETPIXEL_MUL(RGB_FROM_RGB555(*pixels, sr, sg, sb),\ 159 RGB555_FROM_RGB(*pixels, sr, sg, sb)) 160 161#define DRAW_SETPIXELXY_RGB555(x, y) \ 162 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB555) 163 164#define DRAW_SETPIXELXY_BLEND_RGB555(x, y) \ 165 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB555) 166 167#define DRAW_SETPIXELXY_BLEND_CLAMPED_RGB555(x, y) \ 168 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_CLAMPED_RGB555) 169 170#define DRAW_SETPIXELXY_ADD_RGB555(x, y) \ 171 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB555) 172 173#define DRAW_SETPIXELXY_MOD_RGB555(x, y) \ 174 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB555) 175 176#define DRAW_SETPIXELXY_MUL_RGB555(x, y) \ 177 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MUL_RGB555) 178 179/* 180 * Define draw operators for RGB565 181 */ 182 183#define DRAW_SETPIXEL_RGB565 \ 184 DRAW_SETPIXEL(RGB565_FROM_RGB(*pixels, sr, sg, sb)) 185 186#define DRAW_SETPIXEL_BLEND_RGB565 \ 187 DRAW_SETPIXEL_BLEND(RGB_FROM_RGB565(*pixels, sr, sg, sb),\ 188 RGB565_FROM_RGB(*pixels, sr, sg, sb)) 189 190#define DRAW_SETPIXEL_BLEND_CLAMPED_RGB565 \ 191 DRAW_SETPIXEL_BLEND_CLAMPED(RGB_FROM_RGB565(*pixels, sr, sg, sb),\ 192 RGB565_FROM_RGB(*pixels, sr, sg, sb)) 193 194#define DRAW_SETPIXEL_ADD_RGB565 \ 195 DRAW_SETPIXEL_ADD(RGB_FROM_RGB565(*pixels, sr, sg, sb),\ 196 RGB565_FROM_RGB(*pixels, sr, sg, sb)) 197 198#define DRAW_SETPIXEL_MOD_RGB565 \ 199 DRAW_SETPIXEL_MOD(RGB_FROM_RGB565(*pixels, sr, sg, sb),\ 200 RGB565_FROM_RGB(*pixels, sr, sg, sb)) 201 202#define DRAW_SETPIXEL_MUL_RGB565 \ 203 DRAW_SETPIXEL_MUL(RGB_FROM_RGB565(*pixels, sr, sg, sb),\ 204 RGB565_FROM_RGB(*pixels, sr, sg, sb)) 205 206#define DRAW_SETPIXELXY_RGB565(x, y) \ 207 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB565) 208 209#define DRAW_SETPIXELXY_BLEND_RGB565(x, y) \ 210 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB565) 211 212#define DRAW_SETPIXELXY_BLEND_CLAMPED_RGB565(x, y) \ 213 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_CLAMPED_RGB565) 214 215#define DRAW_SETPIXELXY_ADD_RGB565(x, y) \ 216 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB565) 217 218#define DRAW_SETPIXELXY_MOD_RGB565(x, y) \ 219 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB565) 220 221#define DRAW_SETPIXELXY_MUL_RGB565(x, y) \ 222 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MUL_RGB565) 223 224/* 225 * Define draw operators for RGB888 226 */ 227 228#define DRAW_SETPIXEL_XRGB8888 \ 229 DRAW_SETPIXEL(XRGB8888_FROM_RGB(*pixels, sr, sg, sb)) 230 231#define DRAW_SETPIXEL_BLEND_XRGB8888 \ 232 DRAW_SETPIXEL_BLEND(RGB_FROM_XRGB8888(*pixels, sr, sg, sb),\ 233 XRGB8888_FROM_RGB(*pixels, sr, sg, sb)) 234 235#define DRAW_SETPIXEL_BLEND_CLAMPED_XRGB8888 \ 236 DRAW_SETPIXEL_BLEND_CLAMPED(RGB_FROM_XRGB8888(*pixels, sr, sg, sb),\ 237 XRGB8888_FROM_RGB(*pixels, sr, sg, sb)) 238 239#define DRAW_SETPIXEL_ADD_XRGB8888 \ 240 DRAW_SETPIXEL_ADD(RGB_FROM_XRGB8888(*pixels, sr, sg, sb),\ 241 XRGB8888_FROM_RGB(*pixels, sr, sg, sb)) 242 243#define DRAW_SETPIXEL_MOD_XRGB8888 \ 244 DRAW_SETPIXEL_MOD(RGB_FROM_XRGB8888(*pixels, sr, sg, sb),\ 245 XRGB8888_FROM_RGB(*pixels, sr, sg, sb)) 246 247#define DRAW_SETPIXEL_MUL_XRGB8888 \ 248 DRAW_SETPIXEL_MUL(RGB_FROM_XRGB8888(*pixels, sr, sg, sb),\ 249 XRGB8888_FROM_RGB(*pixels, sr, sg, sb)) 250 251#define DRAW_SETPIXELXY_XRGB8888(x, y) \ 252 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_XRGB8888) 253 254#define DRAW_SETPIXELXY_BLEND_XRGB8888(x, y) \ 255 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_XRGB8888) 256 257#define DRAW_SETPIXELXY_BLEND_CLAMPED_XRGB8888(x, y) \ 258 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_CLAMPED_XRGB8888) 259 260#define DRAW_SETPIXELXY_ADD_XRGB8888(x, y) \ 261 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_XRGB8888) 262 263#define DRAW_SETPIXELXY_MOD_XRGB8888(x, y) \ 264 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_XRGB8888) 265 266#define DRAW_SETPIXELXY_MUL_XRGB8888(x, y) \ 267 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MUL_XRGB8888) 268 269/* 270 * Define draw operators for ARGB8888 271 */ 272 273#define DRAW_SETPIXEL_ARGB8888 \ 274 DRAW_SETPIXEL(ARGB8888_FROM_RGBA(*pixels, sr, sg, sb, sa)) 275 276#define DRAW_SETPIXEL_BLEND_ARGB8888 \ 277 DRAW_SETPIXEL_BLEND(RGBA_FROM_ARGB8888(*pixels, sr, sg, sb, sa),\ 278 ARGB8888_FROM_RGBA(*pixels, sr, sg, sb, sa)) 279 280#define DRAW_SETPIXEL_BLEND_CLAMPED_ARGB8888 \ 281 DRAW_SETPIXEL_BLEND_CLAMPED(RGBA_FROM_ARGB8888(*pixels, sr, sg, sb, sa),\ 282 ARGB8888_FROM_RGBA(*pixels, sr, sg, sb, sa)) 283 284#define DRAW_SETPIXEL_ADD_ARGB8888 \ 285 DRAW_SETPIXEL_ADD(RGBA_FROM_ARGB8888(*pixels, sr, sg, sb, sa),\ 286 ARGB8888_FROM_RGBA(*pixels, sr, sg, sb, sa)) 287 288#define DRAW_SETPIXEL_MOD_ARGB8888 \ 289 DRAW_SETPIXEL_MOD(RGBA_FROM_ARGB8888(*pixels, sr, sg, sb, sa),\ 290 ARGB8888_FROM_RGBA(*pixels, sr, sg, sb, sa)) 291 292#define DRAW_SETPIXEL_MUL_ARGB8888 \ 293 DRAW_SETPIXEL_MUL(RGBA_FROM_ARGB8888(*pixels, sr, sg, sb, sa),\ 294 ARGB8888_FROM_RGBA(*pixels, sr, sg, sb, sa)) 295 296#define DRAW_SETPIXELXY_ARGB8888(x, y) \ 297 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ARGB8888) 298 299#define DRAW_SETPIXELXY_BLEND_ARGB8888(x, y) \ 300 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_ARGB8888) 301 302#define DRAW_SETPIXELXY_BLEND_CLAMPED_ARGB8888(x, y) \ 303 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_CLAMPED_ARGB8888) 304 305#define DRAW_SETPIXELXY_ADD_ARGB8888(x, y) \ 306 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_ARGB8888) 307 308#define DRAW_SETPIXELXY_MOD_ARGB8888(x, y) \ 309 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_ARGB8888) 310 311#define DRAW_SETPIXELXY_MUL_ARGB8888(x, y) \ 312 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MUL_ARGB8888) 313 314/* 315 * Define draw operators for general RGB 316 */ 317 318#define DRAW_SETPIXEL_RGB \ 319 DRAW_SETPIXEL(PIXEL_FROM_RGB(*pixels, fmt, sr, sg, sb)) 320 321#define DRAW_SETPIXEL_BLEND_RGB \ 322 DRAW_SETPIXEL_BLEND(RGB_FROM_PIXEL(*pixels, fmt, sr, sg, sb),\ 323 PIXEL_FROM_RGB(*pixels, fmt, sr, sg, sb)) 324 325#define DRAW_SETPIXEL_BLEND_CLAMPED_RGB \ 326 DRAW_SETPIXEL_BLEND_CLAMPED(RGB_FROM_PIXEL(*pixels, fmt, sr, sg, sb),\ 327 PIXEL_FROM_RGB(*pixels, fmt, sr, sg, sb)) 328 329#define DRAW_SETPIXEL_ADD_RGB \ 330 DRAW_SETPIXEL_ADD(RGB_FROM_PIXEL(*pixels, fmt, sr, sg, sb),\ 331 PIXEL_FROM_RGB(*pixels, fmt, sr, sg, sb)) 332 333#define DRAW_SETPIXEL_MOD_RGB \ 334 DRAW_SETPIXEL_MOD(RGB_FROM_PIXEL(*pixels, fmt, sr, sg, sb),\ 335 PIXEL_FROM_RGB(*pixels, fmt, sr, sg, sb)) 336 337#define DRAW_SETPIXEL_MUL_RGB \ 338 DRAW_SETPIXEL_MUL(RGB_FROM_PIXEL(*pixels, fmt, sr, sg, sb),\ 339 PIXEL_FROM_RGB(*pixels, fmt, sr, sg, sb)) 340 341#define DRAW_SETPIXELXY2_RGB(x, y) \ 342 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB) 343 344#define DRAW_SETPIXELXY4_RGB(x, y) \ 345 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGB) 346 347#define DRAW_SETPIXELXY2_BLEND_RGB(x, y) \ 348 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB) 349 350#define DRAW_SETPIXELXY2_BLEND_CLAMPED_RGB(x, y) \ 351 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_CLAMPED_RGB) 352 353#define DRAW_SETPIXELXY4_BLEND_RGB(x, y) \ 354 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGB) 355 356#define DRAW_SETPIXELXY4_BLEND_CLAMPED_RGB(x, y) \ 357 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_CLAMPED_RGB) 358 359#define DRAW_SETPIXELXY2_ADD_RGB(x, y) \ 360 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB) 361 362#define DRAW_SETPIXELXY4_ADD_RGB(x, y) \ 363 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGB) 364 365#define DRAW_SETPIXELXY2_MOD_RGB(x, y) \ 366 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB) 367 368#define DRAW_SETPIXELXY4_MOD_RGB(x, y) \ 369 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGB) 370 371#define DRAW_SETPIXELXY2_MUL_RGB(x, y) \ 372 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MUL_RGB) 373 374#define DRAW_SETPIXELXY4_MUL_RGB(x, y) \ 375 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MUL_RGB) 376 377/* 378 * Define draw operators for general RGBA 379 */ 380 381#define DRAW_SETPIXEL_RGBA \ 382 DRAW_SETPIXEL(PIXEL_FROM_RGBA(*pixels, fmt, sr, sg, sb, sa)) 383 384#define DRAW_SETPIXEL_BLEND_RGBA \ 385 DRAW_SETPIXEL_BLEND(RGBA_FROM_PIXEL(*pixels, fmt, sr, sg, sb, sa),\ 386 PIXEL_FROM_RGBA(*pixels, fmt, sr, sg, sb, sa)) 387 388#define DRAW_SETPIXEL_BLEND_CLAMPED_RGBA \ 389 DRAW_SETPIXEL_BLEND_CLAMPED(RGBA_FROM_PIXEL(*pixels, fmt, sr, sg, sb, sa),\ 390 PIXEL_FROM_RGBA(*pixels, fmt, sr, sg, sb, sa)) 391 392#define DRAW_SETPIXEL_ADD_RGBA \ 393 DRAW_SETPIXEL_ADD(RGBA_FROM_PIXEL(*pixels, fmt, sr, sg, sb, sa),\ 394 PIXEL_FROM_RGBA(*pixels, fmt, sr, sg, sb, sa)) 395 396#define DRAW_SETPIXEL_MOD_RGBA \ 397 DRAW_SETPIXEL_MOD(RGBA_FROM_PIXEL(*pixels, fmt, sr, sg, sb, sa),\ 398 PIXEL_FROM_RGBA(*pixels, fmt, sr, sg, sb, sa)) 399 400#define DRAW_SETPIXEL_MUL_RGBA \ 401 DRAW_SETPIXEL_MUL(RGBA_FROM_PIXEL(*pixels, fmt, sr, sg, sb, sa),\ 402 PIXEL_FROM_RGBA(*pixels, fmt, sr, sg, sb, sa)) 403 404#define DRAW_SETPIXELXY4_RGBA(x, y) \ 405 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGBA) 406 407#define DRAW_SETPIXELXY4_BLEND_RGBA(x, y) \ 408 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGBA) 409 410#define DRAW_SETPIXELXY4_BLEND_CLAMPED_RGBA(x, y) \ 411 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_CLAMPED_RGBA) 412 413#define DRAW_SETPIXELXY4_ADD_RGBA(x, y) \ 414 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGBA) 415 416#define DRAW_SETPIXELXY4_MOD_RGBA(x, y) \ 417 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGBA) 418 419#define DRAW_SETPIXELXY4_MUL_RGBA(x, y) \ 420 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MUL_RGBA) 421 422/* 423 * Define line drawing macro 424 */ 425 426#define ABS(_x) ((_x) < 0 ? -(_x) : (_x)) 427 428// Horizontal line 429#define HLINE(type, op, draw_end) \ 430 { \ 431 int length; \ 432 int pitch = (dst->pitch / dst->fmt->bytes_per_pixel); \ 433 type *pixels; \ 434 if (x1 <= x2) { \ 435 pixels = (type *)dst->pixels + y1 * pitch + x1; \ 436 length = draw_end ? (x2 - x1 + 1) : (x2 - x1); \ 437 } else { \ 438 pixels = (type *)dst->pixels + y1 * pitch + x2; \ 439 if (!draw_end) { \ 440 ++pixels; \ 441 } \ 442 length = draw_end ? (x1 - x2 + 1) : (x1 - x2); \ 443 } \ 444 while (length--) { \ 445 op; \ 446 ++pixels; \ 447 } \ 448 } 449 450// Vertical line 451#define VLINE(type, op, draw_end) \ 452 { \ 453 int length; \ 454 int pitch = (dst->pitch / dst->fmt->bytes_per_pixel); \ 455 type *pixels; \ 456 if (y1 <= y2) { \ 457 pixels = (type *)dst->pixels + y1 * pitch + x1; \ 458 length = draw_end ? (y2 - y1 + 1) : (y2 - y1); \ 459 } else { \ 460 pixels = (type *)dst->pixels + y2 * pitch + x1; \ 461 if (!draw_end) { \ 462 pixels += pitch; \ 463 } \ 464 length = draw_end ? (y1 - y2 + 1) : (y1 - y2); \ 465 } \ 466 while (length--) { \ 467 op; \ 468 pixels += pitch; \ 469 } \ 470 } 471 472// Diagonal line 473#define DLINE(type, op, draw_end) \ 474 { \ 475 int length; \ 476 int pitch = (dst->pitch / dst->fmt->bytes_per_pixel); \ 477 type *pixels; \ 478 if (y1 <= y2) { \ 479 pixels = (type *)dst->pixels + y1 * pitch + x1; \ 480 if (x1 <= x2) { \ 481 ++pitch; \ 482 } else { \ 483 --pitch; \ 484 } \ 485 length = (y2 - y1); \ 486 } else { \ 487 pixels = (type *)dst->pixels + y2 * pitch + x2; \ 488 if (x2 <= x1) { \ 489 ++pitch; \ 490 } else { \ 491 --pitch; \ 492 } \ 493 if (!draw_end) { \ 494 pixels += pitch; \ 495 } \ 496 length = (y1 - y2); \ 497 } \ 498 if (draw_end) { \ 499 ++length; \ 500 } \ 501 while (length--) { \ 502 op; \ 503 pixels += pitch; \ 504 } \ 505 } 506 507// Bresenham's line algorithm 508#define BLINE(x1, y1, x2, y2, op, draw_end) \ 509 { \ 510 int i, deltax, deltay, numpixels; \ 511 int d, dinc1, dinc2; \ 512 int x, xinc1, xinc2; \ 513 int y, yinc1, yinc2; \ 514 \ 515 deltax = ABS(x2 - x1); \ 516 deltay = ABS(y2 - y1); \ 517 \ 518 if (deltax >= deltay) { \ 519 numpixels = deltax + 1; \ 520 d = (2 * deltay) - deltax; \ 521 dinc1 = deltay * 2; \ 522 dinc2 = (deltay - deltax) * 2; \ 523 xinc1 = 1; \ 524 xinc2 = 1; \ 525 yinc1 = 0; \ 526 yinc2 = 1; \ 527 } else { \ 528 numpixels = deltay + 1; \ 529 d = (2 * deltax) - deltay; \ 530 dinc1 = deltax * 2; \ 531 dinc2 = (deltax - deltay) * 2; \ 532 xinc1 = 0; \ 533 xinc2 = 1; \ 534 yinc1 = 1; \ 535 yinc2 = 1; \ 536 } \ 537 \ 538 if (x1 > x2) { \ 539 xinc1 = -xinc1; \ 540 xinc2 = -xinc2; \ 541 } \ 542 if (y1 > y2) { \ 543 yinc1 = -yinc1; \ 544 yinc2 = -yinc2; \ 545 } \ 546 \ 547 x = x1; \ 548 y = y1; \ 549 \ 550 if (!draw_end) { \ 551 --numpixels; \ 552 } \ 553 for (i = 0; i < numpixels; ++i) { \ 554 op(x, y); \ 555 if (d < 0) { \ 556 d += dinc1; \ 557 x += xinc1; \ 558 y += yinc1; \ 559 } else { \ 560 d += dinc2; \ 561 x += xinc2; \ 562 y += yinc2; \ 563 } \ 564 } \ 565 } 566 567// Xiaolin Wu's line algorithm, based on Michael Abrash's implementation 568#define WULINE(x1, y1, x2, y2, opaque_op, blend_op, draw_end) \ 569 { \ 570 Uint16 ErrorAdj, ErrorAcc; \ 571 Uint16 ErrorAccTemp, Weighting; \ 572 int DeltaX, DeltaY, Temp, XDir; \ 573 unsigned r, g, b, a, inva; \ 574 \ 575 /* Draw the initial pixel, which is always exactly intersected by \ 576 the line and so needs no weighting */ \ 577 opaque_op(x1, y1); \ 578 \ 579 /* Draw the final pixel, which is always exactly intersected by the line \ 580 and so needs no weighting */ \ 581 if (draw_end) { \ 582 opaque_op(x2, y2); \ 583 } \ 584 \ 585 /* Make sure the line runs top to bottom */ \ 586 if (y1 > y2) { \ 587 Temp = y1; \ 588 y1 = y2; \ 589 y2 = Temp; \ 590 Temp = x1; \ 591 x1 = x2; \ 592 x2 = Temp; \ 593 } \ 594 DeltaY = y2 - y1; \ 595 \ 596 if ((DeltaX = x2 - x1) >= 0) { \ 597 XDir = 1; \ 598 } else { \ 599 XDir = -1; \ 600 DeltaX = -DeltaX; /* make DeltaX positive */ \ 601 } \ 602 \ 603 /* line is not horizontal, diagonal, or vertical */ \ 604 ErrorAcc = 0; /* initialize the line error accumulator to 0 */ \ 605 \ 606 /* Is this an X-major or Y-major line? */ \ 607 if (DeltaY > DeltaX) { \ 608 /* Y-major line; calculate 16-bit fixed-point fractional part of a \ 609 pixel that X advances each time Y advances 1 pixel, truncating the \ 610 result so that we won't overrun the endpoint along the X axis */ \ 611 ErrorAdj = ((unsigned long)DeltaX << 16) / (unsigned long)DeltaY; \ 612 /* Draw all pixels other than the first and last */ \ 613 while (--DeltaY) { \ 614 ErrorAccTemp = ErrorAcc; /* remember current accumulated error */ \ 615 ErrorAcc += ErrorAdj; /* calculate error for next pixel */ \ 616 if (ErrorAcc <= ErrorAccTemp) { \ 617 /* The error accumulator turned over, so advance the X coord */ \ 618 x1 += XDir; \ 619 } \ 620 y1++; /* Y-major, so always advance Y */ \ 621 /* The IntensityBits most significant bits of ErrorAcc give us the \ 622 intensity weighting for this pixel, and the complement of the \ 623 weighting for the paired pixel */ \ 624 Weighting = ErrorAcc >> 8; \ 625 { \ 626 a = DRAW_MUL(_a, (Weighting ^ 255)); \ 627 r = DRAW_MUL(_r, a); \ 628 g = DRAW_MUL(_g, a); \ 629 b = DRAW_MUL(_b, a); \ 630 inva = (a ^ 0xFF); \ 631 blend_op(x1, y1); \ 632 } \ 633 { \ 634 a = DRAW_MUL(_a, Weighting); \ 635 r = DRAW_MUL(_r, a); \ 636 g = DRAW_MUL(_g, a); \ 637 b = DRAW_MUL(_b, a); \ 638 inva = (a ^ 0xFF); \ 639 blend_op(x1 + XDir, y1); \ 640 } \ 641 } \ 642 } else { \ 643 /* X-major line; calculate 16-bit fixed-point fractional part of a \ 644 pixel that Y advances each time X advances 1 pixel, truncating the \ 645 result to avoid overrunning the endpoint along the X axis */ \ 646 ErrorAdj = ((unsigned long)DeltaY << 16) / (unsigned long)DeltaX; \ 647 /* Draw all pixels other than the first and last */ \ 648 while (--DeltaX) { \ 649 ErrorAccTemp = ErrorAcc; /* remember current accumulated error */ \ 650 ErrorAcc += ErrorAdj; /* calculate error for next pixel */ \ 651 if (ErrorAcc <= ErrorAccTemp) { \ 652 /* The error accumulator turned over, so advance the Y coord */ \ 653 y1++; \ 654 } \ 655 x1 += XDir; /* X-major, so always advance X */ \ 656 /* The IntensityBits most significant bits of ErrorAcc give us the \ 657 intensity weighting for this pixel, and the complement of the \ 658 weighting for the paired pixel */ \ 659 Weighting = ErrorAcc >> 8; \ 660 { \ 661 a = DRAW_MUL(_a, (Weighting ^ 255)); \ 662 r = DRAW_MUL(_r, a); \ 663 g = DRAW_MUL(_g, a); \ 664 b = DRAW_MUL(_b, a); \ 665 inva = (a ^ 0xFF); \ 666 blend_op(x1, y1); \ 667 } \ 668 { \ 669 a = DRAW_MUL(_a, Weighting); \ 670 r = DRAW_MUL(_r, a); \ 671 g = DRAW_MUL(_g, a); \ 672 b = DRAW_MUL(_b, a); \ 673 inva = (a ^ 0xFF); \ 674 blend_op(x1, y1 + 1); \ 675 } \ 676 } \ 677 } \ 678 } 679 680#ifdef AA_LINES 681#define AALINE(x1, y1, x2, y2, opaque_op, blend_op, draw_end) \ 682 WULINE(x1, y1, x2, y2, opaque_op, blend_op, draw_end) 683#else 684#define AALINE(x1, y1, x2, y2, opaque_op, blend_op, draw_end) \ 685 BLINE(x1, y1, x2, y2, opaque_op, draw_end) 686#endif 687 688/* 689 * Define fill rect macro 690 */ 691 692#define FILLRECT(type, op) \ 693 do { \ 694 int width = rect->w; \ 695 int height = rect->h; \ 696 int pitch = (dst->pitch / dst->fmt->bytes_per_pixel); \ 697 int skip = pitch - width; \ 698 type *pixels = (type *)dst->pixels + rect->y * pitch + rect->x;\ 699 while (height--) { \ 700 { \ 701 int n = (width + 3) / 4; \ 702 switch (width & 3) { \ 703 case 0: \ 704 do { \ 705 op; \ 706 pixels++; \ 707 SDL_FALLTHROUGH; \ 708 case 3: \ 709 op; \ 710 pixels++; \ 711 SDL_FALLTHROUGH; \ 712 case 2: \ 713 op; \ 714 pixels++; \ 715 SDL_FALLTHROUGH; \ 716 case 1: \ 717 op; \ 718 pixels++; \ 719 } while (--n > 0); \ 720 } \ 721 } \ 722 pixels += skip; \ 723 } \ 724 } while (0) 725[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.