cleaned all trailing white space from source files.
[sdk] / ecere / src / gfx / DisplaySystem.ec
1 namespace gfx;
2
3 import "Mutex"
4 import "Display"
5
6 String printingDocumentName;
7
8 public void SetPrintingDocumentName(String name)
9 {
10    printingDocumentName = name;
11 }
12
13 public class DisplaySystemResPtr : struct
14 {
15    DisplaySystemResPtr prev, next;
16    Resource resource;
17 };
18
19 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
20 static void FreeTexture(NamedLink texture)
21 {
22    ((Bitmap)texture.data).Free();
23    delete texture.name;
24 }
25
26 static void FreeMaterial(Material material)
27 {
28    delete material.name;
29 }
30 #endif
31
32 public class DisplaySystem
33 {
34 public:
35    class_no_expansion;
36    ~DisplaySystem()
37    {
38 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
39       OldLink mesh;
40
41       materials.Free(Material::Free);
42       textures.Free(FreeTexture);
43       for(mesh = meshes.first; mesh; mesh = mesh.next)
44          ((Mesh)mesh.data).Free(0);
45       meshes.Free(null);
46 #endif
47
48       if(driverData)
49          driver.DestroyDisplaySystem(this);
50
51       if(driver.displaySystem == this)
52          driver.displaySystem = null;
53    }
54
55    bool Create(char * driverName, void * window, bool fullScreen)
56    {
57       bool result = false;
58       subclass(DisplayDriver) displayDriver = GetDisplayDriver(driverName);
59       if(displayDriver)
60       {
61          flags.fullScreen = fullScreen;
62          this.window = window;
63          if(displayDriver.CreateDisplaySystem(this))
64             result = true;
65
66          driver = displayDriver;
67          if(!result)
68          {
69             // LogErrorCode(GERR_DISPLAY_INIT_FAILED, driver);
70             displayDriver.displaySystem = null;
71          }
72          else if(!displayDriver.displaySystem)
73          {
74             displayDriver.displaySystem = this;
75          }
76       }
77       return result;
78    }
79
80    Font LoadFont(char * faceName, float size, FontFlags flags)
81    {
82       Font result = null;
83       subclass(DisplayDriver) driver = this ? this.driver : ((subclass(DisplayDriver))class(LFBDisplayDriver));
84       char * string = CopyString(faceName);
85       char *fonts[32];
86       if(string)
87       {
88          int numFonts = TokenizeWith(string, 32, fonts, ",", true);
89          int c;
90          for(c = 0; c<numFonts; c++)
91          {
92             TrimLSpaces(fonts[c],fonts[c]);
93             TrimRSpaces(fonts[c],fonts[c]);
94             if((result = driver.LoadFont(this, fonts[c], size, flags)))
95             {
96                break;
97             }
98          }
99          delete string;
100       }
101       return result;
102    }
103
104    void UnloadFont(Font font)
105    {
106       subclass(DisplayDriver) driver = this ? this.driver : ((subclass(DisplayDriver))class(LFBDisplayDriver));
107       driver.UnloadFont(this, font);
108    }
109
110    void FontExtent(Font font, byte * text, int len, int * width, int * height)
111    {
112       if(this && text)
113          driver.FontExtent(this, font, text, len, width, height);
114       else
115       {
116          if(width) *width = 0;
117          if(height) *height = 0;
118       }
119    }
120
121    void * LoadResource(Resource resource)
122    {
123       DisplaySystemResPtr res;
124       for(res = resources.first; res; res = res.next)
125       {
126          Resource existing = res.resource;
127
128          if(existing._class == resource._class)
129          {
130             if(resource.OnCompare(existing) == 0)
131                break;
132          }
133       }
134       if(!res)
135       {
136          // res = DisplaySystemResPtr { resource = new classof(resource) };
137          res = DisplaySystemResPtr { resource = eInstance_New(resource._class) };
138          resources.Add(res);
139
140          // This will load e.g. the Bitmap *
141          res.resource.Load(resource, this);
142       }
143       // This would copy e.g. the Bitmap *
144       incref res.resource;
145       resource.Reference(res.resource);
146
147       return res;
148    }
149
150    void UnloadResource(Resource resource, DisplaySystemResPtr res)
151    {
152       // This would clear e.g. the Bitmap *
153       resource.Dereference();
154       if(res)
155       {
156          if(res.resource._refCount == 1)
157          {
158             delete res.resource;
159             resources.Delete(res);
160          }
161          else
162             res.resource._refCount--;
163       }
164    }
165
166    bool Lock()
167    {
168       bool result = false;
169       mutex.Wait();
170
171       if(!current)
172          result = driver.LockSystem(this);
173       else
174          result = true;
175       current++;
176       return result;
177    }
178
179    void Unlock(void)
180    {
181       current--;
182       if(!current)
183          driver.UnlockSystem(this);
184
185       mutex.Release();
186    }
187 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
188    // --- Materials List Management ---
189
190    Material AddNamedMaterial(char * name)
191    {
192       Material material = materials.FindName(name, false);
193       if(!material)
194       {
195          material = Material { };
196          if(material)
197          {
198             material.name = CopyString(name);
199             if(name)
200                materials.AddName(material);
201             else
202                materials.Add(material);
203          }
204       }
205       return material;
206    }
207
208    bool AddMaterial(Material material)
209    {
210       if(material.name)
211          materials.AddName(material);
212       else
213          materials.Add(material);
214       return true;
215    }
216
217    Material GetMaterial(char * name)
218    {
219       return materials.FindName(name, false);
220    }
221
222    bool RemoveMaterial(Material material)
223    {
224       delete material.name;
225       materials.Delete(material);
226       return true;
227    }
228
229    void ClearMaterials()
230    {
231       materials.Free(FreeMaterial);
232    }
233
234    // --- Textures List Management ---
235
236    NamedLink AddTexture(char * name, Bitmap bitmap)
237    {
238       NamedLink item { };
239       if(item)
240       {
241          item.data = bitmap;
242          item.name = new char[strlen(name) + 1];
243          strcpy(item.name, name);
244          textures.AddName(item);
245       }
246       return item;
247    }
248
249    Bitmap GetTexture(char * name)
250    {
251       return textures.FindNamedLink(name, false);
252    }
253
254    bool RemoveTexture(char * name)
255    {
256       NamedLink item = textures.FindName(name, false);
257       if(item)
258       {
259          FreeTexture(item);
260          textures.Delete(item);
261          return true;
262       }
263       return false;
264    }
265
266    void ClearTextures()
267    {
268       textures.Free(FreeTexture);
269    }
270
271    // --- Meshes List Management ---
272
273    OldLink AddMesh(Mesh mesh)
274    {
275       if(this)
276       {
277          OldLink item { };
278          if(item)
279          {
280             item.data = mesh;
281             mesh.displaySystem = this;
282             meshes.Add(item);
283          }
284          return item;
285       }
286       return null;
287    }
288
289    bool RemoveMesh(Mesh mesh)
290    {
291       OldLink item = meshes.FindLink(mesh);
292       if(item)
293       {
294          mesh.Free(0);
295          meshes.Delete(item);
296          return true;
297       }
298       return false;
299    }
300
301    void ClearMeshes()
302    {
303       OldLink mesh;
304       for(mesh = meshes.first; mesh; mesh = mesh.next)
305          ((Mesh)mesh.data).Free(0);
306       meshes.Free(null);
307    }
308 #endif
309
310 private:
311    subclass(DisplayDriver) driver;
312    void * data;
313    void * window;
314    public PixelFormat pixelFormat;
315    public DisplayFlags flags;
316    int numDisplays;
317
318    OldList resources;
319
320 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
321    // This will all go in resources
322    OldList materials;
323    OldList textures;
324    OldList meshes;
325 #endif
326
327    void * driverData;
328    int current;
329    Mutex mutex { };
330 };