glfw window with no title bar - c++

I am trying to make a way to toggle my window between windowed mode and fullscreen mode. I had done it successfully except for one problem. The title bar is not working! You can’t move the window either. Without this piece of code everything works just fine.
setFullscreen method:
void Window::setFullscreen(bool fullscreen)
{
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
if (fullscreen) {
glfwSetWindowMonitor(m_window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
glViewport(0, 0, mode->width, mode->height);
}
if (!fullscreen) {
glfwSetWindowMonitor(m_window, nullptr, 0, 0, m_width, m_height, GLFW_DONT_CARE);
glViewport(0, 0, m_width, m_height);
}
}
The result of the code:

#tomasantunes help me figure this out.
in setFullscreen I am setting the window to be at 0, 0 or the top left of the screen. The title bar didn't actually disappear is was just off screen. so if I set the window to be at 100, 100 instead I get the title bar back. This was pretty dumb of me to make a stupid mistake like this.
if (!fullscreen) {
glfwSetWindowMonitor(m_window, nullptr, 0, 0, m_width, m_height, GLFW_DONT_CARE); // I set the position to 0,0 in the 3rd and 4th parameter
glViewport(0, 0, m_width, m_height);
}
Fixed code:
if (!fullscreen) {
glfwSetWindowMonitor(m_window, nullptr, 100, 100, m_width, m_height, GLFW_DONT_CARE); // I set the position to 100, 100
glViewport(0, 0, m_width, m_height);
}
New result:

Not sure if you are still looking for an answer, however, the code I have here fixes that same problem you were initially having.
Before changing to full-screen mode, save the window position and size.
int xPos, yPos, width, height; //have somewhere stored out of function scope.
glfwGetWindowPos(windowPtr, &xPos, &yPos);
glfwGetWindowSize(windowPtr, &width, &height);
and then when changing from full-screen back to windowed mode, apply the saved positions and size properties.
glfwSetWindowMonitor(windowPtr, nullptr, xPos, yPos, width, height, 0); //Refresh rate is ignored without an active monitor.

Related

Cairo: How to clip text to a rect?

Using Cairo under C++ on a Raspberry Pi, and trying to clip text drawing to inside a given rectangle.
I'd have thought that it would be as simple as this:
cairo_t* cp = cairo_create(psurface);
// set font, etc
cairo_rectangle(cp, 0, 0, 100, 100); // Desired clipping rect
cairo_clip(cp);
cairo_show_text(cp, "pretend that this string is > 100px wide");
cairo_destroy(cp);
but it always causes no text to appear. If I omit the call to cairo_clip() the text does appear (albeit unclipped).
I'm wanting only the last few chars of the string to get clipped.
What's the trick?
Works for me.
#include <cairo.h>
int main()
{
cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 150, 50);
cairo_t *cr = cairo_create(s);
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_paint(cr);
cairo_rectangle(cr, 0, 0, 100, 100);
cairo_clip(cr);
cairo_move_to(cr, 50, 25);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_show_text(cr, "pretend that this string is > 100px wide");
cairo_destroy(cr);
cairo_surface_write_to_png(s, "out.png");
cairo_surface_destroy(s);
return 0;
}

X11 Window/GLXWindow attributes to GLFWwindow

Im created GLX window, that contains CWOverrideRedirect attribute, and destroyed input shape, so it looks like OpenGL overlay. Now I am wanted to draw a 2d text or use ImGUI. A lot of examples use GLFW library. So, the main problem is to make GLFWwindow transparency and without input shape.
int attr_mask =
CWOverrideRedirect| //borderless, always on top
CWColormap|
CWBorderPixel|
CWEventMask;
window_handle = XCreateWindow( Xdisplay,
Xroot,
x, y, width, height,
0,
visual->depth,
InputOutput,
visual->visual,
attr_mask, &attr);
..............
// Destroy input shape
XRectangle rect;
XserverRegion region = XFixesCreateRegion(Xdisplay, &rect, 1);
XFixesSetWindowShapeRegion(Xdisplay, window_handle, ShapeInput, 0, 0, region);
XFixesDestroyRegion(Xdisplay, region);
if ((del_atom = XInternAtom(Xdisplay, "WM_DELETE_WINDOW", 0)) != None) {
XSetWMProtocols(Xdisplay, window_handle, &del_atom, 1);
}
From ImGUI opengl3 example
GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui OpenGL3 example", NULL, NULL);
glfwMakeContextCurrent(window);
gl3wInit();

How to show a PNG image with 25% opacity using GDI+? (MFC)

I am trying to output a PNG image by using GDI+, MFC. I want to output it with 25% opacity. Below is the way to output a PNG image on x=10, y=10:
CDC *pDC =GetDC();
Graphics graphics(pDC->m_hDC);
Image image(L"test1.png", FALSE);
graphics.DrawImage(&image, 10, 10);
But I don't know how to make it translucent. Any idea?
To draw the image with alpha blending, declare Gdiplus::ImageAttributes and Gdiplus::ColorMatrix with required alpha channel:
float alpha = 0.25f;
Gdiplus::ColorMatrix matrix =
{
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, alpha, 0,
0, 0, 0, 0, 1
};
Gdiplus::ImageAttributes attrib;
attrib.SetColorMatrix(&matrix);
graphics.DrawImage(&image,
Gdiplus::Rect(10, 10, image.GetWidth(), image.GetHeight()),
0, 0, image.GetWidth(), image.GetHeight(), Gdiplus::UnitPixel, &attrib);
See also: Using a Color Matrix to Transform a Single Color
Note that GetDC() is usually not used in MFC. If you do use it, be sure to call ReleaseDC(pDC) when pDC is no longer needed. Or simply use CClientDC dc(this) which has automatic cleanup. If painting is done in OnPaint then use CPaintDC which also has automatic cleanup:
void CMyWnd::OnPaint()
{
CPaintDC dc(this);
Gdiplus::Graphics graphics(dc);
...
}

Invalid conversion from int to SDL_RendererFlip

I'm trying to flip an image both horizontally and vertically. It doesn't seem to be working though:
SDL_RenderCopyEx(renderer, ResourceManager::GetInstance().getTexture("rpg_pack"), &wood_corner, &rect, 0, 0, SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
I'm running SDL, image, mixer, ttf and net.
Try
SDL_RendererFlip flip = (SDL_RendererFlip)(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
SDL_RenderCopyEx(renderer, ResourceManager::GetInstance().getTexture("rpg_pack"), &wood_corner, &rect, 0, 0, flip);

Use a sprite C++ MFC

I need to display an image from a sprite using MFC. A search on google led me to this link which led me to this code :
//This code is in the OnPaint function
//img is a CImage, declared as a class member
img.Load(_T("icon-sprite.png"));
HDC imgDc = img.GetDC();
int height = 24;
int width = 24;
//Sprite Icon is a CStatic
CDC* spriteDc = spriteIcon.GetDC();
HDC spriteHdc = spriteDc->GetSafeHdc();
CClientDC pDC(this);
HDC hdcWindow = pDC->GetSafeHdc();
//img.StretchBlt(imgDc, 0, 0, 600, 203, SRCCOPY);
//img.BitBlt(imgDc, width, height, 600, 203, 0, 0, SRCAND);
//img.BitBlt(imgDc, width, height, 640, 480, 0, 0, SRCPAINT);
StretchBlt(imgDc, 0, 0, 600, 203,
imgDc, 0, 0, 200, 203, SRCCOPY);
BitBlt(imgDc, width, height, 600, 203,
imgDc, 0, 0, SRCAND);
BitBlt(imgDc, width, height, 640, 480,
imgDc, 0, 0, SRCPAINT);
spriteIcon.SetBitmap((HBITMAP)img);
With this code spriteIcon only display a rectangle of the size of the sprite image.
What did I do wrong ?
Most probably CImage is local and going out of scope, thus the image (and HBITMAP) is invalid. You have two options:
Declare CImage such that it would exist even after this function returns.
Use CImage::Detach which returns a HBITMAP and relinquishes its ownership with handle (i.e. won't delete on destructor).
Please ensure that given image exists and is loaded properly.
Also, you don't need to new CClientDC, you can have it on stack.