diff --git a/source/app.d b/source/app.d index 22d63c4..4e7181d 100644 --- a/source/app.d +++ b/source/app.d @@ -7,14 +7,25 @@ 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. @@ -22,19 +33,30 @@ 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); + } + }); }); } diff --git a/source/warabe/initialize.d b/source/warabe/initialize.d index b3ea59d..59e51cd 100644 --- a/source/warabe/initialize.d +++ b/source/warabe/initialize.d @@ -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. @@ -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(); diff --git a/source/warabe/opengl/exception.d b/source/warabe/opengl/exception.d new file mode 100644 index 0000000..95850f4 --- /dev/null +++ b/source/warabe/opengl/exception.d @@ -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); + } +} + diff --git a/source/warabe/opengl/load.d b/source/warabe/opengl/load.d new file mode 100644 index 0000000..e45a359 --- /dev/null +++ b/source/warabe/opengl/load.d @@ -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(); +} + diff --git a/source/warabe/opengl/package.d b/source/warabe/opengl/package.d new file mode 100644 index 0000000..31b4b97 --- /dev/null +++ b/source/warabe/opengl/package.d @@ -0,0 +1,7 @@ +/** +OpenGL package. +*/ +module warabe.opengl; + +public import warabe.opengl.load; + diff --git a/source/warabe/sdl/exception.d b/source/warabe/sdl/exception.d index dbe699d..b2c1c26 100644 --- a/source/warabe/sdl/exception.d +++ b/source/warabe/sdl/exception.d @@ -14,7 +14,7 @@ import warabe.sdl.types : Result; /** SDL related exception. */ -class SdlException : WarabeException +class SDLException : WarabeException { /** construct by message. @@ -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; } @@ -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. } @@ -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)) { @@ -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])); } diff --git a/source/warabe/sdl/load.d b/source/warabe/sdl/load.d index d46ff8c..a687092 100644 --- a/source/warabe/sdl/load.d +++ b/source/warabe/sdl/load.d @@ -7,7 +7,7 @@ import std.traits : isCallable; static import bindbc.sdl; -import warabe.sdl.exception : SdlException; +import warabe.sdl.exception : SDLException; /** load SDL library and using it. @@ -15,13 +15,12 @@ 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: @@ -29,7 +28,7 @@ private: /** load SDL library. */ -void loadSdl() @system +void loadSDL() @system { immutable loadedVersion = bindbc.sdl.loadSDL(); if (loadedVersion != bindbc.sdl.sdlSupport) @@ -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(); } diff --git a/source/warabe/sdl/opengl.d b/source/warabe/sdl/opengl.d new file mode 100644 index 0000000..47ab901 --- /dev/null +++ b/source/warabe/sdl/opengl.d @@ -0,0 +1,127 @@ +/** +SDL OpenGL module. +*/ +module warabe.sdl.opengl; + +import sdl = bindbc.sdl; +import bindbc.sdl : + SDL_GL_CreateContext, + SDL_GL_DeleteContext, + SDL_GL_SetAttribute, + SDL_GL_SetSwapInterval, + SDL_GL_SwapWindow; + +import warabe.sdl.types : Result; +import warabe.sdl.window : Window; + +enum GLAttr : sdl.SDL_GLattr +{ + redSize = sdl.SDL_GL_RED_SIZE, + greenSize = sdl.SDL_GL_GREEN_SIZE, + blueSize = sdl.SDL_GL_BLUE_SIZE, + alphaSize = sdl.SDL_GL_ALPHA_SIZE, + bufferSize = sdl.SDL_GL_BUFFER_SIZE, + doubleBuffer = sdl.SDL_GL_DOUBLEBUFFER, + depthSize = sdl.SDL_GL_DEPTH_SIZE, + stencilSize = sdl.SDL_GL_STENCIL_SIZE, + accumRedSize = sdl.SDL_GL_ACCUM_RED_SIZE, + accumGreenSize = sdl.SDL_GL_ACCUM_GREEN_SIZE, + accumBlueSize = sdl.SDL_GL_ACCUM_BLUE_SIZE, + accumAlphaSize = sdl.SDL_GL_ACCUM_ALPHA_SIZE, + stereo = sdl.SDL_GL_STEREO, + multiSampleBuffers = sdl.SDL_GL_MULTISAMPLEBUFFERS, + multiSampleSamples = sdl.SDL_GL_MULTISAMPLESAMPLES, + acceleratedVisual = sdl.SDL_GL_ACCELERATED_VISUAL, + contextMajorVersion = sdl.SDL_GL_CONTEXT_MAJOR_VERSION, + contextMinorVersion = sdl.SDL_GL_CONTEXT_MINOR_VERSION, + contextFlags = sdl.SDL_GL_CONTEXT_FLAGS, + contextProfileMask = sdl.SDL_GL_CONTEXT_PROFILE_MASK, + shareWithCurrentContext = sdl.SDL_GL_SHARE_WITH_CURRENT_CONTEXT, +} + +enum GLContextFlag : sdl.SDL_GLcontextFlag +{ + debugFlag = sdl.SDL_GL_CONTEXT_DEBUG_FLAG, + forwardCompatibleFlag = sdl.SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG, + robustAccessFlag = sdl.SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG, + resetIsorationFlag = sdl.SDL_GL_CONTEXT_RESET_ISOLATION_FLAG, +} + +enum GLProfile : sdl.SDL_GLprofile +{ + core = sdl.SDL_GL_CONTEXT_PROFILE_CORE, + compatibility = sdl.SDL_GL_CONTEXT_PROFILE_COMPATIBILITY, + es = sdl.SDL_GL_CONTEXT_PROFILE_ES, +} + +enum GLSwapInterval : int +{ + immediate = 0, + vsync = 1, + adaptiveVsync = -1, +} + +alias GLContext = sdl.SDL_GLContext; + +/** +set OpenGL attribute. + +Params: + attribute = OpenGL attribute. + value = attribute value. +Returns: + set attribute result. +*/ +Result GLSetAttribute(GLAttr attribute, int value) @nogc nothrow +{ + return Result(SDL_GL_SetAttribute(attribute, value)); +} + +/** +set OpenGL swap interval. + +Params: + interval = swap interval flag.. +Returns: + set interval result. +*/ +Result GLSetSwapInterval(GLSwapInterval interval) @nogc nothrow +{ + return Result(SDL_GL_SetSwapInterval(interval)); +} + +/** +create OpenGL context. + +Params: + window = OpenGL window. +Returns: + OpenGL context. +*/ +GLContext GLCreateContext(Window* window) @nogc nothrow +{ + return SDL_GL_CreateContext(window); +} + +/** +delete OpenGL context. + +Params: + context = OpenGL context. +*/ +void GLDeleteContext(GLContext context) @nogc nothrow +{ + SDL_GL_DeleteContext(context); +} + +/** +swap window. + +Params: + window = target window. +*/ +void GLSwapWindow(Window* window) @nogc nothrow +{ + SDL_GL_SwapWindow(window); +} + diff --git a/source/warabe/sdl/package.d b/source/warabe/sdl/package.d index bf41843..411130d 100644 --- a/source/warabe/sdl/package.d +++ b/source/warabe/sdl/package.d @@ -7,6 +7,7 @@ public import warabe.sdl.events; public import warabe.sdl.exception; public import warabe.sdl.initialize; public import warabe.sdl.load; +public import warabe.sdl.opengl; public import warabe.sdl.timer; public import warabe.sdl.types; public import warabe.sdl.window;