nuklear_gdip.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*
  2. * Nuklear - 1.40.8 - public domain
  3. * no warrenty implied; use at your own risk.
  4. * authored from 2015-2017 by Micha Mettke
  5. */
  6. /*
  7. * ==============================================================
  8. *
  9. * API
  10. *
  11. * ===============================================================
  12. */
  13. #ifndef NK_GDIP_H_
  14. #define NK_GDIP_H_
  15. #define WIN32_LEAN_AND_MEAN
  16. #include <windows.h>
  17. #include <shlwapi.h>
  18. /* font */
  19. typedef struct GdipFont GdipFont;
  20. NK_API GdipFont* nk_gdipfont_create(const char *name, int size);
  21. NK_API GdipFont* nk_gdipfont_create_from_file(const WCHAR* filename, int size);
  22. NK_API GdipFont* nk_gdipfont_create_from_memory(const void* membuf, int membufSize, int size);
  23. NK_API void nk_gdipfont_del(GdipFont *font);
  24. NK_API struct nk_context* nk_gdip_init(HWND hwnd, unsigned int width, unsigned int height);
  25. NK_API void nk_gdip_set_font(GdipFont *font);
  26. NK_API int nk_gdip_handle_event(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
  27. NK_API void nk_gdip_render(enum nk_anti_aliasing AA, struct nk_color clear);
  28. NK_API void nk_gdip_shutdown(void);
  29. /* image */
  30. NK_API struct nk_image nk_gdip_load_image_from_file(const WCHAR* filename);
  31. NK_API struct nk_image nk_gdip_load_image_from_memory(const void* membuf, nk_uint membufSize);
  32. NK_API void nk_gdip_image_free(struct nk_image image);
  33. #endif
  34. /*
  35. * ==============================================================
  36. *
  37. * IMPLEMENTATION
  38. *
  39. * ===============================================================
  40. */
  41. #ifdef NK_GDIP_IMPLEMENTATION
  42. #include <stdlib.h>
  43. #include <malloc.h>
  44. #define _USE_MATH_DEFINES
  45. #include <math.h>
  46. /* manually declare everything GDI+ needs, because
  47. GDI+ headers are not usable from C */
  48. #define WINGDIPAPI __stdcall
  49. #define GDIPCONST const
  50. typedef struct GpGraphics GpGraphics;
  51. typedef struct GpImage GpImage;
  52. typedef struct GpPen GpPen;
  53. typedef struct GpBrush GpBrush;
  54. typedef struct GpStringFormat GpStringFormat;
  55. typedef struct GpFont GpFont;
  56. typedef struct GpFontFamily GpFontFamily;
  57. typedef struct GpFontCollection GpFontCollection;
  58. typedef GpImage GpBitmap;
  59. typedef GpBrush GpSolidFill;
  60. typedef int Status;
  61. typedef Status GpStatus;
  62. typedef float REAL;
  63. typedef DWORD ARGB;
  64. typedef POINT GpPoint;
  65. typedef enum {
  66. TextRenderingHintSystemDefault = 0,
  67. TextRenderingHintSingleBitPerPixelGridFit = 1,
  68. TextRenderingHintSingleBitPerPixel = 2,
  69. TextRenderingHintAntiAliasGridFit = 3,
  70. TextRenderingHintAntiAlias = 4,
  71. TextRenderingHintClearTypeGridFit = 5
  72. } TextRenderingHint;
  73. typedef enum {
  74. StringFormatFlagsDirectionRightToLeft = 0x00000001,
  75. StringFormatFlagsDirectionVertical = 0x00000002,
  76. StringFormatFlagsNoFitBlackBox = 0x00000004,
  77. StringFormatFlagsDisplayFormatControl = 0x00000020,
  78. StringFormatFlagsNoFontFallback = 0x00000400,
  79. StringFormatFlagsMeasureTrailingSpaces = 0x00000800,
  80. StringFormatFlagsNoWrap = 0x00001000,
  81. StringFormatFlagsLineLimit = 0x00002000,
  82. StringFormatFlagsNoClip = 0x00004000
  83. } StringFormatFlags;
  84. typedef enum
  85. {
  86. QualityModeInvalid = -1,
  87. QualityModeDefault = 0,
  88. QualityModeLow = 1,
  89. QualityModeHigh = 2
  90. } QualityMode;
  91. typedef enum
  92. {
  93. SmoothingModeInvalid = QualityModeInvalid,
  94. SmoothingModeDefault = QualityModeDefault,
  95. SmoothingModeHighSpeed = QualityModeLow,
  96. SmoothingModeHighQuality = QualityModeHigh,
  97. SmoothingModeNone,
  98. SmoothingModeAntiAlias,
  99. SmoothingModeAntiAlias8x4 = SmoothingModeAntiAlias,
  100. SmoothingModeAntiAlias8x8
  101. } SmoothingMode;
  102. typedef enum
  103. {
  104. FontStyleRegular = 0,
  105. FontStyleBold = 1,
  106. FontStyleItalic = 2,
  107. FontStyleBoldItalic = 3,
  108. FontStyleUnderline = 4,
  109. FontStyleStrikeout = 8
  110. } FontStyle;
  111. typedef enum {
  112. FillModeAlternate,
  113. FillModeWinding
  114. } FillMode;
  115. typedef enum {
  116. CombineModeReplace,
  117. CombineModeIntersect,
  118. CombineModeUnion,
  119. CombineModeXor,
  120. CombineModeExclude,
  121. CombineModeComplement
  122. } CombineMode;
  123. typedef enum {
  124. UnitWorld,
  125. UnitDisplay,
  126. UnitPixel,
  127. UnitPoint,
  128. UnitInch,
  129. UnitDocument,
  130. UnitMillimeter
  131. } Unit;
  132. typedef struct {
  133. FLOAT X;
  134. FLOAT Y;
  135. FLOAT Width;
  136. FLOAT Height;
  137. } RectF;
  138. typedef enum {
  139. DebugEventLevelFatal,
  140. DebugEventLevelWarning
  141. } DebugEventLevel;
  142. typedef VOID (WINAPI *DebugEventProc)(DebugEventLevel level, CHAR *message);
  143. typedef struct {
  144. UINT32 GdiplusVersion;
  145. DebugEventProc DebugEventCallback;
  146. BOOL SuppressBackgroundThread;
  147. BOOL SuppressExternalCodecs;
  148. } GdiplusStartupInput;
  149. typedef Status (WINAPI *NotificationHookProc)(OUT ULONG_PTR *token);
  150. typedef VOID (WINAPI *NotificationUnhookProc)(ULONG_PTR token);
  151. typedef struct {
  152. NotificationHookProc NotificationHook;
  153. NotificationUnhookProc NotificationUnhook;
  154. } GdiplusStartupOutput;
  155. /* startup & shutdown */
  156. Status WINAPI GdiplusStartup(
  157. OUT ULONG_PTR *token,
  158. const GdiplusStartupInput *input,
  159. OUT GdiplusStartupOutput *output);
  160. VOID WINAPI GdiplusShutdown(ULONG_PTR token);
  161. /* image */
  162. GpStatus WINGDIPAPI
  163. GdipCreateBitmapFromGraphics(INT width,
  164. INT height,
  165. GpGraphics* target,
  166. GpBitmap** bitmap);
  167. GpStatus WINGDIPAPI
  168. GdipDisposeImage(GpImage *image);
  169. GpStatus WINGDIPAPI
  170. GdipGetImageGraphicsContext(GpImage *image, GpGraphics **graphics);
  171. GpStatus WINGDIPAPI
  172. GdipGetImageWidth(GpImage *image, UINT *width);
  173. GpStatus WINGDIPAPI
  174. GdipGetImageHeight(GpImage *image, UINT *height);
  175. GpStatus WINGDIPAPI
  176. GdipLoadImageFromFile(GDIPCONST WCHAR* filename, GpImage **image);
  177. GpStatus WINGDIPAPI
  178. GdipLoadImageFromStream(IStream* stream, GpImage **image);
  179. /* pen */
  180. GpStatus WINGDIPAPI
  181. GdipCreatePen1(ARGB color, REAL width, Unit unit, GpPen **pen);
  182. GpStatus WINGDIPAPI
  183. GdipDeletePen(GpPen *pen);
  184. GpStatus WINGDIPAPI
  185. GdipSetPenWidth(GpPen *pen, REAL width);
  186. GpStatus WINGDIPAPI
  187. GdipSetPenColor(GpPen *pen, ARGB argb);
  188. /* brush */
  189. GpStatus WINGDIPAPI
  190. GdipCreateSolidFill(ARGB color, GpSolidFill **brush);
  191. GpStatus WINGDIPAPI
  192. GdipDeleteBrush(GpBrush *brush);
  193. GpStatus WINGDIPAPI
  194. GdipSetSolidFillColor(GpSolidFill *brush, ARGB color);
  195. /* font */
  196. GpStatus WINGDIPAPI
  197. GdipCreateFont(
  198. GDIPCONST GpFontFamily *fontFamily,
  199. REAL emSize,
  200. INT style,
  201. Unit unit,
  202. GpFont **font
  203. );
  204. GpStatus WINGDIPAPI
  205. GdipDeleteFont(GpFont* font);
  206. GpStatus WINGDIPAPI
  207. GdipGetFontSize(GpFont *font, REAL *size);
  208. GpStatus WINGDIPAPI
  209. GdipCreateFontFamilyFromName(GDIPCONST WCHAR *name,
  210. GpFontCollection *fontCollection,
  211. GpFontFamily **fontFamily);
  212. GpStatus WINGDIPAPI
  213. GdipDeleteFontFamily(GpFontFamily *fontFamily);
  214. GpStatus WINGDIPAPI
  215. GdipStringFormatGetGenericTypographic(GpStringFormat **format);
  216. GpStatus WINGDIPAPI
  217. GdipSetStringFormatFlags(GpStringFormat *format, INT flags);
  218. GpStatus WINGDIPAPI
  219. GdipDeleteStringFormat(GpStringFormat *format);
  220. GpStatus WINGDIPAPI
  221. GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
  222. GDIPCONST void* memory, INT length);
  223. GpStatus WINGDIPAPI
  224. GdipPrivateAddFontFile(GpFontCollection* fontCollection,
  225. GDIPCONST WCHAR* filename);
  226. GpStatus WINGDIPAPI
  227. GdipNewPrivateFontCollection(GpFontCollection** fontCollection);
  228. GpStatus WINGDIPAPI
  229. GdipDeletePrivateFontCollection(GpFontCollection** fontCollection);
  230. GpStatus WINGDIPAPI
  231. GdipGetFontCollectionFamilyList(GpFontCollection* fontCollection,
  232. INT numSought, GpFontFamily* gpfamilies[], INT* numFound);
  233. GpStatus WINGDIPAPI
  234. GdipGetFontCollectionFamilyCount(GpFontCollection* fontCollection, INT* numFound);
  235. /* graphics */
  236. GpStatus WINGDIPAPI
  237. GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics);
  238. GpStatus WINGDIPAPI
  239. GdipCreateFromHDC(HDC hdc, GpGraphics **graphics);
  240. GpStatus WINGDIPAPI
  241. GdipDeleteGraphics(GpGraphics *graphics);
  242. GpStatus WINGDIPAPI
  243. GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode smoothingMode);
  244. GpStatus WINGDIPAPI
  245. GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
  246. INT width, INT height, CombineMode combineMode);
  247. GpStatus WINGDIPAPI
  248. GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1, INT y1,
  249. INT x2, INT y2);
  250. GpStatus WINGDIPAPI
  251. GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x, INT y,
  252. INT width, INT height, REAL startAngle, REAL sweepAngle);
  253. GpStatus WINGDIPAPI
  254. GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x, INT y,
  255. INT width, INT height, REAL startAngle, REAL sweepAngle);
  256. GpStatus WINGDIPAPI
  257. GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x, INT y,
  258. INT width, INT height, REAL startAngle, REAL sweepAngle);
  259. GpStatus WINGDIPAPI
  260. GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x, INT y,
  261. INT width, INT height);
  262. GpStatus WINGDIPAPI
  263. GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush, INT x, INT y,
  264. INT width, INT height);
  265. GpStatus WINGDIPAPI
  266. GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
  267. GDIPCONST GpPoint *points, INT count, FillMode fillMode);
  268. GpStatus WINGDIPAPI
  269. GdipDrawPolygonI(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPoint *points,
  270. INT count);
  271. GpStatus WINGDIPAPI
  272. GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x, INT y,
  273. INT width, INT height);
  274. GpStatus WINGDIPAPI
  275. GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x, INT y,
  276. INT width, INT height);
  277. GpStatus WINGDIPAPI
  278. GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1, INT y1,
  279. INT x2, INT y2, INT x3, INT y3, INT x4, INT y4);
  280. GpStatus WINGDIPAPI
  281. GdipDrawString(
  282. GpGraphics *graphics,
  283. GDIPCONST WCHAR *string,
  284. INT length,
  285. GDIPCONST GpFont *font,
  286. GDIPCONST RectF *layoutRect,
  287. GDIPCONST GpStringFormat *stringFormat,
  288. GDIPCONST GpBrush *brush
  289. );
  290. GpStatus WINGDIPAPI
  291. GdipGraphicsClear(GpGraphics *graphics, ARGB color);
  292. GpStatus WINGDIPAPI
  293. GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x, INT y);
  294. GpStatus WINGDIPAPI
  295. GdipDrawImageRectI(GpGraphics *graphics, GpImage *image, INT x, INT y,
  296. INT width, INT height);
  297. GpStatus WINGDIPAPI
  298. GdipMeasureString(
  299. GpGraphics *graphics,
  300. GDIPCONST WCHAR *string,
  301. INT length,
  302. GDIPCONST GpFont *font,
  303. GDIPCONST RectF *layoutRect,
  304. GDIPCONST GpStringFormat *stringFormat,
  305. RectF *boundingBox,
  306. INT *codepointsFitted,
  307. INT *linesFilled
  308. );
  309. GpStatus WINGDIPAPI
  310. GdipSetTextRenderingHint(GpGraphics *graphics, TextRenderingHint mode);
  311. LWSTDAPI_(IStream *) SHCreateMemStream(const BYTE *pInit, _In_ UINT cbInit);
  312. struct GdipFont
  313. {
  314. struct nk_user_font nk;
  315. GpFont* handle;
  316. };
  317. static struct {
  318. ULONG_PTR token;
  319. GpGraphics *window;
  320. GpGraphics *memory;
  321. GpImage *bitmap;
  322. GpPen *pen;
  323. GpSolidFill *brush;
  324. GpStringFormat *format;
  325. GpFontCollection *fontCollection[10];
  326. INT curFontCollection;
  327. struct nk_context ctx;
  328. } gdip;
  329. static ARGB convert_color(struct nk_color c)
  330. {
  331. return (c.a << 24) | (c.r << 16) | (c.g << 8) | c.b;
  332. }
  333. static void
  334. nk_gdip_scissor(float x, float y, float w, float h)
  335. {
  336. GdipSetClipRectI(gdip.memory, (INT)x, (INT)y, (INT)(w + 1), (INT)(h + 1), CombineModeReplace);
  337. }
  338. static void
  339. nk_gdip_stroke_line(short x0, short y0, short x1,
  340. short y1, unsigned int line_thickness, struct nk_color col)
  341. {
  342. GdipSetPenWidth(gdip.pen, (REAL)line_thickness);
  343. GdipSetPenColor(gdip.pen, convert_color(col));
  344. GdipDrawLineI(gdip.memory, gdip.pen, x0, y0, x1, y1);
  345. }
  346. static void
  347. nk_gdip_stroke_rect(short x, short y, unsigned short w,
  348. unsigned short h, unsigned short r, unsigned short line_thickness, struct nk_color col)
  349. {
  350. GdipSetPenWidth(gdip.pen, (REAL)line_thickness);
  351. GdipSetPenColor(gdip.pen, convert_color(col));
  352. if (r == 0) {
  353. GdipDrawRectangleI(gdip.memory, gdip.pen, x, y, w, h);
  354. } else {
  355. INT d = 2 * r;
  356. GdipDrawArcI(gdip.memory, gdip.pen, x, y, d, d, 180, 90);
  357. GdipDrawLineI(gdip.memory, gdip.pen, x + r, y, x + w - r, y);
  358. GdipDrawArcI(gdip.memory, gdip.pen, x + w - d, y, d, d, 270, 90);
  359. GdipDrawLineI(gdip.memory, gdip.pen, x + w, y + r, x + w, y + h - r);
  360. GdipDrawArcI(gdip.memory, gdip.pen, x + w - d, y + h - d, d, d, 0, 90);
  361. GdipDrawLineI(gdip.memory, gdip.pen, x, y + r, x, y + h - r);
  362. GdipDrawArcI(gdip.memory, gdip.pen, x, y + h - d, d, d, 90, 90);
  363. GdipDrawLineI(gdip.memory, gdip.pen, x + r, y + h, x + w - r, y + h);
  364. }
  365. }
  366. static void
  367. nk_gdip_fill_rect(short x, short y, unsigned short w,
  368. unsigned short h, unsigned short r, struct nk_color col)
  369. {
  370. GdipSetSolidFillColor(gdip.brush, convert_color(col));
  371. if (r == 0) {
  372. GdipFillRectangleI(gdip.memory, gdip.brush, x, y, w, h);
  373. } else {
  374. INT d = 2 * r;
  375. GdipFillRectangleI(gdip.memory, gdip.brush, x + r, y, w - d, h);
  376. GdipFillRectangleI(gdip.memory, gdip.brush, x, y + r, r, h - d);
  377. GdipFillRectangleI(gdip.memory, gdip.brush, x + w - r, y + r, r, h - d);
  378. GdipFillPieI(gdip.memory, gdip.brush, x, y, d, d, 180, 90);
  379. GdipFillPieI(gdip.memory, gdip.brush, x + w - d, y, d, d, 270, 90);
  380. GdipFillPieI(gdip.memory, gdip.brush, x + w - d, y + h - d, d, d, 0, 90);
  381. GdipFillPieI(gdip.memory, gdip.brush, x, y + h - d, d, d, 90, 90);
  382. }
  383. }
  384. static BOOL
  385. SetPoint(POINT *p, LONG x, LONG y)
  386. {
  387. if (!p)
  388. return FALSE;
  389. p->x = x;
  390. p->y = y;
  391. return TRUE;
  392. }
  393. static void
  394. nk_gdip_fill_triangle(short x0, short y0, short x1,
  395. short y1, short x2, short y2, struct nk_color col)
  396. {
  397. POINT points[3];
  398. SetPoint(&points[0], x0, y0);
  399. SetPoint(&points[1], x1, y1);
  400. SetPoint(&points[2], x2, y2);
  401. GdipSetSolidFillColor(gdip.brush, convert_color(col));
  402. GdipFillPolygonI(gdip.memory, gdip.brush, points, 3, FillModeAlternate);
  403. }
  404. static void
  405. nk_gdip_stroke_triangle(short x0, short y0, short x1,
  406. short y1, short x2, short y2, unsigned short line_thickness, struct nk_color col)
  407. {
  408. POINT points[4];
  409. SetPoint(&points[0], x0, y0);
  410. SetPoint(&points[1], x1, y1);
  411. SetPoint(&points[2], x2, y2);
  412. SetPoint(&points[3], x0, y0);
  413. GdipSetPenWidth(gdip.pen, (REAL)line_thickness);
  414. GdipSetPenColor(gdip.pen, convert_color(col));
  415. GdipDrawPolygonI(gdip.memory, gdip.pen, points, 4);
  416. }
  417. static void
  418. nk_gdip_fill_polygon(const struct nk_vec2i *pnts, int count, struct nk_color col)
  419. {
  420. int i = 0;
  421. #define MAX_POINTS 64
  422. POINT points[MAX_POINTS];
  423. GdipSetSolidFillColor(gdip.brush, convert_color(col));
  424. for (i = 0; i < count && i < MAX_POINTS; ++i) {
  425. points[i].x = pnts[i].x;
  426. points[i].y = pnts[i].y;
  427. }
  428. GdipFillPolygonI(gdip.memory, gdip.brush, points, i, FillModeAlternate);
  429. #undef MAX_POINTS
  430. }
  431. static void
  432. nk_gdip_stroke_polygon(const struct nk_vec2i *pnts, int count,
  433. unsigned short line_thickness, struct nk_color col)
  434. {
  435. GdipSetPenWidth(gdip.pen, (REAL)line_thickness);
  436. GdipSetPenColor(gdip.pen, convert_color(col));
  437. if (count > 0) {
  438. int i;
  439. for (i = 1; i < count; ++i)
  440. GdipDrawLineI(gdip.memory, gdip.pen, pnts[i-1].x, pnts[i-1].y, pnts[i].x, pnts[i].y);
  441. GdipDrawLineI(gdip.memory, gdip.pen, pnts[count-1].x, pnts[count-1].y, pnts[0].x, pnts[0].y);
  442. }
  443. }
  444. static void
  445. nk_gdip_stroke_polyline(const struct nk_vec2i *pnts,
  446. int count, unsigned short line_thickness, struct nk_color col)
  447. {
  448. GdipSetPenWidth(gdip.pen, (REAL)line_thickness);
  449. GdipSetPenColor(gdip.pen, convert_color(col));
  450. if (count > 0) {
  451. int i;
  452. for (i = 1; i < count; ++i)
  453. GdipDrawLineI(gdip.memory, gdip.pen, pnts[i-1].x, pnts[i-1].y, pnts[i].x, pnts[i].y);
  454. }
  455. }
  456. static void
  457. nk_gdip_fill_circle(short x, short y, unsigned short w,
  458. unsigned short h, struct nk_color col)
  459. {
  460. GdipSetSolidFillColor(gdip.brush, convert_color(col));
  461. GdipFillEllipseI(gdip.memory, gdip.brush, x, y, w, h);
  462. }
  463. static void
  464. nk_gdip_stroke_circle(short x, short y, unsigned short w,
  465. unsigned short h, unsigned short line_thickness, struct nk_color col)
  466. {
  467. GdipSetPenWidth(gdip.pen, (REAL)line_thickness);
  468. GdipSetPenColor(gdip.pen, convert_color(col));
  469. GdipDrawEllipseI(gdip.memory, gdip.pen, x, y, w, h);
  470. }
  471. static void
  472. nk_gdip_stroke_curve(struct nk_vec2i p1,
  473. struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4,
  474. unsigned short line_thickness, struct nk_color col)
  475. {
  476. GdipSetPenWidth(gdip.pen, (REAL)line_thickness);
  477. GdipSetPenColor(gdip.pen, convert_color(col));
  478. GdipDrawBezierI(gdip.memory, gdip.pen, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y);
  479. }
  480. static void
  481. nk_gdip_fill_arc(short cx, short cy, unsigned short r, float amin, float adelta, struct nk_color col)
  482. {
  483. GdipSetSolidFillColor(gdip.brush, convert_color(col));
  484. GdipFillPieI(gdip.memory, gdip.brush, cx - r, cy - r, r * 2, r * 2, amin * (180/M_PI), adelta * (180/M_PI));
  485. }
  486. static void
  487. nk_gdip_stroke_arc(short cx, short cy, unsigned short r, float amin, float adelta, unsigned short line_thickness, struct nk_color col)
  488. {
  489. GdipSetPenWidth(gdip.pen, (REAL)line_thickness);
  490. GdipSetPenColor(gdip.pen, convert_color(col));
  491. GdipDrawPieI(gdip.memory, gdip.pen, cx - r, cy - r, r * 2, r * 2, amin * (180/M_PI), adelta * (180/M_PI));
  492. }
  493. static void
  494. nk_gdip_draw_text(short x, short y, unsigned short w, unsigned short h,
  495. const char *text, int len, GdipFont *font, struct nk_color cbg, struct nk_color cfg)
  496. {
  497. int wsize;
  498. WCHAR* wstr;
  499. RectF layout;
  500. layout.X = x;
  501. layout.Y = y;
  502. layout.Width = w;
  503. layout.Height = h;
  504. if(!text || !font || !len) return;
  505. wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
  506. wstr = (WCHAR*)_alloca(wsize * sizeof(wchar_t));
  507. MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
  508. GdipSetSolidFillColor(gdip.brush, convert_color(cfg));
  509. GdipDrawString(gdip.memory, wstr, wsize, font->handle, &layout, gdip.format, gdip.brush);
  510. }
  511. static void
  512. nk_gdip_draw_image(short x, short y, unsigned short w, unsigned short h,
  513. struct nk_image img, struct nk_color col)
  514. {
  515. GpImage *image = img.handle.ptr;
  516. GdipDrawImageRectI(gdip.memory, image, x, y, w, h);
  517. }
  518. static void
  519. nk_gdip_clear(struct nk_color col)
  520. {
  521. GdipGraphicsClear(gdip.memory, convert_color(col));
  522. }
  523. static void
  524. nk_gdip_blit(GpGraphics *graphics)
  525. {
  526. GdipDrawImageI(graphics, gdip.bitmap, 0, 0);
  527. }
  528. static struct nk_image
  529. nk_gdip_image_to_nk(GpImage *image) {
  530. struct nk_image img;
  531. UINT uwidth, uheight;
  532. img = nk_image_ptr( (void*)image );
  533. GdipGetImageHeight(image, &uheight);
  534. GdipGetImageWidth(image, &uwidth);
  535. img.h = uheight;
  536. img.w = uwidth;
  537. return img;
  538. }
  539. struct nk_image
  540. nk_gdip_load_image_from_file(const WCHAR *filename)
  541. {
  542. GpImage *image;
  543. if (GdipLoadImageFromFile(filename, &image))
  544. return nk_image_id(0);
  545. return nk_gdip_image_to_nk(image);
  546. }
  547. struct nk_image
  548. nk_gdip_load_image_from_memory(const void *membuf, nk_uint membufSize)
  549. {
  550. GpImage* image;
  551. GpStatus status;
  552. IStream *stream = SHCreateMemStream((const BYTE*)membuf, membufSize);
  553. if (!stream)
  554. return nk_image_id(0);
  555. status = GdipLoadImageFromStream(stream, &image);
  556. stream->lpVtbl->Release(stream);
  557. if (status)
  558. return nk_image_id(0);
  559. return nk_gdip_image_to_nk(image);
  560. }
  561. void
  562. nk_gdip_image_free(struct nk_image image)
  563. {
  564. if (!image.handle.ptr)
  565. return;
  566. GdipDisposeImage(image.handle.ptr);
  567. }
  568. GdipFont*
  569. nk_gdipfont_create(const char *name, int size)
  570. {
  571. GdipFont *font = (GdipFont*)calloc(1, sizeof(GdipFont));
  572. GpFontFamily *family;
  573. int wsize = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
  574. WCHAR* wname = (WCHAR*)_alloca((wsize + 1) * sizeof(wchar_t));
  575. MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, wsize);
  576. wname[wsize] = 0;
  577. GdipCreateFontFamilyFromName(wname, NULL, &family);
  578. GdipCreateFont(family, (REAL)size, FontStyleRegular, UnitPixel, &font->handle);
  579. GdipDeleteFontFamily(family);
  580. return font;
  581. }
  582. GpFontCollection*
  583. nk_gdip_getCurFontCollection(){
  584. return gdip.fontCollection[gdip.curFontCollection];
  585. }
  586. GdipFont*
  587. nk_gdipfont_create_from_collection(int size){
  588. GpFontFamily **families;
  589. INT count;
  590. GdipFont *font = (GdipFont*)calloc(1, sizeof(GdipFont));
  591. if( GdipGetFontCollectionFamilyCount(nk_gdip_getCurFontCollection(), &count) ) return NULL;
  592. families = (GpFontFamily**)calloc(1, sizeof(GpFontFamily*));
  593. if( !families ) return NULL;
  594. if( GdipGetFontCollectionFamilyList(nk_gdip_getCurFontCollection(), count, families, &count) ) return NULL;
  595. if( count < 1 ) return NULL;
  596. if( GdipCreateFont(families[count-1], (REAL)size, FontStyleRegular, UnitPixel, &font->handle) ) return NULL;
  597. free(families);
  598. gdip.curFontCollection++;
  599. return font;
  600. }
  601. GdipFont*
  602. nk_gdipfont_create_from_memory(const void* membuf, int membufSize, int size)
  603. {
  604. if( !nk_gdip_getCurFontCollection() )
  605. if( GdipNewPrivateFontCollection(&gdip.fontCollection[gdip.curFontCollection]) ) return NULL;
  606. if( GdipPrivateAddMemoryFont(nk_gdip_getCurFontCollection(), membuf, membufSize) ) return NULL;
  607. return nk_gdipfont_create_from_collection(size);
  608. }
  609. GdipFont*
  610. nk_gdipfont_create_from_file(const WCHAR* filename, int size)
  611. {
  612. if( !nk_gdip_getCurFontCollection() )
  613. if( GdipNewPrivateFontCollection(&gdip.fontCollection[gdip.curFontCollection]) ) return NULL;
  614. if( GdipPrivateAddFontFile(nk_gdip_getCurFontCollection(), filename) ) return NULL;
  615. return nk_gdipfont_create_from_collection(size);
  616. }
  617. static float
  618. nk_gdipfont_get_text_width(nk_handle handle, float height, const char *text, int len)
  619. {
  620. GdipFont *font = (GdipFont *)handle.ptr;
  621. RectF layout = { 0.0f, 0.0f, 65536.0f, 65536.0f };
  622. RectF bbox;
  623. int wsize;
  624. WCHAR* wstr;
  625. if (!font || !text)
  626. return 0;
  627. (void)height;
  628. wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
  629. wstr = (WCHAR*)_malloca(wsize * sizeof(wchar_t));
  630. MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
  631. GdipMeasureString(gdip.memory, wstr, wsize, font->handle, &layout, gdip.format, &bbox, NULL, NULL);
  632. return bbox.Width;
  633. }
  634. void
  635. nk_gdipfont_del(GdipFont *font)
  636. {
  637. if(!font) return;
  638. GdipDeleteFont(font->handle);
  639. free(font);
  640. }
  641. static void
  642. nk_gdip_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
  643. {
  644. (void)usr;
  645. if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
  646. {
  647. HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
  648. if (mem)
  649. {
  650. SIZE_T size = GlobalSize(mem) - 1;
  651. if (size)
  652. {
  653. LPCWSTR wstr = (LPCWSTR)GlobalLock(mem);
  654. if (wstr)
  655. {
  656. int utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), NULL, 0, NULL, NULL);
  657. if (utf8size)
  658. {
  659. char* utf8 = (char*)malloc(utf8size);
  660. if (utf8)
  661. {
  662. WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), utf8, utf8size, NULL, NULL);
  663. nk_textedit_paste(edit, utf8, utf8size);
  664. free(utf8);
  665. }
  666. }
  667. GlobalUnlock(mem);
  668. }
  669. }
  670. }
  671. CloseClipboard();
  672. }
  673. }
  674. static void
  675. nk_gdip_clipboard_copy(nk_handle usr, const char* text, int len)
  676. {
  677. if (OpenClipboard(NULL))
  678. {
  679. int wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
  680. if (wsize)
  681. {
  682. HGLOBAL mem = (HGLOBAL)GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
  683. if (mem)
  684. {
  685. wchar_t* wstr = (wchar_t*)GlobalLock(mem);
  686. if (wstr)
  687. {
  688. MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
  689. wstr[wsize] = 0;
  690. GlobalUnlock(mem);
  691. SetClipboardData(CF_UNICODETEXT, mem);
  692. }
  693. }
  694. }
  695. CloseClipboard();
  696. }
  697. }
  698. NK_API struct nk_context*
  699. nk_gdip_init(HWND hwnd, unsigned int width, unsigned int height)
  700. {
  701. int i;
  702. GdiplusStartupInput startup = { 1, NULL, FALSE, TRUE };
  703. GdiplusStartup(&gdip.token, &startup, NULL);
  704. GdipCreateFromHWND(hwnd, &gdip.window);
  705. GdipCreateBitmapFromGraphics(width, height, gdip.window, &gdip.bitmap);
  706. GdipGetImageGraphicsContext(gdip.bitmap, &gdip.memory);
  707. GdipCreatePen1(0, 1.0f, UnitPixel, &gdip.pen);
  708. GdipCreateSolidFill(0, &gdip.brush);
  709. GdipStringFormatGetGenericTypographic(&gdip.format);
  710. GdipSetStringFormatFlags(gdip.format, StringFormatFlagsNoFitBlackBox |
  711. StringFormatFlagsMeasureTrailingSpaces | StringFormatFlagsNoWrap |
  712. StringFormatFlagsNoClip);
  713. for(i=0; i< sizeof(gdip.fontCollection)/sizeof(gdip.fontCollection[0]); i++)
  714. gdip.fontCollection[i] = NULL;
  715. nk_init_default(&gdip.ctx, NULL);
  716. gdip.ctx.clip.copy = nk_gdip_clipboard_copy;
  717. gdip.ctx.clip.paste = nk_gdip_clipboard_paste;
  718. gdip.curFontCollection = 0;
  719. return &gdip.ctx;
  720. }
  721. NK_API void
  722. nk_gdip_set_font(GdipFont *gdipfont)
  723. {
  724. struct nk_user_font *font = &gdipfont->nk;
  725. font->userdata = nk_handle_ptr(gdipfont);
  726. GdipGetFontSize(gdipfont->handle, &font->height);
  727. font->width = nk_gdipfont_get_text_width;
  728. nk_style_set_font(&gdip.ctx, font);
  729. }
  730. NK_API int
  731. nk_gdip_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
  732. {
  733. switch (msg)
  734. {
  735. case WM_SIZE:
  736. if (gdip.window)
  737. {
  738. unsigned int width = LOWORD(lparam);
  739. unsigned int height = HIWORD(lparam);
  740. GdipDeleteGraphics(gdip.window);
  741. GdipDeleteGraphics(gdip.memory);
  742. GdipDisposeImage(gdip.bitmap);
  743. GdipCreateFromHWND(wnd, &gdip.window);
  744. GdipCreateBitmapFromGraphics(width, height, gdip.window, &gdip.bitmap);
  745. GdipGetImageGraphicsContext(gdip.bitmap, &gdip.memory);
  746. }
  747. break;
  748. case WM_PAINT:
  749. {
  750. PAINTSTRUCT paint;
  751. HDC dc = BeginPaint(wnd, &paint);
  752. GpGraphics *graphics;
  753. GdipCreateFromHDC(dc, &graphics);
  754. nk_gdip_blit(graphics);
  755. GdipDeleteGraphics(graphics);
  756. EndPaint(wnd, &paint);
  757. return 1;
  758. }
  759. case WM_KEYDOWN:
  760. case WM_KEYUP:
  761. case WM_SYSKEYDOWN:
  762. case WM_SYSKEYUP:
  763. {
  764. int down = !((lparam >> 31) & 1);
  765. int ctrl = GetKeyState(VK_CONTROL) & (1 << 15);
  766. switch (wparam)
  767. {
  768. case VK_SHIFT:
  769. case VK_LSHIFT:
  770. case VK_RSHIFT:
  771. nk_input_key(&gdip.ctx, NK_KEY_SHIFT, down);
  772. return 1;
  773. case VK_DELETE:
  774. nk_input_key(&gdip.ctx, NK_KEY_DEL, down);
  775. return 1;
  776. case VK_RETURN:
  777. case VK_SEPARATOR:
  778. nk_input_key(&gdip.ctx, NK_KEY_ENTER, down);
  779. return 1;
  780. case VK_TAB:
  781. nk_input_key(&gdip.ctx, NK_KEY_TAB, down);
  782. return 1;
  783. case VK_LEFT:
  784. if (ctrl)
  785. nk_input_key(&gdip.ctx, NK_KEY_TEXT_WORD_LEFT, down);
  786. else
  787. nk_input_key(&gdip.ctx, NK_KEY_LEFT, down);
  788. return 1;
  789. case VK_RIGHT:
  790. if (ctrl)
  791. nk_input_key(&gdip.ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  792. else
  793. nk_input_key(&gdip.ctx, NK_KEY_RIGHT, down);
  794. return 1;
  795. case VK_BACK:
  796. nk_input_key(&gdip.ctx, NK_KEY_BACKSPACE, down);
  797. return 1;
  798. case VK_HOME:
  799. nk_input_key(&gdip.ctx, NK_KEY_TEXT_START, down);
  800. nk_input_key(&gdip.ctx, NK_KEY_SCROLL_START, down);
  801. return 1;
  802. case VK_END:
  803. nk_input_key(&gdip.ctx, NK_KEY_TEXT_END, down);
  804. nk_input_key(&gdip.ctx, NK_KEY_SCROLL_END, down);
  805. return 1;
  806. case VK_NEXT:
  807. nk_input_key(&gdip.ctx, NK_KEY_SCROLL_DOWN, down);
  808. return 1;
  809. case VK_PRIOR:
  810. nk_input_key(&gdip.ctx, NK_KEY_SCROLL_UP, down);
  811. return 1;
  812. case 'A':
  813. if (ctrl) {
  814. nk_input_key(&gdip.ctx, NK_KEY_TEXT_SELECT_ALL, down);
  815. return 1;
  816. }
  817. break;
  818. case 'C':
  819. if (ctrl) {
  820. nk_input_key(&gdip.ctx, NK_KEY_COPY, down);
  821. return 1;
  822. }
  823. break;
  824. case 'V':
  825. if (ctrl) {
  826. nk_input_key(&gdip.ctx, NK_KEY_PASTE, down);
  827. return 1;
  828. }
  829. break;
  830. case 'X':
  831. if (ctrl) {
  832. nk_input_key(&gdip.ctx, NK_KEY_CUT, down);
  833. return 1;
  834. }
  835. break;
  836. case 'Z':
  837. if (ctrl) {
  838. nk_input_key(&gdip.ctx, NK_KEY_TEXT_UNDO, down);
  839. return 1;
  840. }
  841. break;
  842. case 'R':
  843. if (ctrl) {
  844. nk_input_key(&gdip.ctx, NK_KEY_TEXT_REDO, down);
  845. return 1;
  846. }
  847. break;
  848. }
  849. return 0;
  850. }
  851. case WM_CHAR:
  852. if (wparam >= 32)
  853. {
  854. nk_input_unicode(&gdip.ctx, (nk_rune)wparam);
  855. return 1;
  856. }
  857. break;
  858. case WM_LBUTTONDOWN:
  859. nk_input_button(&gdip.ctx, NK_BUTTON_LEFT, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
  860. SetCapture(wnd);
  861. return 1;
  862. case WM_LBUTTONUP:
  863. nk_input_button(&gdip.ctx, NK_BUTTON_DOUBLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
  864. nk_input_button(&gdip.ctx, NK_BUTTON_LEFT, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
  865. ReleaseCapture();
  866. return 1;
  867. case WM_RBUTTONDOWN:
  868. nk_input_button(&gdip.ctx, NK_BUTTON_RIGHT, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
  869. SetCapture(wnd);
  870. return 1;
  871. case WM_RBUTTONUP:
  872. nk_input_button(&gdip.ctx, NK_BUTTON_RIGHT, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
  873. ReleaseCapture();
  874. return 1;
  875. case WM_MBUTTONDOWN:
  876. nk_input_button(&gdip.ctx, NK_BUTTON_MIDDLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
  877. SetCapture(wnd);
  878. return 1;
  879. case WM_MBUTTONUP:
  880. nk_input_button(&gdip.ctx, NK_BUTTON_MIDDLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
  881. ReleaseCapture();
  882. return 1;
  883. case WM_MOUSEWHEEL:
  884. nk_input_scroll(&gdip.ctx, nk_vec2(0,(float)(short)HIWORD(wparam) / WHEEL_DELTA));
  885. return 1;
  886. case WM_MOUSEMOVE:
  887. nk_input_motion(&gdip.ctx, (short)LOWORD(lparam), (short)HIWORD(lparam));
  888. return 1;
  889. case WM_LBUTTONDBLCLK:
  890. nk_input_button(&gdip.ctx, NK_BUTTON_DOUBLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
  891. return 1;
  892. }
  893. return 0;
  894. }
  895. NK_API void
  896. nk_gdip_shutdown(void)
  897. {
  898. int i;
  899. for(i=0; i< gdip.curFontCollection; i++)
  900. GdipDeletePrivateFontCollection( &gdip.fontCollection[i] );
  901. GdipDeleteGraphics(gdip.window);
  902. GdipDeleteGraphics(gdip.memory);
  903. GdipDisposeImage(gdip.bitmap);
  904. GdipDeletePen(gdip.pen);
  905. GdipDeleteBrush(gdip.brush);
  906. GdipDeleteStringFormat(gdip.format);
  907. GdiplusShutdown(gdip.token);
  908. nk_free(&gdip.ctx);
  909. }
  910. NK_API void
  911. nk_gdip_prerender_gui(enum nk_anti_aliasing AA)
  912. {
  913. const struct nk_command *cmd;
  914. GdipSetTextRenderingHint(gdip.memory, AA != NK_ANTI_ALIASING_OFF ?
  915. TextRenderingHintClearTypeGridFit : TextRenderingHintSingleBitPerPixelGridFit);
  916. GdipSetSmoothingMode(gdip.memory, AA != NK_ANTI_ALIASING_OFF ?
  917. SmoothingModeHighQuality : SmoothingModeNone);
  918. nk_foreach(cmd, &gdip.ctx)
  919. {
  920. switch (cmd->type) {
  921. case NK_COMMAND_NOP: break;
  922. case NK_COMMAND_SCISSOR: {
  923. const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd;
  924. nk_gdip_scissor(s->x, s->y, s->w, s->h);
  925. } break;
  926. case NK_COMMAND_LINE: {
  927. const struct nk_command_line *l = (const struct nk_command_line *)cmd;
  928. nk_gdip_stroke_line(l->begin.x, l->begin.y, l->end.x,
  929. l->end.y, l->line_thickness, l->color);
  930. } break;
  931. case NK_COMMAND_RECT: {
  932. const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
  933. nk_gdip_stroke_rect(r->x, r->y, r->w, r->h,
  934. (unsigned short)r->rounding, r->line_thickness, r->color);
  935. } break;
  936. case NK_COMMAND_RECT_FILLED: {
  937. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
  938. nk_gdip_fill_rect(r->x, r->y, r->w, r->h,
  939. (unsigned short)r->rounding, r->color);
  940. } break;
  941. case NK_COMMAND_CIRCLE: {
  942. const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
  943. nk_gdip_stroke_circle(c->x, c->y, c->w, c->h, c->line_thickness, c->color);
  944. } break;
  945. case NK_COMMAND_CIRCLE_FILLED: {
  946. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  947. nk_gdip_fill_circle(c->x, c->y, c->w, c->h, c->color);
  948. } break;
  949. case NK_COMMAND_TRIANGLE: {
  950. const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
  951. nk_gdip_stroke_triangle(t->a.x, t->a.y, t->b.x, t->b.y,
  952. t->c.x, t->c.y, t->line_thickness, t->color);
  953. } break;
  954. case NK_COMMAND_TRIANGLE_FILLED: {
  955. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
  956. nk_gdip_fill_triangle(t->a.x, t->a.y, t->b.x, t->b.y,
  957. t->c.x, t->c.y, t->color);
  958. } break;
  959. case NK_COMMAND_POLYGON: {
  960. const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
  961. nk_gdip_stroke_polygon(p->points, p->point_count, p->line_thickness,p->color);
  962. } break;
  963. case NK_COMMAND_POLYGON_FILLED: {
  964. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
  965. nk_gdip_fill_polygon(p->points, p->point_count, p->color);
  966. } break;
  967. case NK_COMMAND_POLYLINE: {
  968. const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
  969. nk_gdip_stroke_polyline(p->points, p->point_count, p->line_thickness, p->color);
  970. } break;
  971. case NK_COMMAND_TEXT: {
  972. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  973. nk_gdip_draw_text(t->x, t->y, t->w, t->h,
  974. (const char*)t->string, t->length,
  975. (GdipFont*)t->font->userdata.ptr,
  976. t->background, t->foreground);
  977. } break;
  978. case NK_COMMAND_CURVE: {
  979. const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
  980. nk_gdip_stroke_curve(q->begin, q->ctrl[0], q->ctrl[1],
  981. q->end, q->line_thickness, q->color);
  982. } break;
  983. case NK_COMMAND_IMAGE: {
  984. const struct nk_command_image *i = (const struct nk_command_image *)cmd;
  985. nk_gdip_draw_image(i->x, i->y, i->w, i->h, i->img, i->col);
  986. } break;
  987. case NK_COMMAND_ARC: {
  988. const struct nk_command_arc *i = (const struct nk_command_arc *)cmd;
  989. nk_gdip_stroke_arc(i->cx, i->cy, i->r, i->a[0], i->a[1], i->line_thickness, i->color);
  990. } break;
  991. case NK_COMMAND_ARC_FILLED: {
  992. const struct nk_command_arc_filled *i = (const struct nk_command_arc_filled *)cmd;
  993. nk_gdip_fill_arc(i->cx, i->cy, i->r, i->a[0], i->a[1], i->color);
  994. } break;
  995. case NK_COMMAND_RECT_MULTI_COLOR:
  996. default: break;
  997. }
  998. }
  999. }
  1000. NK_API void
  1001. nk_gdip_render_gui(enum nk_anti_aliasing AA)
  1002. {
  1003. nk_gdip_prerender_gui(AA);
  1004. nk_gdip_blit(gdip.window);
  1005. nk_clear(&gdip.ctx);
  1006. }
  1007. NK_API void
  1008. nk_gdip_render(enum nk_anti_aliasing AA, struct nk_color clear)
  1009. {
  1010. nk_gdip_clear(clear);
  1011. nk_gdip_render_gui(AA);
  1012. }
  1013. #endif