sdl2surface_rawfb.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2016-2017 Patrick Rudolph <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. /*
  23. * ==============================================================
  24. *
  25. * API
  26. *
  27. * ===============================================================
  28. */
  29. /* Adapted from nulear_rawfb.h for use with SDL_Surface by Martijn Versteegh*/
  30. #ifndef NK_SDLSURFACE_H_
  31. #define NK_SDLSURFACE_H_
  32. #include <SDL.h>
  33. #include <SDL_surface.h>
  34. struct sdlsurface_context *nk_sdlsurface_init(SDL_Surface *fb, float fontSize);
  35. void nk_sdlsurface_render(const struct sdlsurface_context *sdlsurface, const struct nk_color clear, const unsigned char enable_clear);
  36. void nk_sdlsurface_shutdown(struct sdlsurface_context *sdlsurface);
  37. #endif
  38. /*
  39. * ==============================================================
  40. *
  41. * IMPLEMENTATION
  42. *
  43. * ===============================================================
  44. */
  45. #ifdef NK_SDLSURFACE_IMPLEMENTATION
  46. struct sdlsurface_context {
  47. struct nk_context ctx;
  48. struct nk_rect scissors;
  49. struct SDL_Surface *fb;
  50. struct SDL_Surface *font_tex;
  51. struct nk_font_atlas atlas;
  52. };
  53. #ifndef MIN
  54. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  55. #endif
  56. #ifndef MAX
  57. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  58. #endif
  59. static unsigned int
  60. nk_sdlsurface_color2int(const struct nk_color c, const SDL_PixelFormat *format)
  61. {
  62. return SDL_MapRGBA(format, c.r, c.g, c.b, c.a);
  63. }
  64. static struct nk_color
  65. nk_sdlsurface_int2color(const unsigned int i, const SDL_PixelFormat *format)
  66. {
  67. struct nk_color col = {0,0,0,0};
  68. SDL_GetRGBA(i, format, &col.r, &col.g, &col.b, &col.a);
  69. return col;
  70. }
  71. static void
  72. nk_sdlsurface_ctx_setpixel(const struct sdlsurface_context *sdlsurface,
  73. const short x0, const short y0, const struct nk_color col)
  74. {
  75. unsigned int c = nk_sdlsurface_color2int(col, sdlsurface->fb->format);
  76. unsigned char *pixels = sdlsurface->fb->pixels;
  77. pixels += y0 * sdlsurface->fb->pitch;
  78. if (y0 < sdlsurface->scissors.h && y0 >= sdlsurface->scissors.y &&
  79. x0 >= sdlsurface->scissors.x && x0 < sdlsurface->scissors.w) {
  80. if (sdlsurface->fb->format->BytesPerPixel == 4) {
  81. *((Uint32 *)pixels + x0) = c;
  82. } else if (sdlsurface->fb->format->BytesPerPixel == 2) {
  83. *((Uint16 *)pixels + x0) = c;
  84. } else {
  85. *((Uint8 *)pixels + x0) = c;
  86. }
  87. }
  88. }
  89. static void
  90. nk_sdlsurface_line_horizontal(const struct sdlsurface_context *sdlsurface,
  91. const short x0, const short y, const short x1, const struct nk_color col)
  92. {
  93. /* This function is called the most. Try to optimize it a bit...
  94. * It does not check for scissors or image borders.
  95. * The caller has to make sure it does no exceed bounds. */
  96. unsigned int i, n;
  97. unsigned char c[16 * 4];
  98. unsigned char *pixels = sdlsurface->fb->pixels;
  99. unsigned int bpp = sdlsurface->fb->format->BytesPerPixel;
  100. pixels += (y * sdlsurface->fb->pitch) + (x0 * bpp);
  101. n = (x1 - x0) * bpp;
  102. if (bpp == 4) {
  103. for (i = 0; i < sizeof(c) / bpp; i++)
  104. ((Uint32 *)c)[i] = nk_sdlsurface_color2int(col, sdlsurface->fb->format);
  105. } else if (bpp == 2) {
  106. for (i = 0; i < sizeof(c) / bpp; i++)
  107. ((Uint16 *)c)[i] = nk_sdlsurface_color2int(col, sdlsurface->fb->format);
  108. } else {
  109. for (i = 0; i < sizeof(c) / bpp; i++)
  110. ((Uint8 *)c)[i] = nk_sdlsurface_color2int(col, sdlsurface->fb->format);
  111. }
  112. while (n > sizeof(c)) {
  113. memcpy((void*)pixels, c, sizeof(c));
  114. n -= sizeof(c); pixels += sizeof(c);
  115. } for (i = 0; i < n; i++)
  116. pixels[i] = c[i];
  117. }
  118. static void
  119. nk_sdlsurface_img_setpixel(const struct SDL_Surface *img,
  120. const int x0, const int y0, const struct nk_color col)
  121. {
  122. unsigned int c = nk_sdlsurface_color2int(col, img->format);
  123. unsigned char *ptr;
  124. NK_ASSERT(img);
  125. if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
  126. ptr = (unsigned char *)img->pixels + (img->pitch * y0);
  127. if (img->format == NK_FONT_ATLAS_ALPHA8) {
  128. ptr[x0] = col.a;
  129. } else if (img->format->BytesPerPixel == 4) {
  130. ((Uint32 *)ptr)[x0] = c;
  131. } else if (img->format->BytesPerPixel == 2) {
  132. ((Uint16 *)ptr)[x0] = c;
  133. } else {
  134. ((Uint8 *)ptr)[x0] = c;
  135. }
  136. }
  137. }
  138. static struct nk_color
  139. nk_sdlsurface_img_getpixel(const struct SDL_Surface *img, const int x0, const int y0)
  140. {
  141. struct nk_color col = {0, 0, 0, 0};
  142. unsigned char *ptr;
  143. unsigned int pixel;
  144. NK_ASSERT(img);
  145. if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
  146. ptr = (unsigned char *)img->pixels + (img->pitch * y0);
  147. if (img->format == NK_FONT_ATLAS_ALPHA8) {
  148. col.a = ptr[x0];
  149. col.b = col.g = col.r = 0xff;
  150. } else if (img->format->BytesPerPixel == 4) {
  151. pixel = ((Uint32 *)ptr)[x0];
  152. col = nk_sdlsurface_int2color(pixel, img->format);
  153. } else if (img->format->BytesPerPixel == 2) {
  154. pixel = ((Uint16 *)ptr)[x0];
  155. col = nk_sdlsurface_int2color(pixel, img->format);
  156. } else {
  157. pixel = ((Uint8 *)ptr)[x0];
  158. col = nk_sdlsurface_int2color(pixel, img->format);
  159. }
  160. } return col;
  161. }
  162. static void
  163. nk_sdlsurface_img_blendpixel(const struct SDL_Surface *img,
  164. const int x0, const int y0, struct nk_color col)
  165. {
  166. struct nk_color col2;
  167. unsigned char inv_a;
  168. if (col.a == 0)
  169. return;
  170. inv_a = 0xff - col.a;
  171. col2 = nk_sdlsurface_img_getpixel(img, x0, y0);
  172. col.r = (col.r * col.a + col2.r * inv_a) >> 8;
  173. col.g = (col.g * col.a + col2.g * inv_a) >> 8;
  174. col.b = (col.b * col.a + col2.b * inv_a) >> 8;
  175. nk_sdlsurface_img_setpixel(img, x0, y0, col);
  176. }
  177. static void
  178. nk_sdlsurface_scissor(struct sdlsurface_context *sdlsurface,
  179. const float x,
  180. const float y,
  181. const float w,
  182. const float h)
  183. {
  184. sdlsurface->scissors.x = MIN(MAX(x, 0), sdlsurface->fb->w);
  185. sdlsurface->scissors.y = MIN(MAX(y, 0), sdlsurface->fb->h);
  186. sdlsurface->scissors.w = MIN(MAX(w + x, 0), sdlsurface->fb->w);
  187. sdlsurface->scissors.h = MIN(MAX(h + y, 0), sdlsurface->fb->h);
  188. }
  189. static void
  190. nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
  191. short x0, short y0, short x1, short y1,
  192. const unsigned int line_thickness, const struct nk_color col)
  193. {
  194. short tmp;
  195. int dy, dx, stepx, stepy;
  196. NK_UNUSED(line_thickness);
  197. dy = y1 - y0;
  198. dx = x1 - x0;
  199. /* fast path */
  200. if (dy == 0) {
  201. if (dx == 0 || y0 >= sdlsurface->scissors.h || y0 < sdlsurface->scissors.y)
  202. return;
  203. if (dx < 0) {
  204. /* swap x0 and x1 */
  205. tmp = x1;
  206. x1 = x0;
  207. x0 = tmp;
  208. }
  209. x1 = MIN(sdlsurface->scissors.w, x1);
  210. x0 = MIN(sdlsurface->scissors.w, x0);
  211. x1 = MAX(sdlsurface->scissors.x, x1);
  212. x0 = MAX(sdlsurface->scissors.x, x0);
  213. nk_sdlsurface_line_horizontal(sdlsurface, x0, y0, x1, col);
  214. return;
  215. }
  216. if (dy < 0) {
  217. dy = -dy;
  218. stepy = -1;
  219. } else stepy = 1;
  220. if (dx < 0) {
  221. dx = -dx;
  222. stepx = -1;
  223. } else stepx = 1;
  224. dy <<= 1;
  225. dx <<= 1;
  226. nk_sdlsurface_ctx_setpixel(sdlsurface, x0, y0, col);
  227. if (dx > dy) {
  228. int fraction = dy - (dx >> 1);
  229. while (x0 != x1) {
  230. if (fraction >= 0) {
  231. y0 += stepy;
  232. fraction -= dx;
  233. }
  234. x0 += stepx;
  235. fraction += dy;
  236. nk_sdlsurface_ctx_setpixel(sdlsurface, x0, y0, col);
  237. }
  238. } else {
  239. int fraction = dx - (dy >> 1);
  240. while (y0 != y1) {
  241. if (fraction >= 0) {
  242. x0 += stepx;
  243. fraction -= dy;
  244. }
  245. y0 += stepy;
  246. fraction += dx;
  247. nk_sdlsurface_ctx_setpixel(sdlsurface, x0, y0, col);
  248. }
  249. }
  250. }
  251. static void
  252. nk_sdlsurface_fill_polygon(const struct sdlsurface_context *sdlsurface,
  253. const struct nk_vec2i *pnts, int count, const struct nk_color col)
  254. {
  255. int i = 0;
  256. #define MAX_POINTS 64
  257. int left = 10000, top = 10000, bottom = 0, right = 0;
  258. int nodes, nodeX[MAX_POINTS], pixelX, pixelY, j, swap ;
  259. if (count == 0) return;
  260. if (count > MAX_POINTS)
  261. count = MAX_POINTS;
  262. /* Get polygon dimensions */
  263. for (i = 0; i < count; i++) {
  264. if (left > pnts[i].x)
  265. left = pnts[i].x;
  266. if (right < pnts[i].x)
  267. right = pnts[i].x;
  268. if (top > pnts[i].y)
  269. top = pnts[i].y;
  270. if (bottom < pnts[i].y)
  271. bottom = pnts[i].y;
  272. } bottom++; right++;
  273. /* Polygon scanline algorithm released under public-domain by Darel Rex Finley, 2007 */
  274. /* Loop through the rows of the image. */
  275. for (pixelY = top; pixelY < bottom; pixelY ++) {
  276. nodes = 0; /* Build a list of nodes. */
  277. j = count - 1;
  278. for (i = 0; i < count; i++) {
  279. if (((pnts[i].y < pixelY) && (pnts[j].y >= pixelY)) ||
  280. ((pnts[j].y < pixelY) && (pnts[i].y >= pixelY))) {
  281. nodeX[nodes++]= (int)((float)pnts[i].x
  282. + ((float)pixelY - (float)pnts[i].y) / ((float)pnts[j].y - (float)pnts[i].y)
  283. * ((float)pnts[j].x - (float)pnts[i].x));
  284. } j = i;
  285. }
  286. /* Sort the nodes, via a simple “Bubble” sort. */
  287. i = 0;
  288. while (i < nodes - 1) {
  289. if (nodeX[i] > nodeX[i+1]) {
  290. swap = nodeX[i];
  291. nodeX[i] = nodeX[i+1];
  292. nodeX[i+1] = swap;
  293. if (i) i--;
  294. } else i++;
  295. }
  296. /* Fill the pixels between node pairs. */
  297. for (i = 0; i < nodes; i += 2) {
  298. if (nodeX[i+0] >= right) break;
  299. if (nodeX[i+1] > left) {
  300. if (nodeX[i+0] < left) nodeX[i+0] = left ;
  301. if (nodeX[i+1] > right) nodeX[i+1] = right;
  302. for (pixelX = nodeX[i]; pixelX < nodeX[i + 1]; pixelX++)
  303. nk_sdlsurface_ctx_setpixel(sdlsurface, pixelX, pixelY, col);
  304. }
  305. }
  306. }
  307. #undef MAX_POINTS
  308. }
  309. static void
  310. nk_sdlsurface_stroke_arc(const struct sdlsurface_context *sdlsurface,
  311. short x0, short y0, short w, short h, const short s,
  312. const short line_thickness, const struct nk_color col)
  313. {
  314. /* Bresenham's ellipses - modified to draw one quarter */
  315. const int a2 = (w * w) / 4;
  316. const int b2 = (h * h) / 4;
  317. const int fa2 = 4 * a2, fb2 = 4 * b2;
  318. int x, y, sigma;
  319. NK_UNUSED(line_thickness);
  320. if (s != 0 && s != 90 && s != 180 && s != 270) return;
  321. if (w < 1 || h < 1) return;
  322. /* Convert upper left to center */
  323. h = (h + 1) / 2;
  324. w = (w + 1) / 2;
  325. x0 += w; y0 += h;
  326. /* First half */
  327. for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
  328. if (s == 180)
  329. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col);
  330. else if (s == 270)
  331. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col);
  332. else if (s == 0)
  333. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col);
  334. else if (s == 90)
  335. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col);
  336. if (sigma >= 0) {
  337. sigma += fa2 * (1 - y);
  338. y--;
  339. } sigma += b2 * ((4 * x) + 6);
  340. }
  341. /* Second half */
  342. for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
  343. if (s == 180)
  344. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col);
  345. else if (s == 270)
  346. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col);
  347. else if (s == 0)
  348. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col);
  349. else if (s == 90)
  350. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col);
  351. if (sigma >= 0) {
  352. sigma += fb2 * (1 - x);
  353. x--;
  354. } sigma += a2 * ((4 * y) + 6);
  355. }
  356. }
  357. static void
  358. nk_sdlsurface_fill_arc(const struct sdlsurface_context *sdlsurface, short x0, short y0,
  359. short w, short h, const short s, const struct nk_color col)
  360. {
  361. /* Bresenham's ellipses - modified to fill one quarter */
  362. const int a2 = (w * w) / 4;
  363. const int b2 = (h * h) / 4;
  364. const int fa2 = 4 * a2, fb2 = 4 * b2;
  365. int x, y, sigma;
  366. struct nk_vec2i pnts[3];
  367. if (w < 1 || h < 1) return;
  368. if (s != 0 && s != 90 && s != 180 && s != 270)
  369. return;
  370. /* Convert upper left to center */
  371. h = (h + 1) / 2;
  372. w = (w + 1) / 2;
  373. x0 += w;
  374. y0 += h;
  375. pnts[0].x = x0;
  376. pnts[0].y = y0;
  377. pnts[2].x = x0;
  378. pnts[2].y = y0;
  379. /* First half */
  380. for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
  381. if (s == 180) {
  382. pnts[1].x = x0 + x; pnts[1].y = y0 + y;
  383. } else if (s == 270) {
  384. pnts[1].x = x0 - x; pnts[1].y = y0 + y;
  385. } else if (s == 0) {
  386. pnts[1].x = x0 + x; pnts[1].y = y0 - y;
  387. } else if (s == 90) {
  388. pnts[1].x = x0 - x; pnts[1].y = y0 - y;
  389. }
  390. nk_sdlsurface_fill_polygon(sdlsurface, pnts, 3, col);
  391. pnts[2] = pnts[1];
  392. if (sigma >= 0) {
  393. sigma += fa2 * (1 - y);
  394. y--;
  395. } sigma += b2 * ((4 * x) + 6);
  396. }
  397. /* Second half */
  398. for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
  399. if (s == 180) {
  400. pnts[1].x = x0 + x; pnts[1].y = y0 + y;
  401. } else if (s == 270) {
  402. pnts[1].x = x0 - x; pnts[1].y = y0 + y;
  403. } else if (s == 0) {
  404. pnts[1].x = x0 + x; pnts[1].y = y0 - y;
  405. } else if (s == 90) {
  406. pnts[1].x = x0 - x; pnts[1].y = y0 - y;
  407. }
  408. nk_sdlsurface_fill_polygon(sdlsurface, pnts, 3, col);
  409. pnts[2] = pnts[1];
  410. if (sigma >= 0) {
  411. sigma += fb2 * (1 - x);
  412. x--;
  413. } sigma += a2 * ((4 * y) + 6);
  414. }
  415. }
  416. static void
  417. nk_sdlsurface_stroke_rect(const struct sdlsurface_context *sdlsurface,
  418. const short x, const short y, const short w, const short h,
  419. const short r, const short line_thickness, const struct nk_color col)
  420. {
  421. if (r == 0) {
  422. nk_sdlsurface_stroke_line(sdlsurface, x, y, x + w, y, line_thickness, col);
  423. nk_sdlsurface_stroke_line(sdlsurface, x, y + h, x + w, y + h, line_thickness, col);
  424. nk_sdlsurface_stroke_line(sdlsurface, x, y, x, y + h, line_thickness, col);
  425. nk_sdlsurface_stroke_line(sdlsurface, x + w, y, x + w, y + h, line_thickness, col);
  426. } else {
  427. const short xc = x + r;
  428. const short yc = y + r;
  429. const short wc = (short)(w - 2 * r);
  430. const short hc = (short)(h - 2 * r);
  431. nk_sdlsurface_stroke_line(sdlsurface, xc, y, xc + wc, y, line_thickness, col);
  432. nk_sdlsurface_stroke_line(sdlsurface, x + w, yc, x + w, yc + hc, line_thickness, col);
  433. nk_sdlsurface_stroke_line(sdlsurface, xc, y + h, xc + wc, y + h, line_thickness, col);
  434. nk_sdlsurface_stroke_line(sdlsurface, x, yc, x, yc + hc, line_thickness, col);
  435. nk_sdlsurface_stroke_arc(sdlsurface, xc + wc - r, y,
  436. (unsigned)r*2, (unsigned)r*2, 0 , line_thickness, col);
  437. nk_sdlsurface_stroke_arc(sdlsurface, x, y,
  438. (unsigned)r*2, (unsigned)r*2, 90 , line_thickness, col);
  439. nk_sdlsurface_stroke_arc(sdlsurface, x, yc + hc - r,
  440. (unsigned)r*2, (unsigned)r*2, 270 , line_thickness, col);
  441. nk_sdlsurface_stroke_arc(sdlsurface, xc + wc - r, yc + hc - r,
  442. (unsigned)r*2, (unsigned)r*2, 180 , line_thickness, col);
  443. }
  444. }
  445. static void
  446. nk_sdlsurface_fill_rect(const struct sdlsurface_context *sdlsurface,
  447. const short x, const short y, const short w, const short h,
  448. const short r, const struct nk_color col)
  449. {
  450. int i;
  451. if (r == 0) {
  452. for (i = 0; i < h; i++)
  453. nk_sdlsurface_stroke_line(sdlsurface, x, y + i, x + w, y + i, 1, col);
  454. } else {
  455. const short xc = x + r;
  456. const short yc = y + r;
  457. const short wc = (short)(w - 2 * r);
  458. const short hc = (short)(h - 2 * r);
  459. struct nk_vec2i pnts[12];
  460. pnts[0].x = x;
  461. pnts[0].y = yc;
  462. pnts[1].x = xc;
  463. pnts[1].y = yc;
  464. pnts[2].x = xc;
  465. pnts[2].y = y;
  466. pnts[3].x = xc + wc;
  467. pnts[3].y = y;
  468. pnts[4].x = xc + wc;
  469. pnts[4].y = yc;
  470. pnts[5].x = x + w;
  471. pnts[5].y = yc;
  472. pnts[6].x = x + w;
  473. pnts[6].y = yc + hc;
  474. pnts[7].x = xc + wc;
  475. pnts[7].y = yc + hc;
  476. pnts[8].x = xc + wc;
  477. pnts[8].y = y + h;
  478. pnts[9].x = xc;
  479. pnts[9].y = y + h;
  480. pnts[10].x = xc;
  481. pnts[10].y = yc + hc;
  482. pnts[11].x = x;
  483. pnts[11].y = yc + hc;
  484. nk_sdlsurface_fill_polygon(sdlsurface, pnts, 12, col);
  485. nk_sdlsurface_fill_arc(sdlsurface, xc + wc - r, y,
  486. (unsigned)r*2, (unsigned)r*2, 0 , col);
  487. nk_sdlsurface_fill_arc(sdlsurface, x, y,
  488. (unsigned)r*2, (unsigned)r*2, 90 , col);
  489. nk_sdlsurface_fill_arc(sdlsurface, x, yc + hc - r,
  490. (unsigned)r*2, (unsigned)r*2, 270 , col);
  491. nk_sdlsurface_fill_arc(sdlsurface, xc + wc - r, yc + hc - r,
  492. (unsigned)r*2, (unsigned)r*2, 180 , col);
  493. }
  494. }
  495. NK_API void
  496. nk_sdlsurface_draw_rect_multi_color(const struct sdlsurface_context *sdlsurface,
  497. const short x, const short y, const short w, const short h, struct nk_color tl,
  498. struct nk_color tr, struct nk_color br, struct nk_color bl)
  499. {
  500. int i, j;
  501. struct nk_color *edge_buf;
  502. struct nk_color *edge_t;
  503. struct nk_color *edge_b;
  504. struct nk_color *edge_l;
  505. struct nk_color *edge_r;
  506. struct nk_color pixel;
  507. edge_buf = malloc(((2*w) + (2*h)) * sizeof(struct nk_color));
  508. if (edge_buf == NULL)
  509. return;
  510. edge_t = edge_buf;
  511. edge_b = edge_buf + w;
  512. edge_l = edge_buf + (w*2);
  513. edge_r = edge_buf + (w*2) + h;
  514. /* Top and bottom edge gradients */
  515. for (i=0; i<w; i++)
  516. {
  517. edge_t[i].r = (((((float)tr.r - tl.r)/(w-1))*i) + 0.5) + tl.r;
  518. edge_t[i].g = (((((float)tr.g - tl.g)/(w-1))*i) + 0.5) + tl.g;
  519. edge_t[i].b = (((((float)tr.b - tl.b)/(w-1))*i) + 0.5) + tl.b;
  520. edge_t[i].a = (((((float)tr.a - tl.a)/(w-1))*i) + 0.5) + tl.a;
  521. edge_b[i].r = (((((float)br.r - bl.r)/(w-1))*i) + 0.5) + bl.r;
  522. edge_b[i].g = (((((float)br.g - bl.g)/(w-1))*i) + 0.5) + bl.g;
  523. edge_b[i].b = (((((float)br.b - bl.b)/(w-1))*i) + 0.5) + bl.b;
  524. edge_b[i].a = (((((float)br.a - bl.a)/(w-1))*i) + 0.5) + bl.a;
  525. }
  526. /* Left and right edge gradients */
  527. for (i=0; i<h; i++)
  528. {
  529. edge_l[i].r = (((((float)bl.r - tl.r)/(h-1))*i) + 0.5) + tl.r;
  530. edge_l[i].g = (((((float)bl.g - tl.g)/(h-1))*i) + 0.5) + tl.g;
  531. edge_l[i].b = (((((float)bl.b - tl.b)/(h-1))*i) + 0.5) + tl.b;
  532. edge_l[i].a = (((((float)bl.a - tl.a)/(h-1))*i) + 0.5) + tl.a;
  533. edge_r[i].r = (((((float)br.r - tr.r)/(h-1))*i) + 0.5) + tr.r;
  534. edge_r[i].g = (((((float)br.g - tr.g)/(h-1))*i) + 0.5) + tr.g;
  535. edge_r[i].b = (((((float)br.b - tr.b)/(h-1))*i) + 0.5) + tr.b;
  536. edge_r[i].a = (((((float)br.a - tr.a)/(h-1))*i) + 0.5) + tr.a;
  537. }
  538. for (i=0; i<h; i++) {
  539. for (j=0; j<w; j++) {
  540. if (i==0) {
  541. nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_t[j]);
  542. } else if (i==h-1) {
  543. nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_b[j]);
  544. } else {
  545. if (j==0) {
  546. nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_l[i]);
  547. } else if (j==w-1) {
  548. nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_r[i]);
  549. } else {
  550. pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r;
  551. pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g;
  552. pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b;
  553. pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a;
  554. nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, pixel);
  555. }
  556. }
  557. }
  558. }
  559. free(edge_buf);
  560. }
  561. static void
  562. nk_sdlsurface_fill_triangle(const struct sdlsurface_context *sdlsurface,
  563. const short x0, const short y0, const short x1, const short y1,
  564. const short x2, const short y2, const struct nk_color col)
  565. {
  566. struct nk_vec2i pnts[3];
  567. pnts[0].x = x0;
  568. pnts[0].y = y0;
  569. pnts[1].x = x1;
  570. pnts[1].y = y1;
  571. pnts[2].x = x2;
  572. pnts[2].y = y2;
  573. nk_sdlsurface_fill_polygon(sdlsurface, pnts, 3, col);
  574. }
  575. static void
  576. nk_sdlsurface_stroke_triangle(const struct sdlsurface_context *sdlsurface,
  577. const short x0, const short y0, const short x1, const short y1,
  578. const short x2, const short y2, const unsigned short line_thickness,
  579. const struct nk_color col)
  580. {
  581. nk_sdlsurface_stroke_line(sdlsurface, x0, y0, x1, y1, line_thickness, col);
  582. nk_sdlsurface_stroke_line(sdlsurface, x1, y1, x2, y2, line_thickness, col);
  583. nk_sdlsurface_stroke_line(sdlsurface, x2, y2, x0, y0, line_thickness, col);
  584. }
  585. static void
  586. nk_sdlsurface_stroke_polygon(const struct sdlsurface_context *sdlsurface,
  587. const struct nk_vec2i *pnts, const int count,
  588. const unsigned short line_thickness, const struct nk_color col)
  589. {
  590. int i;
  591. for (i = 1; i < count; ++i)
  592. nk_sdlsurface_stroke_line(sdlsurface, pnts[i-1].x, pnts[i-1].y, pnts[i].x,
  593. pnts[i].y, line_thickness, col);
  594. nk_sdlsurface_stroke_line(sdlsurface, pnts[count-1].x, pnts[count-1].y,
  595. pnts[0].x, pnts[0].y, line_thickness, col);
  596. }
  597. static void
  598. nk_sdlsurface_stroke_polyline(const struct sdlsurface_context *sdlsurface,
  599. const struct nk_vec2i *pnts, const int count,
  600. const unsigned short line_thickness, const struct nk_color col)
  601. {
  602. int i;
  603. for (i = 0; i < count-1; ++i)
  604. nk_sdlsurface_stroke_line(sdlsurface, pnts[i].x, pnts[i].y,
  605. pnts[i+1].x, pnts[i+1].y, line_thickness, col);
  606. }
  607. static void
  608. nk_sdlsurface_fill_circle(const struct sdlsurface_context *sdlsurface,
  609. short x0, short y0, short w, short h, const struct nk_color col)
  610. {
  611. /* Bresenham's ellipses */
  612. const int a2 = (w * w) / 4;
  613. const int b2 = (h * h) / 4;
  614. const int fa2 = 4 * a2, fb2 = 4 * b2;
  615. int x, y, sigma;
  616. /* Convert upper left to center */
  617. h = (h + 1) / 2;
  618. w = (w + 1) / 2;
  619. x0 += w;
  620. y0 += h;
  621. /* First half */
  622. for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
  623. nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
  624. nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
  625. if (sigma >= 0) {
  626. sigma += fa2 * (1 - y);
  627. y--;
  628. } sigma += b2 * ((4 * x) + 6);
  629. }
  630. /* Second half */
  631. for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
  632. nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
  633. nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
  634. if (sigma >= 0) {
  635. sigma += fb2 * (1 - x);
  636. x--;
  637. } sigma += a2 * ((4 * y) + 6);
  638. }
  639. }
  640. static void
  641. nk_sdlsurface_stroke_circle(const struct sdlsurface_context *sdlsurface,
  642. short x0, short y0, short w, short h, const short line_thickness,
  643. const struct nk_color col)
  644. {
  645. /* Bresenham's ellipses */
  646. const int a2 = (w * w) / 4;
  647. const int b2 = (h * h) / 4;
  648. const int fa2 = 4 * a2, fb2 = 4 * b2;
  649. int x, y, sigma;
  650. NK_UNUSED(line_thickness);
  651. /* Convert upper left to center */
  652. h = (h + 1) / 2;
  653. w = (w + 1) / 2;
  654. x0 += w;
  655. y0 += h;
  656. /* First half */
  657. for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
  658. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col);
  659. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col);
  660. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col);
  661. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col);
  662. if (sigma >= 0) {
  663. sigma += fa2 * (1 - y);
  664. y--;
  665. } sigma += b2 * ((4 * x) + 6);
  666. }
  667. /* Second half */
  668. for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
  669. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col);
  670. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col);
  671. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col);
  672. nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col);
  673. if (sigma >= 0) {
  674. sigma += fb2 * (1 - x);
  675. x--;
  676. } sigma += a2 * ((4 * y) + 6);
  677. }
  678. }
  679. static void
  680. nk_sdlsurface_stroke_curve(const struct sdlsurface_context *sdlsurface,
  681. const struct nk_vec2i p1, const struct nk_vec2i p2,
  682. const struct nk_vec2i p3, const struct nk_vec2i p4,
  683. const unsigned int num_segments, const unsigned short line_thickness,
  684. const struct nk_color col)
  685. {
  686. unsigned int i_step, segments;
  687. float t_step;
  688. struct nk_vec2i last = p1;
  689. segments = MAX(num_segments, 1);
  690. t_step = 1.0f/(float)segments;
  691. for (i_step = 1; i_step <= segments; ++i_step) {
  692. float t = t_step * (float)i_step;
  693. float u = 1.0f - t;
  694. float w1 = u*u*u;
  695. float w2 = 3*u*u*t;
  696. float w3 = 3*u*t*t;
  697. float w4 = t * t *t;
  698. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  699. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  700. nk_sdlsurface_stroke_line(sdlsurface, last.x, last.y,
  701. (short)x, (short)y, line_thickness,col);
  702. last.x = (short)x; last.y = (short)y;
  703. }
  704. }
  705. static void
  706. nk_sdlsurface_clear(const struct sdlsurface_context *sdlsurface, const struct nk_color col)
  707. {
  708. nk_sdlsurface_fill_rect(sdlsurface, 0, 0, sdlsurface->fb->w, sdlsurface->fb->h, 0, col);
  709. }
  710. struct sdlsurface_context*
  711. nk_sdlsurface_init(SDL_Surface *fb, float fontSize)
  712. {
  713. const void *tex;
  714. int texh, texw;
  715. struct sdlsurface_context *sdlsurface;
  716. assert((fb->format->format == SDL_PIXELFORMAT_ARGB8888)
  717. || (fb->format->format == SDL_PIXELFORMAT_RGBA8888));
  718. sdlsurface = malloc(sizeof(struct sdlsurface_context));
  719. if (!sdlsurface)
  720. return NULL;
  721. memset(sdlsurface, 0, sizeof(struct sdlsurface_context));
  722. sdlsurface->fb = fb;
  723. if (0 == nk_init_default(&sdlsurface->ctx, 0)) {
  724. free(sdlsurface);
  725. return NULL;
  726. }
  727. nk_font_atlas_init_default(&sdlsurface->atlas);
  728. nk_font_atlas_begin(&sdlsurface->atlas);
  729. sdlsurface->atlas.default_font = nk_font_atlas_add_default(&sdlsurface->atlas, fontSize, 0);
  730. tex = nk_font_atlas_bake(&sdlsurface->atlas, &texw, &texh, NK_FONT_ATLAS_RGBA32);
  731. if (!tex) {
  732. free(sdlsurface);
  733. return NULL;
  734. }
  735. sdlsurface->font_tex = SDL_CreateRGBSurface(0, texw, texh, 32, 0xff, 0xff00, 0xff0000, 0xff000000);
  736. memcpy(sdlsurface->font_tex->pixels, tex, texw * texh * 4);
  737. nk_font_atlas_end(&sdlsurface->atlas, nk_handle_ptr(NULL), NULL);
  738. if (sdlsurface->atlas.default_font)
  739. nk_style_set_font(&sdlsurface->ctx, &sdlsurface->atlas.default_font->handle);
  740. nk_style_load_all_cursors(&sdlsurface->ctx, sdlsurface->atlas.cursors);
  741. nk_sdlsurface_scissor(sdlsurface, 0, 0, sdlsurface->fb->w, sdlsurface->fb->h);
  742. return sdlsurface;
  743. }
  744. static void
  745. nk_sdlsurface_stretch_image(const struct SDL_Surface *dst,
  746. const struct SDL_Surface *src, const struct nk_rect *dst_rect,
  747. const struct nk_rect *src_rect, const struct nk_rect *dst_scissors,
  748. const struct nk_color *fg)
  749. {
  750. short i, j;
  751. struct nk_color col;
  752. float xinc = src_rect->w / dst_rect->w;
  753. float yinc = src_rect->h / dst_rect->h;
  754. float xoff = src_rect->x, yoff = src_rect->y;
  755. /* Simple nearest filtering rescaling */
  756. /* TODO: use bilinear filter */
  757. for (j = 0; j < (short)dst_rect->h; j++) {
  758. for (i = 0; i < (short)dst_rect->w; i++) {
  759. if (dst_scissors) {
  760. if (i + (int)(dst_rect->x + 0.5f) < dst_scissors->x || i + (int)(dst_rect->x + 0.5f) >= dst_scissors->w)
  761. continue;
  762. if (j + (int)(dst_rect->y + 0.5f) < dst_scissors->y || j + (int)(dst_rect->y + 0.5f) >= dst_scissors->h)
  763. continue;
  764. }
  765. col = nk_sdlsurface_img_getpixel(src, (int)xoff, (int) yoff);
  766. if (col.r || col.g || col.b)
  767. {
  768. col.r = fg->r;
  769. col.g = fg->g;
  770. col.b = fg->b;
  771. }
  772. nk_sdlsurface_img_blendpixel(dst, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
  773. xoff += xinc;
  774. }
  775. xoff = src_rect->x;
  776. yoff += yinc;
  777. }
  778. }
  779. static void
  780. nk_sdlsurface_font_query_font_glyph(nk_handle handle, const float height,
  781. struct nk_user_font_glyph *glyph, const nk_rune codepoint,
  782. const nk_rune next_codepoint)
  783. {
  784. float scale;
  785. const struct nk_font_glyph *g;
  786. struct nk_font *font;
  787. NK_ASSERT(glyph);
  788. NK_UNUSED(next_codepoint);
  789. font = (struct nk_font*)handle.ptr;
  790. NK_ASSERT(font);
  791. NK_ASSERT(font->glyphs);
  792. if (!font || !glyph)
  793. return;
  794. scale = height/font->info.height;
  795. g = nk_font_find_glyph(font, codepoint);
  796. glyph->width = (g->x1 - g->x0) * scale;
  797. glyph->height = (g->y1 - g->y0) * scale;
  798. glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
  799. glyph->xadvance = (g->xadvance * scale);
  800. glyph->uv[0] = nk_vec2(g->u0, g->v0);
  801. glyph->uv[1] = nk_vec2(g->u1, g->v1);
  802. }
  803. NK_API void
  804. nk_sdlsurface_draw_text(const struct sdlsurface_context *sdlsurface,
  805. const struct nk_user_font *font, const struct nk_rect rect,
  806. const char *text, const int len, const float font_height,
  807. const struct nk_color fg)
  808. {
  809. float x = 0;
  810. int text_len = 0;
  811. nk_rune unicode = 0;
  812. nk_rune next = 0;
  813. int glyph_len = 0;
  814. int next_glyph_len = 0;
  815. struct nk_user_font_glyph g;
  816. if (!len || !text) return;
  817. x = 0;
  818. glyph_len = nk_utf_decode(text, &unicode, len);
  819. if (!glyph_len) return;
  820. /* draw every glyph image */
  821. while (text_len < len && glyph_len) {
  822. struct nk_rect src_rect;
  823. struct nk_rect dst_rect;
  824. float char_width = 0;
  825. if (unicode == NK_UTF_INVALID) break;
  826. /* query currently drawn glyph information */
  827. next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
  828. nk_sdlsurface_font_query_font_glyph(font->userdata, font_height, &g, unicode,
  829. (next == NK_UTF_INVALID) ? '\0' : next);
  830. /* calculate and draw glyph drawing rectangle and image */
  831. char_width = g.xadvance;
  832. src_rect.x = g.uv[0].x * sdlsurface->font_tex->w;
  833. src_rect.y = g.uv[0].y * sdlsurface->font_tex->h;
  834. src_rect.w = g.uv[1].x * sdlsurface->font_tex->w - g.uv[0].x * sdlsurface->font_tex->w;
  835. src_rect.h = g.uv[1].y * sdlsurface->font_tex->h - g.uv[0].y * sdlsurface->font_tex->h;
  836. dst_rect.x = x + g.offset.x + rect.x;
  837. dst_rect.y = g.offset.y + rect.y;
  838. dst_rect.w = ceil(g.width);
  839. dst_rect.h = ceil(g.height);
  840. /* Use software rescaling to blit glyph from font_text to framebuffer */
  841. nk_sdlsurface_stretch_image(sdlsurface->fb, sdlsurface->font_tex, &dst_rect, &src_rect, &sdlsurface->scissors, &fg);
  842. /* offset next glyph */
  843. text_len += glyph_len;
  844. x += char_width;
  845. glyph_len = next_glyph_len;
  846. unicode = next;
  847. }
  848. }
  849. NK_API void
  850. nk_sdlsurface_drawimage(const struct sdlsurface_context *sdlsurface,
  851. const int x, const int y, const int w, const int h,
  852. const struct nk_image *img, const struct nk_color *col)
  853. {
  854. struct nk_rect src_rect;
  855. struct nk_rect dst_rect;
  856. src_rect.x = img->region[0];
  857. src_rect.y = img->region[1];
  858. src_rect.w = img->region[2];
  859. src_rect.h = img->region[3];
  860. dst_rect.x = x;
  861. dst_rect.y = y;
  862. dst_rect.w = w;
  863. dst_rect.h = h;
  864. nk_sdlsurface_stretch_image(sdlsurface->fb, sdlsurface->font_tex, &dst_rect, &src_rect, &sdlsurface->scissors, col);
  865. }
  866. NK_API void
  867. nk_sdlsurface_shutdown(struct sdlsurface_context *sdlsurface)
  868. {
  869. if (sdlsurface) {
  870. SDL_FreeSurface(sdlsurface->font_tex);
  871. nk_free(&sdlsurface->ctx);
  872. memset(sdlsurface, 0, sizeof(struct sdlsurface_context));
  873. free(sdlsurface);
  874. }
  875. }
  876. NK_API void
  877. nk_sdlsurface_render(const struct sdlsurface_context *sdlsurface,
  878. const struct nk_color clear,
  879. const unsigned char enable_clear)
  880. {
  881. const struct nk_command *cmd;
  882. if (enable_clear)
  883. nk_sdlsurface_clear(sdlsurface, clear);
  884. nk_foreach(cmd, (struct nk_context*)&sdlsurface->ctx) {
  885. switch (cmd->type) {
  886. case NK_COMMAND_NOP: break;
  887. case NK_COMMAND_SCISSOR: {
  888. const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd;
  889. nk_sdlsurface_scissor((struct sdlsurface_context *)sdlsurface, s->x, s->y, s->w, s->h);
  890. } break;
  891. case NK_COMMAND_LINE: {
  892. const struct nk_command_line *l = (const struct nk_command_line *)cmd;
  893. nk_sdlsurface_stroke_line(sdlsurface, l->begin.x, l->begin.y, l->end.x,
  894. l->end.y, l->line_thickness, l->color);
  895. } break;
  896. case NK_COMMAND_RECT: {
  897. const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
  898. nk_sdlsurface_stroke_rect(sdlsurface, r->x, r->y, r->w, r->h,
  899. (unsigned short)r->rounding, r->line_thickness, r->color);
  900. } break;
  901. case NK_COMMAND_RECT_FILLED: {
  902. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
  903. nk_sdlsurface_fill_rect(sdlsurface, r->x, r->y, r->w, r->h,
  904. (unsigned short)r->rounding, r->color);
  905. } break;
  906. case NK_COMMAND_CIRCLE: {
  907. const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
  908. nk_sdlsurface_stroke_circle(sdlsurface, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
  909. } break;
  910. case NK_COMMAND_CIRCLE_FILLED: {
  911. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  912. nk_sdlsurface_fill_circle(sdlsurface, c->x, c->y, c->w, c->h, c->color);
  913. } break;
  914. case NK_COMMAND_TRIANGLE: {
  915. const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
  916. nk_sdlsurface_stroke_triangle(sdlsurface, t->a.x, t->a.y, t->b.x, t->b.y,
  917. t->c.x, t->c.y, t->line_thickness, t->color);
  918. } break;
  919. case NK_COMMAND_TRIANGLE_FILLED: {
  920. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
  921. nk_sdlsurface_fill_triangle(sdlsurface, t->a.x, t->a.y, t->b.x, t->b.y,
  922. t->c.x, t->c.y, t->color);
  923. } break;
  924. case NK_COMMAND_POLYGON: {
  925. const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
  926. nk_sdlsurface_stroke_polygon(sdlsurface, p->points, p->point_count, p->line_thickness,p->color);
  927. } break;
  928. case NK_COMMAND_POLYGON_FILLED: {
  929. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
  930. nk_sdlsurface_fill_polygon(sdlsurface, p->points, p->point_count, p->color);
  931. } break;
  932. case NK_COMMAND_POLYLINE: {
  933. const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
  934. nk_sdlsurface_stroke_polyline(sdlsurface, p->points, p->point_count, p->line_thickness, p->color);
  935. } break;
  936. case NK_COMMAND_TEXT: {
  937. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  938. nk_sdlsurface_draw_text(sdlsurface, t->font, nk_rect(t->x, t->y, t->w, t->h),
  939. t->string, t->length, t->height, t->foreground);
  940. } break;
  941. case NK_COMMAND_CURVE: {
  942. const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
  943. nk_sdlsurface_stroke_curve(sdlsurface, q->begin, q->ctrl[0], q->ctrl[1],
  944. q->end, 22, q->line_thickness, q->color);
  945. } break;
  946. case NK_COMMAND_RECT_MULTI_COLOR: {
  947. const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
  948. nk_sdlsurface_draw_rect_multi_color(sdlsurface, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
  949. } break;
  950. case NK_COMMAND_IMAGE: {
  951. const struct nk_command_image *q = (const struct nk_command_image *)cmd;
  952. nk_sdlsurface_drawimage(sdlsurface, q->x, q->y, q->w, q->h, &q->img, &q->col);
  953. } break;
  954. case NK_COMMAND_ARC: {
  955. assert(0 && "NK_COMMAND_ARC not implemented\n");
  956. } break;
  957. case NK_COMMAND_ARC_FILLED: {
  958. assert(0 && "NK_COMMAND_ARC_FILLED not implemented\n");
  959. } break;
  960. default: break;
  961. }
  962. }
  963. nk_clear((struct nk_context*)&sdlsurface->ctx);
  964. }
  965. #endif