Skip to content

Commit

Permalink
Merge pull request #65 from outlandkarasu/64-create-opengl-context
Browse files Browse the repository at this point in the history
close #64 create opengl context
  • Loading branch information
outlandkarasu committed Sep 21, 2019
2 parents 0bdd49b + 4160e13 commit 87694fc
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 32 deletions.
34 changes: 28 additions & 6 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,56 @@ import std.string : toStringz;

import warabe : usingWarabe;

import warabe.opengl : usingOpenGL;

import warabe.sdl : delay, pollEvent, Event, EventType;

import warabe.sdl : enforceSdl;
import warabe.sdl : enforceSDL;
import warabe.sdl :
createWindow,
destroyWindow,
WindowFlags,
WindowPos;
import warabe.sdl.opengl :
GLAttr,
GLCreateContext,
GLDeleteContext,
GLProfile,
GLSetAttribute,
GLSetSwapInterval,
GLSwapInterval,
GLSwapWindow;

/**
Main function.
*/
void main()
{
usingWarabe!({
GLSetAttribute(GLAttr.contextMajorVersion, 2);
GLSetAttribute(GLAttr.contextMinorVersion, 0);
GLSetAttribute(GLAttr.contextProfileMask, GLProfile.core);
GLSetSwapInterval(GLSwapInterval.adaptiveVsync);

auto window = createWindow(
toStringz(""),
WindowPos.centered,
WindowPos.centered,
640,
480,
WindowFlags.shown).enforceSdl;
WindowFlags.shown | WindowFlags.openGL).enforceSDL;
scope(exit) destroyWindow(window);

while (processEvent())
{
delay(16);
}
auto glContext = GLCreateContext(window);
scope(exit) GLDeleteContext(glContext);

usingOpenGL!({
while (processEvent())
{
delay(16);
GLSwapWindow(window);
}
});
});
}

Expand Down
8 changes: 4 additions & 4 deletions source/warabe/initialize.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ module warabe.initialize;
import std.traits : isCallable;

import warabe.sdl :
enforceSdl,
enforceSDL,
init,
quit,
InitFlags,
usingSdl;
usingSDL;

/**
initialize Warabe and using it.
Expand All @@ -20,8 +20,8 @@ Params:
*/
void usingWarabe(alias F)() if (isCallable!F)
{
usingSdl!({
enforceSdl(init(InitFlags.everything));
usingSDL!({
enforceSDL(init(InitFlags.everything));
scope(exit) quit();

F();
Expand Down
32 changes: 32 additions & 0 deletions source/warabe/opengl/exception.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
Warabe OpenGL exception.
*/
module warabe.opengl.exception;

import std.string : fromStringz;

import bindbc.opengl : glGetError;

import warabe.exception : WarabeException;

/**
OpenGL related exception.
*/
class OpenGLException : WarabeException
{
/**
construct by message.
Params:
msg = exception message.
file = file name.
line = source line number.
nextInChain = exception chain.
*/
@nogc nothrow pure @safe
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null)
{
super(msg, file, line, nextInChain);
}
}

55 changes: 55 additions & 0 deletions source/warabe/opengl/load.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
OpenGL load module.
*/
module warabe.opengl.load;

import std.traits : isCallable;

static import bindbc.opengl;

import warabe.opengl.exception : OpenGLException;

/**
load OpenGL library and using it.
Params:
F = function using OpenGL.
*/
void usingOpenGL(alias F)() if(isCallable!F)
{
loadOpenGL();
scope(exit) unloadOpenGL();

F();
}

private:

/**
load OpenGL library.
*/
void loadOpenGL() @system
{
immutable loadedVersion = bindbc.opengl.loadOpenGL();
if (loadedVersion == bindbc.opengl.GLSupport.noLibrary)
{
throw new OpenGLException("OpenGL not found.");
}
else if (loadedVersion == bindbc.opengl.GLSupport.badLibrary)
{
throw new OpenGLException("OpenGL bad library.");
}
else if (loadedVersion == bindbc.opengl.GLSupport.noContext)
{
throw new OpenGLException("OpenGL no context.");
}
}

/**
unload OpenGL library.
*/
void unloadOpenGL() @system
{
bindbc.opengl.unloadOpenGL();
}

7 changes: 7 additions & 0 deletions source/warabe/opengl/package.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
OpenGL package.
*/
module warabe.opengl;

public import warabe.opengl.load;

28 changes: 14 additions & 14 deletions source/warabe/sdl/exception.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import warabe.sdl.types : Result;
/**
SDL related exception.
*/
class SdlException : WarabeException
class SDLException : WarabeException
{
/**
construct by message.
Expand Down Expand Up @@ -42,14 +42,14 @@ Params:
Returns:
result.
Throws:
SdlException if result is error.
SDLException if result is error.
*/
T enforceSdl(string file = __FILE__, size_t line = __LINE__, T)(T result)
T enforceSDL(string file = __FILE__, size_t line = __LINE__, T)(T result)
{
if (isSdlFailed(result))
if (isSDLFailed(result))
{
immutable message = fromStringz(SDL_GetError()).idup;
throw new SdlException(message, file, line);
throw new SDLException(message, file, line);
}
return result;
}
Expand All @@ -59,11 +59,11 @@ T enforceSdl(string file = __FILE__, size_t line = __LINE__, T)(T result)
{
try
{
import warabe.sdl : usingSdl;
import warabe.sdl : usingSDL;
immutable result = Result(1);
usingSdl!({ enforceSdl(result); });
usingSDL!({ enforceSDL(result); });
}
catch(SdlException e)
catch(SDLException e)
{
// expected exception.
}
Expand All @@ -84,7 +84,7 @@ Returns:
true if result is failed.
*/
@nogc nothrow pure @safe
bool isSdlFailed(T)(T result)
bool isSDLFailed(T)(T result)
{
static if (is(Unqual!T == Result))
{
Expand All @@ -103,12 +103,12 @@ bool isSdlFailed(T)(T result)
///
@nogc nothrow pure @safe unittest
{
assert(!isSdlFailed(Result(0)));
assert( isSdlFailed(Result(-1)));
assert( isSdlFailed(Result(1)));
assert(!isSDLFailed(Result(0)));
assert( isSDLFailed(Result(-1)));
assert( isSDLFailed(Result(1)));

string value = "abc";
assert( isSdlFailed(null));
assert(!isSdlFailed(&value[0]));
assert( isSDLFailed(null));
assert(!isSDLFailed(&value[0]));
}

15 changes: 7 additions & 8 deletions source/warabe/sdl/load.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@ import std.traits : isCallable;

static import bindbc.sdl;

import warabe.sdl.exception : SdlException;
import warabe.sdl.exception : SDLException;

/**
load SDL library and using it.
Params:
F = function using SDL.
*/
void usingSdl(alias F)() if (isCallable!F)
void usingSDL(alias F)() if(isCallable!F)
{
loadSdl();
loadSDL();
scope(exit) unloadSDL();

F();

scope(exit) unloadSdl();
}

private:

/**
load SDL library.
*/
void loadSdl() @system
void loadSDL() @system
{
immutable loadedVersion = bindbc.sdl.loadSDL();
if (loadedVersion != bindbc.sdl.sdlSupport)
Expand All @@ -44,14 +43,14 @@ void loadSdl() @system
message = "SDL2 bad library.";
}

throw new SdlException(message);
throw new SDLException(message);
}
}

/**
unload SDL library.
*/
void unloadSdl() @system
void unloadSDL() @system
{
bindbc.sdl.unloadSDL();
}
Expand Down
Loading

0 comments on commit 87694fc

Please sign in to comment.