XRender Create Offscreen Picture - c++

I'm trying to create a compositing window manager. So far it works, but when a window overlays another, it flickers like crazy. I found that it was because I was creating a Picture, and then painting to it, which caused it to paint to the screen.
My desired behaviour would be to have an offscreen Picture that I can draw to, and then use XComposite to draw that to an onscreen window. Is there any way to have an offscreen Picture that is the same size as the root window?
So far (this code runs in an infinite loop thingy):
Window root, parent, *children;
uint children_count;
XQueryTree(disp, DefaultRootWindow(disp), &root, &parent, &children, &children_count);
// I'd like the following picture to be offscreen.
// I suspect that I have to put else something where rootPicture is
// Currently, when I draw to this picture, X11 renders it to the screen immediately, which is what I don't want.
Picture pictureBuf = XRenderCreatePicture(disp, /* rootPicture */, XRenderFindVisualFormat(disp, DefaultVisual(disp, DefaultScreen(disp))), CPSubwindowMode, &RootAttributes);
for (uint i = 0; i < children_count; i++) {
// collapsed some stuff that doesn't matter
Picture picture = XRenderCreatePicture(disp, children[i], pictureFormat, CPSubwindowMode, &pictureAttributes);
// The following line should composite to an offscreen picture
XRenderComposite(disp, hasAlpha ? PictOpOver : PictOpSrc, picture, None, pictureBuf, 0, 0, 0, 0, windowRect.x(), windowRect.y(), windowRect.width(), windowRect.height());
// collapsed some stuff that doesn't matter
}
// The following line should composite from the offscreen picture to an onscreen picture
XRenderComposite(disp, PictOpSrc, pictureBuf, None, rootPicture, 0, 0, 0, 0, RootAttr.x, RootAttr.y, RootAttr.width, RootAttr.height);

I was recently looking into something similar and figured I'd reply. The below fragment of code shows how you would create new pixmap from an existing window without drawing directly to that window. Hopefully this helps.
Pixmap new_pixmap;
new_pixmap = XCompositeNameWindowPixmap( display, src_window);
Drawable draw = new_pixmap;
if (!draw) draw = src_window;
Picture origin;
origin = XRenderCreatePicture( display, draw, format, CPSubWindowMode, &attributes);

Related

How to display a image on a user defined rectangle with SDL_surface?

Im working on a project with my friend and we have run into an issue with surfaces and windows in SDL.
Currently we are able to create a window and display a rectangle on that window and move it around. The next thing we want to do is take a image and display it on a rectangle and then move it around the screen.
We started with taking the SDL_window* and turning it into SDL_surface* though this would take the image and display it on the background of the window.
Is there a way to turn a rectangle we create into a surface and display the image on that rectangle?
I have also tried using textures and it distorts the image when I tried to move it and the whole image doesn’t move with the rectangle.
// this happens in the constructor
temp_image_sur = IMG_Load( image_location.c_str() );
if( temp_image_sur == NULL )
{
std::cout << "Image could not be loaded" <<std::endl;
exit(1);
}
// This is in the actual draw function.
display_surface = SDL_GetWindowSurface( display_window );
if(display_surface == NULL )
{
printf(" null im exiting here %s\n", SDL_GetError());
exit(1);
}
image_surface = SDL_ConvertSurface( temp_image_sur, display_surface->format, 0 );
image_size = { this->location.x, this->location.y, this->size.width, this->size.height };
SDL_BlitSurface( image_surface, &image_size, display_surface, &image_size );
This is what we did for our first attempt, and the image was displaying on the base window. I believe I understand why it is displaying on the base window, it is because we are using that window as the surface, though I'm confused how do I make a user defined rectangle the surface?
We did try using SDL_CreateRGBSurface, though nothing is being displayed on the screen when we do this either.
display_surface = SDL_CreateRGBSurface(0, this->size.width, this->size.height, 1, this->color.red, this->color.green, this->color.blue, this->color.alpha);
Thanks guys!
Please let me know if there is anymore information you need, this is my first time posting and I tried to put all the info that I could think of.
Create a texture from your image surface by using SDL_CreateTextureFromSurface:
SDL_Texture* image_surface = SDL_CreateTextureFromSurface(renderer, temp_image_sur);
(remember to free it with SDL_DestroyTexture)
then use SDL_RenderCopy to draw it:
SDL_RenderCopy(renderer, image_texture, nullptr, &image_rect);
where image_rect is a SDL_Rect and the destination rectangle you want to draw your image to, for example:
SDL_rect image_rect = {10, 10, 200, 200};
To move your image simply change image_rect.x and/or image_rect.y

Draw over Dialog Margins in an MFC Dialog

I want a dialog to be borderless and yet have a dialog shadow. I came across this solution Borderless Window Using Areo Snap, Shadow, Minimize Animation, and Shake which uses a workaround by making the Dialog having a Margin of 1 px and extending the Client Area to it.
MARGINS borderless = { 1, 1, 1, 1 };
DwmExtendFrameInfoClientArea(this->GetSafeHwnd(), &borderless);
The post mentioned that the Client Area is literally being extended and Transparent drawing makes the Dialog edges of 1px each visible again.
Now this is exactly what happened, when I tried to paint a Solid Rectangle onto the whole dialog:
// getting the client area
CRect clientRect;
GetClientRect(&clientRect);
// expanding it to the new margins
clientRect.left -= 1;
clientRect.top -= 1;
clientRect.right += 2;
clientRect.bottom += 2;
// set the Device Context to draw non transparent and with a black background
pDC->SetBkMode(OPAQUE);
pDC->SetBkColor(RGB(0, 0, 0));
// finally draw a rectangle to it
CBrush brush_back_ground(RGB(0, 0, 0));
pDC->FillRect(clientRect, &brush_back_ground);
But the dialog is still drawn with its margins:
How would it be possible to draw something stretched on the margins? Later I'm going to use pictures as dialog Background which should be drawn on the margins aswell.
Thanks to Hans Passant for his comment. The solution is to use GDI+ drawing instead of GDI drawing
// making a Gdi+ graphics object from my CDC*
Graphics g(*pDC);
// getting the client area
CRect clientRect;
GetClientRect(&clientRect);
// making a Gdi+ rect
Rect bkRect(0,0,clientRect.Width(), clientRect.Height());
// making a pen for the Rect Drawing
Pen bkPen(Color(255,0,0,0));
// draw the rectangle over the full dialog
g.DrawRectangle(&bkPen, bkRect);

How to get Windowed Window's Screen coordinate of each Corner in X-window?

Like attached my photo, I want to get "Windowed Window's Screen coordinate of each Corner in X-window". (I draw red dots, which I want to get as Screen coordinates, in the following image. What I am going to do later is to get exact middle point of my OpenGL window in 2D screen coordinate.
I tried following code already:
int* getWindowPos(Display *dpy) {
int winPos[2];
Window myWin;
myWin = XRootWindow(dpy, 0);
XWindowAttributes xwa;
XGetWindowAttributes(dpy, myWin, &xwa);
// printf("%d %d\n", xwa.x, xwa.y);
return winPos;
}
but this "XWindowAttributes" always gives me 0 in x point ,0 in y point, and width 1600 and height 900, which is same as my screen resolution.
following is what I coded to create this windowed window.
GLWin.win = XCreateWindow(GLWin.dpy, RootWindow(GLWin.dpy, vi->screen),
0, 0, 800, 600, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &GLWin.attr);
You're storing your window into GLWin.win, but are querying the root window for its size and location. The "root window" is the full screen background window (desktop), so it makes sense that it's returning your screen resolution. Just pass your actual window (GLWin.win) to XGetAttributes() if you want those dimensions.

Change SDL background

Is there any way to change the color of an empty SDL window to be white instead of black? I don't want to change any default settings. I'm just trying to change it for this particular program that I'm writing. I don't want to use an image file, but if I have to, I will.
I don't know if this matters, but I'm using SDL_SetVideoMode()
My code is very basic:
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
return 1;
SDL_Surface * screen = NULL;
screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE);
SDL_FillRect(screen, NULL, 0xFFFFFF);
SDL_Delay(3000);
You need to call SDL_Flip for your changes to show up.
Acquire the surface from your Window using surf = SDL_SetVideoMode(...) and then do
SDL_FillRect(surf, NULL, 0xFFFFFF); // 0xFFFFFF = white in RGB, NULL = full window
SDL_Flip(surf);
You could use SDL_FillRect to fill the screen/a surface with your desired color.
You need to call SDL_UpdateRect after SDL_FillRect.

Black border around characters when draw Image to a transparent Bitmap

I have to draw a String on a transparent bitmap at first, then draw A to destination canvas.
However on certain case, there is black border around the characters.
Bitmap* tempImg = new Bitmap(1000, 1000, PixelFormat32bppARGB);
Graphics tempGr(tempImg);
tempGr.Clear(Color(0, 255,255,255));
Gdiplus::SolidBrush* brush = new SolidBrush(Color(255, 255, 0, 0 ));
Gdiplus::FontFamily fontFamily(L"Times New Roman");
Gdiplus::Font* font = new Gdiplus::Font(&fontFamily, 19, FontStyleRegular, UnitPixel);
RectF rec(400, 400, 1000, 10000);
tempGr.DrawString(
L"Merry Chrismas",
-1,
font,
rec,
NULL,
brush
);
Graphics desGr(hdc);
desGr.Clear(Color::Gray);
desGr.DrawImage(tempImg , 0,0, 1000, 1000);
The character draw on desGr have black board for some fontsize.
How can I avoid this problem?
Many thanks!
I think the problem here is that you are drawing the text onto a transparent background.
You could try adding this line after the call to tempGr.Clear...
tempGr.TextRenderingHint = TextRenderingHint.AntiAlias;
ps - sorry not sure the exact syntax in C++ ;)
I just solved this problem in XNA:
Clear background to the same as the foreground color. The only difference is that the background should have Alpha=0, and the foreground with Alpha >> 0
The black border comes from blending of your background and foreground of different colors. Try to clear the background to some contrasting color to fully appreciate the phenomenon.