1 /*
2 Copyright (c) 2019 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.game.game;
29 
30 import dlib.core.memory;
31 import dlib.core.ownership;
32 
33 import dagon.core.bindings;
34 import dagon.core.event;
35 import dagon.core.time;
36 import dagon.core.application;
37 import dagon.graphics.state;
38 import dagon.graphics.entity;
39 import dagon.resource.scene;
40 import dagon.game.renderer;
41 import dagon.game.deferredrenderer;
42 import dagon.game.postprocrenderer;
43 import dagon.game.presentrenderer;
44 import dagon.game.hudrenderer;
45 
46 class Game: Application
47 {
48     // TODO: scene manager
49     Scene currentScene;
50 
51     Renderer renderer;
52     DeferredRenderer deferredRenderer;
53     PostProcRenderer postProcessingRenderer;
54     PresentRenderer presentRenderer;
55     HUDRenderer hudRenderer;
56     
57     alias deferred = deferredRenderer;
58     alias postProc = postProcessingRenderer;
59     alias present = presentRenderer;
60     alias hud = hudRenderer;
61 
62     this(uint w, uint h, bool fullscreen, string title, string[] args)
63     {
64         super(w, h, fullscreen, title, args);
65 
66         deferredRenderer = New!DeferredRenderer(eventManager, this);
67         renderer = deferredRenderer;
68         postProcessingRenderer = New!PostProcRenderer(eventManager, deferredRenderer.outputBuffer, deferredRenderer.gbuffer, this);
69         presentRenderer = New!PresentRenderer(eventManager, postProcessingRenderer.outputBuffer, this);
70         hudRenderer = New!HUDRenderer(eventManager, this);
71         
72         deferredRenderer.setViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
73         postProcessingRenderer.setViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
74         presentRenderer.setViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
75         hudRenderer.setViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
76         
77         deferredRenderer.ssaoEnabled = false;
78         postProcessingRenderer.motionBlurEnabled = false;
79         postProcessingRenderer.glowEnabled = false;
80         postProcessingRenderer.fxaaEnabled = false;
81         postProcessingRenderer.lutEnabled = false;
82         postProcessingRenderer.lensDistortionEnabled = false;
83     }
84 
85     override void onUpdate(Time t)
86     {
87         if (currentScene)
88         {
89             currentScene.update(t);
90             deferredRenderer.scene = currentScene;
91             hudRenderer.scene = currentScene;
92             deferredRenderer.update(t);
93             postProcessingRenderer.activeCamera = deferredRenderer.activeCamera;
94             postProcessingRenderer.update(t);
95             presentRenderer.update(t);
96             hudRenderer.update(t);
97         }
98     }
99 
100     override void onRender()
101     {
102         if (currentScene)
103         {
104             if (currentScene.canRender)
105             {
106                 deferredRenderer.render();
107                 postProcessingRenderer.render();
108                 presentRenderer.inputBuffer = postProcessingRenderer.outputBuffer;
109                 presentRenderer.render();
110                 hudRenderer.render();
111             }
112         }
113     }
114     
115     override void onResize(int width, int height)
116     {
117         deferredRenderer.setViewport(0, 0, width, height);
118         postProcessingRenderer.setViewport(0, 0, width, height);
119         presentRenderer.setViewport(0, 0, width, height);
120         hudRenderer.setViewport(0, 0, width, height);
121     }
122 }