Menu

Initialization

Milan Davidovic

The library exports 2 functions: CreateRenderContext() and DestroyRenderContext() (on Linux there is one more function GetVisualInfo()). These functions are declared in GLSlayer/RenderContextInit.h. These functions can be linked on load time or at run time. For run time linking, declarations of pointer to function types are provided in the header.

CreateRenderContext() function has different parameters on different platforms, on Windows it takes a HINSTANCE and a HWND, on Linux a Display and a Window.

Windows

Functions are declared as:

IRenderContext* CreateRenderContext(
        uint version, HINSTANCE instance_handle, HWND window_handle,
        const FramebufferFormat* format, bool debug_context, IRenderLogger* logger);

void DestroyRenderContext(IRenderContext* render_context);

Creating rendering context is performed by filling FramebufferFormat structure and calling CreateRenderContext:

#include <GLSlayer/RenderContextInit.h>
#include <GLSlayer/RenderContext.h>

// ...

gls::FramebufferFormat fbufFormat;
fbufFormat.colorBits = 32;
fbufFormat.colorBufferType = gls::COLOR_BUFFER_TYPE_RGBA;
fbufFormat.depthBits = 24;
fbufFormat.stencilBits = 8;
fbufFormat.doubleBuffer = true;
fbufFormat.multisampleSamples = 8;
fbufFormat.sRGB = true;
fbufFormat.swapMethod = gls::SWAP_EXCHANGE;

gls::IRenderContext* renderContext = gls::CreateRenderContext(
        420, hinstance, hwnd, &fbufFormat, false, nullptr);

renderContext->SetCurrentContext();

// ...

gls::DestroyRenderContext(renderContext);

This creates 4.2 forward compatible context. The context is made current for the calling thread by calling renderContext->SetCurrentContext().

Linux

For Linux, there is an additional function that needs to be called before creating the window, to get the visual with which the window must be created. This visual corresponds to required framebuffer config. Declarations are:

IRenderContext* CreateRenderContext(
        uint version, Display* display, Window window, const FramebufferFormat* format,
        bool debug_context, IRenderLogger* logger);

bool GetVisualInfo(
        Display* display, const FramebufferFormat& format, XVisualInfo& visual_info);

void DestroyRenderContext(IRenderContext* render_context);

Creating rendering context:

#include <GLSlayer/RenderContextInit.h>
#include <GLSlayer/RenderContext.h>

// ...

gls::FramebufferFormat fbufFormat;
fbufFormat.colorBits = 32;
fbufFormat.colorBufferType = gls::COLOR_BUFFER_TYPE_RGBA;
fbufFormat.depthBits = 24;
fbufFormat.stencilBits = 8;
fbufFormat.doubleBuffer = true;
fbufFormat.multisampleSamples = 8;
fbufFormat.sRGB = true;
fbufFormat.swapMethod = gls::SWAP_EXCHANGE; // ignored for now on Linux

XVisualInfo visualInfo;
if(!gls::GetVisualInfo(display, fbufFormat, visualInfo))
    return 1;

// Create the window using this visual
// Window window = XCreateWindow(...);

gls::IRenderContext* renderContext = CreateRenderContext(
        420, display, window, &fbufFormat, false, &_console);

renderContext->SetCurrentContext();

// ...

gls::DestroyRenderContext(renderContext);

Related

Wiki: Home