SDL: Get window height/width/rect? - sdl

in previously versions of SDL I was able to get the height and width of my problem using the main surface, which had a clip_rect member. Since 2.0 I am using SDL_Renderer & SDL_Window.
How do I get the window size or even better the rect of my current program?

To get the height and width of the window:
void SDL_GetWindowSize(SDL_Window* window,int* w,int* h)
You can look here for more functions for whatever you want that deals with windows. https://wiki.libsdl.org/CategoryVideo

If you're just looking for width and height and anything else you'd normally find within an SDL_Surface this works just as well, if not better:
Width:
SDL_GetWindowSurface(m_window)->w;
Height:
SDL_GetWindowSurface(m_window)->h;

Related

How to resize an image using SDL2

I have been looking around on many links to find out how to resize an image on SDL2, like in pygame, the "pygame.transform.scale()" function, and I have tried to look to see how to implement one but all of the links are unique to the question or are very unclear to me. How can I implement a function that returns a surface/texture, whatever is used for SDL_RenderCopy() of a newly-resized image, so when I want to use the rectangle it has, it's rectangle is now resized to what i tell it to do. As in:
SDL_Surface/SDL_Texture ResizeImage(int width, int height){
//Process code here to effectively do the equivalent in python of:
newSurface/Texture = pygame.transform.scale(img, width, height);
return newSurface/Texture;
}

How to determine size of the drawable area of an OpenGL 3.3 window using GLFW

I'm trying to create an game / application using GLFW and OpenGL 3.3. I'd like to be able to detect collision with the sides of the window, but it seems that the drawable area of the window differs from the size of the window set using glfwCreateWindow().
So my question is, how do I get that drawable area, ie. the size of the window minus the border? I'd rather not have to use the WinAPI so as to make it more cross-platform, and glfwGetWindowFrameSize() is in GLFW 3.1, which isn't completed yet.
Edit: My question makes it seem like I need to use GLFW do accomplish this, which isn't true. I just wanted to note that I'm using GLFW as a window / input handler.
You want glfwGetFramebufferSize.
glfwGetVideoMode returns the video mode of the specified monitor, not the size of your window. For fullscreen windows, they happen to be the same, but for other windows they are likely to be very different.
From the looks of it, you do not need to know the size of the window, I'm assuming in pixels? If you want to do collision detection with the border of the window, you just need to detect the the NDC of your vertex, and once it reaches x or y = (-1, 1) then you would've had a collision. Nonetheless, if you want to get the size in pixels of your OpenGL context then use glfwGetVideoMode().

Resizing image with SDL_gfx at specific size C++

I am using SDL_gfx trying to resize an image but i can't really get it done.
The case is that I have an :
SDL_Surface* screen = SDL_SetVideoMode(0,0,32,SDL_FULLSCREEN);
and I also have a:
SDL_Surface* back_img = SDL_Load("back.jpg");
As you can see, I set the videomode to full screen, so the size of the screen surface will vary from pc to pc. I want to find a way to make back_img FIT on screen but i have two major problems.
screen->w and screen->h return a false value!
I can't resize the image to the specific size I want.
Any help?
Your screen->w values return 0 because you set them to zero with SDL_SetVideoMode(0,0,...
Set them to some proper values like SDL_SetVideoMode(800,600,32,SDL_FULLSCREEN); you can change them later by calling the function again with different parametes.
Note; setting SDL fullscreen will automatically resize and stretch the image to fit the screen
you can use SDL_VideoInfo to get the current resolution. See the code below:
const SDL_VideoInfo *info = SDL_GetVideoInfo();
SDL_Surface *screen = SDL_SetVideoMode( info->current_w, info->current_h, 32, SDL_FULLSCREEN );
Now your screen's resolution will match every PC's resolution.

Qt Screen Resolution Splash Screen

I have a splash screen image that I display with splash.showFullScreen() but it doesn't re size it to the screen resolution so it either comes out tiled or to large depending on the display. I have tried everything I can think of but nothing works. This might sound like a stupid question which it probably is but I can't find the answer so if any can just help me with this? If it makes a difference I use a QPixmap named pixmap for the splash image. By the way I want the image to be stretched to the screen resolution.
You should scale the pixmap to the size of the screen with QPixmap::scaled(). You can get the screen resolution by calling QDesktopWidget::screenGeometry(). The desktop widget can be obtained by QApplication::desktop().
You can try something like this:
QDesktopWidget* desktopWidget = qApp->desktop();
QRect screenGeometry = desktopWidget->screenGeometry();
int screenWidth = screenGeometry.width();
int screenHeight = screenGeometry.height();
QPixmap pixmapForSplash = yourPixmap.scaled(screenWidth, screenHeight);
QSplashScreen splashScreen(pixmapForSplash);
(I'm sorry, I can not check this, because I do not have a development environment on this computer... I hope it is correct.)
I think you should call resize() method for your splash screen by the size of the available desktop geometry that you can get using QDesktopWidget::availableGeometry method. The QApplication::desktop() function is used to get an instance of QDesktopWidget.
slpashScreen.resize(QApplication::desktop()->avaiableGeometry().size());
If you use a QLabel to display the image, make sure the label is in a layout that will cause it to fill the entire parent widget and set the label to scale its contents using setScaledContents(true).

Create Square Window C++

Stuck on a little fiddly problem. I'm creating a GUI in C++ using XP and VS C++ using the command CreateWindow().
My question is, how do I make the inside paintable region a perfect square. When passing in the size of the window to create, some of this is deducted for the menu bar at the top, border all around etc. Are there any real time variables I can pass in, e.g. to create a 500x500 window would be:
...500+BORDER,500+MENU_TOP+BORDER...
Thanks everyone
The way I usually do it is with AdjustWindowRect. I find it simpler than the other suggested methods (which should work just as well, it's your choice). Use it as such:
RECT rect = {0, 0, desiredWidth, desiredHeight};
AdjustWindowRect(&rect, windowStyle, hasMenu);
const int realWidth = rect.right - rect.left;
const int realHeight = rect.bottom - rect.top;
And pass realWidth & realHeight to CreateWindow.
The function will, as its name suggests, adjust the window according to your window style and menu use, so that the client region matches your desired size.
you can find all the relevant size (windows framewidth, menubar height, etc) here: GetSystemMetrics(). Using these values you should be able to create a perfect square window
You can get all the UI metrics from the GetSystemMetrics() API call.
For example, the menu will be SM_CXMENU and SM_CYMENU.