1 /*
2 Copyright (c) 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.room;
30 
31 import std.stdio;
32 import dlib.core.memory;
33 import dlib.image.color;
34 import dlib.container.array;
35 import dlib.container.aarray;
36 import dgl.core.application;
37 import dgl.core.event;
38 import dgl.core.layer;
39 
40 //version = MemoryDebug;
41 
42 class Room: EventListener
43 {
44     RoomApplication app;
45     DynamicArray!Layer layers;
46 
47     this(EventManager em, RoomApplication app)
48     {
49         super(em);
50         this.app = app;
51     }
52 
53     // Add window-aligned layer
54     Layer addLayer(LayerType type)
55     {
56         Layer layer = New!Layer(
57             eventManager,
58             type);
59         layers.append(layer);
60         return layer;
61     }
62 
63     // Add user layer
64     Layer addLayer(Layer layer)
65     {
66         layers.append(layer);
67         return layer;
68     }
69 
70     void onEnter()
71     {
72     }
73 
74     void onUpdate()
75     {
76         processEvents();
77         foreach(i, layer; layers.data)
78         {
79             layer.processEvents();
80         }
81     }
82 
83     void onRedraw()
84     {
85         foreach(i, layer; layers.data)
86         {
87             layer.draw(eventManager.deltaTime);
88         }
89     }
90 
91     override void free()
92     {
93         Delete(this);
94     }
95 
96     ~this()
97     {
98         version (MemoryDebug) writefln("Deleting %s layer(s) in Room...", layers.length);
99         foreach(i, layer; layers.data)
100             layer.free();
101         layers.free();
102     }
103 
104     override void onResize(int width, int height)
105     {
106         foreach(i, layer; layers.data)
107             layer.onResize(width, height);
108     }
109 }
110 
111 class RoomApplication: Application
112 {
113     AArray!Room rooms;
114     Room currentRoom;
115     string currentRoomName;
116 
117     // TODO: configuration manager
118     this(
119         uint width = 800,
120         uint height = 600,
121         string caption = "DGL application",
122         bool unicodeInput = true,
123         bool showCursor = true,
124         bool resizableWindow = true)
125     {
126         super(width, height, caption, unicodeInput, showCursor, resizableWindow);
127         rooms = New!(AArray!Room)();
128     }
129 
130     Room getRoom(string name)
131     {
132         return rooms[name];
133     }
134 
135     void addRoom(string name, Room room)
136     {
137         if (name in rooms)
138         {
139             rooms[name].free();
140             rooms.remove(name);
141         }
142         rooms[name] = room;
143     }
144 
145     void loadRoom(string name, bool deleteCurrent = false)
146     {
147         setCurrentRoom(name, deleteCurrent);
148     }
149 
150     void setCurrentRoom(string name, bool deleteCurrent = false)
151     {
152         if (currentRoom)
153         {
154             if (deleteCurrent)
155             {
156                 currentRoom.free();
157                 rooms.remove(currentRoomName);
158             }
159         }
160         currentRoom = rooms[name];
161         currentRoomName = name;
162         currentRoom.onEnter();
163     }
164 
165     override void onUpdate()
166     {
167         if (currentRoom)
168             currentRoom.onUpdate();
169     }
170 
171     override void onRedraw()
172     {
173         if (currentRoom)
174             currentRoom.onRedraw();
175     }
176 
177     override void onResize(int width, int height)
178     {
179         super.onResize(width, height);
180         foreach(i, r; rooms)
181             r.onResize(width, height);
182     }
183 
184     override void free()
185     {
186         Delete(this);
187     }
188 
189     ~this()
190     {
191         version (MemoryDebug) writefln("Deleting RoomApplication...");
192 
193         foreach(i, room; rooms)
194             room.free();
195         Delete(rooms);
196     }
197 }