Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing SDL_WINDOW_RESIZABLE when creating the window, instead of calling SDL_SetWindowResizable #6324

Closed
8Observer8 opened this issue Oct 4, 2022 · 3 comments
Labels
abandoned Bug has been abandoned for various reasons

Comments

@8Observer8
Copy link

8Observer8 commented Oct 4, 2022

It works:

    const int winW = 300;
    const int winH = 300;
    window = SDL_CreateWindow("OpenGL, SDL2, C++",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        winW,
        winH,
        SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

But SDL_SetWindowResizable does not work:

resize-opengl-window-sdl2-cpp

#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>

#include <glad/glad.h>
#include <iostream>

SDL_Window *window;
const float maxFPS = 5.f;

void fatalError(const std::string &message)
{
    std::cout << message << std::endl;
    if (window)
    {
        SDL_DestroyWindow(window);
    }
    SDL_Quit();
    exit(-1);
}

int main()
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fatalError("Failed to initialize");
    }
    
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    const int winW = 300;
    const int winH = 300;
    window = SDL_CreateWindow("OpenGL, SDL2, C++",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        winW,
        winH,
        SDL_WINDOW_OPENGL);
    if (!window)
    {
        fatalError("Failed to create the SDL window");
    }
    
    SDL_SetWindowResizable(window, SDL_TRUE);

    SDL_GLContext glContext = SDL_GL_CreateContext(window);
    if (!glContext)
    {
        fatalError("Failed to create the SDL_GL context");
    }

    if (!gladLoadGL())
    {
        fatalError("Failed to initialize the GLAD library");
    }

    glViewport(0, 0, winW, winH);
    glClearColor(0.5f, 0.5f, 1.f, 1.f);
    
    SDL_Event event;
    bool running = true;
    while (running)
    {
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_WINDOWEVENT:
                switch (event.window.event)
                {
                case SDL_WINDOWEVENT_RESIZED:
                    glViewport(0, 0, event.window.data1, event.window.data2);
                    break;
                }
                break;
            }
        }
        float startTicks = SDL_GetTicks();

        glClear(GL_COLOR_BUFFER_BIT);

        SDL_GL_SwapWindow(window);

        // Limit FPS to the max FPS
        float frameTicks = SDL_GetTicks() - startTicks;
        if (1000.f / maxFPS > frameTicks)
        {
            SDL_Delay(1000.f / maxFPS - frameTicks);
        }
    }

    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
@slouken
Copy link
Collaborator

slouken commented Oct 5, 2022

That's odd, I copied your program and removed the glad OpenGL code (since I don't have that installed) and it worked for me:

#include <stdlib.h>
#include <SDL.h>
#include "SDL_opengl.h"

SDL_Window *window;
const float maxFPS = 5.f;

void
fatalError(const char *message)
{
    SDL_Log("%s", message);
    if (window) {
        SDL_DestroyWindow(window);
    }
    SDL_Quit();
    exit(-1);
}

int
main()
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        fatalError("Failed to initialize");
    }

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    const int winW = 300;
    const int winH = 300;
    window = SDL_CreateWindow("OpenGL, SDL2, C++",
                              SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED,
                              winW,
                              winH,
                              SDL_WINDOW_OPENGL);
    if (!window) {
        fatalError("Failed to create the SDL window");
    }

    SDL_SetWindowResizable(window, SDL_TRUE);

    SDL_GLContext glContext = SDL_GL_CreateContext(window);
    if (!glContext) {
        fatalError("Failed to create the SDL_GL context");
    }

    glViewport(0, 0, winW, winH);
    glClearColor(0.5f, 0.5f, 1.f, 1.f);

    SDL_Event event;
    int running = 1;
    while (running) {
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_QUIT:
                running = 0;
                break;
            case SDL_WINDOWEVENT:
                switch (event.window.event) {
                case SDL_WINDOWEVENT_RESIZED:
                    glViewport(0, 0, event.window.data1, event.window.data2);
                    break;
                }
                break;
            }
        }
        float startTicks = SDL_GetTicks();

        glClear(GL_COLOR_BUFFER_BIT);

        SDL_GL_SwapWindow(window);

        // Limit FPS to the max FPS
        float frameTicks = SDL_GetTicks() - startTicks;
        if (1000.f / maxFPS > frameTicks) {
            SDL_Delay(1000.f / maxFPS - frameTicks);
        }
    }

    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

@8Observer8
Copy link
Author

8Observer8 commented Oct 5, 2022

HolyBlackCat wrote here:

It appears to be a SDL bug, though for me the effect was different (the window wasn't resizable at all).

Instead of calling SDL_SetWindowResizable(window, SDL_TRUE);, pass SDL_WINDOW_RESIZABLE when creating the window.

Whoever reads this, please, test it on your computer too. It would be nice if my example tested at least 2-3 people. I use Windows 10 64-bit.

@slouken slouken added the abandoned Bug has been abandoned for various reasons label Nov 7, 2023
@slouken
Copy link
Collaborator

slouken commented Nov 7, 2023

SDL 2.0 is now in maintenance mode, and all inactive issues are being closed. If this issue is impacting you, please feel free to reopen it with additional information.

@slouken slouken closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
abandoned Bug has been abandoned for various reasons
Projects
None yet
Development

No branches or pull requests

2 participants