2019-10-10 00:00:00 +00:00
|
|
|
/// License: MIT
|
2019-10-10 00:00:00 +00:00
|
|
|
import std;
|
2019-10-10 00:00:00 +00:00
|
|
|
|
2019-10-10 00:00:00 +00:00
|
|
|
import derelict.sfml2.audio,
|
2019-10-12 00:00:00 +00:00
|
|
|
derelict.sfml2.graphics,
|
2019-10-10 00:00:00 +00:00
|
|
|
derelict.sfml2.system,
|
|
|
|
derelict.sfml2.window;
|
|
|
|
|
2019-10-13 00:00:00 +00:00
|
|
|
import gl4d, ft4d;
|
2019-10-10 00:00:00 +00:00
|
|
|
|
|
|
|
import sj.Args,
|
|
|
|
sj.Game,
|
|
|
|
sj.KeyInput;
|
|
|
|
|
|
|
|
enum WindowTitle = "shapes-juke";
|
|
|
|
|
|
|
|
int main(string[] unparsed_args) {
|
|
|
|
Args args;
|
|
|
|
if (!ParseArgs(unparsed_args, args)) return 1;
|
|
|
|
|
|
|
|
auto win = CreateWindow(args);
|
2019-10-14 00:00:00 +00:00
|
|
|
auto game = new Game(args);
|
2019-10-10 00:00:00 +00:00
|
|
|
scope(exit) game.destroy();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
sfEvent e;
|
|
|
|
sfWindow_pollEvent(win, &e);
|
|
|
|
if (e.type == sfEvtClosed) break;
|
|
|
|
|
|
|
|
game.Update(GetKeyInput());
|
|
|
|
game.Draw();
|
|
|
|
win.Flush();
|
|
|
|
}
|
2019-10-10 00:00:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-10-10 00:00:00 +00:00
|
|
|
|
|
|
|
private bool ParseArgs(string[] unparsed_args, out Args args) {
|
|
|
|
auto helpinfo = unparsed_args.getopt(
|
|
|
|
"window-size", &args.window_size
|
|
|
|
);
|
|
|
|
|
|
|
|
auto valid = true;
|
|
|
|
if (args.window_size <= 100) {
|
|
|
|
"invalid window size (it should be 100 or more)".writeln;
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid || helpinfo.helpWanted) {
|
|
|
|
defaultGetoptPrinter(WindowTitle, helpinfo.options);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private KeyInput GetKeyInput() {
|
|
|
|
KeyInput result;
|
|
|
|
result.left = !!sfKeyboard_isKeyPressed(sfKeyLeft);
|
|
|
|
result.right = !!sfKeyboard_isKeyPressed(sfKeyRight);
|
|
|
|
result.up = !!sfKeyboard_isKeyPressed(sfKeyUp);
|
|
|
|
result.down = !!sfKeyboard_isKeyPressed(sfKeyDown);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private auto CreateWindow(ref in Args args) {
|
2019-10-13 00:00:00 +00:00
|
|
|
ft.Initialize();
|
|
|
|
|
2019-10-12 00:00:00 +00:00
|
|
|
DerelictSFML2Audio .load();
|
|
|
|
DerelictSFML2Graphics.load();
|
|
|
|
DerelictSFML2System .load();
|
|
|
|
DerelictSFML2Window .load();
|
2019-10-10 00:00:00 +00:00
|
|
|
|
|
|
|
sfContextSettings specs;
|
|
|
|
specs.depthBits = 24;
|
|
|
|
specs.stencilBits = 8;
|
|
|
|
specs.antialiasingLevel = 1;
|
|
|
|
specs.majorVersion = 3;
|
|
|
|
specs.minorVersion = 3;
|
|
|
|
specs.attributeFlags = sfContextCore;
|
|
|
|
|
|
|
|
auto win = sfWindow_create(
|
|
|
|
sfVideoMode(args.window_size, args.window_size),
|
|
|
|
WindowTitle.toStringz,
|
|
|
|
sfClose | sfTitlebar,
|
|
|
|
&specs).
|
|
|
|
enforce;
|
|
|
|
|
|
|
|
sfWindow_setVerticalSyncEnabled(win, true);
|
|
|
|
sfWindow_setActive(win, true).enforce;
|
|
|
|
|
|
|
|
gl.ApplyContext();
|
|
|
|
gl.Enable(GL_BLEND);
|
|
|
|
gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
|
|
|
static struct Window {
|
|
|
|
public:
|
|
|
|
~this() {
|
|
|
|
sfWindow_destroy(window_);
|
2019-10-13 00:00:00 +00:00
|
|
|
ft.Dispose();
|
2019-10-10 00:00:00 +00:00
|
|
|
}
|
|
|
|
void Flush() {
|
|
|
|
sfWindow_display(window_);
|
|
|
|
}
|
|
|
|
|
|
|
|
@property auto ptr() { return window_; }
|
|
|
|
alias ptr this;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ReturnType!sfWindow_create window_;
|
|
|
|
}
|
|
|
|
return Window(win);
|
|
|
|
}
|