Example #1
0
void SDLAppDisplay::init(std::string window_title, int width, int height, bool fullscreen) {

    this->width  = width;
    this->height = height;

    this->fullscreen = fullscreen;

    int flags = SDLFlags(fullscreen);

    SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
    atexit(SDL_Quit);

    //vsync
    if(vsync) SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
    else SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);

    if(multi_sample > 0) {
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, (GLuint) multi_sample);
    }

    if(enable_alpha) {
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    }

#ifdef _WIN32
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    surface = SDL_SetVideoMode(width, height, 32, flags);
#elif _RPI
    surface = SDL_SetVideoMode(0, 0, 0, SDL_SWSURFACE | SDL_FULLSCREEN);
#else
    if(enable_shaders) {
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
        surface = SDL_SetVideoMode(width, height, 32, flags);
    } else {
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
        surface = SDL_SetVideoMode(width, height, 24, flags);
    }
#endif

    if (!surface) {
        std::string sdlerr(SDL_GetError());
        throw SDLInitException(sdlerr);
    }

#ifdef SDLAPP_SHADER_SUPPORT
    if(enable_shaders) {
        setupARBExtensions();
    }
#endif

    SDL_WM_SetCaption(window_title.c_str(),0);
}
Example #2
0
void SDLAppDisplay::toggleFullscreen() {

    int width  = this->width;
    int height = this->height;

    if(!fullscreen) {

        //save windowed width and height
        windowed_width  = width;
        windowed_height = height;

        int fullscreen_width  = desktop_width;
        int fullscreen_height = desktop_height;
            
        float aspect_ratio = fullscreen_width / (float) fullscreen_height;
        
        // if the aspect ratio suggests the person is using multiple monitors
        // find a supported resolution with a lower aspect ratio with the same
        // fullscreen height

#if SDL_VERSION_ATLEAST(1,3,0)
        // TODO: do something with the 1.3 API here
#else
        if(aspect_ratio >= 2.5) {
        
            SDL_Rect** modes = SDL_ListModes(0, SDLFlags(true));
        
            if(modes != (SDL_Rect**)0 && modes != (SDL_Rect**)-1) {
            
                for (int i=0; modes[i]; i++) {
                    if(modes[i]->h == fullscreen_height && (modes[i]->w/(float)modes[i]->h) < 2.5) {
                        fullscreen_width = modes[i]->w;
                        break;
                    }
                }
            }
        }
#endif

        width  = fullscreen_width;
        height = fullscreen_height;
        
    } else {
        //switch back to window dimensions, if known
        if(windowed_width != 0) {
            width  = windowed_width;
            height = windowed_height;
        }
    }

    fullscreen = !fullscreen;

    setVideoMode(width, height, fullscreen);

    int resized_width, resized_height;

#if SDL_VERSION_ATLEAST(1,3,0)
    SDL_GetWindowSize(sdl_window, &resized_width, &resized_height);
#else
    const SDL_VideoInfo* display_info = SDL_GetVideoInfo();

    resized_width  = display_info->current_w;
    resized_height = display_info->current_h;
#endif

    //set viewport to match what we ended up on
    glViewport(0, 0, resized_width, resized_height);

    this->width  = resized_width;
    this->height = resized_height;
}
Example #3
0
void SDLAppDisplay::setVideoMode(int width, int height, bool fullscreen) {

    int gl_depth     = 24;
    int colour_depth = 32;
#ifndef _WIN32
    if(!enable_shaders) {
        gl_depth     = 16;
        colour_depth = 24;
    }
#endif

    bool no_cursor = false;

#ifdef SDLAPP_XWINDOWS

    /* check for window_id in ENV */
    if(xwindow == 0) {
        char* xscreensaver_window_env = getenv("XSCREENSAVER_WINDOW");

        if(xscreensaver_window_env != 0) {
            //parse xscreensaver window id
            sscanf(xscreensaver_window_env, "0x%lx", &xwindow);
            if (!xwindow) sscanf(xscreensaver_window_env, "%lu", &xwindow);
            if (!xwindow) throw SDLInitException("Invalid window");
        }
    }

    if(xwindow != 0) {
        char sdl_window_env[100];
        snprintf(sdl_window_env, 100, "SDL_WINDOWID=%d", xwindow);
        putenv(sdl_window_env);


        Display* dpy = XOpenDisplay(0);

        if(!dpy) {
            throw SDLInitException("Cannot get display");
        }

        XWindowAttributes win_attributes;

        if (!XGetWindowAttributes(dpy, xwindow, &win_attributes)) {
            throw SDLInitException("Cannot get window attributes");
        }

        width  = win_attributes.width;
        height = win_attributes.height;

        colour_depth = win_attributes.depth;

        screensaver = true;
        fullscreen  = false;

        // assume window opened at y=0 by xscreensaver is actually running as a screensaver and not a preview
        if(win_attributes.y == 0) no_cursor = true;
    }
#endif

#if SDL_VERSION_ATLEAST(1,3,0)

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
    if(resizable && !fullscreen) flags |= SDL_WINDOW_RESIZABLE;
    if(fullscreen) flags |= SDL_WINDOW_FULLSCREEN;

    if(gl_context != 0) SDL_GL_DeleteContext(gl_context);
    if(sdl_window != 0) SDL_DestroyWindow(sdl_window);

    sdl_window = SDL_CreateWindow(
	gSDLAppTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        width, height, flags);


    if (!sdl_window) {
        std::string sdlerr(SDL_GetError());
        throw SDLInitException(sdlerr);
    }

    gl_context = SDL_GL_CreateContext(sdl_window);

    if(vsync) SDL_GL_SetSwapInterval(1);
#else
    int depth = 32;

    int flags = SDLFlags(fullscreen);

    if(no_cursor) SDL_ShowCursor(false);
    if(vsync) SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
    else SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);

    if(multi_sample > 0) {
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, (GLuint) multi_sample);
    }

    if(enable_alpha) {
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    }

#ifdef _WIN32
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    surface = SDL_SetVideoMode(width, height, depth, flags);
#else
    if(enable_shaders) {
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
        surface = SDL_SetVideoMode(width, height, depth, flags);
    } else {
        depth = 24;
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
        surface = SDL_SetVideoMode(width, height, depth, flags);
    }
#endif
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, gl_depth);
    surface = SDL_SetVideoMode(width, height, colour_depth, flags);

    if (!surface) {
        if (multi_sample > 0) {
#ifndef _WIN32
            // Retry without multi-sampling before failing
            std::cerr << "Failed to set video mode: " << SDL_GetError() << std::endl
                      << "Trying again without multi-sampling" << std::endl;
#endif
            multi_sample = 0;
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
            surface = SDL_SetVideoMode(width, height, depth, flags);
        }

        if (!surface) {
            std::string sdlerr(SDL_GetError());
            throw SDLInitException(sdlerr);
        }
    }
#endif

    setupExtensions();
}