Related
I am trying to migrate my graphics interface project from Gdiplus to Direct2D.
Currently, I have a code that calculates clipping area for an rendering object:
Graphics g(hdc);
Region regC = Rect(x, y, cx + padding[2] + padding[0], cy + padding[3] + padding[1]);
RecursRegPos(this->parent, ®C);
RecursRegClip(this->parent, ®C);
g.setClip(g);
...
inline void RecursRegClip(Object *parent, Region* reg)
{
if (parent == CARD_PARENT)
return;
if (parent->overflow != OVISIBLE)
{
Rect regC(parent->getAbsoluteX(), parent->getAbsoluteY(), parent->getCx(), parent->getCy()); // Implementation of these function is not necceassary
GraphicsPath path;
path.Reset();
GetRoundRectPath(&path, regC, parent->borderRadius[0]);
// source https://stackoverflow.com/a/71431813/15220214, but if diameter is zero, just plain rect is returned
reg->Intersect(&path);
}
RecursRegClip(parent->parent, reg);
}
inline void RecursRegPos(Object* parent, Rect* reg)
{
if (parent == CARD_PARENT)
return;
reg->X += parent->getX() + parent->padding[0];
reg->Y += parent->getY() + parent->padding[1];
if (parent->overflow == OSCROLL || parent->overflow == OSCROLLH)
{
reg->X -= parent->scrollLeft;
reg->Y -= parent->scrollTop;
}
RecursRegPos(parent->parent, reg);
}
And now I need to convert it to Direct2D. As You may notice, there is no need to create Graphics object to get complete calculated clipping region, so I it would be cool if there is way to just convert Region to ID2D1Geometry*, that, as far, as I understand from msdn article need to create clipping layer.
Also, there is probably way to convert existing code (RecursRegClip, RecursRegPos) to Direct2D, but I am facing problems, because I need to work with path, but current functions get region as an argument.
Update 1
There is Region::GetData method that returns, as I understand array of points, so maybe there is possibility to define either ID2D1PathGeometry or ID2D1GeometrySink by points?
Update 2
Oh, maybe
ID2D1GeometrySink::AddLines(const D2D1_POINT_2F *points, UINT32 pointsCount)
is what do I need?
Unfortunately, GetData of region based on just (0,0,4,4) rectangle returns 36 mystique values:
Region reg(Rect(0, 0, 4, 4));
auto so = reg.GetDataSize();
BYTE* are = new BYTE[so];
UINT fi = 0;
reg.GetData(are, so, &fi);
wchar_t ou[1024]=L"\0";
for (int i = 0; i < fi; i++)
{
wchar_t buf[10] = L"";
_itow_s(are[i], buf, 10, 10);
wcscat_s(ou, 1024, buf);
wcscat_s(ou, 1024, L", ");
}
// ou - 28, 0, 0, 0, 188, 90, 187, 128, 2, 16, 192, 219, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 64, 0, 0, 128, 64,
I rewrote the solution completely, it seems to be working:
// zclip is ID2D1PathGeometry*
inline void Render(ID2D1HwndRenderTarget *target)
{
ID2D1RoundedRectangleGeometry* mask = nullptr;
ID2D1Layer* clip = nullptr;
if(ONE_OF_PARENTS_CLIPS_THIS || THIS_HAS_BORDER_RADIUS)
{
Region reg = Rect(x, y, cx + padding[2] + padding[0], cy + padding[3] + padding[1]);
RecursRegPos(this->parent, ®);
D2D1_ROUNDED_RECT maskRect;
maskRect.rect.left = reg.X;
maskRect.rect.top = reg.Y;
maskRect.rect.right = reg.X + reg.Width;
maskRect.rect.bottom = reg.Y + reg.Height;
maskRect.radiusX = this->borderRadius[0];
maskRect.radiusY = this->borderRadius[1];
factory->CreateRoundedRectangleGeometry(maskRect, &mask);
RecursGeoClip(this->parent, mask);
target->CreateLayer(NULL, &clip);
if(zclip)
target->PushLayer(D2D1::LayerParameters(D2D1::InfiniteRect(), zclip), clip);
else
target->PushLayer(D2D1::LayerParameters(D2D1::InfiniteRect(), mask), clip);
SafeRelease(&mask);
}
// Draw stuff here
if (clip)
{
target->PopLayer();
SafeRelease(&clip);
SafeRelease(&mask);
SafeRelease(&zclip);
}
}
...
inline void RecursGeoClip(Object* parent, ID2D1Geometry* geo)
{
if (parent == CARD_PARENT)
return;
ID2D1RoundedRectangleGeometry* maskParent = nullptr;
if (parent->overflow != OVISIBLE)
{
Rect regC(parent->getAbsoluteX(), parent->getAbsoluteY(), parent->getCx(), parent->getCy());
ID2D1GeometrySink* sink = nullptr;
ID2D1PathGeometry* path = nullptr;
SafeRelease(&path);
factory->CreatePathGeometry(&path);
D2D1_ROUNDED_RECT maskRect;
maskRect.rect.left = regC.X;
maskRect.rect.top = regC.Y;
maskRect.rect.right = regC.X + regC.Width;
maskRect.rect.bottom = regC.Y + regC.Height;
maskRect.radiusX = parent->borderRadius[0];
maskRect.radiusY = parent->borderRadius[1];
path->Open(&sink);
factory->CreateRoundedRectangleGeometry(maskRect, &maskParent);
geo->CombineWithGeometry(maskParent, D2D1_COMBINE_MODE_INTERSECT, NULL, sink);
sink->Close();
SafeRelease(&sink);
SafeRelease(&this->zclip);
this->zclip = path;
RecursGeoClip(parent->parent, this->zclip);
}
else
RecursGeoClip(parent->parent, geo);
SafeRelease(&maskParent);
}
Now I can enjoy drawing one image and two rectangles in more than 60 fps, instead of 27 (in case of 200x200 image size, higher size - lower fps) with Gdi+ -_- :
I am using the SDL_ShowMessageBox(...) function from the SDL2 library.
This is my current code:
const SDL_MessageBoxButtonData buttons[] = {
{SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "OK"},
};
const SDL_MessageBoxColorScheme colorScheme = {
{{255, 0, 0},
{0, 255, 0},
{255, 255, 0},
{0, 0, 255},
{255, 0, 255}}};
char _score[10];
sprintf(_score, "%d", score);
char text[50] = "Game over\nYour Score: ";
strcat(text, _score);
const SDL_MessageBoxData messageboxdata = {
SDL_MESSAGEBOX_INFORMATION,
NULL,
"You lost",
text,
SDL_arraysize(buttons),
buttons,
&colorScheme};
int buttonid;
if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0)
SDL_Log("error opening window");
exit(0);
I need to position the window on my display. I can't find anything about positioning in the documentation or through other sources on the internet. Thanks!
I'm writing a c++ code to create a animate SWF file from multi JPG pictures.
Now, I have pictures like "image_0" to "image_100". I find a swflib library. I think it will help. So far, I can use this library's method to create .SWF file and the size of .SWF file is the sum of pictures in .JPG format.
So, I think I almost done. But ,SWF does not play. I am crazy.
Below this is the code which I modified:
`void CSWFLIBTestProjectDlg::CreateSWFMovie_Bitmap()
{
// Set movie params
SIZE_F movieSize = {100, 100};
int frameRate = 14;
POINT_F pt;
// Create empty .SWF file
CSWFMovie swfMovie;
swfMovie.OpenSWFFile(_T("SWF Sample Movies/Sample2.swf"), movieSize, frameRate);
SWF_RGB bgColor = {255, 255, 255};
swfMovie.SetBackgroundColor(bgColor);
// Define bitmap object
CSWFBitmap bitmap(2, (UCHAR*)"gif_0.jpg");//bm128
swfMovie.DefineObject(&bitmap, -1, true);
// Define custom shape
RECT_F shapeRect = {0, 0, 1000, 1000};
CSWFShape shape(1, shapeRect, 1);
SWF_RGBA lineColor = {0, 0, 0, 255};
shape.AddLineStyle(0, lineColor);
RECT_F bitmapRect = {0, 0, 1246, 622}; // size of the bitmap
RECT_F clipRect = {0, 0, 100, 100}; // where to fill
shape.AddBitmapFillStyle(bitmap.m_ID, SWF_FILLSTYLETYPE_BITMAP_0, bitmapRect, clipRect);
pt.x = 0;
pt.y = 0;
shape.ChangeStyle(1, 1, 0, &pt);
shape.AddLineSegment(100, 0);
shape.AddLineSegment(0, 100);
shape.AddLineSegment(-100, 0);
shape.AddLineSegment(0, -100);
swfMovie.DefineObject(&shape, shape.m_Depth, true);
swfMovie.ShowFrame();
/****************************************
* move
****************************************/
float i;
for (i=0; i<10; i++)
{
shape.Translate(i, 0);
swfMovie.UpdateObject(&shape, shape.m_Depth, NULL, -1);
swfMovie.ShowFrame();
//if (i>200)
//{
// swfMovie.RemoveObject(shape.m_Depth);
//}
}
/****************************************
* add jpg
****************************************/
swfMovie.RemoveObject(shape.m_Depth);
for (int i=1;i<=100;i++)
{
char filename[100];
sprintf(filename,"gif_%d%s",i,".jpg");
CSWFBitmap bitmap2(3, (UCHAR*)filename);//gif_0
swfMovie.DefineObject(&bitmap2, -1, true);//
// Define custom shape
RECT_F shapeRect2 = {0, 0, 1000, 1000};
CSWFShape shape2(1, shapeRect2, 1);
SWF_RGBA lineColor2 = {0, 0, 0, 255};
shape2.AddLineStyle(0, lineColor2);
RECT_F bitmapRect2 = {0, 0, 1246, 622}; // size of the bitmap
RECT_F clipRect2 = {0, 0, 100, 100}; // where to fill
shape2.AddBitmapFillStyle(bitmap2.m_ID, SWF_FILLSTYLETYPE_BITMAP_0, bitmapRect2, clipRect2);
pt.x = 0;
pt.y = 0;
shape2.ChangeStyle(1, 1, 0, &pt);
shape2.AddLineSegment(100, 0);
shape2.AddLineSegment(0, 100);
shape2.AddLineSegment(-100, 0);
shape2.AddLineSegment(0, -100);
swfMovie.UpdateObject(&shape2, shape2.m_Depth, NULL, -1);
//swfMovie.DefineObject(&shape2, shape2.m_Depth, true);
swfMovie.ShowFrame();
//swfMovie.RemoveObject(shape2.m_Depth);
}
// Close .SWF file
swfMovie.CloseSWFFile();
}`
can you tell me what's wrong with my code ? Thanks in advance.
I have found a way to create a .SWF file from a set of image using C++,and a swflib library.you can download it from the lib file website .in which there is a method named :CreateSWFMovie_Bitmap() here is the code i modified
void CSWFLIBTestProjectDlg::CreateSWFMovie_Bitmap()
{
SIZE_F movieSize = {4000, 4000};
int frameRate = 40;
POINT_F pt;
CSWFMovie swfMovie;
swfMovie.OpenSWFFile(_T("SWF Sample Movies/Sample2.swf"), movieSize, frameRate);
SWF_RGB bgColor = {255, 255, 255};
swfMovie.SetBackgroundColor(bgColor);
CSWFBitmap bitmap(2, (UCHAR*)"bm128.jpg");
swfMovie.DefineObject(&bitmap, -1, false);
RECT_F shapeRect = {0, 0, 1000, 1000};//shape大小
CSWFShape shape(1, shapeRect, 1);
SWF_RGBA lineColor = {0, 0, 0, 255};
shape.AddLineStyle(3, lineColor);
RECT_F bitmapRect = {0, 0, 128, 128}; // size of the bitmap
RECT_F clipRect = {0, 0, 100, 100}; // where to fill
shape.AddBitmapFillStyle(bitmap.m_ID, SWF_FILLSTYLETYPE_BITMAP_0, bitmapRect, clipRect);
pt.x = 0;
pt.y = 0;
shape.ChangeStyle(1, 1, 0, &pt);
shape.AddLineSegment(100, 0);
shape.AddLineSegment(0, 100);
shape.AddLineSegment(-100, 0);
shape.AddLineSegment(0, -100);
swfMovie.DefineObject(&shape, shape.m_Depth, true);
swfMovie.ShowFrame();
for (int i=0; i<100; i++)
{
char filename[50]="";
sprintf(filename,"gif_%d%s",i,".jpg");
CSWFBitmap bitmap2(300+i, (UCHAR*)filename);
swfMovie.DefineObject(&bitmap2, -1, false);//false
CSWFShape shape2(7+i,shapeRect,2+i);//depth值也不能一样 id不能重复
shape2.AddLineStyle(3, lineColor);
RECT_F bitmapRect2 = {0, 0, 128, 128};
RECT_F clipRect2 = {0, 0, 100, 100};
shape2.AddBitmapFillStyle(bitmap2.m_ID, SWF_FILLSTYLETYPE_BITMAP_0, bitmapRect2, clipRect2);
pt.x = 0;
pt.y = 0;
shape2.ChangeStyle(1, 1, 0, &pt);
shape2.AddLineSegment(1000, 0);
shape2.AddLineSegment(0, 1000);
shape2.AddLineSegment(-1000, 0);
shape2.AddLineSegment(0, -1000);
shape2.Translate(80, 0);
swfMovie.DefineObject(&shape2, shape2.m_Depth, true);//必须define才能动 可以不update
swfMovie.ShowFrame();
}
}
I'm Working with Kinect SDK 1.5 and found some problem when i want to track multiple face.
In my code I first use
myFaceTracker->DetectFaces(&sensor, NULL, candidates, &nums)
to get the currently detect face, and compare the their position with already tracked faces.
Those are not currently tracked faces will be added to a queue for the tracking in next step.
But when I want to track them using the StartTracking function, it doesn't give expected result.
for example:
//Time for us to track those not tracked
RECT rect = queue_face.front();
//Make the region larger than orginal
int length_vertical = rect.bottom - rect.top;
int length_horizontal = rect.right - rect.left;
rect.top = rect.top - length_vertical * 2;
rect.bottom = rect.bottom + length_vertical * 2;
rect.left = rect.left - length_horizontal * 2;
rect.right = rect.right + length_horizontal * 2;
//Use OpenCV to see if it gets right
CvPoint LT = cvPoint(rect.left, rect.top);
CvPoint LB = cvPoint(rect.left, rect.bottom);
CvPoint RT = cvPoint(rect.right, rect.top);
CvPoint RB = cvPoint(rect.right, rect.bottom);
cvLine(_img, LT, LB, cvScalar(0, 255, 0), 2);
cvLine(_img, LB, RB, cvScalar(0, 255, 0), 2);
cvLine(_img, RB, RT, cvScalar(0, 255, 0), 2);
cvLine(_img, RT, LT, cvScalar(0, 255, 0), 2);
//Here turns out to be some problem
result = facetracker->StartTracking(&sensor, &rect, NULL, faceresult);
//Check Result
is_track = SUCCEEDED(result) && SUCCEEDED(faceresult->GetStatus());
queue_face.pop();
....
The Code above gives me a totally useless result even if the status says succeed
if we use
faceresult->GetFaceRect(rect);
it gives us a rect with (0, 0, 0, 0)!
But if we change
result = facetracker->StartTracking(&sensor, &rect, NULL, faceresult);
to
result = facetracker->StartTracking(&sensor, NULL, NULL, faceresult);
Now it'll give us something useful, but in this way, its nearly impossible to control whom we want to track.
I also tried to see the input rect to (0, 0, width-1, height-1), but even worse, now it says it can't track anything.
Would anyone give me help?
Thanks a lot.
I have got a trouble using the StretchDIBits function.
I want to draw a bitmap made from a buffer. However, the colors I define in the buffer are different from the result on screen.
I have read the documentation and I played with the biCompression (BI_RGB and BI_BITFIELDS) and biClrUsed (0 / 3) parameters of the BITMAPINFOHEADER. I can see some differences depending on their values, but the result is still different from what I am expecting.
Here is the code I am using (it can be inserted in the OnDraw method of a template SDI project to demonstrate the problem).
void CTestStretchDIBitsView::OnDraw(CDC* /*pDC*/)
{
...
CClientDC dc(this);
CRect rect;
GetClientRect(&rect);
DWORD* pBuffer = new DWORD[500 * 500];
memset(pBuffer, RGB(255, 255, 0), 500 * 500 * sizeof(DWORD));
LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pBmpInfo->bmiHeader.biWidth = 500;
pBmpInfo->bmiHeader.biHeight = 500;
pBmpInfo->bmiHeader.biPlanes = 1;
pBmpInfo->bmiHeader.biBitCount = 32;
pBmpInfo->bmiHeader.biCompression = BI_BITFIELDS;
pBmpInfo->bmiHeader.biSizeImage = 500 * 500;
pBmpInfo->bmiHeader.biXPelsPerMeter = 0;
pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
pBmpInfo->bmiHeader.biClrUsed = 0;
pBmpInfo->bmiHeader.biClrImportant = 0;
SetStretchBltMode(dc.m_hDC, STRETCH_DELETESCANS);
StretchDIBits(dc.m_hDC,
0,
rect.Height(),
rect.Width(),
-rect.Height(),
0,
0,
500,
500,
pBuffer,
pBmpInfo,
DIB_RGB_COLORS,
SRCCOPY);
delete[] pBmpInfo;
delete[] pBuffer;
}
You have to use the following mode
SetStretchBltMode(hdcWindow,HALFTONE);
instead of
SetStretchBltMode(dc.m_hDC, STRETCH_DELETESCANS);
because halftone is the best mode according to my research.
The problem didn't come from the StretchDIBits function but from the initialization of the buffer used as the bitmap here.
memset(...) function was misused.
With an initialization such as :
int Color = RGB(255, 0, 0);
for (int i = 0 ; i < 500 * 500 ; i++)
pBuffer[i] = Color;
I get a perfectly blue image as expected.