After SDL_SetWindowSize resizes, drawing is done offset down the screen

无人久伴 提交于 2019-12-11 01:57:22

问题


I'm using Ubuntu 19.0.4 with SDL version 2.0.9+dfsg1-1ubuntu1. The code works fine with Visual Studio and MinGW's g++, and if I read others' comments right, other versions of Unix. But with Ubuntu, my version, it shows the rectangle drawn ~200 pixels down the screen (should be at 0, 0).

There's a commented-out section that seems to fix it by delaying, but a) that's weird and b) I don't entirely trust it. Am I doing something wrong? Surely there's a fix that makes sense.

#include <SDL.h>
#include <stdio.h>

using namespace std;

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;

    //Have to do this, or later SDL_GetWindowSize may give wrong results:
    // the earlier dimensions, not the ones set by SDL_SetWindowSize
    SDL_Event event; while (SDL_PollEvent(&event));  

    SDL_SetWindowSize(window, 600, 300);
    printf("SetWindowSize\n");

    //The next 3 lines, uncommented, fix the problem, usually
    //SDL_RenderPresent(renderer);
    //for (int i = 0; i < 6; ++i) //Must be 6 or more to be reliable
    //  SDL_Delay(10);          //10 works; 1 doesn't

    int w, h; SDL_GetWindowSize (window, &w, &h);
    printf("%dx%d\n", w, h);

    //Now let's draw something and see if it shows up in the right place. Nope -- it's offset down the screen 100 pixels, ish
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    SDL_Rect rect1 = {0, 0, 100, 100};
    SDL_RenderFillRect(renderer, &rect1);
    SDL_RenderPresent (renderer);

    //Wait for key hit, so we can see rect
    SDL_Event sdlEvent; 
    do
        SDL_WaitEvent (&sdlEvent);
    while (sdlEvent.type != SDL_KEYDOWN);

    return 0;
}

来源:https://stackoverflow.com/questions/57193038/after-sdl-setwindowsize-resizes-drawing-is-done-offset-down-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!