libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
Functions
glctx.h File Reference

Go to the source code of this file.

Functions

API_EXPORT glctx_t * glctx_create_invisible (void *win_ptr, glctx_t *share_ctx, int major_ver, int minor_ver, bool_t fwd_compat, bool_t debug)
 
API_EXPORT void * glctx_get_xplane_win_ptr (void)
 
API_EXPORT bool_t glctx_make_current (glctx_t *ctx)
 
API_EXPORT void * glctx_get_window_system_handle (glctx_t *ctx)
 
API_EXPORT glctx_t * glctx_get_current (void)
 
API_EXPORT bool_t glctx_is_current (glctx_t *ctx)
 
API_EXPORT void * glctx_get_handle (const glctx_t *ctx)
 
API_EXPORT void glctx_destroy (glctx_t *ctx)
 

Detailed Description

This file contains a portable method for creating, destroying and managing OpenGL contexts in X-Plane. While X-Plane will provide you an OpenGL context on the main thread, it is sometimes useful to create other contexts for background threads for asynchronous work.

See also
glctx_create_invisible()
glctx_make_current()
glctx_destroy()

OpenGL Versions

OpenGL comes in two major flavors:

There are major incompatibilities between the two branches and you SHOULD always strive to write modern OpenGL code, not legacy. On Windows and Linux, you can request a modern OpenGL context (version 4.0 or later) and most reasonably recent drivers will transparently support legacy OpenGL constructs (unless you set fwd_compat=B_TRUE, in which case we will instruct the driver to explicitly reject legacy features).

However, on MacOS this separation is enforced with an iron hand. So on MacOS, glctx_create_invisible() ignores the version arguments except for major_ver:

Please note that MacOS doesn't support any later OpenGL revisions, so the maximum you can get reliably is the OpenGL 4.1 core profile.

Definition in file glctx.h.

Function Documentation

◆ glctx_create_invisible()

API_EXPORT glctx_t * glctx_create_invisible ( void *  win_ptr,
glctx_t *  share_ctx,
int  major_ver,
int  minor_ver,
bool_t  fwd_compat,
bool_t  debug 
)

Creates an invisible OpenGL context of a specified version in a platform-independent way. Please note that this must be called on the main X-Plane thread. To create a background rendering thread, call glctx_create_invisible() on the main thread, then hand off the created glctx_t to the background thread, which will subsequently set it as its current context using glctx_make_current().

Parameters
win_ptrAn opaque window pointer to a backing window that the OpenGL will be based on. The context will NOT be drawing to this window. The window handle is only used to fetch hardware context information such as the target GPU and supported capabilities. Unless you know exactly what you are doing, you should always pass the return value of glctx_get_xplane_win_ptr() here.
share_ctxAn optional OpenGL context that you want the new context to share resources with. Please note that this will also share the command pipeline between the contexts, which can introduced undesirable command contention, so use with care.
major_verMajor OpenGL version requested. See "OpenGL Versions".
minor_verMajor OpenGL version requested. See "OpenGL Versions".
fwd_compatIf set to true, the returned OpenGL context will NOT be backwards compatible. See "OpenGL Versions" for more info.
debugEnables additional error checking and reporting on platforms that support it (Linux & Windows), often at the cost of some performance. Enable on debug code, disable on production code.
Returns
On success, an initialized OpenGL context. On failure, the function returns NULL and prints an error reason to the log. The returned context will NOT yet have been made the current context. To make the context current, use glctx_make_current.

Definition at line 314 of file glctx.c.

◆ glctx_destroy()

API_EXPORT void glctx_destroy ( glctx_t *  ctx)

Destroys a previously constructed glctx_t structure. For contexts created using glctx_get_current, this DOESN'T destroy the underlying context. Contexts created using glctx_create_invisible, this DOES destroy the underlying context and MUST be called when you are done using the context.

Definition at line 514 of file glctx.c.

◆ glctx_get_current()

API_EXPORT glctx_t * glctx_get_current ( void  )

Constructs a glctx_t from the current context and returns it. If there is no current context, this function returns NULL. Please note that you MUST release the returned glctx_t structure using glctx_destroy when you are done with it. This does NOT destroy the underlying context. You should only use this to grab X-Plane's main rendering context for the purpose of passing it to glctx_create_invisible as a share_ctx.

Definition at line 370 of file glctx.c.

◆ glctx_get_handle()

API_EXPORT void * glctx_get_handle ( const glctx_t *  ctx)

Gets the underlying OpenGL context handle from a glctx_t. This is highly platform-specific:

  1. On Windows, this returns an HGLRC
  2. On Linux, this returns a GLXContext
  3. On Mac, this returns a CGLContextObj

Definition at line 428 of file glctx.c.

◆ glctx_get_window_system_handle()

API_EXPORT void * glctx_get_window_system_handle ( glctx_t *  ctx)

Returns an opaque window system connection handle that can be used to interrogate various hardware properties. This is highly platform specific:

  1. On Windows, this returns the HWND of the associated context.
  2. On Linux, this returns the Display pointer.
  3. On Mac, this function always returns NULL.

Definition at line 499 of file glctx.c.

◆ glctx_get_xplane_win_ptr()

API_EXPORT void * glctx_get_xplane_win_ptr ( void  )

Returns an opaque handle to the X-Plane main window that can be used in glctx_create_invisible to create a context with the same hardware target. This function must ONLY be called on the main X-Plane thread. The returned value is platform-specific as follows:

  1. On Windows, this returns the HWND of the main X-Plane window.
  2. On Linux, this returns the value of the "DISPLAY" environment variable (or NULL if the variable isn't defined), which can then be passed to XOpenDisplay() to create an X-Windows connection on the same server as where X-Plane is running.
  3. On Mac, this function always returns NULL.

Definition at line 361 of file glctx.c.

◆ glctx_is_current()

API_EXPORT bool_t glctx_is_current ( glctx_t *  ctx)
Returns
B_TRUE if ctx is the current rendering context, B_FALSE otheriwse.

Definition at line 415 of file glctx.c.

◆ glctx_make_current()

API_EXPORT bool_t glctx_make_current ( glctx_t *  ctx)

Sets a glctx_t as the calling thread's current context. To remove any context as the current context, pass NULL for ctx in this function.

Definition at line 440 of file glctx.c.