Skip to content

Commit

Permalink
Merge pull request #63 from outlandkarasu/62-add-window
Browse files Browse the repository at this point in the history
close #62 add window
  • Loading branch information
outlandkarasu authored Sep 21, 2019
2 parents 0934e94 + 4b068c6 commit 0bdd49b
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 15 deletions.
44 changes: 42 additions & 2 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,57 @@ Application module.
*/
module app;

import std.stdio : writeln;
import std.string : toStringz;

import warabe : usingWarabe;

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

import warabe.sdl : enforceSdl;
import warabe.sdl :
createWindow,
destroyWindow,
WindowFlags,
WindowPos;

/**
Main function.
*/
void main()
{
usingWarabe!({
writeln("Warabe!");
auto window = createWindow(
toStringz(""),
WindowPos.centered,
WindowPos.centered,
640,
480,
WindowFlags.shown).enforceSdl;
scope(exit) destroyWindow(window);

while (processEvent())
{
delay(16);
}
});
}

bool processEvent() @nogc nothrow
{
Event event;
if (!pollEvent(event))
{
return true;
}

switch (event.type)
{
case EventType.quit:
return false;
default:
break;
}

return true;
}

4 changes: 2 additions & 2 deletions source/warabe/initialize.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import warabe.sdl :
enforceSdl,
init,
quit,
SdlInit,
InitFlags,
usingSdl;

/**
Expand All @@ -21,7 +21,7 @@ Params:
void usingWarabe(alias F)() if (isCallable!F)
{
usingSdl!({
enforceSdl(init(SdlInit.everything));
enforceSdl(init(InitFlags.everything));
scope(exit) quit();

F();
Expand Down
112 changes: 112 additions & 0 deletions source/warabe/sdl/events.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
Events module.
*/
module warabe.sdl.events;

import sdl = bindbc.sdl;
import bindbc.sdl : SDL_PollEvent;

alias Event = sdl.SDL_Event;
alias CommonEvent = sdl.SDL_CommonEvent;
alias WindowEvent = sdl.SDL_WindowEvent;
alias KeyboardEvent = sdl.SDL_KeyboardEvent;
alias TextEditingEvent = sdl.SDL_TextEditingEvent;
alias TextInputEvent = sdl.SDL_TextInputEvent;
alias MouseMotionEvent = sdl.SDL_MouseMotionEvent;
alias MouseButtonEvent = sdl.SDL_MouseButtonEvent;
alias MouseWheelEvent = sdl.SDL_MouseWheelEvent;
alias JoyAxisEvent = sdl.SDL_JoyAxisEvent;
alias JoyBallEvent = sdl.SDL_JoyBallEvent;
alias JoyHatEvent = sdl.SDL_JoyHatEvent;
alias JoyButtonEvent = sdl.SDL_JoyButtonEvent;
alias JoyDeviceEvent = sdl.SDL_JoyDeviceEvent;
alias ControllerAxisEvent = sdl.SDL_ControllerAxisEvent;
alias ControllerButtonEvent = sdl.SDL_ControllerButtonEvent;
alias ControllerDeviceEvent = sdl.SDL_ControllerDeviceEvent;
alias QuitEvent = sdl.SDL_QuitEvent;
alias UserEvent = sdl.SDL_UserEvent;
alias SysWMEvent = sdl.SDL_SysWMEvent;
alias TouchFingerEvent = sdl.SDL_TouchFingerEvent;
alias MultiGestureEvent = sdl.SDL_MultiGestureEvent;
alias DollarGestureEvent = sdl.SDL_DollarGestureEvent;

/**
SDL event type enum.
*/
enum EventType : sdl.SDL_EventType
{
firstEvent = sdl.SDL_FIRSTEVENT,
quit = sdl.SDL_QUIT,

appTerminating = sdl.SDL_APP_TERMINATING,
appLowMemory = sdl.SDL_APP_LOWMEMORY,
appWillEnterBackground = sdl.SDL_APP_WILLENTERBACKGROUND,
appDidEnterBackground = sdl.SDL_APP_DIDENTERBACKGROUND,
appWillEnterForeground = sdl.SDL_APP_WILLENTERFOREGROUND,
appDidEnterForeground = sdl.SDL_APP_DIDENTERFOREGROUND,

windowEvent = sdl.SDL_WINDOWEVENT,
sysWMEvent = sdl.SDL_SYSWMEVENT,

keyDown = sdl.SDL_KEYDOWN,
keyUp = sdl.SDL_KEYUP,
textEditing = sdl.SDL_TEXTEDITING,
textInput = sdl.SDL_TEXTINPUT,

mouseMotion = sdl.SDL_MOUSEMOTION,
mouseButtonDown = sdl.SDL_MOUSEBUTTONDOWN,
mouseButtonUp = sdl.SDL_MOUSEBUTTONUP,
mouseWheel = sdl.SDL_MOUSEWHEEL,

joyAxisMotion = sdl.SDL_JOYAXISMOTION,
joyBallMotion = sdl.SDL_JOYBALLMOTION,
joyHatMotion = sdl.SDL_JOYHATMOTION,
joyButtonDown = sdl.SDL_JOYBUTTONDOWN,
joyButtonUp = sdl.SDL_JOYBUTTONUP,
joyDeviceAdded = sdl.SDL_JOYDEVICEADDED,
joyDeviceRemoved = sdl.SDL_JOYDEVICEREMOVED,

controllerAxisMotion = sdl.SDL_CONTROLLERAXISMOTION,
controllerButtonDown = sdl.SDL_CONTROLLERBUTTONDOWN,
controllerButtonUp = sdl.SDL_CONTROLLERBUTTONUP,
controllerDeviceAdded = sdl.SDL_CONTROLLERDEVICEADDED,
controllerDeviceRemoved = sdl.SDL_CONTROLLERDEVICEREMOVED,
controllerDeviceRemapped = sdl.SDL_CONTROLLERDEVICEREMAPPED,
fingerDown = sdl.SDL_FINGERDOWN,
fingerUp = sdl.SDL_FINGERUP,
fingerMotion = sdl.SDL_FINGERMOTION,

dollarGesture = sdl.SDL_DOLLARGESTURE,
dollarRecord = sdl.SDL_DOLLARRECORD,
multiGesture = sdl.SDL_MULTIGESTURE,

clipBoardUpdate = sdl.SDL_CLIPBOARDUPDATE,

userEvent = sdl.SDL_USEREVENT,
lastEvent = sdl.SDL_LASTEVENT,
}

/**
poll event.
Params:
event = filled event
Returns:
true if there is a pending event.
*/
bool pollEvent(out Event event) @nogc nothrow
{
return SDL_PollEvent(&event) == 1;
}

/**
poll event.
Returns:
true if there is a pending event.
*/
bool pollEvent() @nogc nothrow
{
return SDL_PollEvent(null) == 1;
}

12 changes: 6 additions & 6 deletions source/warabe/sdl/exception.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import std.traits : Unqual;
import bindbc.sdl : SDL_GetError;

import warabe.exception : WarabeException;
import warabe.sdl.types: SdlResult;
import warabe.sdl.types : Result;

/**
SDL related exception.
Expand Down Expand Up @@ -60,7 +60,7 @@ T enforceSdl(string file = __FILE__, size_t line = __LINE__, T)(T result)
try
{
import warabe.sdl : usingSdl;
immutable result = SdlResult(1);
immutable result = Result(1);
usingSdl!({ enforceSdl(result); });
}
catch(SdlException e)
Expand All @@ -86,7 +86,7 @@ Returns:
@nogc nothrow pure @safe
bool isSdlFailed(T)(T result)
{
static if (is(Unqual!T == SdlResult))
static if (is(Unqual!T == Result))
{
return result != 0;
}
Expand All @@ -103,9 +103,9 @@ bool isSdlFailed(T)(T result)
///
@nogc nothrow pure @safe unittest
{
assert(!isSdlFailed(SdlResult(0)));
assert( isSdlFailed(SdlResult(-1)));
assert( isSdlFailed(SdlResult(1)));
assert(!isSdlFailed(Result(0)));
assert( isSdlFailed(Result(-1)));
assert( isSdlFailed(Result(1)));

string value = "abc";
assert( isSdlFailed(null));
Expand Down
9 changes: 5 additions & 4 deletions source/warabe/sdl/initialize.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import bindbc.sdl :

import bindbc.sdl : Uint32;

import warabe.sdl.types : SdlResult;
import warabe.sdl.types : Result;

/**
SDL initialize flags.
*/
enum SdlInit : Uint32
enum InitFlags : Uint32
{
nothing = 0,
timer = SDL_INIT_TIMER,
Expand All @@ -46,9 +46,9 @@ Params:
Returns:
initialized status.
*/
SdlResult init(SdlInit flags) @nogc nothrow
Result init(InitFlags flags) @nogc nothrow
{
return SdlResult(SDL_Init(flags));
return Result(SDL_Init(flags));
}

/**
Expand All @@ -58,3 +58,4 @@ void quit() @nogc nothrow
{
SDL_Quit();
}

3 changes: 3 additions & 0 deletions source/warabe/sdl/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ SDL package module.
*/
module warabe.sdl;

public import warabe.sdl.events;
public import warabe.sdl.exception;
public import warabe.sdl.initialize;
public import warabe.sdl.load;
public import warabe.sdl.timer;
public import warabe.sdl.types;
public import warabe.sdl.window;

18 changes: 18 additions & 0 deletions source/warabe/sdl/timer.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
Timer module.
*/
module warabe.sdl.timer;

import bindbc.sdl : SDL_Delay;

/**
Delay timer.
Params:
ms = milliseconds.
*/
void delay(uint ms) @nogc nothrow
{
SDL_Delay(ms);
}

2 changes: 1 addition & 1 deletion source/warabe/sdl/types.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import std.typecons : Typedef;
/**
SDL result type.
*/
alias SdlResult = Typedef!(uint, uint.init, "SdlResult");
alias Result = Typedef!(uint, uint.init, "warabe.sdl.types.Result");

95 changes: 95 additions & 0 deletions source/warabe/sdl/window.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
Window module.
*/
module warabe.sdl.window;

import bindbc.sdl :
SDL_CreateWindow,
SDL_DestroyWindow;

import bindbc.sdl :
SDL_WINDOW_FULLSCREEN,
SDL_WINDOW_FULLSCREEN_DESKTOP,
SDL_WINDOW_OPENGL,
SDL_WINDOW_HIDDEN,
SDL_WINDOW_BORDERLESS,
SDL_WINDOW_RESIZABLE,
SDL_WINDOW_MINIMIZED,
SDL_WINDOW_MAXIMIZED,
SDL_WINDOW_INPUT_GRABBED,
SDL_WINDOW_SHOWN;

import bindbc.sdl :
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_UNDEFINED;

import bindbc.sdl :
SDL_Window,
SDL_WindowFlags,
Uint32;

/**
Window type.
*/
alias Window = SDL_Window;

/**
Window flags.
*/
enum WindowFlags : SDL_WindowFlags
{
fullscrenn = SDL_WINDOW_FULLSCREEN,
fullscreenDesktop = SDL_WINDOW_FULLSCREEN_DESKTOP,
openGL = SDL_WINDOW_OPENGL,
hidden = SDL_WINDOW_HIDDEN,
borderless = SDL_WINDOW_BORDERLESS,
resizable = SDL_WINDOW_RESIZABLE,
minimized = SDL_WINDOW_MINIMIZED,
maximized = SDL_WINDOW_MAXIMIZED,
inputGrabbed = SDL_WINDOW_INPUT_GRABBED,
shown = SDL_WINDOW_SHOWN,
}

/**
Window position constants.
*/
enum WindowPos : int
{
centered = SDL_WINDOWPOS_CENTERED,
undefined = SDL_WINDOWPOS_UNDEFINED,
}

/**
Create a window.
Params:
tile = window title.
x = window position x.
y = window position y.
w = window width.
h = window height.
flags = window flags.
*/
Window* createWindow(
scope const(char)* title,
int x,
int y,
int w,
int h,
WindowFlags flags) @nogc nothrow
{
return SDL_CreateWindow(title, x, y, w, h, flags);
}

/**
Destroy window.
Params:
window = destroy window and clear pointer.
*/
void destroyWindow(scope ref Window* window) @nogc nothrow
{
SDL_DestroyWindow(window);
window = null;
}

0 comments on commit 0bdd49b

Please sign in to comment.