diff options
Diffstat (limited to 'src/libui_sdl/libui')
-rw-r--r-- | src/libui_sdl/libui/ui.h | 17 | ||||
-rw-r--r-- | src/libui_sdl/libui/windows/draw.cpp | 54 | ||||
-rw-r--r-- | src/libui_sdl/libui/windows/draw.hpp | 8 |
3 files changed, 78 insertions, 1 deletions
diff --git a/src/libui_sdl/libui/ui.h b/src/libui_sdl/libui/ui.h index 27574a5..835b0c2 100644 --- a/src/libui_sdl/libui/ui.h +++ b/src/libui_sdl/libui/ui.h @@ -342,6 +342,8 @@ typedef struct uiDrawMatrix uiDrawMatrix; typedef struct uiDrawBrushGradientStop uiDrawBrushGradientStop; +typedef struct uiDrawBitmap uiDrawBitmap; + _UI_ENUM(uiDrawBrushType) { uiDrawBrushTypeSolid, uiDrawBrushTypeLinearGradient, @@ -429,6 +431,15 @@ struct uiDrawStrokeParams { double DashPhase; }; +struct uiRect { + int X; + int Y; + int Width; + int Height; +}; + +typedef struct uiRect uiRect; + _UI_EXTERN uiDrawPath *uiDrawNewPath(uiDrawFillMode fillMode); _UI_EXTERN void uiDrawFreePath(uiDrawPath *p); @@ -475,6 +486,12 @@ _UI_EXTERN void uiDrawClip(uiDrawContext *c, uiDrawPath *path); _UI_EXTERN void uiDrawSave(uiDrawContext *c); _UI_EXTERN void uiDrawRestore(uiDrawContext *c); +// bitmap API +_UI_EXTERN uiDrawBitmap* uiDrawNewBitmap(uiDrawContext* c, int width, int height); +_UI_EXTERN void uiDrawBitmapUpdate(uiDrawBitmap* bmp, const void* data); +_UI_EXTERN void uiDrawBitmapDraw(uiDrawContext* c, uiDrawBitmap* bmp, uiRect* srcrect, uiRect* dstrect); +_UI_EXTERN void uiDrawFreeBitmap(uiDrawBitmap* bmp); + // TODO manage the use of Text, Font, and TextFont, and of the uiDrawText prefix in general ///// TODO reconsider this diff --git a/src/libui_sdl/libui/windows/draw.cpp b/src/libui_sdl/libui/windows/draw.cpp index 5f4d29f..ad8ea96 100644 --- a/src/libui_sdl/libui/windows/draw.cpp +++ b/src/libui_sdl/libui/windows/draw.cpp @@ -38,7 +38,7 @@ ID2D1HwndRenderTarget *makeHWNDRenderTarget(HWND hwnd) logLastError(L"error getting DC to find DPI"); ZeroMemory(&props, sizeof (D2D1_RENDER_TARGET_PROPERTIES)); - props.type = D2D1_RENDER_TARGET_TYPE_DEFAULT; + props.type = D2D1_RENDER_TARGET_TYPE_HARDWARE;//DEFAULT; props.pixelFormat.format = DXGI_FORMAT_UNKNOWN; props.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN; props.dpiX = GetDeviceCaps(dc, LOGPIXELSX); @@ -509,3 +509,55 @@ void uiDrawRestore(uiDrawContext *c) // no need to explicitly addref or release; just transfer the ref c->currentClip = state.clip; } + + +// bitmap API + +uiDrawBitmap* uiDrawNewBitmap(uiDrawContext* c, int width, int height) +{ + uiDrawBitmap* bmp; + HRESULT hr; + + bmp = uiNew(uiDrawBitmap); + + D2D1_BITMAP_PROPERTIES bp2 = D2D1::BitmapProperties(); + bp2.dpiX = 0; + bp2.dpiY = 0; + bp2.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_IGNORE); + //bp2.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE); + // TODO: fallback: convert to BGRA if needed (RGBA only works in hardware mode) + + c->rt->BeginDraw(); + + hr = c->rt->CreateBitmap(D2D1::SizeU(width,height), NULL, 0, &bp2, &bmp->bmp); + if (hr != S_OK) + logHRESULT(L"error creating bitmap", hr); + + c->rt->EndDraw(); + + bmp->Width = width; + bmp->Height = height; + bmp->Stride = width*4; + + return bmp; +} + +void uiDrawBitmapUpdate(uiDrawBitmap* bmp, const void* data) +{ + D2D1_RECT_U rekt = D2D1::RectU(0, 0, bmp->Width, bmp->Height); + bmp->bmp->CopyFromMemory(&rekt, data, bmp->Stride); +} + +void uiDrawBitmapDraw(uiDrawContext* c, uiDrawBitmap* bmp, uiRect* srcrect, uiRect* dstrect) +{ + D2D_RECT_F _srcrect = D2D1::RectF(srcrect->X, srcrect->Y, srcrect->X+srcrect->Width-1, srcrect->Y+srcrect->Height-1); + D2D_RECT_F _dstrect = D2D1::RectF(dstrect->X, dstrect->Y, dstrect->X+dstrect->Width-1, dstrect->Y+dstrect->Height-1); + + c->rt->DrawBitmap(bmp->bmp, &_dstrect, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, &_srcrect); +} + +void uiDrawFreeBitmap(uiDrawBitmap* bmp) +{ + bmp->bmp->Release(); + uiFree(bmp); +} diff --git a/src/libui_sdl/libui/windows/draw.hpp b/src/libui_sdl/libui/windows/draw.hpp index b015791..992d050 100644 --- a/src/libui_sdl/libui/windows/draw.hpp +++ b/src/libui_sdl/libui/windows/draw.hpp @@ -9,6 +9,14 @@ struct uiDrawContext { ID2D1PathGeometry *currentClip; }; +struct uiDrawBitmap { + int Width; + int Height; + int Stride; + + ID2D1Bitmap* bmp; +}; + // drawpath.cpp extern ID2D1PathGeometry *pathGeometry(uiDrawPath *p); |