ScummVM API documentation
gfx_driver_factory_base.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 //=============================================================================
23 //
24 // Base implementation of IGfxDriverFactory
25 //
26 // GfxDriverFactoryBase is a template implementation of basic driver factory
27 // functionality, such as creating and destruction of graphics driver, and
28 // managing graphic filters.
29 //
30 //=============================================================================
31 
32 #ifndef AGS_ENGINE_GFX_GFX_DRIVER_FACTORY_BASE_H
33 #define AGS_ENGINE_GFX_GFX_DRIVER_FACTORY_BASE_H
34 
35 #include "common/std/vector.h"
36 #include "ags/engine/gfx/gfx_driver_factory.h"
37 #include "ags/engine/gfx/gfxfilter.h"
38 
39 namespace AGS3 {
40 namespace AGS {
41 namespace Engine {
42 
43 template <class TGfxDriverClass, class TGfxFilterClass>
45 protected:
46  ~GfxDriverFactoryBase() override {
47  delete _driver;
48  }
49 
50 public:
51  void Shutdown() override {
52  delete this;
53  }
54 
55  IGraphicsDriver *GetDriver() override {
56  if (!_driver)
57  _driver = EnsureDriverCreated();
58  return _driver;
59  }
60 
61  void DestroyDriver() override {
62  delete _driver;
63  _driver = nullptr;
64  }
65 
66  PGfxFilter SetFilter(const String &id, String &filter_error) override {
67  TGfxDriverClass *driver = EnsureDriverCreated();
68  if (!driver) {
69  filter_error = "Graphics driver was not created";
70  return PGfxFilter();
71  }
72 
73  const int color_depth = driver->GetDisplayMode().ColorDepth;
74  if (color_depth == 0) {
75  filter_error = "Graphics mode is not set";
76  return PGfxFilter();
77  }
78 
79  std::shared_ptr<TGfxFilterClass> filter(CreateFilter(id));
80  if (!filter) {
81  filter_error = "Filter does not exist";
82  return PGfxFilter();
83  }
84 
85  if (!filter->Initialize(color_depth, filter_error)) {
86  return PGfxFilter();
87  }
88 
89  driver->SetGraphicsFilter(filter);
90  return filter;
91  }
92 
93 protected:
95  : _driver(nullptr) {
96  }
97 
98  virtual TGfxDriverClass *EnsureDriverCreated() = 0;
99  virtual TGfxFilterClass *CreateFilter(const String &id) = 0;
100 
101  TGfxDriverClass *_driver;
102 };
103 
104 } // namespace Engine
105 } // namespace AGS
106 } // namespace AGS3
107 
108 #endif
Definition: achievements_tables.h:27
Definition: gfx_driver_factory_base.h:44
Definition: graphics_driver.h:98
Definition: string.h:62
Definition: ptr.h:159
Definition: gfx_driver_factory.h:51
Definition: engine.h:143
Definition: ags.h:40