Ejemplo n.º 1
0
void SDLAppDisplay::init(std::string window_title, int width, int height, bool fullscreen) {

    if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO) != 0) {
        throw SDLInitException(SDL_GetError());
    }

    const SDL_VideoInfo* display_info = SDL_GetVideoInfo();

    //save the desktop resolution
    desktop_width  = display_info->current_w;
    desktop_height = display_info->current_h;
      
    atexit(SDL_Quit);

    SDL_EnableUNICODE(1);

#if SDL_VERSION_ATLEAST(1,3,0)
#else
    SDL_WM_SetCaption(window_title.c_str(),0);
#endif

    setVideoMode(width, height, fullscreen);

    //get actual opengl viewport
    GLint viewport[4];
    glGetIntegerv( GL_VIEWPORT, viewport );

    this->width      = viewport[2];
    this->height     = viewport[3];
    this->fullscreen = fullscreen;
}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
0
void SDLAppDisplay::setupExtensions() {

    GLenum err = glewInit();

    if (GLEW_OK != err) {
        /* Problem: glewInit failed, something is seriously wrong. */
        char glewerr[1024];
        snprintf(glewerr, 1024, "GLEW Error: %s", glewGetErrorString(err));

        throw SDLInitException(std::string(glewerr));
    }
}
Ejemplo n.º 4
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();
}