1 /*
2 Copyright (c) 2019-2022 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 std.stdio;
31 
32 import dlib.core.memory;
33 import dlib.core.ownership;
34 
35 import dagon.core.bindings;
36 import dagon.core.event;
37 import dagon.core.time;
38 import dagon.core.application;
39 import dagon.core.config;
40 import dagon.graphics.state;
41 import dagon.graphics.entity;
42 import dagon.resource.scene;
43 import dagon.game.renderer;
44 import dagon.game.deferredrenderer;
45 import dagon.game.postprocrenderer;
46 import dagon.game.presentrenderer;
47 import dagon.game.hudrenderer;
48 
49 version(Windows)
50 {
51     pragma(lib, "user32");
52     import core.sys.windows.windows;
53 }
54 
55 class Game: Application
56 {
57     Scene currentScene;
58 
59     Renderer renderer;
60     DeferredRenderer deferredRenderer;
61     PostProcRenderer postProcessingRenderer;
62     PresentRenderer presentRenderer;
63     HUDRenderer hudRenderer;
64     
65     alias deferred = deferredRenderer;
66     alias postProc = postProcessingRenderer;
67     alias present = presentRenderer;
68     alias hud = hudRenderer;
69     
70     Configuration config;
71     
72     bool dynamicViewport = true;
73 
74     this(uint w, uint h, bool fullscreen, string title, string[] args)
75     {
76         config = New!Configuration(this);
77         if (config.fromFile("settings.conf"))
78         {
79             if ("windowWidth" in config.props)
80                 w = config.props["windowWidth"].toUInt;
81             if ("windowHeight" in config.props)
82                 h = config.props["windowHeight"].toUInt;
83             if ("fullscreen" in config.props)
84                 fullscreen = cast(bool)(config.props["fullscreen"].toUInt);
85             if ("windowTitle" in config.props)
86                 title = config.props["windowTitle"].toString;
87             version(Windows) if ("hideConsole" in config.props)
88                 if (config.props["hideConsole"].toUInt)
89                     ShowWindow(GetConsoleWindow(), SW_HIDE);
90         }
91         else
92         {
93             writeln("Warning: no \"settings.conf\" found");
94         }
95         
96         super(w, h, fullscreen, title, args);
97         
98         deferredRenderer = New!DeferredRenderer(eventManager, this);
99         renderer = deferredRenderer;
100         postProcessingRenderer = New!PostProcRenderer(eventManager, deferredRenderer.outputBuffer, deferredRenderer.gbuffer, this);
101         presentRenderer = New!PresentRenderer(eventManager, postProcessingRenderer.outputBuffer, this);
102         hudRenderer = New!HUDRenderer(eventManager, this);
103         
104         renderer.setViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
105         postProcessingRenderer.setViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
106         presentRenderer.setViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
107         hudRenderer.setViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
108         
109         deferredRenderer.ssaoEnabled = false;
110         postProcessingRenderer.motionBlurEnabled = false;
111         postProcessingRenderer.glowEnabled = false;
112         postProcessingRenderer.fxaaEnabled = false;
113         postProcessingRenderer.lutEnabled = false;
114         postProcessingRenderer.lensDistortionEnabled = false;
115     }
116 
117     override void onUpdate(Time t)
118     {
119         if (currentScene)
120         {
121             currentScene.update(t);
122             
123             renderer.scene = currentScene;
124             renderer.update(t);
125             
126             postProcessingRenderer.activeCamera = renderer.activeCamera;
127             postProcessingRenderer.update(t);
128             
129             presentRenderer.scene = currentScene;
130             presentRenderer.update(t);
131             
132             hudRenderer.scene = currentScene;
133             hudRenderer.update(t);
134         }
135     }
136 
137     override void onRender()
138     {
139         if (currentScene)
140         {
141             if (currentScene.canRender)
142             {
143                 renderer.render();
144                 postProcessingRenderer.render();
145                 presentRenderer.inputBuffer = postProcessingRenderer.outputBuffer;
146                 presentRenderer.render();
147                 hudRenderer.render();
148             }
149         }
150     }
151     
152     void resize(int width, int height)
153     {
154         renderer.setViewport(0, 0, width, height);
155         postProcessingRenderer.setViewport(0, 0, width, height);
156         presentRenderer.setViewport(0, 0, width, height);
157         hudRenderer.setViewport(0, 0, width, height);
158     }
159     
160     override void onResize(int width, int height)
161     {
162         if (dynamicViewport)
163             resize(width, height);
164     }
165     
166     GLuint frameTexture() @property
167     {
168         return presentRenderer.inputBuffer.colorTexture;
169     }
170 }