0

This is my code, it compiles fine without any errors or warnings, and the 2 window opens fine, except can't close it cause alt+F4 doesn't work anymore(nor does the X icon)... If I comment out W2 in main, it works again, with a single window... Window::Init() seems to break it, and I can't figure out why.

#include <iostream>

#include <SDL2/SDL.h>

using namespace std;

bool Run = true;
uint8_t DisplayID[2] = {1, 0};
bool UseGPU = true;

class Window
{
    private:
        SDL_Rect ScreenRes;
        SDL_Window *W;
        SDL_Renderer *Renderer;
    public:
        Window () {};
        bool Init (uint8_t DisplayID, bool Focus = false);
        void Refresh ();
        void Destroy ();
    
};

bool Window::Init (uint8_t DisplayID, bool Focus)
{
    if (DisplayID >= SDL_GetNumVideoDisplays ())
    {
        cout << "Fatal: DisplayID >= Number of displays" << "\n";
        return 1;
    }
    if (SDL_GetDisplayBounds(DisplayID, &ScreenRes) != 0)
    {
        cout << SDL_GetError () << "\n";
        return 1;
    }
    else
    {
        uint32_t Flags;
        if (Focus)
            Flags = SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS; // SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI
        else
            Flags = SDL_WINDOW_ALWAYS_ON_TOP; // SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI
        W = SDL_CreateWindow("Experiment3", ScreenRes.x, ScreenRes.y, ScreenRes.w, ScreenRes.h, Flags);
        if (W == NULL)
        {
            cout << SDL_GetError () << "\n";
            return 1;
        }
        else
        {
            if (UseGPU)
                Renderer = SDL_CreateRenderer (W, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);
            return 0;
        }
    }
}

void Window::Refresh ()
{
    if (UseGPU)
    {
        SDL_SetRenderDrawColor (Renderer, 40, 0, 0, 255);
        SDL_RenderClear (Renderer);
        SDL_RenderPresent (Renderer);
    }
    else
    {
        SDL_Surface *WindowSurface = SDL_GetWindowSurface(W);
        SDL_FillRect (WindowSurface, NULL, SDL_MapRGB (WindowSurface->format, 0, 0, 40));
        SDL_UpdateWindowSurface (W);
        SDL_FreeSurface (WindowSurface);
    }
}

void Window::Destroy ()
{
    if (UseGPU)
        SDL_DestroyRenderer(Renderer);
    SDL_DestroyWindow(W);
}





int main (int argc, char *argv[])
{
    if (SDL_Init (SDL_INIT_TIMER) != 0)
        cout << SDL_GetError () << "\n";
    if (SDL_Init (SDL_INIT_AUDIO) != 0)
        cout << SDL_GetError () << "\n";
    if (SDL_Init (SDL_INIT_VIDEO) != 0)
        cout << SDL_GetError () << "\n";
    if (SDL_Init (SDL_INIT_EVENTS) != 0)
        cout << SDL_GetError () << "\n";
    
    Window W1 = Window ();
    Window W2 = Window ();
    
    if (!W1.Init (DisplayID[0]) && !W2.Init (DisplayID[1]))
    {
        
        SDL_DisableScreenSaver();
        SDL_Event Event;
        
        while (Run)
        {
            while (SDL_PollEvent (&Event))
            {
                if (Event.type == SDL_QUIT)
                    Run = false;
            }
            
            W1.Refresh ();
            W2.Refresh ();
            
            SDL_Delay (1000 / 60);
        }
        
        W1.Destroy ();
        W2.Destroy ();
    }
    
    SDL_Quit ();
}

I've tried specifying SDL_WINDOW_INPUT_FOCUS and SDL_WINDOW_MOUSE_FOCUS for neither, both, and both of the windows 1 by 1, to no effect. I've tried CPU and GPU rendering the windows, also doesn't change anything. Searched for solution, but couldn't find anything useful on this issue.

Tried it on both Ubuntu 20.10 with SDL 2.0.14 and on Ubuntu 21.04 with SLD 2.0.16 both does the same.

Does anyone know how to solve this?

  • 2
    You found [this](https://stackoverflow.com/a/57762646/15033741) already? – timemage Sep 19 '21 at 03:30
  • Answer linked by @timemage is correct except for one small bit. Documentation says "*An SDL_QUIT event is generated when the user clicks on the close button of the last existing window. This happens in addition to the SDL_WINDOWEVENT/SDL_WINDOWEVENT_CLOSE event, so the application can check whichever is appropriate, or both, or neither*" – keltar Sep 19 '21 at 07:04
  • Ok, guys, I'll try this in a minute! :) Ty! – RPBCACUEAIIBH Sep 19 '21 at 20:22
  • I checked and it works. Ty again! – RPBCACUEAIIBH Sep 19 '21 at 20:40

0 Answers0