[update] Added Context methods related to framebuffers.

This commit is contained in:
falsycat 2019-10-09 00:00:00 +00:00
parent 93ac2c1e2a
commit e485bf4ef5
3 changed files with 32 additions and 8 deletions

View File

@ -15,6 +15,7 @@ import sjplayer.Actor,
sjplayer.ContextBuilderInterface, sjplayer.ContextBuilderInterface,
sjplayer.ElementDrawerInterface, sjplayer.ElementDrawerInterface,
sjplayer.ElementInterface, sjplayer.ElementInterface,
sjplayer.PostEffect,
sjplayer.ProgramSet, sjplayer.ProgramSet,
sjplayer.ScheduledControllerInterface, sjplayer.ScheduledControllerInterface,
sjplayer.VarStore; sjplayer.VarStore;
@ -23,9 +24,10 @@ import sjplayer.Actor,
class Context { class Context {
public: public:
/// ///
this(ParametersBlock[] params, ProgramSet programs) { this(ParametersBlock[] params, vec2i window_size, ProgramSet programs) {
actor_ = new Actor(programs.Get!ActorProgram); actor_ = new Actor(programs.Get!ActorProgram);
background_ = new Background(programs.Get!BackgroundProgram); background_ = new Background(programs.Get!BackgroundProgram);
posteffect_ = new PostEffect(programs.Get!PostEffectProgram, window_size);
auto builder = new Builder; auto builder = new Builder;
auto varstore = new VarStore(actor_); auto varstore = new VarStore(actor_);
@ -60,10 +62,14 @@ class Context {
/// ///
~this() { ~this() {
controllers_.each!destroy; controllers_.each!destroy;
actor_controller_.destroy();
drawers_.each!destroy; drawers_.each!destroy;
elements_.each!destroy; elements_.each!destroy;
actor_.destroy();
background_.destroy(); background_.destroy();
posteffect_.destroy();
} }
/// ///
@ -80,6 +86,16 @@ class Context {
controllers_.each!(x => x.Operate(time)); controllers_.each!(x => x.Operate(time));
} }
///
void StartDrawing() {
posteffect_.BindFramebuffer();
}
///
void EndDrawing() {
posteffect_.UnbindFramebuffer();
posteffect_.DrawFramebuffer();
}
/// ///
void DrawBackground() { void DrawBackground() {
background_.Draw(); background_.Draw();
@ -119,6 +135,9 @@ class Context {
Background background_; Background background_;
invariant(background_); invariant(background_);
PostEffect posteffect_;
invariant(posteffect_);
ElementInterface[] elements_; ElementInterface[] elements_;
ElementDrawerInterface[] drawers_; ElementDrawerInterface[] drawers_;

View File

@ -1,6 +1,8 @@
/// License: MIT /// License: MIT
module sjplayer; module sjplayer;
import gl4d;
import sjscript; import sjscript;
public { public {
@ -9,11 +11,11 @@ public {
} }
/// ///
Context CreateContextFromText(string src, ProgramSet programs) { Context CreateContextFromText(string src, vec2i window_size, ProgramSet programs) {
return src.CreateScriptAst().CreateContextFromScriptAst(programs); return src.CreateScriptAst().CreateContextFromScriptAst(window_size, programs);
} }
/// ///
Context CreateContextFromScriptAst( Context CreateContextFromScriptAst(
ParametersBlock[] params, ProgramSet programs) { ParametersBlock[] params, vec2i window_size, ProgramSet programs) {
return new Context(params, programs); return new Context(params, window_size, programs);
} }

View File

@ -25,7 +25,8 @@ int main(string[] args) {
auto programs = new ProgramSet; auto programs = new ProgramSet;
scope(exit) programs.destroy(); scope(exit) programs.destroy();
auto context = script_file.readText.CreateContextFromText(programs); auto context = script_file.readText.
CreateContextFromText(vec2i(600, 600), programs);
scope(exit) context.destroy(); scope(exit) context.destroy();
while (true) { while (true) {
@ -39,10 +40,13 @@ int main(string[] args) {
context.OperateScheduledControllers(beat); context.OperateScheduledControllers(beat);
context.UpdateActor(vec2(0, 0)); context.UpdateActor(vec2(0, 0));
gl.Clear(GL_COLOR_BUFFER_BIT); context.StartDrawing();
context.DrawBackground(); context.DrawBackground();
context.DrawElements(); context.DrawElements();
context.DrawActor(); context.DrawActor();
context.EndDrawing();
sfWindow_display(win); sfWindow_display(win);
} }
return 0; return 0;
@ -70,6 +74,5 @@ sfWindow* Initialize() {
gl.ApplyContext(); gl.ApplyContext();
gl.Enable(GL_BLEND); gl.Enable(GL_BLEND);
gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return win; return win;
} }