问题
I can't use SDL_GetWindowSize
cross-platform, because in Unix it doesn't know that the window size changed (though I can see it did). Visual Studio and MinGW have no problem. (I keep running into cases where Unix is an outlier, but it's always been that I was doing something wrong and the other 2 platforms didn't notice.)
I was running SDL2's version 2.0.8+dfsg1-4ubuntu on 64-bit Ubuntu 4.18.0 on VirtualBox; have since upgraded to SDL2 2.0.9+dfsg1-1ubuntu1 on Ubuntu 5.0.0, but behavior is the same. It's using GNOME Shell with X11.
The interaction with SDL_PollEvent
is strange. But I do think it's possible and reasonable to call it sometimes!
So how can I get SDL_GetWindowSize
to accurately report the size?
MCVE:
#include <cassert>
#include <SDL.h>
using namespace std;
int main (int argc, char** argv)
{
//set things up
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return -1;
SDL_Window* window = SDL_CreateWindow("",0, 0,640, 480,0);
if (!window) return -1;
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) return -1;
//I need to be able to resize at times, so this is in the MCVE
SDL_SetWindowSize(window, 600, 100);
//Without this line, no problem
SDL_PollEvent(nullptr);
//Assertions fail -- in Unix, not Visual Studio
int w, h; SDL_GetWindowSize (window, &w, &h);
assert (w == 600);
assert (h == 100);
return 0;
}
EDIT: trying a new test at commenter's suggestion. It does resolve the problem with SDL_GetWindowSize
reporting the wrong dimensions.
#include <cassert>
#include <SDL.h>
#include <stdio.h>
using namespace std;
static void flush_event_queue(void) {
SDL_Event ev;
while(SDL_PollEvent(&ev)) {
printf("ev.type=%d\n", ev.type);
if(ev.type == SDL_WINDOWEVENT) {
printf("window event %d\n", ev.window.event);
if(ev.window.event == SDL_WINDOWEVENT_RESIZED || ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
printf("resized %d %dx%d\n", ev.window.event, ev.window.data1, ev.window.data2);
}
}
}
}
int main (int argc, char** argv) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return -1;
SDL_Window* window = SDL_CreateWindow("",0, 0,640, 480,0);
if (!window) return -1;
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) return -1;
flush_event_queue();
SDL_SetWindowSize(window, 600, 100);
printf("SetWindowSize\n");
flush_event_queue();
int w, h; SDL_GetWindowSize (window, &w, &h);
printf("%dx%d\n", w, h);
assert (w == 600);
assert (h == 100);
//Now let's draw something and see if it shows up. No--
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect rect1 = {0, 0, 100, 100};
SDL_RenderFillRect(renderer, &rect1);
SDL_RenderPresent (renderer);
SDL_Event sdlEvent; //Wait for key hit, so we can see rect
do
SDL_WaitEvent (&sdlEvent);
while (sdlEvent.type != SDL_KEYDOWN);
return 0;
}
The resulting output is
ev.type=4352
ev.type=4352
ev.type=512
window event 1
ev.type=512
window event 2
ev.type=512
window event 1
ev.type=512
window event 4
ev.type=512
window event 3
ev.type=512
window event 12
ev.type=512
window event 15
ev.type=770
SetWindowSize
ev.type=512
window event 6
resized 6 600x100
600x100
If I comment out that first flush_event_queue()
call in the new test, I don't ever get to the output -- it crashes on my assert
s.
来源:https://stackoverflow.com/questions/57051494/sdl-setwindowsize-resizes-window-but-sdl-getwindowsize-reports-old-size-in-u