1 /* 2 Copyright (c) 2014-2015 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module dgl.core.layer; 30 31 import std.stdio; 32 import std.conv; 33 34 import dlib.core.memory; 35 import dlib.container.array; 36 37 import derelict.opengl.gl; 38 import derelict.opengl.glu; 39 import derelict.sdl.sdl; 40 41 import dgl.core.event; 42 import dgl.core.interfaces; 43 import dgl.core.application; 44 45 enum LayerType 46 { 47 Layer2D, 48 Layer3D 49 } 50 51 class Layer: EventListener, Drawable 52 { 53 LayerType type; 54 float aspectRatio; 55 56 DynamicArray!Drawable drawables; 57 DynamicArray!Modifier modifiers; 58 59 this(EventManager emngr, LayerType type) 60 { 61 super(emngr); 62 this.type = type; 63 this.aspectRatio = cast(float)emngr.windowWidth / cast(float)emngr.windowHeight; 64 } 65 66 void addDrawable(Drawable d) 67 { 68 drawables.append(d); 69 } 70 71 void addModifier(Modifier m) 72 { 73 modifiers.append(m); 74 } 75 76 void draw(double dt) 77 { 78 glViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight); 79 80 glMatrixMode(GL_PROJECTION); 81 glPushMatrix(); 82 glLoadIdentity(); 83 84 if (type == LayerType.Layer2D) 85 glOrtho(0, eventManager.windowWidth, 0, eventManager.windowHeight, 0, 1); 86 else 87 gluPerspective(60, aspectRatio, 0.1, 400.0); 88 glMatrixMode(GL_MODELVIEW); 89 90 glLoadIdentity(); 91 92 foreach(i, m; modifiers.data) 93 m.bind(dt); 94 foreach(i, drw; drawables.data) 95 drw.draw(dt); 96 foreach(i, m; modifiers.data) 97 m.unbind(); 98 99 glMatrixMode(GL_PROJECTION); 100 glPopMatrix(); 101 glMatrixMode(GL_MODELVIEW); 102 } 103 104 override void free() 105 { 106 Delete(this); 107 } 108 109 ~this() 110 { 111 version (MemoryDebug) writefln("Deleting %s drawable(s) in layer...", drawables.length); 112 foreach(i, drw; drawables.data) 113 drw.free(); 114 drawables.free(); 115 } 116 117 override void onResize(int width, int height) 118 { 119 writefln("Layer received resize event: %s x %s", width, height); 120 aspectRatio = cast(float)width / cast(float)height; 121 } 122 }