graphics.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2012-2018 K. Lange
  5. *
  6. * Generic Graphics library for ToaruOS
  7. */
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <fcntl.h>
  13. #include <dlfcn.h>
  14. #include <sys/ioctl.h>
  15. #ifndef NO_SSE
  16. #include <xmmintrin.h>
  17. #include <emmintrin.h>
  18. #endif
  19. #include <kernel/video.h>
  20. #include <toaru/graphics.h>
  21. static inline int32_t min(int32_t a, int32_t b) {
  22. return (a < b) ? a : b;
  23. }
  24. static inline int32_t max(int32_t a, int32_t b) {
  25. return (a > b) ? a : b;
  26. }
  27. static inline uint16_t min16(uint16_t a, uint16_t b) {
  28. return (a < b) ? a : b;
  29. }
  30. static inline uint16_t max16(uint16_t a, uint16_t b) {
  31. return (a > b) ? a : b;
  32. }
  33. static int _is_in_clip(gfx_context_t * ctx, int32_t y) {
  34. if (!ctx->clips) return 1;
  35. if (y < 0 || y >= ctx->clips_size) return 1;
  36. return ctx->clips[y];
  37. }
  38. void gfx_add_clip(gfx_context_t * ctx, int32_t x, int32_t y, int32_t w, int32_t h) {
  39. (void)x;
  40. (void)w; // TODO Horizontal clipping
  41. if (!ctx->clips) {
  42. ctx->clips = malloc(ctx->height);
  43. memset(ctx->clips, 0, ctx->height);
  44. ctx->clips_size = ctx->height;
  45. }
  46. for (int i = max(y,0); i < min(y+h,ctx->clips_size); ++i) {
  47. ctx->clips[i] = 1;
  48. }
  49. }
  50. void gfx_clear_clip(gfx_context_t * ctx) {
  51. if (ctx->clips) {
  52. memset(ctx->clips, 0, ctx->clips_size);
  53. }
  54. }
  55. void gfx_no_clip(gfx_context_t * ctx) {
  56. void * tmp = ctx->clips;
  57. if (!tmp) return;
  58. ctx->clips = NULL;
  59. free(tmp);
  60. }
  61. /* Pointer to graphics memory */
  62. void flip(gfx_context_t * ctx) {
  63. if (ctx->clips) {
  64. for (size_t i = 0; i < ctx->height; ++i) {
  65. if (_is_in_clip(ctx,i)) {
  66. memcpy(&ctx->buffer[i*GFX_S(ctx)], &ctx->backbuffer[i*GFX_S(ctx)], 4 * ctx->width);
  67. }
  68. }
  69. } else {
  70. memcpy(ctx->buffer, ctx->backbuffer, ctx->size);
  71. }
  72. }
  73. void clearbuffer(gfx_context_t * ctx) {
  74. memset(ctx->backbuffer, 0, ctx->size);
  75. }
  76. /* Deprecated */
  77. static int framebuffer_fd = 0;
  78. gfx_context_t * init_graphics_fullscreen() {
  79. gfx_context_t * out = malloc(sizeof(gfx_context_t));
  80. out->clips = NULL;
  81. if (!framebuffer_fd) {
  82. framebuffer_fd = open("/dev/fb0", 0, 0);
  83. }
  84. if (framebuffer_fd < 0) {
  85. /* oh shit */
  86. free(out);
  87. return NULL;
  88. }
  89. ioctl(framebuffer_fd, IO_VID_WIDTH, &out->width);
  90. ioctl(framebuffer_fd, IO_VID_HEIGHT, &out->height);
  91. ioctl(framebuffer_fd, IO_VID_DEPTH, &out->depth);
  92. ioctl(framebuffer_fd, IO_VID_STRIDE, &out->stride);
  93. ioctl(framebuffer_fd, IO_VID_ADDR, &out->buffer);
  94. ioctl(framebuffer_fd, IO_VID_SIGNAL, NULL);
  95. out->size = GFX_H(out) * GFX_S(out);
  96. out->backbuffer = out->buffer;
  97. return out;
  98. }
  99. uint32_t framebuffer_stride(void) {
  100. uint32_t stride;
  101. ioctl(framebuffer_fd, IO_VID_STRIDE, &stride);
  102. return stride;
  103. }
  104. gfx_context_t * init_graphics_fullscreen_double_buffer() {
  105. gfx_context_t * out = init_graphics_fullscreen();
  106. if (!out) return NULL;
  107. out->backbuffer = malloc(GFX_S(out) * GFX_H(out));
  108. return out;
  109. }
  110. gfx_context_t * init_graphics_subregion(gfx_context_t * base, int x, int y, int width, int height) {
  111. gfx_context_t * out = malloc(sizeof(gfx_context_t));
  112. out->clips = NULL;
  113. out->depth = 32;
  114. out->width = width;
  115. out->height = height;
  116. out->stride = base->stride;
  117. out->backbuffer = base->buffer + (base->stride * y) + x * 4;
  118. out->buffer = base->buffer + (base->stride * y) + x * 4;
  119. if (base->clips) {
  120. for (int _y = 0; _y < height; ++_y) {
  121. if (_is_in_clip(base, y + _y)) {
  122. gfx_add_clip(out,0,_y,width,1);
  123. }
  124. }
  125. }
  126. out->size = 0; /* don't allow flip or clear operations */
  127. return out;
  128. }
  129. void reinit_graphics_fullscreen(gfx_context_t * out) {
  130. ioctl(framebuffer_fd, IO_VID_WIDTH, &out->width);
  131. ioctl(framebuffer_fd, IO_VID_HEIGHT, &out->height);
  132. ioctl(framebuffer_fd, IO_VID_DEPTH, &out->depth);
  133. ioctl(framebuffer_fd, IO_VID_STRIDE, &out->stride);
  134. out->size = GFX_H(out) * GFX_S(out);
  135. if (out->clips && out->clips_size != out->height) {
  136. free(out->clips);
  137. out->clips = NULL;
  138. out->clips_size = 0;
  139. }
  140. if (out->buffer != out->backbuffer) {
  141. ioctl(framebuffer_fd, IO_VID_ADDR, &out->buffer);
  142. out->backbuffer = realloc(out->backbuffer, GFX_S(out) * GFX_H(out));
  143. } else {
  144. ioctl(framebuffer_fd, IO_VID_ADDR, &out->buffer);
  145. out->backbuffer = out->buffer;
  146. }
  147. }
  148. gfx_context_t * init_graphics_sprite(sprite_t * sprite) {
  149. gfx_context_t * out = malloc(sizeof(gfx_context_t));
  150. out->clips = NULL;
  151. out->width = sprite->width;
  152. out->stride = sprite->width * sizeof(uint32_t);
  153. out->height = sprite->height;
  154. out->depth = 32;
  155. out->size = GFX_H(out) * GFX_W(out) * GFX_B(out);
  156. out->buffer = (char *)sprite->bitmap;
  157. out->backbuffer = out->buffer;
  158. return out;
  159. }
  160. sprite_t * create_sprite(size_t width, size_t height, int alpha) {
  161. sprite_t * out = malloc(sizeof(sprite_t));
  162. /*
  163. uint16_t width;
  164. uint16_t height;
  165. uint32_t * bitmap;
  166. uint32_t * masks;
  167. uint32_t blank;
  168. uint8_t alpha;
  169. */
  170. out->width = width;
  171. out->height = height;
  172. out->bitmap = malloc(sizeof(uint32_t) * out->width * out->height);
  173. out->masks = NULL;
  174. out->blank = 0x00000000;
  175. out->alpha = alpha;
  176. return out;
  177. }
  178. void sprite_free(sprite_t * sprite) {
  179. if (sprite->masks) {
  180. free(sprite->masks);
  181. }
  182. free(sprite->bitmap);
  183. free(sprite);
  184. }
  185. inline uint32_t rgb(uint8_t r, uint8_t g, uint8_t b) {
  186. return 0xFF000000 | (r << 16) | (g << 8) | (b);
  187. }
  188. inline uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
  189. return (a << 24U) | (r << 16) | (g << 8) | (b);
  190. }
  191. uint32_t alpha_blend(uint32_t bottom, uint32_t top, uint32_t mask) {
  192. uint8_t a = _RED(mask);
  193. uint8_t red = (_RED(bottom) * (255 - a) + _RED(top) * a) / 255;
  194. uint8_t gre = (_GRE(bottom) * (255 - a) + _GRE(top) * a) / 255;
  195. uint8_t blu = (_BLU(bottom) * (255 - a) + _BLU(top) * a) / 255;
  196. uint8_t alp = (int)a + (int)_ALP(bottom) > 255 ? 255 : a + _ALP(bottom);
  197. return rgba(red,gre,blu, alp);
  198. }
  199. #define DONT_USE_FLOAT_FOR_ALPHA 1
  200. uint32_t alpha_blend_rgba(uint32_t bottom, uint32_t top) {
  201. if (_ALP(bottom) == 0) return top;
  202. if (_ALP(top) == 255) return top;
  203. if (_ALP(top) == 0) return bottom;
  204. #if DONT_USE_FLOAT_FOR_ALPHA
  205. uint16_t a = _ALP(top);
  206. uint16_t c = 255 - a;
  207. uint16_t b = ((int)_ALP(bottom) * c) / 255;
  208. uint16_t alp = min16(a + b, 255);
  209. uint16_t red = min16((uint32_t)(_RED(bottom) * c + _RED(top) * 255) / 255, 255);
  210. uint16_t gre = min16((uint32_t)(_GRE(bottom) * c + _GRE(top) * 255) / 255, 255);
  211. uint16_t blu = min16((uint32_t)(_BLU(bottom) * c + _BLU(top) * 255) / 255, 255);
  212. return rgba(red,gre,blu,alp);
  213. #else
  214. double a = _ALP(top) / 255.0;
  215. double c = 1.0 - a;
  216. double b = (_ALP(bottom) / 255.0) * c;
  217. double alp = a + b; if (alp > 1.0) alp = 1.0;
  218. double red = (_RED(bottom) / 255.0) * c + (_RED(top) / 255.0); if (red > 1.0) red = 1.0;
  219. double gre = (_GRE(bottom) / 255.0) * c + (_GRE(top) / 255.0); if (gre > 1.0) gre = 1.0;
  220. double blu = (_BLU(bottom) / 255.0) * c + (_BLU(top) / 255.0); if (blu > 1.0) blu = 1.0;
  221. return rgba(red * 255, gre * 255, blu * 255, alp * 255);
  222. #endif
  223. }
  224. uint32_t premultiply(uint32_t color) {
  225. uint16_t a = _ALP(color);
  226. uint16_t r = _RED(color);
  227. uint16_t g = _GRE(color);
  228. uint16_t b = _BLU(color);
  229. r = r * a / 255;
  230. g = g * a / 255;
  231. b = b * a / 255;
  232. return rgba(r,g,b,a);
  233. }
  234. static int clamp(int a, int l, int h) {
  235. return a < l ? l : (a > h ? h : a);
  236. }
  237. static void _box_blur_horizontal(gfx_context_t * _src, int radius) {
  238. int w = _src->width;
  239. int h = _src->height;
  240. int half_radius = radius / 2;
  241. uint32_t * out_color = calloc(sizeof(uint32_t), w);
  242. for (int y = 0; y < h; y++) {
  243. int hits = 0;
  244. int r = 0;
  245. int g = 0;
  246. int b = 0;
  247. int a = 0;
  248. for (int x = -half_radius; x < w; x++) {
  249. int old_p = x - half_radius - 1;
  250. if (old_p >= 0)
  251. {
  252. uint32_t col = GFX(_src, clamp(old_p,0,w-1), y);
  253. if (col) {
  254. r -= _RED(col);
  255. g -= _GRE(col);
  256. b -= _BLU(col);
  257. a -= _ALP(col);
  258. }
  259. hits--;
  260. }
  261. int newPixel = x + half_radius;
  262. if (newPixel < w) {
  263. uint32_t col = GFX(_src, clamp(newPixel,0,w-1), y);
  264. if (col != 0) {
  265. r += _RED(col);
  266. g += _GRE(col);
  267. b += _BLU(col);
  268. a += _ALP(col);
  269. }
  270. hits++;
  271. }
  272. if (x >= 0 && x < w) {
  273. out_color[x] = rgba(r / hits, g / hits, b / hits, a / hits);
  274. }
  275. }
  276. if (!_is_in_clip(_src, y)) continue;
  277. for (int x = 0; x < w; x++) {
  278. GFX(_src,x,y) = out_color[x];
  279. }
  280. }
  281. free(out_color);
  282. }
  283. static void _box_blur_vertical(gfx_context_t * _src, int radius) {
  284. int w = _src->width;
  285. int h = _src->height;
  286. int half_radius = radius / 2;
  287. uint32_t * out_color = calloc(sizeof(uint32_t), h);
  288. for (int x = 0; x < w; x++) {
  289. int hits = 0;
  290. int r = 0;
  291. int g = 0;
  292. int b = 0;
  293. int a = 0;
  294. for (int y = -half_radius; y < h; y++) {
  295. int old_p = y - half_radius - 1;
  296. if (old_p >= 0) {
  297. uint32_t col = GFX(_src,x,clamp(old_p,0,h-1));
  298. if (col != 0) {
  299. r -= _RED(col);
  300. g -= _GRE(col);
  301. b -= _BLU(col);
  302. a -= _ALP(col);
  303. }
  304. hits--;
  305. }
  306. int newPixel = y + half_radius;
  307. if (newPixel < h) {
  308. uint32_t col = GFX(_src,x,clamp(newPixel,0,h-1));
  309. if (col != 0)
  310. {
  311. r += _RED(col);
  312. g += _GRE(col);
  313. b += _BLU(col);
  314. a += _ALP(col);
  315. }
  316. hits++;
  317. }
  318. if (y >= 0 && y < h) {
  319. out_color[y] = rgba(r / hits, g / hits, b / hits, a / hits);
  320. }
  321. }
  322. for (int y = 0; y < h; y++) {
  323. if (!_is_in_clip(_src, y)) continue;
  324. GFX(_src,x,y) = out_color[y];
  325. }
  326. }
  327. free(out_color);
  328. }
  329. void blur_context_box(gfx_context_t * _src, int radius) {
  330. _box_blur_horizontal(_src,radius);
  331. _box_blur_vertical(_src,radius);
  332. }
  333. static int (*load_sprite_jpg)(sprite_t *, char *) = NULL;
  334. static int (*load_sprite_png)(sprite_t *, char *) = NULL;
  335. /**
  336. * TODO: This should probably use some config file or plugin path
  337. * for better discovery; we could rename these libraries and
  338. * not have applications / other libraries depend on them
  339. * directly and instead go through libtoaru_graphics.
  340. */
  341. __attribute__((constructor)) static void _load_format_libraries() {
  342. void * _lib_jpeg = dlopen("libtoaru_jpeg.so", 0);
  343. if (_lib_jpeg) load_sprite_jpg = dlsym(_lib_jpeg, "load_sprite_jpg");
  344. void * _lib_png = dlopen("libtoaru_png.so", 0);
  345. if (_lib_png) load_sprite_png = dlsym(_lib_png, "load_sprite_png");
  346. }
  347. static char * extension_from_filename(char * filename) {
  348. char * ext = strrchr(filename, '.');
  349. if (ext && *ext == '.') return ext + 1;
  350. return "";
  351. }
  352. int load_sprite(sprite_t * sprite, char * filename) {
  353. char * ext = extension_from_filename(filename);
  354. if (!strcmp(ext,"png") || !strcmp(ext,"sdf")) return load_sprite_png(sprite, filename);
  355. if (!strcmp(ext,"jpg") || !strcmp(ext,"jpeg")) return load_sprite_jpg(sprite, filename);
  356. /* Fall back to bitmap */
  357. return load_sprite_bmp(sprite, filename);
  358. }
  359. int load_sprite_bmp(sprite_t * sprite, char * filename) {
  360. /* Open the requested binary */
  361. FILE * image = fopen(filename, "r");
  362. if (!image) return 1;
  363. size_t image_size= 0;
  364. fseek(image, 0, SEEK_END);
  365. image_size = ftell(image);
  366. fseek(image, 0, SEEK_SET);
  367. /* Alright, we have the length */
  368. char * bufferb = malloc(image_size);
  369. fread(bufferb, image_size, 1, image);
  370. if (bufferb[0] == 'B' && bufferb[1] == 'M') {
  371. /* Bitmaps */
  372. uint16_t x = 0; /* -> 212 */
  373. uint16_t y = 0; /* -> 68 */
  374. /* Get the width / height of the image */
  375. signed int *bufferi = (signed int *)((uintptr_t)bufferb + 2);
  376. uint32_t width = bufferi[4];
  377. uint32_t height = bufferi[5];
  378. uint16_t bpp = bufferi[6] / 0x10000;
  379. uint32_t row_width = (bpp * width + 31) / 32 * 4;
  380. /* Skip right to the important part */
  381. size_t i = bufferi[2];
  382. sprite->width = width;
  383. sprite->height = height;
  384. sprite->bitmap = malloc(sizeof(uint32_t) * width * height);
  385. sprite->masks = NULL;
  386. int alpha_after = ((unsigned char *)&bufferi[13])[2] == 0xFF;
  387. #define _BMP_A 0x1000000
  388. #define _BMP_R 0x1
  389. #define _BMP_G 0x100
  390. #define _BMP_B 0x10000
  391. if (bpp == 32) {
  392. sprite->alpha = ALPHA_EMBEDDED;
  393. }
  394. for (y = 0; y < height; ++y) {
  395. for (x = 0; x < width; ++x) {
  396. if (i > image_size) goto _cleanup_sprite;
  397. /* Extract the color */
  398. uint32_t color;
  399. if (bpp == 24) {
  400. color = (bufferb[i + 3 * x] & 0xFF) +
  401. (bufferb[i+1 + 3 * x] & 0xFF) * 0x100 +
  402. (bufferb[i+2 + 3 * x] & 0xFF) * 0x10000 + 0xFF000000;
  403. } else if (bpp == 32 && alpha_after == 0) {
  404. color = (bufferb[i + 4 * x] & 0xFF) * _BMP_A +
  405. (bufferb[i+1 + 4 * x] & 0xFF) * _BMP_R +
  406. (bufferb[i+2 + 4 * x] & 0xFF) * _BMP_G +
  407. (bufferb[i+3 + 4 * x] & 0xFF) * _BMP_B;
  408. color = premultiply(color);
  409. } else if (bpp == 32 && alpha_after == 1) {
  410. color = (bufferb[i + 4 * x] & 0xFF) * _BMP_R +
  411. (bufferb[i+1 + 4 * x] & 0xFF) * _BMP_G +
  412. (bufferb[i+2 + 4 * x] & 0xFF) * _BMP_B +
  413. (bufferb[i+3 + 4 * x] & 0xFF) * _BMP_A;
  414. color = premultiply(color);
  415. } else {
  416. color = rgb(bufferb[i + x],bufferb[i + x],bufferb[i + x]); /* Unsupported */
  417. }
  418. /* Set our point */
  419. sprite->bitmap[(height - y - 1) * width + x] = color;
  420. }
  421. i += row_width;
  422. }
  423. } else {
  424. /* Assume targa; limited support */
  425. struct Header {
  426. uint8_t id_length;
  427. uint8_t color_map_type;
  428. uint8_t image_type;
  429. uint16_t color_map_first_entry;
  430. uint16_t color_map_length;
  431. uint8_t color_map_entry_size;
  432. uint16_t x_origin;
  433. uint16_t y_origin;
  434. uint16_t width;
  435. uint16_t height;
  436. uint8_t depth;
  437. uint8_t descriptor;
  438. } __attribute__((packed));
  439. struct Header * header = (struct Header *)bufferb;
  440. if (header->id_length || header->color_map_type || (header->image_type != 2)) {
  441. /* Unable to parse */
  442. goto _cleanup_sprite;
  443. }
  444. sprite->width = header->width;
  445. sprite->height = header->height;
  446. sprite->bitmap = malloc(sizeof(uint32_t) * sprite->width * sprite->height);
  447. sprite->masks = NULL;
  448. uint16_t x = 0;
  449. uint16_t y = 0;
  450. int i = sizeof(struct Header);
  451. if (header->depth == 24) {
  452. for (y = 0; y < sprite->height; ++y) {
  453. for (x = 0; x < sprite->width; ++x) {
  454. uint32_t color = rgb(
  455. bufferb[i+2 + 3 * x],
  456. bufferb[i+1 + 3 * x],
  457. bufferb[i + 3 * x]);
  458. sprite->bitmap[(sprite->height - y - 1) * sprite->width + x] = color;
  459. }
  460. i += sprite->width * 3;
  461. }
  462. } else if (header->depth == 32) {
  463. for (y = 0; y < sprite->height; ++y) {
  464. for (x = 0; x < sprite->width; ++x) {
  465. uint32_t color = rgba(
  466. bufferb[i+2 + 4 * x],
  467. bufferb[i+1 + 4 * x],
  468. bufferb[i + 4 * x],
  469. bufferb[i+3 + 4 * x]);
  470. sprite->bitmap[(sprite->height - y - 1) * sprite->width + x] = color;
  471. }
  472. i += sprite->width * 4;
  473. }
  474. }
  475. }
  476. _cleanup_sprite:
  477. fclose(image);
  478. free(bufferb);
  479. return 0;
  480. }
  481. #ifndef NO_SSE
  482. static __m128i mask00ff;
  483. static __m128i mask0080;
  484. static __m128i mask0101;
  485. __attribute__((constructor)) static void _masks(void) {
  486. mask00ff = _mm_set1_epi16(0x00FF);
  487. mask0080 = _mm_set1_epi16(0x0080);
  488. mask0101 = _mm_set1_epi16(0x0101);
  489. }
  490. #endif
  491. __attribute__((__force_align_arg_pointer__))
  492. void draw_sprite(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y) {
  493. int32_t _left = max(x, 0);
  494. int32_t _top = max(y, 0);
  495. int32_t _right = min(x + sprite->width, ctx->width - 1);
  496. int32_t _bottom = min(y + sprite->height, ctx->height - 1);
  497. if (sprite->alpha == ALPHA_MASK) {
  498. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  499. if (!_is_in_clip(ctx, y + _y)) continue;
  500. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  501. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  502. continue;
  503. GFX(ctx, x + _x, y + _y) = alpha_blend(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y), SMASKS(sprite, _x, _y));
  504. }
  505. }
  506. } else if (sprite->alpha == ALPHA_EMBEDDED) {
  507. /* Alpha embedded is the most important step. */
  508. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  509. if (!_is_in_clip(ctx, y + _y)) continue;
  510. #ifdef NO_SSE
  511. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  512. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  513. continue;
  514. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y));
  515. }
  516. #else
  517. uint16_t _x = 0;
  518. /* Ensure alignment */
  519. for (; _x < sprite->width; ++_x) {
  520. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  521. continue;
  522. if (!((uintptr_t)&GFX(ctx, x + _x, y + _y) & 15))
  523. break;
  524. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y));
  525. }
  526. for (; _x < sprite->width - 3; _x += 4) {
  527. if (x + _x < _left || y + _y < _top || y + _y > _bottom) {
  528. continue;
  529. }
  530. if (x + _x + 3 > _right)
  531. break;
  532. __m128i d = _mm_load_si128((void *)&GFX(ctx, x + _x, y + _y));
  533. __m128i s = _mm_loadu_si128((void *)&SPRITE(sprite, _x, _y));
  534. __m128i d_l, d_h;
  535. __m128i s_l, s_h;
  536. // unpack destination
  537. d_l = _mm_unpacklo_epi8(d, _mm_setzero_si128());
  538. d_h = _mm_unpackhi_epi8(d, _mm_setzero_si128());
  539. // unpack source
  540. s_l = _mm_unpacklo_epi8(s, _mm_setzero_si128());
  541. s_h = _mm_unpackhi_epi8(s, _mm_setzero_si128());
  542. __m128i a_l, a_h;
  543. __m128i t_l, t_h;
  544. // extract source alpha RGBA → AAAA
  545. a_l = _mm_shufflehi_epi16(_mm_shufflelo_epi16(s_l, _MM_SHUFFLE(3,3,3,3)), _MM_SHUFFLE(3,3,3,3));
  546. a_h = _mm_shufflehi_epi16(_mm_shufflelo_epi16(s_h, _MM_SHUFFLE(3,3,3,3)), _MM_SHUFFLE(3,3,3,3));
  547. // negate source alpha
  548. t_l = _mm_xor_si128(a_l, mask00ff);
  549. t_h = _mm_xor_si128(a_h, mask00ff);
  550. // apply source alpha to destination
  551. d_l = _mm_mulhi_epu16(_mm_adds_epu16(_mm_mullo_epi16(d_l,t_l),mask0080),mask0101);
  552. d_h = _mm_mulhi_epu16(_mm_adds_epu16(_mm_mullo_epi16(d_h,t_h),mask0080),mask0101);
  553. // combine source and destination
  554. d_l = _mm_adds_epu8(s_l,d_l);
  555. d_h = _mm_adds_epu8(s_h,d_h);
  556. // pack low + high and write back to memory
  557. _mm_storeu_si128((void*)&GFX(ctx, x + _x, y + _y), _mm_packus_epi16(d_l,d_h));
  558. }
  559. for (; _x < sprite->width; ++_x) {
  560. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  561. continue;
  562. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y));
  563. }
  564. #endif
  565. }
  566. } else if (sprite->alpha == ALPHA_INDEXED) {
  567. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  568. if (!_is_in_clip(ctx, y + _y)) continue;
  569. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  570. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  571. continue;
  572. if (SPRITE(sprite, _x, _y) != sprite->blank) {
  573. GFX(ctx, x + _x, y + _y) = SPRITE(sprite, _x, _y) | 0xFF000000;
  574. }
  575. }
  576. }
  577. } else if (sprite->alpha == ALPHA_FORCE_SLOW_EMBEDDED) {
  578. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  579. if (!_is_in_clip(ctx, y + _y)) continue;
  580. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  581. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  582. continue;
  583. #if 1
  584. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y));
  585. #else
  586. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(rgba(255,255,0,255), SPRITE(sprite, _x, _y));
  587. #endif
  588. }
  589. }
  590. } else {
  591. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  592. if (!_is_in_clip(ctx, y + _y)) continue;
  593. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  594. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  595. continue;
  596. GFX(ctx, x + _x, y + _y) = SPRITE(sprite, _x, _y) | 0xFF000000;
  597. }
  598. }
  599. }
  600. }
  601. void draw_line(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color) {
  602. int deltax = abs(x1 - x0);
  603. int deltay = abs(y1 - y0);
  604. int sx = (x0 < x1) ? 1 : -1;
  605. int sy = (y0 < y1) ? 1 : -1;
  606. int error = deltax - deltay;
  607. while (1) {
  608. if (x0 >= 0 && y0 >= 0 && x0 < ctx->width && y0 < ctx->height) {
  609. GFX(ctx, x0, y0) = color;
  610. }
  611. if (x0 == x1 && y0 == y1) break;
  612. int e2 = 2 * error;
  613. if (e2 > -deltay) {
  614. error -= deltay;
  615. x0 += sx;
  616. }
  617. if (e2 < deltax) {
  618. error += deltax;
  619. y0 += sy;
  620. }
  621. }
  622. }
  623. void draw_line_thick(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color, char thickness) {
  624. int deltax = abs(x1 - x0);
  625. int deltay = abs(y1 - y0);
  626. int sx = (x0 < x1) ? 1 : -1;
  627. int sy = (y0 < y1) ? 1 : -1;
  628. int error = deltax - deltay;
  629. while (1) {
  630. for (char j = -thickness; j <= thickness; ++j) {
  631. for (char i = -thickness; i <= thickness; ++i) {
  632. if (x0 + i >= 0 && x0 + i < ctx->width && y0 + j >= 0 && y0 + j < ctx->height) {
  633. GFX(ctx, x0 + i, y0 + j) = color;
  634. }
  635. }
  636. }
  637. if (x0 == x1 && y0 == y1) break;
  638. int e2 = 2 * error;
  639. if (e2 > -deltay) {
  640. error -= deltay;
  641. x0 += sx;
  642. }
  643. if (e2 < deltax) {
  644. error += deltax;
  645. y0 += sy;
  646. }
  647. }
  648. }
  649. void draw_fill(gfx_context_t * ctx, uint32_t color) {
  650. for (uint16_t y = 0; y < ctx->height; ++y) {
  651. for (uint16_t x = 0; x < ctx->width; ++x) {
  652. GFX(ctx, x, y) = color;
  653. }
  654. }
  655. }
  656. /* Bilinear filtering from Wikipedia */
  657. uint32_t getBilinearFilteredPixelColor(sprite_t * tex, double u, double v) {
  658. u *= tex->width;
  659. v *= tex->height;
  660. int x = floor(u);
  661. int y = floor(v);
  662. if (x >= tex->width) return 0;
  663. if (y >= tex->height) return 0;
  664. if (x <= 0) return 0;
  665. if (y <= 0) return 0;
  666. double u_ratio = u - x;
  667. double v_ratio = v - y;
  668. double u_o = 1 - u_ratio;
  669. double v_o = 1 - v_ratio;
  670. int r_ALP = 255;
  671. if (tex->alpha == ALPHA_MASK) {
  672. if (x == tex->width - 1 || y == tex->height - 1) return (SPRITE(tex,x,y) | 0xFF000000) & (0xFFFFFF + ((uint32_t)_RED(SMASKS(tex,x,y)) << 24));
  673. r_ALP = (_RED(SMASKS(tex,x,y)) * u_o + _RED(SMASKS(tex,x+1,y)) * u_ratio) * v_o + (_RED(SMASKS(tex,x,y+1)) * u_o + _RED(SMASKS(tex,x+1,y+1)) * u_ratio) * v_ratio;
  674. } else if (tex->alpha == ALPHA_EMBEDDED) {
  675. if (x == tex->width - 1 || y == tex->height - 1) return (SPRITE(tex,x,y) | 0xFF000000) & (0xFFFFFF + ((uint32_t)_ALP(SPRITE(tex,x,y)) << 24));
  676. r_ALP = (_ALP(SPRITE(tex,x,y)) * u_o + _ALP(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_ALP(SPRITE(tex,x,y+1)) * u_o + _ALP(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio;
  677. }
  678. if (x == tex->width - 1 || y == tex->height - 1) return SPRITE(tex,x,y);
  679. int r_RED = (_RED(SPRITE(tex,x,y)) * u_o + _RED(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_RED(SPRITE(tex,x,y+1)) * u_o + _RED(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio;
  680. int r_BLU = (_BLU(SPRITE(tex,x,y)) * u_o + _BLU(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_BLU(SPRITE(tex,x,y+1)) * u_o + _BLU(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio;
  681. int r_GRE = (_GRE(SPRITE(tex,x,y)) * u_o + _GRE(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_GRE(SPRITE(tex,x,y+1)) * u_o + _GRE(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio;
  682. return rgb(r_RED,r_GRE,r_BLU) & (0xFFFFFF + ((uint32_t)r_ALP << 24));
  683. }
  684. void draw_sprite_scaled(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, uint16_t width, uint16_t height) {
  685. int32_t _left = max(x, 0);
  686. int32_t _top = max(y, 0);
  687. int32_t _right = min(x + width, ctx->width - 1);
  688. int32_t _bottom = min(y + height, ctx->height - 1);
  689. for (uint16_t _y = 0; _y < height; ++_y) {
  690. if (!_is_in_clip(ctx, y + _y)) continue;
  691. for (uint16_t _x = 0; _x < width; ++_x) {
  692. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  693. continue;
  694. if (sprite->alpha > 0) {
  695. uint32_t n_color = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height);
  696. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), n_color);
  697. } else {
  698. GFX(ctx, x + _x, y + _y) = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height);
  699. }
  700. }
  701. }
  702. }
  703. void draw_sprite_alpha(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, float alpha) {
  704. int32_t _left = max(x, 0);
  705. int32_t _top = max(y, 0);
  706. int32_t _right = min(x + sprite->width, ctx->width - 1);
  707. int32_t _bottom = min(y + sprite->height, ctx->height - 1);
  708. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  709. if (!_is_in_clip(ctx, y + _y)) continue;
  710. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  711. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  712. continue;
  713. uint32_t n_color = SPRITE(sprite, _x, _y);
  714. uint32_t f_color = premultiply((n_color & 0xFFFFFF) | ((uint32_t)(255 * alpha) << 24));
  715. f_color = (f_color & 0xFFFFFF) | ((uint32_t)(alpha * _ALP(n_color)) << 24);
  716. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), f_color);
  717. }
  718. }
  719. }
  720. void draw_sprite_alpha_paint(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, float alpha, uint32_t c) {
  721. int32_t _left = max(x, 0);
  722. int32_t _top = max(y, 0);
  723. int32_t _right = min(x + sprite->width, ctx->width - 1);
  724. int32_t _bottom = min(y + sprite->height, ctx->height - 1);
  725. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  726. if (!_is_in_clip(ctx, y + _y)) continue;
  727. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  728. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  729. continue;
  730. uint32_t n_color = SPRITE(sprite, _x, _y);
  731. uint32_t f_color = rgb(_ALP(n_color) * alpha, 0, 0);
  732. GFX(ctx, x + _x, y + _y) = alpha_blend(GFX(ctx, x + _x, y + _y), c, f_color);
  733. }
  734. }
  735. }
  736. void draw_sprite_scaled_alpha(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, uint16_t width, uint16_t height, float alpha) {
  737. int32_t _left = max(x, 0);
  738. int32_t _top = max(y, 0);
  739. int32_t _right = min(x + width, ctx->width - 1);
  740. int32_t _bottom = min(y + height, ctx->height - 1);
  741. for (uint16_t _y = 0; _y < height; ++_y) {
  742. if (!_is_in_clip(ctx, y + _y)) continue;
  743. for (uint16_t _x = 0; _x < width; ++_x) {
  744. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  745. continue;
  746. uint32_t n_color = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height);
  747. uint32_t f_color = premultiply((n_color & 0xFFFFFF) | ((uint32_t)(255 * alpha) << 24));
  748. f_color = (f_color & 0xFFFFFF) | ((uint32_t)(alpha * _ALP(n_color)) << 24);
  749. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), f_color);
  750. }
  751. }
  752. }
  753. uint32_t interp_colors(uint32_t bottom, uint32_t top, uint8_t interp) {
  754. uint8_t red = (_RED(bottom) * (255 - interp) + _RED(top) * interp) / 255;
  755. uint8_t gre = (_GRE(bottom) * (255 - interp) + _GRE(top) * interp) / 255;
  756. uint8_t blu = (_BLU(bottom) * (255 - interp) + _BLU(top) * interp) / 255;
  757. uint8_t alp = (_ALP(bottom) * (255 - interp) + _ALP(top) * interp) / 255;
  758. return rgba(red,gre,blu, alp);
  759. }
  760. void draw_rectangle(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, uint32_t color) {
  761. int32_t _left = max(x, 0);
  762. int32_t _top = max(y, 0);
  763. int32_t _right = min(x + width, ctx->width - 1);
  764. int32_t _bottom = min(y + height, ctx->height - 1);
  765. for (uint16_t _y = 0; _y < height; ++_y) {
  766. if (!_is_in_clip(ctx, y + _y)) continue;
  767. for (uint16_t _x = 0; _x < width; ++_x) {
  768. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  769. continue;
  770. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), color);
  771. }
  772. }
  773. }
  774. void draw_rectangle_solid(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, uint32_t color) {
  775. int32_t _left = max(x, 0);
  776. int32_t _top = max(y, 0);
  777. int32_t _right = min(x + width, ctx->width - 1);
  778. int32_t _bottom = min(y + height, ctx->height - 1);
  779. for (uint16_t _y = 0; _y < height; ++_y) {
  780. if (!_is_in_clip(ctx, y + _y)) continue;
  781. for (uint16_t _x = 0; _x < width; ++_x) {
  782. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  783. continue;
  784. GFX(ctx, x + _x, y + _y) = color;
  785. }
  786. }
  787. }
  788. void draw_rounded_rectangle(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, int radius, uint32_t color) {
  789. /* Draw a rounded rectangle */
  790. if (radius > width / 2) {
  791. radius = width / 2;
  792. }
  793. if (radius > height / 2) {
  794. radius = height / 2;
  795. }
  796. uint32_t c = premultiply(color);
  797. for (int row = y; row < y + height; row++){
  798. for (int col = x; col < x + width; col++) {
  799. if ((col < x + radius || col > x + width - radius - 1) &&
  800. (row < y + radius || row > y + height - radius - 1)) {
  801. continue;
  802. }
  803. GFX(ctx, col, row) = alpha_blend_rgba(GFX(ctx, col, row), c);
  804. }
  805. }
  806. /* draw the actual rounding */
  807. for (int i = 0; i < radius; ++i) {
  808. long r2 = radius * radius;
  809. long i2 = i * i;
  810. long j2 = r2 - i2;
  811. double j_max = sqrt((double)j2);
  812. for (int j = 0; j <= (int)j_max; ++j) {
  813. int _x = x + width - radius + i;
  814. int _y = y + height - radius + j;
  815. int _z = y + radius - j - 1;
  816. uint32_t c = color;
  817. if (j == (int)j_max) {
  818. c = premultiply(rgba(_RED(c),_GRE(c),_BLU(c),(int)((double)_ALP(c) * (j_max - (double)j))));
  819. } else {
  820. c = premultiply(c);
  821. }
  822. GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), c);
  823. GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), c);
  824. _x = x + radius - i - 1;
  825. GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), c);
  826. GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), c);
  827. }
  828. }
  829. }
  830. void draw_rounded_rectangle_pattern(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, int radius, uint32_t (*pattern)(int32_t x, int32_t y, double alpha, void * extra), void * extra) {
  831. /* Draw a rounded rectangle */
  832. if (radius > width / 2) {
  833. radius = width / 2;
  834. }
  835. if (radius > height / 2) {
  836. radius = height / 2;
  837. }
  838. for (int row = y; row < y + height; row++){
  839. for (int col = x; col < x + width; col++) {
  840. if ((col < x + radius || col > x + width - radius - 1) &&
  841. (row < y + radius || row > y + height - radius - 1)) {
  842. continue;
  843. }
  844. GFX(ctx, col, row) = alpha_blend_rgba(GFX(ctx, col, row), pattern(col,row,1.0,extra));
  845. }
  846. }
  847. /* draw the actual rounding */
  848. for (int i = 0; i < radius; ++i) {
  849. long r2 = radius * radius;
  850. long i2 = i * i;
  851. long j2 = r2 - i2;
  852. double j_max = sqrt((double)j2);
  853. for (int j = 0; j <= (int)j_max; ++j) {
  854. int _x = x + width - radius + i;
  855. int _y = y + height - radius + j;
  856. int _z = y + radius - j - 1;
  857. double alpha = (j_max - (double)j);
  858. GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), pattern(_x,_y,alpha,extra));
  859. GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), pattern(_x,_z,alpha,extra));
  860. _x = x + radius - i - 1;
  861. GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), pattern(_x,_y,alpha,extra));
  862. GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), pattern(_x,_z,alpha,extra));
  863. }
  864. }
  865. }
  866. float gfx_point_distance(struct gfx_point * a, struct gfx_point * b) {
  867. return sqrt((a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y));
  868. }
  869. float gfx_point_distance_squared(struct gfx_point * a, struct gfx_point * b) {
  870. return (a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y);
  871. }
  872. float gfx_point_dot(struct gfx_point * a, struct gfx_point * b) {
  873. return (a->x * b->x) + (a->y * b->y);
  874. }
  875. struct gfx_point gfx_point_sub(struct gfx_point * a, struct gfx_point * b) {
  876. struct gfx_point p = {a->x - b->x, a->y - b->y};
  877. return p;
  878. }
  879. struct gfx_point gfx_point_add(struct gfx_point * a, struct gfx_point * b) {
  880. struct gfx_point p = {a->x + b->x, a->y + b->y};
  881. return p;
  882. }
  883. #define fmax(a,b) ((a) > (b) ? (a) : (b))
  884. #define fmin(a,b) ((a) < (b) ? (a) : (b))
  885. float gfx_line_distance(struct gfx_point * p, struct gfx_point * v, struct gfx_point * w) {
  886. float lengthlength = gfx_point_distance_squared(v,w);
  887. if (lengthlength == 0.0) return gfx_point_distance(p, v); /* point */
  888. struct gfx_point p_v = gfx_point_sub(p,v);
  889. struct gfx_point w_v = gfx_point_sub(w,v);
  890. float tmp = gfx_point_dot(&p_v,&w_v) / lengthlength;
  891. tmp = fmin(1.0,tmp);
  892. float t = fmax(0.0, tmp);
  893. w_v.x *= t;
  894. w_v.y *= t;
  895. struct gfx_point v_t = gfx_point_add(v, &w_v);
  896. return gfx_point_distance(p, &v_t);
  897. }
  898. /**
  899. * This is slow, but it works...
  900. *
  901. * Maybe acceptable for baked UI elements?
  902. */
  903. void draw_line_aa(gfx_context_t * ctx, int x_1, int x_2, int y_1, int y_2, uint32_t color, float thickness) {
  904. struct gfx_point v = {(float)x_1, (float)y_1};
  905. struct gfx_point w = {(float)x_2, (float)y_2};
  906. for (int y = 0; y < ctx->height; ++y) {
  907. for (int x = 0; x < ctx->width; ++x) {
  908. struct gfx_point p = {x,y};
  909. float d = gfx_line_distance(&p,&v,&w);
  910. if (d < thickness + 0.5) {
  911. if (d < thickness - 0.5) {
  912. GFX(ctx,x,y) = color;
  913. } else {
  914. uint32_t f_color = rgb(255 * (1.0 - (d - thickness + 0.5)), 0, 0);
  915. GFX(ctx,x,y) = alpha_blend(GFX(ctx,x,y), color, f_color);
  916. }
  917. }
  918. }
  919. }
  920. }
  921. static void calc_rotation(double x, double y, double px, double py, double s, double c, double * u, double * v) {
  922. /* Translate to pivot */
  923. x -= px;
  924. y -= py;
  925. *u = (x * c - y * s) + px;
  926. *v = (x * s + y * c) + py;
  927. }
  928. void draw_sprite_rotate(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, float rotation, float alpha) {
  929. double originx = (double)sprite->width / 2.0;
  930. double originy = (double)sprite->height / 2.0;
  931. /* Calculate corners */
  932. double ul_x, ul_y;
  933. double ll_x, ll_y;
  934. double ur_x, ur_y;
  935. double lr_x, lr_y;
  936. double _s = sin(rotation);
  937. double _c = cos(rotation);
  938. calc_rotation(-sprite->width/2, -sprite->height/2, 0, 0, _s, _c, &ul_x, &ul_y);
  939. calc_rotation(-sprite->width/2, sprite->height/2, 0, 0, _s, _c, &ll_x, &ll_y);
  940. calc_rotation(sprite->width/2, -sprite->height/2, 0, 0, _s, _c, &ur_x, &ur_y);
  941. calc_rotation(sprite->width/2, sprite->height/2, 0, 0, _s, _c, &lr_x, &lr_y);
  942. _s = sin(-rotation);
  943. _c = cos(-rotation);
  944. /* Calculate bounds */
  945. int32_t _left = min(min(ul_x, ll_x), min(ur_x, lr_x));
  946. int32_t _top = min(min(ul_y, ll_y), min(ur_y, lr_y));
  947. int32_t _right = max(max(ul_x, ll_x), max(ur_x, lr_x));
  948. int32_t _bottom = max(max(ul_y, ll_y), max(ur_y, lr_y));
  949. for (int32_t _y = _top; _y < _bottom; ++_y) {
  950. if (_y + y < 0) continue;
  951. if (_y + y >= ctx->height) break;
  952. if (!_is_in_clip(ctx, y + _y)) continue;
  953. for (int32_t _x = _left; _x < _right; ++_x) {
  954. if (_x + x < 0) continue;
  955. if (_x + x >= ctx->width) break;
  956. double u, v;
  957. calc_rotation(_x + originx, _y + originy, originx, originy, _s, _c, &u, &v);
  958. uint32_t n_color = getBilinearFilteredPixelColor(sprite, u / (double)sprite->width, v/(double)sprite->height);
  959. uint32_t f_color = premultiply((n_color & 0xFFFFFF) | ((uint32_t)(255 * alpha) << 24));
  960. f_color = (f_color & 0xFFFFFF) | ((uint32_t)(alpha * _ALP(n_color)) << 24);
  961. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), f_color);
  962. }
  963. }
  964. }
  965. uint32_t gfx_vertical_gradient_pattern(int32_t x, int32_t y, double alpha, void * extra) {
  966. struct gradient_definition * gradient = extra;
  967. int base_r = _RED(gradient->top), base_g = _GRE(gradient->top), base_b = _BLU(gradient->top);
  968. int last_r = _RED(gradient->bottom), last_g = _GRE(gradient->bottom), last_b = _BLU(gradient->bottom);
  969. double gradpoint = (double)(y - (gradient->y)) / (double)gradient->height;
  970. if (alpha > 1.0) alpha = 1.0;
  971. if (alpha < 0.0) alpha = 0.0;
  972. return premultiply(rgba(
  973. base_r * (1.0 - gradpoint) + last_r * (gradpoint),
  974. base_g * (1.0 - gradpoint) + last_g * (gradpoint),
  975. base_b * (1.0 - gradpoint) + last_b * (gradpoint),
  976. alpha * 255));
  977. }