1 /* 2 * BSD 3-Clause License 3 * 4 * Copyright (c) 2016, Mango-Engine Team 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * * Redistributions of source code must retain the above copyright notice, this 11 * list of conditions and the following disclaimer. 12 * 13 * * Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * * Neither the name of the copyright holder nor the names of its 18 * contributors may be used to endorse or promote products derived from 19 * this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 module mango_engine.graphics.renderer; 33 34 import mango_engine.mango; 35 import mango_engine.game; 36 import mango_engine.event.core; 37 import mango_engine.graphics.backend; 38 import mango_engine.graphics.model; 39 import mango_engine.graphics.scene; 40 41 /++ 42 The main class that handles rendering 43 on the screen. 44 +/ 45 abstract class Renderer { 46 private GameManager _game; 47 private shared Scene _scene; 48 49 @property GameManager game() @trusted nothrow { return cast(GameManager) _game; } 50 51 /// The scene that is currently being rendered. 52 @property Scene scene() @trusted nothrow { return cast(Scene) _scene; } 53 /// The scene that is currently being rendered. 54 void setScene(Scene scene) @trusted nothrow { 55 scene.isRendering = true; 56 if(_scene !is null) 57 _scene.isRendering = false; 58 59 _scene = cast(shared) scene; 60 } 61 62 protected this(GameManager game) @safe { 63 this._game = game; 64 65 game.eventManager.registerEventHook(TickEvent.classinfo.name, 66 EventHook(&this.evtHook_render, false) 67 ); // TODO: PROCESS RENDERING IN SEPERATE THREAD! 68 } 69 70 static Renderer rendererFactory(GameManager game, GraphicsBackendType backend) @safe { 71 import mango_engine.graphics.opengl.gl_renderer : GLRenderer; 72 73 mixin(GenFactory!("Renderer", "game")); 74 } 75 76 private void evtHook_render(Event e) @system { 77 render(); 78 } 79 80 /// Render the scene 81 final void render() @trusted { 82 prepareRender(); 83 foreach(model; scene.models) { 84 renderModel(cast(shared) model); //TODO: synchronization 85 } 86 finishRender(); 87 } 88 89 protected void prepareRender() @system { 90 91 } 92 protected void finishRender() @system { 93 94 } 95 96 protected abstract void renderModel(shared Model model) @system; 97 }