comparison win/dw.c @ 1613:fd3b7784ab53

Initial commit for GDI+ anti-aliased drawing. Code currently does not work, but wanted to commit the framework... test code is in GDIPLUS1 #ifdef Also added dw_draw_line() test to dwtest to allow me to try out this code.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 26 Mar 2012 18:12:52 +0000
parents 02641cd81203
children 702de58e9ef8
comparison
equal deleted inserted replaced
1612:712c270f16d8 1613:fd3b7784ab53
79 UnsupportedGdiplusVersion = 17, 79 UnsupportedGdiplusVersion = 17,
80 GdiplusNotInitialized = 18, 80 GdiplusNotInitialized = 18,
81 PropertyNotFound = 19, 81 PropertyNotFound = 19,
82 PropertyNotSupported = 20, 82 PropertyNotSupported = 20,
83 ProfileNotFound = 21 83 ProfileNotFound = 21
84 } Status; 84 } GpStatus;
85 85
86 Status WINAPI GdipCreateBitmapFromFile(const WCHAR* filename, void **bitmap); 86 typedef enum {
87 Status WINAPI GdipCreateHBITMAPFromBitmap(void *bitmap, HBITMAP* hbmReturn, DWORD background); 87 UnitWorld,
88 Status WINAPI GdipCreateHICONFromBitmap(void *bitmap, HICON *hbmReturn); 88 UnitDisplay,
89 Status WINAPI GdipDisposeImage(void *image); 89 UnitPixel,
90 Status WINAPI GdipGetImagePixelFormat(void *image, INT *format); 90 UnitPoint,
91 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input, void *output); 91 UnitInch,
92 UnitDocument,
93 UnitMillimeter
94 } GpUnit;
95
96 typedef enum {
97 FlushIntentionFlush = 0,
98 FlushIntentionSync = 1
99 } GpFlushIntention;
100
101 typedef void GpGraphics;
102 typedef void GpPen;
103 typedef void GpBitmap;
104 typedef void GpImage;
105 typedef POINT GpPoint;
106 typedef DWORD ARGB;
107 typedef float REAL;
108
109 #define ALPHA_SHIFT 24
110 #define RED_SHIFT 16
111 #define GREEN_SHIFT 8
112 #define BLUE_SHIFT 0
113 #define ALPHA_MASK ((ARGB) 0xff << ALPHA_SHIFT)
114
115 #define MAKEARGB(a, r, g, b) \
116 (((ARGB) ((a) & 0xff) << ALPHA_SHIFT)| \
117 ((ARGB) ((r) & 0xff) << RED_SHIFT) | \
118 ((ARGB) ((g) & 0xff) << GREEN_SHIFT)| \
119 ((ARGB) ((b) & 0xff) << BLUE_SHIFT))
120
121 /* Graphic conversion functions */
122 GpStatus WINAPI GdipCreateBitmapFromFile(const WCHAR* filename, GpBitmap **bitmap);
123 GpStatus WINAPI GdipCreateHBITMAPFromBitmap(GpBitmap *bitmap, HBITMAP* hbmReturn, DWORD background);
124 GpStatus WINAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hbmReturn);
125 GpStatus WINAPI GdipDisposeImage(GpImage *image);
126 GpStatus WINAPI GdipGetImagePixelFormat(GpImage *image, INT *format);
127 /* Startup and Shutdown */
128 GpStatus WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input, void *output);
92 VOID WINAPI GdiplusShutdown(ULONG_PTR token); 129 VOID WINAPI GdiplusShutdown(ULONG_PTR token);
130 /* Drawing functions */
131 GpStatus WINAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics);
132 GpStatus WINAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics);
133 GpStatus WINAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1, INT y1, INT x2, INT y2);
134 GpStatus WINAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, const GpPoint *points, INT count);
135 GpStatus WINAPI GdipDeleteGraphics(GpGraphics *graphics);
136 GpStatus WINAPI GdipCreatePen1(ARGB color, REAL width, GpUnit unit, GpPen **pen);
137 GpStatus WINAPI GdipDeletePen(GpPen *pen);
138 GpStatus WINAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention);
93 139
94 /* Pixel format information */ 140 /* Pixel format information */
95 #define PixelFormatIndexed 0x00010000 141 #define PixelFormatIndexed 0x00010000
96 #define PixelFormatGDI 0x00020000 142 #define PixelFormatGDI 0x00020000
97 #define PixelFormatAlpha 0x00040000 143 #define PixelFormatAlpha 0x00040000
195 241
196 DWORD _foreground; 242 DWORD _foreground;
197 DWORD _background; 243 DWORD _background;
198 DWORD _hPen; 244 DWORD _hPen;
199 DWORD _hBrush; 245 DWORD _hBrush;
246 #ifdef GDIPLUS
247 DWORD _gpPen;
248 #endif
200 249
201 BYTE _red[] = { 0x00, 0xbb, 0x00, 0xaa, 0x00, 0xbb, 0x00, 0xaa, 0x77, 250 BYTE _red[] = { 0x00, 0xbb, 0x00, 0xaa, 0x00, 0xbb, 0x00, 0xaa, 0x77,
202 0xff, 0x00, 0xee, 0x00, 0xff, 0x00, 0xff, 0xaa, 0x00 }; 251 0xff, 0x00, 0xee, 0x00, 0xff, 0x00, 0xff, 0xaa, 0x00 };
203 BYTE _green[] = { 0x00, 0x00, 0xbb, 0xaa, 0x00, 0x00, 0xbb, 0xaa, 0x77, 252 BYTE _green[] = { 0x00, 0x00, 0xbb, 0xaa, 0x00, 0x00, 0xbb, 0xaa, 0x77,
204 0x00, 0xff, 0xee, 0x00, 0x00, 0xee, 0xff, 0xaa, 0x00 }; 253 0x00, 0xff, 0xee, 0x00, 0x00, 0xee, 0xff, 0xaa, 0x00 };
3690 /* Initialize thread local values to the defaults */ 3739 /* Initialize thread local values to the defaults */
3691 void _init_thread(void) 3740 void _init_thread(void)
3692 { 3741 {
3693 COLORREF foreground = RGB(128,128,128); 3742 COLORREF foreground = RGB(128,128,128);
3694 COLORREF background = DW_RGB_TRANSPARENT; 3743 COLORREF background = DW_RGB_TRANSPARENT;
3744 #ifdef GDIPLUS
3745 ARGB gpfore = MAKEARGB(0, 128, 128, 128);
3746 GpPen *pen;
3747
3748 GdipCreatePen1(gpfore, 1.0, UnitPixel, &pen);
3749 TlsSetValue(_gpPen, (LPVOID)pen);
3750 #endif
3695 TlsSetValue(_foreground, (LPVOID)foreground); 3751 TlsSetValue(_foreground, (LPVOID)foreground);
3696 TlsSetValue(_background, (LPVOID)background); 3752 TlsSetValue(_background, (LPVOID)background);
3697 TlsSetValue(_hPen, CreatePen(PS_SOLID, 1, foreground)); 3753 TlsSetValue(_hPen, CreatePen(PS_SOLID, 1, foreground));
3698 TlsSetValue(_hBrush, CreateSolidBrush(foreground)); 3754 TlsSetValue(_hBrush, CreateSolidBrush(foreground));
3699 } 3755 }
3733 /* Initialize our thread local storage */ 3789 /* Initialize our thread local storage */
3734 _foreground = TlsAlloc(); 3790 _foreground = TlsAlloc();
3735 _background = TlsAlloc(); 3791 _background = TlsAlloc();
3736 _hPen = TlsAlloc(); 3792 _hPen = TlsAlloc();
3737 _hBrush = TlsAlloc(); 3793 _hBrush = TlsAlloc();
3794 #ifdef GDIPLUS
3795 _gpPen = TlsAlloc();
3796 #endif
3738 _init_thread(); 3797 _init_thread();
3739 3798
3740 icc.dwSize = sizeof(INITCOMMONCONTROLSEX); 3799 icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
3741 icc.dwICC = ICC_WIN95_CLASSES|ICC_DATE_CLASSES; 3800 icc.dwICC = ICC_WIN95_CLASSES|ICC_DATE_CLASSES;
3742 3801
9470 void API dw_color_foreground_set(unsigned long value) 9529 void API dw_color_foreground_set(unsigned long value)
9471 { 9530 {
9472 HPEN hPen = TlsGetValue(_hPen); 9531 HPEN hPen = TlsGetValue(_hPen);
9473 HBRUSH hBrush = TlsGetValue(_hBrush); 9532 HBRUSH hBrush = TlsGetValue(_hBrush);
9474 COLORREF foreground; 9533 COLORREF foreground;
9534 #ifdef GDIPLUS
9535 GpPen *pen = TlsGetValue(_gpPen);
9536 ARGB gpfore;
9537 #endif
9475 9538
9476 value = _internal_color(value); 9539 value = _internal_color(value);
9477 foreground = RGB(DW_RED_VALUE(value), DW_GREEN_VALUE(value), DW_BLUE_VALUE(value)); 9540 foreground = RGB(DW_RED_VALUE(value), DW_GREEN_VALUE(value), DW_BLUE_VALUE(value));
9541 #ifdef GDIPLUS
9542 gpfore = MAKEARGB(0, DW_RED_VALUE(value), DW_GREEN_VALUE(value), DW_BLUE_VALUE(value));
9543
9544 GdipDeletePen(pen);
9545 GdipCreatePen1(gpfore, 1.0, UnitPixel, &pen);
9546 TlsSetValue(_gpPen, (LPVOID)pen);
9547 #endif
9478 9548
9479 DeleteObject(hPen); 9549 DeleteObject(hPen);
9480 DeleteObject(hBrush); 9550 DeleteObject(hBrush);
9481 TlsSetValue(_foreground, (LPVOID)foreground); 9551 TlsSetValue(_foreground, (LPVOID)foreground);
9482 TlsSetValue(_hPen, CreatePen(PS_SOLID, 1, foreground)); 9552 TlsSetValue(_hPen, CreatePen(PS_SOLID, 1, foreground));
9562 * x2: Second X coordinate. 9632 * x2: Second X coordinate.
9563 * y2: Second Y coordinate. 9633 * y2: Second Y coordinate.
9564 */ 9634 */
9565 void API dw_draw_line(HWND handle, HPIXMAP pixmap, int x1, int y1, int x2, int y2) 9635 void API dw_draw_line(HWND handle, HPIXMAP pixmap, int x1, int y1, int x2, int y2)
9566 { 9636 {
9637 #ifdef GDIPLUS1
9638 GpGraphics *graphics = NULL;
9639 GpPen *pen = TlsGetValue(_gpPen);
9640
9641 if(handle)
9642 GdipCreateFromHWND(handle, &graphics);
9643 else if(pixmap)
9644 GdipCreateFromHDC(pixmap->hdc, &graphics);
9645 else
9646 return;
9647
9648 GdipDrawLineI(graphics, pen, x1, y1, x2, y2);
9649 GdipFlush(graphics, FlushIntentionSync);
9650 GdipDeleteGraphics(graphics);
9651 #else
9567 HDC hdcPaint; 9652 HDC hdcPaint;
9568 HPEN oldPen; 9653 HPEN oldPen;
9569 9654
9570 if(handle) 9655 if(handle)
9571 hdcPaint = GetDC(handle); 9656 hdcPaint = GetDC(handle);
9582 * to draw the last pixel. So I do it myself. 9667 * to draw the last pixel. So I do it myself.
9583 */ 9668 */
9584 SetPixel(hdcPaint, x2, y2, (COLORREF)TlsGetValue(_foreground)); 9669 SetPixel(hdcPaint, x2, y2, (COLORREF)TlsGetValue(_foreground));
9585 if(!pixmap) 9670 if(!pixmap)
9586 ReleaseDC(handle, hdcPaint); 9671 ReleaseDC(handle, hdcPaint);
9672 #endif
9587 } 9673 }
9588 9674
9589 /* Draw a closed polygon on a window (preferably a render window). 9675 /* Draw a closed polygon on a window (preferably a render window).
9590 * Parameters: 9676 * Parameters:
9591 * handle: Handle to the window. 9677 * handle: Handle to the window.
10628 { 10714 {
10629 void (* threadfunc)(void *) = NULL; 10715 void (* threadfunc)(void *) = NULL;
10630 void **tmp = (void **)data; 10716 void **tmp = (void **)data;
10631 HPEN hPen; 10717 HPEN hPen;
10632 HBRUSH hBrush; 10718 HBRUSH hBrush;
10719 #ifdef GDIPLUS
10720 GpPen *pen;
10721 #endif
10633 10722
10634 _init_thread(); 10723 _init_thread();
10635 10724
10636 threadfunc = (void (*)(void *))tmp[0]; 10725 threadfunc = (void (*)(void *))tmp[0];
10637 threadfunc(tmp[1]); 10726 threadfunc(tmp[1]);
10639 free(tmp); 10728 free(tmp);
10640 if((hPen = TlsGetValue(_hPen))) 10729 if((hPen = TlsGetValue(_hPen)))
10641 DeleteObject(hPen); 10730 DeleteObject(hPen);
10642 if((hBrush = TlsGetValue(_hBrush))) 10731 if((hBrush = TlsGetValue(_hBrush)))
10643 DeleteObject(hBrush); 10732 DeleteObject(hBrush);
10733 #ifdef GDIPLUS
10734 if((pen = TlsGetValue(_gpPen)))
10735 GdipDeletePen(pen);
10736 #endif
10644 } 10737 }
10645 10738
10646 /* 10739 /*
10647 * Creates a new thread with a starting point of func. 10740 * Creates a new thread with a starting point of func.
10648 * Parameters: 10741 * Parameters: