2019-10-05 00:00:00 +00:00
|
|
|
/// License: MIT
|
|
|
|
import std;
|
|
|
|
|
|
|
|
import derelict.sfml2.audio,
|
|
|
|
derelict.sfml2.system,
|
|
|
|
derelict.sfml2.window;
|
|
|
|
|
|
|
|
import gl4d;
|
|
|
|
|
2019-10-06 00:00:00 +00:00
|
|
|
import sjplayer;
|
|
|
|
|
2019-10-05 00:00:00 +00:00
|
|
|
int main(string[] args) {
|
|
|
|
(args.length == 4).enforce;
|
|
|
|
const music_file = args[1];
|
|
|
|
const bpm = args[2].to!float;
|
|
|
|
const script_file = args[3];
|
|
|
|
|
|
|
|
auto win = Initialize();
|
|
|
|
scope(exit) sfWindow_destroy(win);
|
|
|
|
|
|
|
|
auto music = sfMusic_createFromFile(music_file.toStringz).enforce;
|
|
|
|
scope(exit) sfMusic_destroy(music);
|
|
|
|
|
2019-10-07 00:00:00 +00:00
|
|
|
auto programs = new ProgramSet;
|
2019-10-05 00:00:00 +00:00
|
|
|
scope(exit) programs.destroy();
|
|
|
|
|
2019-10-10 00:00:00 +00:00
|
|
|
Context context;
|
|
|
|
try {
|
|
|
|
context = script_file.readText.
|
|
|
|
CreateContextFromText(vec2i(600, 600), programs);
|
|
|
|
} catch (ScriptException e) {
|
|
|
|
"[parsing exception] %s at (%d,%d)".
|
|
|
|
writefln(e.msg, e.pos.stline+1, e.pos.stchar+1);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-10-07 00:00:00 +00:00
|
|
|
scope(exit) context.destroy();
|
2019-10-05 00:00:00 +00:00
|
|
|
|
2019-10-10 00:00:00 +00:00
|
|
|
sfMusic_play(music);
|
2019-10-05 00:00:00 +00:00
|
|
|
while (true) {
|
|
|
|
sfEvent e;
|
|
|
|
sfWindow_pollEvent(win, &e);
|
|
|
|
if (e.type == sfEvtClosed) break;
|
|
|
|
|
|
|
|
const msecs = sfMusic_getPlayingOffset(music).microseconds * 1e-6f;
|
|
|
|
const beat = msecs/60f * bpm;
|
|
|
|
|
2019-10-09 00:00:00 +00:00
|
|
|
context.actor.Accelarate(GetAccelarationInput());
|
|
|
|
|
2019-10-10 00:00:00 +00:00
|
|
|
try {
|
|
|
|
context.OperateScheduledControllers(beat);
|
|
|
|
} catch (ScriptRuntimeException e) {
|
|
|
|
"[runtime exception] %s at (%d,%d)".
|
|
|
|
writefln(e.msg, e.srcline+1, e.srcchar+1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-09 00:00:00 +00:00
|
|
|
context.actor.Update();
|
2019-10-09 00:00:00 +00:00
|
|
|
context.posteffect.Update();
|
2019-10-06 00:00:00 +00:00
|
|
|
|
2019-10-09 00:00:00 +00:00
|
|
|
const dmg = context.CalculateDamage();
|
|
|
|
if (dmg.damage != 0) {
|
|
|
|
"damage: %f (%f)".writefln(dmg.damage, beat);
|
2019-10-15 00:00:00 +00:00
|
|
|
context.posteffect.CauseDamagedEffect();
|
2019-10-09 00:00:00 +00:00
|
|
|
}
|
|
|
|
if (dmg.nearness != 0) {
|
2019-10-10 00:00:00 +00:00
|
|
|
"nearby: %f (%f)".writefln(dmg.nearness, beat);
|
2019-10-09 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 00:00:00 +00:00
|
|
|
gl.Clear(GL_COLOR_BUFFER_BIT);
|
2019-10-09 00:00:00 +00:00
|
|
|
context.StartDrawing();
|
|
|
|
|
2019-10-07 00:00:00 +00:00
|
|
|
context.DrawBackground();
|
2019-10-06 00:00:00 +00:00
|
|
|
context.DrawElements();
|
2019-10-08 00:00:00 +00:00
|
|
|
context.DrawActor();
|
2019-10-09 00:00:00 +00:00
|
|
|
|
|
|
|
context.EndDrawing();
|
2019-10-05 00:00:00 +00:00
|
|
|
sfWindow_display(win);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-09 00:00:00 +00:00
|
|
|
vec2 GetAccelarationInput() {
|
|
|
|
auto result = vec2(0, 0);
|
|
|
|
if (sfKeyboard_isKeyPressed(sfKeyLeft)) {
|
|
|
|
result.x -= 1;
|
|
|
|
}
|
|
|
|
if (sfKeyboard_isKeyPressed(sfKeyRight)) {
|
|
|
|
result.x += 1;
|
|
|
|
}
|
|
|
|
if (sfKeyboard_isKeyPressed(sfKeyUp)) {
|
|
|
|
result.y += 1;
|
|
|
|
}
|
|
|
|
if (sfKeyboard_isKeyPressed(sfKeyDown)) {
|
|
|
|
result.y -= 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-10-05 00:00:00 +00:00
|
|
|
sfWindow* Initialize() {
|
|
|
|
DerelictSFML2System.load();
|
|
|
|
DerelictSFML2Window.load();
|
|
|
|
DerelictSFML2Audio .load();
|
|
|
|
|
|
|
|
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(600, 600),
|
|
|
|
"sjplayer".toStringz, sfClose | sfTitlebar, &specs).enforce;
|
|
|
|
sfWindow_setVerticalSyncEnabled(win, true);
|
|
|
|
|
|
|
|
sfWindow_setActive(win, true).enforce;
|
2019-10-05 00:00:00 +00:00
|
|
|
|
2019-10-05 00:00:00 +00:00
|
|
|
gl.ApplyContext();
|
2019-10-05 00:00:00 +00:00
|
|
|
gl.Enable(GL_BLEND);
|
|
|
|
gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2019-10-05 00:00:00 +00:00
|
|
|
return win;
|
|
|
|
}
|