Skip to content
Permalink
Browse files

Added profile enum to GLSettings

  • Loading branch information...
Dgame committed May 16, 2015
1 parent 389f2cb commit 206facd2ef17ef5fd3928393668d0d9488cb63de
@@ -9,6 +9,16 @@ module Dgame.Window.GLSettings;
* Author: Randy Schuett (rswhite4@googlemail.com)
*/
struct GLSettings {
/**
* The profile of the OpenGL Context
*/
enum Profile : ubyte {
Compatibility, /// OpenGL compatibility profile - deprecated functions are allowed (default)
Core, /// OpenGL core profile - deprecated functions are disabled
ES, /// OpenGL ES profile - only a subset of the base OpenGL functionality is available
None, /// profile depends on platform
}

/**
* Major Version of OpenGL. 0 means highest possible.
*/
@@ -26,6 +36,10 @@ struct GLSettings {
* Typical values are 2, 4 or 8
*/
ubyte antiAliasLevel = 0;
/**
* The OpenGL Context profile. Default is Profile.Compatibility
*/
Profile profile = Profile.Compatibility;

/**
* CTor
@@ -32,7 +32,6 @@ import derelict.sdl2.image;
import derelict.sdl2.mixer;

import Dgame.Window.GLSettings;

import Dgame.Internal.Error;

shared static this() {
@@ -127,36 +126,50 @@ void _initSDL() {
}

@nogc
void _initGLAttr(ref GLSettings gl_settings) {
// Mac does not allow deprecated functions / constants, so we have to set the version manually to 2.1
void _initGLAttr(ref GLSettings gl) {
version (OSX) {
if (gl_settings.majorVersion == 0) {
gl_settings.majorVersion = 2;
gl_settings.minorVersion = 1;
if (gl.majorVersion == 0) {
gl.majorVersion = 2;
gl.minorVersion = 1;
}
}

if (gl_settings.majorVersion != 0) {
int result = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, gl_settings.majorVersion);
if (gl.majorVersion != 0) {
int result = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, gl.majorVersion);
assert_fmt(result == 0, "Error by initializing OpenGL: %s\n", SDL_GetError());

result = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, gl_settings.minorVersion);
result = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, gl.minorVersion);
assert_fmt(result == 0, "Error by initializing OpenGL: %s\n", SDL_GetError());
}

if (gl_settings.antiAliasLevel > 0) {
final switch (gl.profile) {
case GLSettings.Profile.None:
break;
case GLSettings.Profile.Core:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
break;
case GLSettings.Profile.Compatibility:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
break;
case GLSettings.Profile.ES:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
break;
}

if (gl.antiAliasLevel > 0) {
int result = SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
assert_fmt(result == 0, "Error by initializing OpenGL: %s\n", SDL_GetError());

int max_samples;
glGetIntegerv(GL_MAX_SAMPLES, &max_samples);

if (gl_settings.antiAliasLevel > max_samples) {
print_fmt("Your anti-alias level (%d) is too high and will be reduced to %d.\n",gl_settings.antiAliasLevel, max_samples);
gl_settings.antiAliasLevel = cast(ubyte) max_samples;
if (gl.antiAliasLevel > max_samples) {
print_fmt("Your anti-alias level (%d) is too high and will be reduced to %d.\n",gl.antiAliasLevel, max_samples);
gl.antiAliasLevel = cast(ubyte) max_samples;
}

result = SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, gl_settings.antiAliasLevel);
result = SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, gl.antiAliasLevel);
assert_fmt(result == 0, "Error by initializing OpenGL: %s\n", SDL_GetError());
}

@@ -30,6 +30,7 @@ import derelict.opengl3.gl;

import Dgame.Graphic.Color;
import Dgame.Graphic.Drawable;
import Dgame.Graphic.Masks;
import Dgame.Graphic.Surface;
import Dgame.Graphic.Texture;

@@ -50,8 +51,9 @@ import Dgame.Internal.Error;
import Dgame.Internal.m3;
import Dgame.Internal.d2c;

static if (!SDL_VERSION_ATLEAST(2, 0, 4))
static if (!SDL_VERSION_ATLEAST(2, 0, 4)) {
enum int SDL_WINDOW_MOUSE_CAPTURE = 0;
}

enum int DefPosX = 100;
enum int DefPosY = 100;
@@ -119,20 +121,16 @@ public:
* CTor
* Position of the Window is default 100x, 100y and the VerticalSync is disabled
*/
this(uint width, uint height, string title, uint style = Style.Default,
GLSettings gl_settings = GLSettings.init)
{
this(Rect(DefPosX, DefPosY, width, height), title, style, gl_settings);
this(uint width, uint height, string title, uint style = Style.Default, GLSettings gl = GLSettings.init) {
this(Rect(DefPosX, DefPosY, width, height), title, style);
}

/**
* CTor
* Position is at 100x, 100y and the VerticalSync is enabled, if mode.refreshRate > 0
*/
this()(auto ref const DisplayMode mode, string title, uint style = Style.Default,
GLSettings gl_settings = GLSettings.init)
{
this(Rect(DefPosX, DefPosY, mode.width, mode.height), title, style, gl_settings);
this()(auto ref const DisplayMode mode, string title, uint style = Style.Default, GLSettings gl = GLSettings.init) {
this(Rect(DefPosX, DefPosY, mode.width, mode.height), title, style);

if (mode.refreshRate > 0)
this.setVerticalSync(VerticalSync.Enable);
@@ -142,16 +140,19 @@ public:
* CTor
* Position is specifiable and the VerticalSync is disabled
*/
this()(auto ref const Rect rect, string title, uint style = Style.Default,
GLSettings gl_settings = GLSettings.init)
{
this()(auto ref const Rect view, string title, uint style = Style.Default, GLSettings gl = GLSettings.init) {
if (_count == 0)
_initSDL();

if (style & Style.OpenGL)
_initGLAttr(gl_settings);

_window = SDL_CreateWindow(toStringz(title), rect.x, rect.y, rect.width, rect.height, style);
_initGLAttr(gl);

_window = SDL_CreateWindow(
toStringz(title),
view.x, view.y,
view.width, view.height,
style
);
assert(_window, "SDL_Window could not be created.");

if (style & Style.OpenGL) {
@@ -162,10 +163,12 @@ public:
if (_count == 0)
_initGL();

this.projection.ortho(Rect(0, 0, rect.width, rect.height));
const Rect rect = Rect(0, 0, view.width, view.height);

this.projection.ortho(rect);
this.loadProjection();

glViewport(0, 0, rect.width, rect.height);
glViewport(rect.x, rect.y, rect.width, rect.height);

this.setClearColor(Color4b.White);
this.setVerticalSync(VerticalSync.Disable);
@@ -260,18 +263,18 @@ public:
Surface capture(Texture.Format fmt = Texture.Format.BGRA) nothrow {
if (this.getStyle() & Style.OpenGL) {
const Size size = this.getSize();
Surface my_capture = Surface(size.width, size.height);
Surface my_capture = Surface(size.width, size.height, 32, Masks.Zero);

glReadBuffer(GL_FRONT);
glReadPixels(0, 0, size.width, size.height, fmt, GL_UNSIGNED_BYTE, my_capture.pixels);

// Flip it

immutable uint lineWidth = size.width * 4;
immutable uint hlw = size.height * lineWidth;

void[] tmpLine = make!(void[])(lineWidth);
scope(exit) unmake(tmpLine);

// Flip it
for (uint i = 0; i < size.height / 2; ++i) {
immutable uint tmpIdx1 = i * lineWidth;
immutable uint tmpIdx2 = (i + 1) * lineWidth;
@@ -17,7 +17,29 @@ <h1>Dgame.Window.GLSettings</h1>
<b>Author:</b><br>
Randy Schuett (rswhite4@googlemail.com)<br><br>

<dl><dt><big><a name="GLSettings.majorVersion"></a>ubyte <u>majorVersion</u>;
<dl><dt><big><a name="GLSettings.Profile"></a>enum <u>Profile</u>: ubyte;
</big></dt>
<dd>The profile of the OpenGL Context<br><br>

<dl><dt><big><a name="GLSettings.Profile.Compatibility"></a><u>Compatibility</u></big></dt>
<dd>OpenGL compatibility profile - deprecated functions are allowed (default)<br><br>

</dd>
<dt><big><a name="GLSettings.Profile.Core"></a><u>Core</u></big></dt>
<dd>OpenGL core profile - deprecated functions are disabled<br><br>

</dd>
<dt><big><a name="GLSettings.Profile.ES"></a><u>ES</u></big></dt>
<dd>OpenGL <u>ES</u> profile - only a subset of the base OpenGL functionality is available<br><br>

</dd>
<dt><big><a name="GLSettings.Profile.None"></a><u>None</u></big></dt>
<dd>profile depends on platform<br><br>

</dd>
</dl>
</dd>
<dt><big><a name="GLSettings.majorVersion"></a>ubyte <u>majorVersion</u>;
</big></dt>
<dd>Major Version of OpenGL. 0 means highest possible.<br><br>

@@ -38,6 +60,11 @@ <h1>Dgame.Window.GLSettings</h1>

Typical values are 2, 4 or 8<br><br>

</dd>
<dt><big><a name="GLSettings.profile"></a>Profile <u>profile</u>;
</big></dt>
<dd>The OpenGL Context <u>profile</u>. Default is Profile.Compatibility<br><br>

</dd>
<dt><big><a name="GLSettings.this"></a>pure nothrow @nogc this(ubyte <i>major</i>, ubyte <i>minor</i>, ubyte <i>antiAlias</i>);
</big></dt>

0 comments on commit 206facd

Please sign in to comment.
You can’t perform that action at this time.