示例#1
0
文件: empic.c 项目: afainer/empic
static void destroy_window()
{
  if (SDL_GL_GetCurrentContext())
    SDL_GL_DeleteContext(SDL_GL_GetCurrentContext());

  if (empic_window)
    SDL_DestroyWindow(empic_window);
}
示例#2
0
const std::shared_ptr<oglx_texture_t>& TextureManager::getTexture(const std::string &filePath)
{
    //Not loaded yet?
    auto result = _textureCache.find(filePath);
    if(result == _textureCache.end()) {

        if(SDL_GL_GetCurrentContext() != nullptr) {
            //We are the main OpenGL context thread so we can load textures
            std::shared_ptr<oglx_texture_t> loadTexture = std::make_shared<oglx_texture_t>();
            ego_texture_load_vfs(loadTexture.get(), filePath.c_str(), TRANSCOLOR);
            _textureCache[filePath] = loadTexture;
        }
        else {
            //We cannot load textures, wait blocking for main thread to load it for us
            std::unique_lock<std::mutex> lock(_deferredLoadingMutex);
            _requestedLoadDeferredTextures.push_front(filePath);
            log_debug("Wait for deferred texture: %s\n", filePath.c_str());
            _notifyDeferredLoadingComplete.wait(lock, [this]{return _requestedLoadDeferredTextures.empty();});
        }

        return _textureCache[filePath];
    }

    //Get cached texture
    else {
        return (*result).second;
    }
}
示例#3
0
void initWindow(std::string name, int w, int h, int wflags, int rflags){
	init();
	SDL_SetHint(SDL_HINT_RENDER_OPENGL_SHADERS, "1");
	SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
	GRAPHICS::window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, wflags | SDL_WINDOW_OPENGL);
    if (GRAPHICS::window == NULL) {std::cout << SDL_GetError() << std::endl;}
	GRAPHICS::renderer = SDL_CreateRenderer(GRAPHICS::window, -1, rflags | SDL_RENDERER_TARGETTEXTURE);
    if (GRAPHICS::renderer == NULL) {std::cout << SDL_GetError() << std::endl;}
	GRAPHICS::glcontext = SDL_GL_CreateContext(GRAPHICS::window);
	GRAPHICS::glcontext = SDL_GL_GetCurrentContext();

	GRAPHICS::setColor(255,255,255,255);
	GRAPHICS::setBackgroundColor(0,0,0,255);

	setViewport(w, h);

	//yay workarounds
	SDL_SetWindowSize(GRAPHICS::window, w, h);
	GRAPHICS::resize();
	initGL();
	/*
		Explanation for window resize work-around:
		The first resize of an application will reset the openGL context,
		all following resizes will have no effect.
		To get around this, a resize is forced to the window.
	*/
	SDL_SetEventFilter(resize_sdl_event, nullptr);
	std::cout << "JE: Initiated Window." << std::endl;
}
示例#4
0
int
WIN_GLES_SetupWindow(_THIS, SDL_Window * window)
{
    /* The current context is lost in here; save it and reset it. */
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
    SDL_Window *current_win = SDL_GL_GetCurrentWindow();
    SDL_GLContext current_ctx = SDL_GL_GetCurrentContext();


    if (_this->egl_data == NULL) {
        if (SDL_EGL_LoadLibrary(_this, NULL, EGL_DEFAULT_DISPLAY) < 0) {
            SDL_EGL_UnloadLibrary(_this);
            return -1;
        }
    }
  
    /* Create the GLES window surface */
    windowdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType)windowdata->hwnd);

    if (windowdata->egl_surface == EGL_NO_SURFACE) {
        return SDL_SetError("Could not create GLES window surface");
    }

    return WIN_GLES_MakeCurrent(_this, current_win, current_ctx);    
}
示例#5
0
static bool _gut_initWindow(const char *title, unsigned width, unsigned height, unsigned sdlflags, SDL_Window **window, SDL_GLContext *context) {
	if (!(gut.core->flags & CTL_SDL_INIT))
		error("GUT not initialised");
	SDL_GLContext old = SDL_GL_GetCurrentContext();
	*window = SDL_CreateWindow(
		title,
		SDL_WINDOWPOS_UNDEFINED,
		SDL_WINDOWPOS_UNDEFINED,
		width,
		height,
		sdlflags
	);
	if (!*window)
		sdl_error("window could not be created");
	if (old) {
		SDL_GL_MakeCurrent(*window, old);
		*context = old;
	} else {
		context = SDL_GL_CreateContext(*window);
		if (!context)
			sdl_error("context could not be created");
	}
	if (gut.core->window.icon)
		SDL_SetWindowIcon(*window, gut.core->window.icon);
	if (!(gut.window.flags & GUT_NO_VSYNC)) {
		wrap_sdl_call(0,==,"vsync not available", GL_SetSwapInterval, 1);
	}
示例#6
0
int
WIN_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
{
    HDC hdc;

    if (!_this->gl_data) {
        return SDL_SetError("OpenGL not initialized");
    }

    /* sanity check that higher level handled this. */
    SDL_assert(window || (!window && !context));

    /* Some Windows drivers freak out if hdc is NULL, even when context is
       NULL, against spec. Since hdc is _supposed_ to be ignored if context
       is NULL, we either use the current GL window, or do nothing if we
       already have no current context. */
    if (!window) {
        window = SDL_GL_GetCurrentWindow();
        if (!window) {
            SDL_assert(SDL_GL_GetCurrentContext() == NULL);
            return 0;  /* already done. */
        }
    }

    hdc = ((SDL_WindowData *) window->driverdata)->hdc;
    if (!_this->gl_data->wglMakeCurrent(hdc, (HGLRC) context)) {
        return WIN_SetError("wglMakeCurrent()");
    }
    return 0;
}
示例#7
0
文件: core.cpp 项目: sgedev/sge
void shutdown(void)
{
	SGE_ASSERT(details::g_window != NULL);
	SGE_ASSERT(details::g_gl_context != NULL);
	SGE_ASSERT(s_imgui_context != NULL);

	shutdown_test();

	s_program.Destroy();

	ImGui::SetCurrentContext(s_imgui_context);
	ImGui_ImplOpenGL3_Shutdown();
	ImGui_ImplSDL2_Shutdown();
	ImGui::SetCurrentContext(NULL);
	ImGui::DestroyContext(s_imgui_context);
	s_imgui_context = NULL;

	if (details::g_gl_context == SDL_GL_GetCurrentContext())
		SDL_GL_MakeCurrent(details::g_window, NULL);

	SDL_GL_DeleteContext(details::g_gl_context);
	details::g_gl_context = NULL;

	SDL_DestroyWindow(details::g_window);
	details::g_window = NULL;
	details::g_window_id = 0;
}
示例#8
0
文件: sdl2.c 项目: AmesianX/panda
void sdl2_window_create(struct sdl2_console *scon)
{
    int flags = 0;

    if (!scon->surface) {
        return;
    }
    assert(!scon->real_window);

    if (gui_fullscreen) {
        flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
    } else {
        flags |= SDL_WINDOW_RESIZABLE;
    }
    if (scon->hidden) {
        flags |= SDL_WINDOW_HIDDEN;
    }

    scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
                                         SDL_WINDOWPOS_UNDEFINED,
                                         surface_width(scon->surface),
                                         surface_height(scon->surface),
                                         flags);
    scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
    if (scon->opengl) {
        scon->winctx = SDL_GL_GetCurrentContext();
    }
    sdl_update_caption(scon);
}
示例#9
0
SDL_bool
OVL_Init(const char* theme_dir, const char* language)
{
    Uint32 mask = SDL_INIT_VIDEO |
                  SDL_INIT_GAMECONTROLLER |
                  SDL_INIT_JOYSTICK;

    if (SDL_WasInit(mask) != mask) {
        SDL_SetError("SDL not initialized"); \
        return SDL_FALSE;
    }

    _this = SDL_calloc(1, sizeof(*_this));

    if (_this == NULL) {
        SDL_OutOfMemory();
        return SDL_FALSE;
    }

	SDL_Window* window = SDL_GL_GetCurrentWindow();
	SDL_GLContext context = SDL_GL_GetCurrentContext();
	
    _this->overlay_window = SDL_CreateWindow("Overlay", 0, 0, 1, 1,
											 SDL_WINDOW_HIDDEN);
    _this->overlay_context = SDL_GL_CreateContext(_this->overlay_window);
	
	SDL_GL_MakeCurrent(window, context);
	
    return SDL_TRUE;
}
示例#10
0
文件: sdl2-gl.c 项目: dota1923/qemu
QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
{
    SDL_GLContext sdlctx;

    sdlctx = SDL_GL_GetCurrentContext();
    return (QEMUGLContext)sdlctx;
}
示例#11
0
文件: glc.cpp 项目: npapier/oglpp
glc_bool_t glc_is_current( glc_t * context )
{
	assert( context != 0 && "Calls glc_is_current() on a null glc context.");
	// @todo glc_status()

	assert( context->drawable != 0 && "Calls glc_is_current() with a context associated with a null drawable." );
	// @todo drawable_status()

#ifdef __SDL2__
	GLC_GLRC_HANDLE glrc = SDL_GL_GetCurrentContext();
#elif defined(GLC_USE_WGL)
	GLC_GLRC_HANDLE glrc = wglGetCurrentContext();

	/*assert(	( glrc == 0 )					||					// no current context		=> is not current 
			( glrc != context->context )	||					// another context is current	=> is not current
			(	(glrc == context->context) &&					// is current
				(wglGetCurrentDC() == context->drawable->dc) )	// and for the good drawable/dc
			&& "The context is current, but not for its associated drawable/graphical context !" );*/
#elif defined(GLC_USE_GLX)
	GLC_GLRC_HANDLE glrc = glXGetCurrentContext();
#else
	#error "Platform not yet supported."
#endif
	return glrc == context->context ? 1 : 0;
}
static void _PlatformCreateOpenGLContextCore(FPlatformOpenGLContext* OutContext)
{
	check(OutContext);
	SDL_HWindow PrevWindow = SDL_GL_GetCurrentWindow();
	SDL_HGLContext PrevContext = SDL_GL_GetCurrentContext();

	OutContext->hGLContext = SDL_GL_CreateContext(OutContext->hWnd);
	SDL_GL_MakeCurrent(PrevWindow, PrevContext);
}
示例#13
0
void 
android_egl_context_backup() 
{
    /* Keep a copy of the EGL Context so we can try to restore it when we resume */
    SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
    data->egl_context = SDL_GL_GetCurrentContext();
    /* We need to do this so the EGLSurface can be freed */
    SDL_GL_MakeCurrent(Android_Window, NULL);
}
示例#14
0
	GraphicsPrivate(RGSSThreadData *rtData)
	    : scRes(DEF_SCREEN_W, DEF_SCREEN_H),
	      scSize(scRes),
	      winSize(rtData->config.defScreenW, rtData->config.defScreenH),
	      screen(scRes.x, scRes.y),
	      threadData(rtData),
	      glCtx(SDL_GL_GetCurrentContext()),
	      frameRate(DEF_FRAMERATE),
	      frameCount(0),
	      brightness(255),
	      fpsLimiter(frameRate),
	      frozen(false)
	{
		recalculateScreenSize(rtData);
		updateScreenResoRatio(rtData);

		TEXFBO::init(frozenScene);
		TEXFBO::allocEmpty(frozenScene, scRes.x, scRes.y);
		TEXFBO::linkFBO(frozenScene);

		TEXFBO::init(currentScene);
		TEXFBO::allocEmpty(currentScene, scRes.x, scRes.y);
		TEXFBO::linkFBO(currentScene);

		FloatRect screenRect(0, 0, scRes.x, scRes.y);
		screenQuad.setTexPosRect(screenRect, screenRect);

		TEXFBO::init(transBuffer);
		TEXFBO::allocEmpty(transBuffer, scRes.x, scRes.y);
		TEXFBO::linkFBO(transBuffer);

		fpsLimiter.resetFrameAdjust();
		
		const std::string &olImage = rtData->config.touchOverlay.image;
		if (!olImage.empty())
		{
			SDL_RWops *ops = SDL_RWFromFile(olImage.c_str(), "rb");
			SDL_Surface *surf = IMG_Load_RW(ops, 1);

			if (surf)
			{
				overlayTex = TEX::gen();

				TEX::bind(overlayTex);
				TEX::setRepeat(false);
				TEX::setSmooth(true);
				TEX::uploadImage(surf->w, surf->h, surf->pixels, GL_RGBA);

				overlayTexSize = Vec2i(surf->w, surf->h);
			}
			else
			{
				Debug() << "failed to load overlay image:" << SDL_GetError();
			}
		}
	}
示例#15
0
int
NACL_GLES_SwapWindow(_THIS, SDL_Window * window)
{
    SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
    struct PP_CompletionCallback callback = { NULL, 0, PP_COMPLETIONCALLBACK_FLAG_NONE };
    if (driverdata->ppb_graphics->SwapBuffers((PP_Resource) SDL_GL_GetCurrentContext(), callback ) != 0) {
        return SDL_SetError("SwapBuffers failed");
    }
    return 0;
}
示例#16
0
int
WIN_GL_SetupWindow(_THIS, SDL_Window * window)
{
    /* The current context is lost in here; save it and reset it. */
    SDL_Window *current_win = SDL_GL_GetCurrentWindow();
    SDL_GLContext current_ctx = SDL_GL_GetCurrentContext();
    const int retval = WIN_GL_SetupWindowInternal(_this, window);
    WIN_GL_MakeCurrent(_this, current_win, current_ctx);
    return retval;
}
示例#17
0
void GraphicsWindowSDL2::setSyncToVBlank(bool on)
{
    SDL_Window *oldWin = SDL_GL_GetCurrentWindow();
    SDL_GLContext oldCtx = SDL_GL_GetCurrentContext();

    SDL_GL_MakeCurrent(mWindow, mContext);

    SDL_GL_SetSwapInterval(on ? 1 : 0);

    SDL_GL_MakeCurrent(oldWin, oldCtx);
}
示例#18
0
SDL_GLContext
SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
{
    EGLint context_attrib_list[] = {
        EGL_CONTEXT_CLIENT_VERSION,
        1,
        EGL_NONE
    };
    
    EGLContext egl_context, share_context = EGL_NO_CONTEXT;
    
    if (!_this->egl_data) {
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
        return NULL;
    }
    
    if (_this->gl_config.share_with_current_context) {
        share_context = (EGLContext)SDL_GL_GetCurrentContext();
    }
    
    /* Bind the API */
    if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
        if (_this->gl_config.major_version) {
            context_attrib_list[1] = _this->gl_config.major_version;
        }

        egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
                                          _this->egl_data->egl_config,
                                          share_context, context_attrib_list);
    }
    else {
        _this->egl_data->eglBindAPI(EGL_OPENGL_API);
        egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
                                          _this->egl_data->egl_config,
                                          share_context, NULL);
    }
    
    if (egl_context == EGL_NO_CONTEXT) {
        SDL_SetError("Could not create EGL context");
        return NULL;
    }
    
    _this->egl_data->egl_swapinterval = 0;
    
    if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
        SDL_EGL_DeleteContext(_this, egl_context);
        SDL_SetError("Could not make EGL context current");
        return NULL;
    }
  
    return (SDL_GLContext) egl_context;
}
//TODO::fix
char * RenderUtil::GetOpenGLVersion()
{
	if (SDL_GL_GetCurrentContext() == NULL)
	{
		//trigger assert
		//LOGERROR("gg");
		int i = 0;
	}

	auto glver = glGetString(GL_VERSION);
	//TCHAR* chars = glver;

	return (char*)glver;
}
示例#20
0
void THMovie::setRenderer(SDL_Renderer *pRenderer)
{
    m_pRenderer = pRenderer;

    SDL_GLContext prevContext = SDL_GL_GetCurrentContext();
    m_pShareWindow = SDL_GL_GetCurrentWindow();

    /* We create a new context that we can use on our video thread, that shares
     * a texture namespace with the main thread's context. */
    SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
    m_shareContext = SDL_GL_CreateContext(m_pShareWindow);

    /* Unfortunately, SDL_GL_CreateContext implicitly makes the new context current, so we revert it. */
    SDL_GL_MakeCurrent(m_pShareWindow, prevContext);
}
示例#21
0
void TextureManager::release_all()
{
	if (SDL_GL_GetCurrentContext() != nullptr) {
		// We are the main OpenGL context thread so we can destroy textures.
		_textureCache.clear();
		_unload.clear();
	} else {
		// We are not the main OpenGL context thread so we can not destroy textures.
		for (auto it = std::begin(_textureCache); it != std::end(_textureCache);)
		{
			_unload.push_front(it->second);
			it = _textureCache.erase(it);
		}
	}
}
示例#22
0
void GraphicsWindowSDL2::init()
{
    if(mValid) return;

    if(!_traits.valid())
        return;

    WindowData *inheritedWindowData = dynamic_cast<WindowData*>(_traits->inheritedWindowData.get());
    mWindow = inheritedWindowData ? inheritedWindowData->mWindow : NULL;

    mOwnsWindow = (mWindow == 0);
    if(mOwnsWindow)
    {
        OSG_FATAL<<"Error: No SDL window provided."<<std::endl;
        return;
    }

    // SDL will change the current context when it creates a new one, so we
    // have to get the current one to be able to restore it afterward.
    SDL_Window *oldWin = SDL_GL_GetCurrentWindow();
    SDL_GLContext oldCtx = SDL_GL_GetCurrentContext();
   
#ifdef OPENGL_ES
     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);    
#endif
    
    mContext = SDL_GL_CreateContext(mWindow);
    if(!mContext)
    {
        OSG_FATAL<< "Error: Unable to create OpenGL graphics context: "<<SDL_GetError() <<std::endl;
        return;
    }

    SDL_GL_SetSwapInterval(_traits->vsync ? 1 : 0);

    SDL_GL_MakeCurrent(oldWin, oldCtx);

    mValid = true;

#if OSG_VERSION_GREATER_OR_EQUAL(3,3,4)
    getEventQueue()->syncWindowRectangleWithGraphicsContext();
#else
    getEventQueue()->syncWindowRectangleWithGraphcisContext();
#endif
}
示例#23
0
void create_opengl_context_impl( SDLOpenglContextImpl* context,
								 int format )
{
    if (context->use_current)
    {
        /* TODO: What do we do here? */
        context->base.window->create_window_impl( context->base.window, SDL_WINDOW_OPENGL );
        context->base.context = SDL_GL_GetCurrentContext();
    }
    else
    {
        SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, lb_opengl_context_get_pixelformat_double_buffer( context, format ) );
        SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, lb_opengl_context_get_pixelformat_depth_size( context, format ) );

        context->base.window->create_window_impl( context->base.window, SDL_WINDOW_OPENGL );
        context->base.context = SDL_GL_CreateContext( context->base.window->window );
    }

	context->created = 1;
}
示例#24
0
void
NACL_SetScreenResolution(int width, int height, Uint32 format)
{
    PP_Resource context;
    
    nacl.w = width;
    nacl.h = height;   
    nacl.format = format;
    
    if (nacl.window) {
        nacl.window->w = width;
        nacl.window->h = height;
        SDL_SendWindowEvent(nacl.window, SDL_WINDOWEVENT_RESIZED, width, height);
    }
    
    /* FIXME: Check threading issues...otherwise use a hardcoded _this->context across all threads */
    context = (PP_Resource) SDL_GL_GetCurrentContext();
    if (context) {
        PSInterfaceGraphics3D()->ResizeBuffers(context, width, height);
    }

}
示例#25
0
SDL_GLContext
X11_GL_CreateContext(_THIS, SDL_Window * window)
{
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    Display *display = data->videodata->display;
    int screen =
        ((SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata)->screen;
    XWindowAttributes xattr;
    XVisualInfo v, *vinfo;
    int n;
    GLXContext context = NULL, share_context;
    PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs = NULL;

    if (_this->gl_config.share_with_current_context) {
        share_context = (GLXContext)SDL_GL_GetCurrentContext();
    } else {
        share_context = NULL;
    }

    /* We do this to create a clean separation between X and GLX errors. */
    XSync(display, False);
    errorBase = _this->gl_data->errorBase;
    handler = XSetErrorHandler(X11_GL_CreateContextErrorHandler);
    XGetWindowAttributes(display, data->xwindow, &xattr);
    v.screen = screen;
    v.visualid = XVisualIDFromVisual(xattr.visual);
    vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
    if (vinfo) {
        if (_this->gl_config.major_version < 3 &&
                _this->gl_config.profile_mask == 0 &&
                _this->gl_config.flags == 0) {
            /* Create legacy context */
            context =
                _this->gl_data->glXCreateContext(display, vinfo, share_context, True);
        } else {
            /* If we want a GL 3.0 context or later we need to get a temporary
               context to grab the new context creation function */
            GLXContext temp_context =
                _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
            if (temp_context) {
                /* max 8 attributes plus terminator */
                int attribs[9] = {
                    GLX_CONTEXT_MAJOR_VERSION_ARB,
                    _this->gl_config.major_version,
                    GLX_CONTEXT_MINOR_VERSION_ARB,
                    _this->gl_config.minor_version,
                    0
                };
                int iattr = 4;

                /* SDL profile bits match GLX profile bits */
                if( _this->gl_config.profile_mask != 0 ) {
                    attribs[iattr++] = GLX_CONTEXT_PROFILE_MASK_ARB;
                    attribs[iattr++] = _this->gl_config.profile_mask;
                }

                /* SDL flags match GLX flags */
                if( _this->gl_config.flags != 0 ) {
                    attribs[iattr++] = GLX_CONTEXT_FLAGS_ARB;
                    attribs[iattr++] = _this->gl_config.flags;
                }

                attribs[iattr++] = 0;

                /* Get a pointer to the context creation function for GL 3.0 */
                glXCreateContextAttribs =
                    (PFNGLXCREATECONTEXTATTRIBSARBPROC) _this->gl_data->
                    glXGetProcAddress((GLubyte *)
                                      "glXCreateContextAttribsARB");
                if (!glXCreateContextAttribs) {
                    SDL_SetError("GL 3.x is not supported");
                    context = temp_context;
                } else {
                    int glxAttribs[64];

                    /* Create a GL 3.x context */
                    GLXFBConfig *framebuffer_config = NULL;
                    int fbcount = 0;
                    GLXFBConfig *(*glXChooseFBConfig) (Display * disp,
                                                       int screen,
                                                       const int *attrib_list,
                                                       int *nelements);

                    glXChooseFBConfig =
                        (GLXFBConfig *
                         (*)(Display *, int, const int *,
                             int *)) _this->gl_data->
                        glXGetProcAddress((GLubyte *) "glXChooseFBConfig");

                    X11_GL_GetAttributes(_this,display,screen,glxAttribs,64,SDL_TRUE);

                    if (!glXChooseFBConfig
                            || !(framebuffer_config =
                                     glXChooseFBConfig(display,
                                                       DefaultScreen(display), glxAttribs,
                                                       &fbcount))) {
                        SDL_SetError
                        ("No good framebuffers found. GL 3.x disabled");
                        context = temp_context;
                    } else {
                        context =
                            glXCreateContextAttribs(display,
                                                    framebuffer_config[0],
                                                    share_context, True, attribs);
                        _this->gl_data->glXDestroyContext(display,
                                                          temp_context);
                    }
                }
            }
        }
        XFree(vinfo);
    }
    XSync(display, False);
    XSetErrorHandler(handler);

    if (!context) {
        SDL_SetError("Could not create GL context");
        return NULL;
    }

    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
        X11_GL_DeleteContext(_this, context);
        return NULL;
    }

    return context;
}
示例#26
0
//==============================================================================
Context NativeWindow::getCurrentContext()
{
	SDL_GLContext sdlCtx = SDL_GL_GetCurrentContext();
	return sdlCtx;
}
示例#27
0
文件: SDL_egl.c 项目: wpbest/UWP
SDL_GLContext
SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
{
    /* max 14 values plus terminator. */
    EGLint attribs[15];
    int attr = 0;

    EGLContext egl_context, share_context = EGL_NO_CONTEXT;
    EGLint profile_mask = _this->gl_config.profile_mask;
    EGLint major_version = _this->gl_config.major_version;
    EGLint minor_version = _this->gl_config.minor_version;
    SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);

    if (!_this->egl_data) {
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
        return NULL;
    }

    if (_this->gl_config.share_with_current_context) {
        share_context = (EGLContext)SDL_GL_GetCurrentContext();
    }

    /* Set the context version and other attributes. */
    if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
            _this->gl_config.flags == 0 &&
            (profile_mask == 0 || profile_es)) {
        /* Create a context without using EGL_KHR_create_context attribs.
         * When creating a GLES context without EGL_KHR_create_context we can
         * only specify the major version. When creating a desktop GL context
         * we can't specify any version, so we only try in that case when the
         * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
         */
        if (profile_es) {
            attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
            attribs[attr++] = SDL_max(major_version, 1);
        }
    } else {
#ifdef EGL_KHR_create_context
        /* The Major/minor version, context profiles, and context flags can
         * only be specified when this extension is available.
         */
        if (SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
            attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
            attribs[attr++] = major_version;
            attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
            attribs[attr++] = minor_version;

            /* SDL profile bits match EGL profile bits. */
            if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
                attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
                attribs[attr++] = profile_mask;
            }

            /* SDL flags match EGL flags. */
            if (_this->gl_config.flags != 0) {
                attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
                attribs[attr++] = _this->gl_config.flags;
            }
        } else
#endif /* EGL_KHR_create_context */
        {
            SDL_SetError("Could not create EGL context (context attributes are not supported)");
            return NULL;
        }
    }

    attribs[attr++] = EGL_NONE;

    /* Bind the API */
    if (profile_es) {
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
    } else {
        _this->egl_data->eglBindAPI(EGL_OPENGL_API);
    }

    egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
                  _this->egl_data->egl_config,
                  share_context, attribs);

    if (egl_context == EGL_NO_CONTEXT) {
        SDL_SetError("Could not create EGL context");
        return NULL;
    }

    _this->egl_data->egl_swapinterval = 0;

    if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
        SDL_EGL_DeleteContext(_this, egl_context);
        SDL_SetError("Could not make EGL context current");
        return NULL;
    }

    return (SDL_GLContext) egl_context;
}
static void PlatformInitOpenGL(void*& ContextPtr, void*& PrevContextPtr, int InMajorVersion, int InMinorVersion)
{
	static bool bInitialized = (SDL_GL_GetCurrentWindow() != NULL) && (SDL_GL_GetCurrentContext() != NULL);

	if (!bInitialized)
	{
		check(InMajorVersion > 3 || (InMajorVersion == 3 && InMinorVersion >= 2));
		if (SDL_WasInit(0) == 0)
		{
			SDL_Init(SDL_INIT_VIDEO);
		}
		else
		{
			Uint32 InitializedSubsystemsMask = SDL_WasInit(SDL_INIT_EVERYTHING);
			if ((InitializedSubsystemsMask & SDL_INIT_VIDEO) == 0)
			{
				SDL_InitSubSystem(SDL_INIT_VIDEO);
			}
		}

		if (SDL_GL_LoadLibrary(NULL))
		{
			UE_LOG(LogOpenGLShaderCompiler, Fatal, TEXT("Unable to dynamically load libGL: %s"), ANSI_TO_TCHAR(SDL_GetError()));
		}

		if	(SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, InMajorVersion))
		{
			UE_LOG(LogOpenGLShaderCompiler, Fatal, TEXT("Failed to set GL major version: %s"), ANSI_TO_TCHAR(SDL_GetError()));
		}

		if	(SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, InMinorVersion))
		{
			UE_LOG(LogOpenGLShaderCompiler, Fatal, TEXT("Failed to set GL minor version: %s"), ANSI_TO_TCHAR(SDL_GetError()));
		}

		if	(SDL_GL_SetAttribute( SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG))
		{
			UE_LOG(LogOpenGLShaderCompiler, Fatal, TEXT("Failed to set GL flags: %s"), ANSI_TO_TCHAR(SDL_GetError()));
		}

		if	(SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE))
		{
			UE_LOG(LogOpenGLShaderCompiler, Fatal, TEXT("Failed to set GL mask/profile: %s"), ANSI_TO_TCHAR(SDL_GetError()));
		}

		// Create a dummy context to verify opengl support.
		FPlatformOpenGLContext DummyContext;
		_PlatformCreateDummyGLWindow(&DummyContext);
		_PlatformCreateOpenGLContextCore(&DummyContext);

		if (DummyContext.hGLContext)
		{
			_ContextMakeCurrent(DummyContext.hWnd, DummyContext.hGLContext);
		}
		else
		{
			UE_LOG(LogOpenGLShaderCompiler, Fatal, TEXT("OpenGL %d.%d not supported by driver"), InMajorVersion, InMinorVersion);
			return;
		}

		PrevContextPtr = NULL;
		ContextPtr = DummyContext.hGLContext;
		bInitialized = true;
	}

	PrevContextPtr = reinterpret_cast<void*>(SDL_GL_GetCurrentContext());
	SDL_HGLContext NewContext = SDL_GL_CreateContext(SDL_GL_GetCurrentWindow());
	SDL_GL_MakeCurrent(SDL_GL_GetCurrentWindow(), NewContext);
	ContextPtr = reinterpret_cast<void*>(NewContext);
}
示例#29
0
SDL_GLContext
WIN_GL_CreateContext(_THIS, SDL_Window * window)
{
    HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
    HGLRC context, share_context;

    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES &&
            !_this->gl_data->HAS_WGL_EXT_create_context_es2_profile) {
#if SDL_VIDEO_OPENGL_EGL
        /* Switch to EGL based functions */
        WIN_GL_UnloadLibrary(_this);
        _this->GL_LoadLibrary = WIN_GLES_LoadLibrary;
        _this->GL_GetProcAddress = WIN_GLES_GetProcAddress;
        _this->GL_UnloadLibrary = WIN_GLES_UnloadLibrary;
        _this->GL_CreateContext = WIN_GLES_CreateContext;
        _this->GL_MakeCurrent = WIN_GLES_MakeCurrent;
        _this->GL_SetSwapInterval = WIN_GLES_SetSwapInterval;
        _this->GL_GetSwapInterval = WIN_GLES_GetSwapInterval;
        _this->GL_SwapWindow = WIN_GLES_SwapWindow;
        _this->GL_DeleteContext = WIN_GLES_DeleteContext;

        if (WIN_GLES_LoadLibrary(_this, NULL) != 0) {
            return NULL;
        }

        return WIN_GLES_CreateContext(_this, window);
#else
        SDL_SetError("SDL not configured with EGL support");
        return NULL;
#endif
    }

    if (_this->gl_config.share_with_current_context) {
        share_context = (HGLRC)SDL_GL_GetCurrentContext();
    } else {
        share_context = 0;
    }

    if (_this->gl_config.major_version < 3 &&
            _this->gl_config.profile_mask == 0 &&
            _this->gl_config.flags == 0) {
        /* Create legacy context */
        context = _this->gl_data->wglCreateContext(hdc);
        if( share_context != 0 ) {
            _this->gl_data->wglShareLists(share_context, context);
        }
    } else {
        PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
        HGLRC temp_context = _this->gl_data->wglCreateContext(hdc);
        if (!temp_context) {
            SDL_SetError("Could not create GL context");
            return NULL;
        }

        /* Make the context current */
        if (WIN_GL_MakeCurrent(_this, window, temp_context) < 0) {
            WIN_GL_DeleteContext(_this, temp_context);
            return NULL;
        }

        wglCreateContextAttribsARB =
            (PFNWGLCREATECONTEXTATTRIBSARBPROC) _this->gl_data->
            wglGetProcAddress("wglCreateContextAttribsARB");
        if (!wglCreateContextAttribsARB) {
            SDL_SetError("GL 3.x is not supported");
            context = temp_context;
        } else {
            /* max 10 attributes plus terminator */
            int attribs[11] = {
                WGL_CONTEXT_MAJOR_VERSION_ARB, _this->gl_config.major_version,
                WGL_CONTEXT_MINOR_VERSION_ARB, _this->gl_config.minor_version,
                0
            };
            int iattr = 4;

            /* SDL profile bits match WGL profile bits */
            if (_this->gl_config.profile_mask != 0) {
                attribs[iattr++] = WGL_CONTEXT_PROFILE_MASK_ARB;
                attribs[iattr++] = _this->gl_config.profile_mask;
            }

            /* SDL flags match WGL flags */
            if (_this->gl_config.flags != 0) {
                attribs[iattr++] = WGL_CONTEXT_FLAGS_ARB;
                attribs[iattr++] = _this->gl_config.flags;
            }

            /* only set if wgl extension is available */
            if (_this->gl_data->HAS_WGL_ARB_context_flush_control) {
                attribs[iattr++] = WGL_CONTEXT_RELEASE_BEHAVIOR_ARB;
                attribs[iattr++] = _this->gl_config.release_behavior ?
                                   WGL_CONTEXT_RELEASE_BEHAVIOR_FLUSH_ARB :
                                   WGL_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB;
            }

            attribs[iattr++] = 0;

            /* Create the GL 3.x context */
            context = wglCreateContextAttribsARB(hdc, share_context, attribs);
            /* Delete the GL 2.x context */
            _this->gl_data->wglDeleteContext(temp_context);
        }
    }

    if (!context) {
        WIN_SetError("Could not create GL context");
        return NULL;
    }

    if (WIN_GL_MakeCurrent(_this, window, context) < 0) {
        WIN_GL_DeleteContext(_this, context);
        return NULL;
    }

    return context;
}
示例#30
0
SDL_GLContext
X11_GL_CreateContext(_THIS, SDL_Window * window)
{
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    Display *display = data->videodata->display;
    int screen =
        ((SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata)->screen;
    XWindowAttributes xattr;
    XVisualInfo v, *vinfo;
    int n;
    GLXContext context = NULL, share_context;

    if (_this->gl_config.share_with_current_context) {
        share_context = (GLXContext)SDL_GL_GetCurrentContext();
    } else {
        share_context = NULL;
    }

    /* We do this to create a clean separation between X and GLX errors. */
    X11_XSync(display, False);
    errorHandlerOperation = "create GL context";
    errorBase = _this->gl_data->errorBase;
    errorCode = Success;
    handler = X11_XSetErrorHandler(X11_GL_ErrorHandler);
    X11_XGetWindowAttributes(display, data->xwindow, &xattr);
    v.screen = screen;
    v.visualid = X11_XVisualIDFromVisual(xattr.visual);
    vinfo = X11_XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
    if (vinfo) {
        if (_this->gl_config.major_version < 3 &&
            _this->gl_config.profile_mask == 0 &&
            _this->gl_config.flags == 0) {
            /* Create legacy context */
            context =
                _this->gl_data->glXCreateContext(display, vinfo, share_context, True);
        } else {
            /* max 10 attributes plus terminator */
            int attribs[11] = {
                GLX_CONTEXT_MAJOR_VERSION_ARB,
                _this->gl_config.major_version,
                GLX_CONTEXT_MINOR_VERSION_ARB,
                _this->gl_config.minor_version,
                0
            };
            int iattr = 4;

            /* SDL profile bits match GLX profile bits */
            if( _this->gl_config.profile_mask != 0 ) {
                attribs[iattr++] = GLX_CONTEXT_PROFILE_MASK_ARB;
                attribs[iattr++] = _this->gl_config.profile_mask;
            }

            /* SDL flags match GLX flags */
            if( _this->gl_config.flags != 0 ) {
                attribs[iattr++] = GLX_CONTEXT_FLAGS_ARB;
                attribs[iattr++] = _this->gl_config.flags;
            }

            /* only set if glx extension is available */
            if( _this->gl_data->HAS_GLX_ARB_context_flush_control ) {
                attribs[iattr++] = GLX_CONTEXT_RELEASE_BEHAVIOR_ARB;
                attribs[iattr++] = 
                    _this->gl_config.release_behavior ? 
                    GLX_CONTEXT_RELEASE_BEHAVIOR_FLUSH_ARB : 
                    GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB;
            }

            attribs[iattr++] = 0;

            /* Get a pointer to the context creation function for GL 3.0 */
            if (!_this->gl_data->glXCreateContextAttribsARB) {
                SDL_SetError("OpenGL 3.0 and later are not supported by this system");
            } else {
                int glxAttribs[64];

                /* Create a GL 3.x context */
                GLXFBConfig *framebuffer_config = NULL;
                int fbcount = 0;

                X11_GL_GetAttributes(_this,display,screen,glxAttribs,64,SDL_TRUE);

                if (!_this->gl_data->glXChooseFBConfig
                    || !(framebuffer_config =
                        _this->gl_data->glXChooseFBConfig(display,
                                          DefaultScreen(display), glxAttribs,
                                          &fbcount))) {
                    SDL_SetError("No good framebuffers found. OpenGL 3.0 and later unavailable");
                } else {
                    context = _this->gl_data->glXCreateContextAttribsARB(display,
                                                    framebuffer_config[0],
                                                    share_context, True, attribs);
                    X11_XFree(framebuffer_config);
                }
            }
        }
        X11_XFree(vinfo);
    }
    X11_XSync(display, False);
    X11_XSetErrorHandler(handler);

    if (!context) {
        if (errorCode == Success) {
            SDL_SetError("Could not create GL context");
        }
        return NULL;
    }

    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
        X11_GL_DeleteContext(_this, context);
        return NULL;
    }

    return context;
}