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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 //=============================================================================
24 //
25 // Base implementation of IGfxDriverFactory
26 //
27 // GfxDriverFactoryBase is a template implementation of basic driver factory
28 // functionality, such as creating and destruction of graphics driver, and
29 // managing graphic filters.
30 //
31 //=============================================================================
32 
33 #ifndef AGS_ENGINE_GFX_GFX_DRIVER_FACTORY_BASE_H
34 #define AGS_ENGINE_GFX_GFX_DRIVER_FACTORY_BASE_H
35 
36 #include "ags/lib/std/vector.h"
37 #include "ags/engine/gfx/gfx_driver_factory.h"
38 #include "ags/engine/gfx/gfxfilter.h"
39 
40 namespace AGS3 {
41 namespace AGS {
42 namespace Engine {
43 
44 template <class TGfxDriverClass, class TGfxFilterClass>
45 class GfxDriverFactoryBase : public IGfxDriverFactory {
46 protected:
~GfxDriverFactoryBase()47 	~GfxDriverFactoryBase() override {
48 		delete _driver;
49 	}
50 
51 public:
Shutdown()52 	void Shutdown() override {
53 		delete this;
54 	}
55 
GetDriver()56 	IGraphicsDriver *GetDriver() override {
57 		if (!_driver)
58 			_driver = EnsureDriverCreated();
59 		return _driver;
60 	}
61 
DestroyDriver()62 	void DestroyDriver() override {
63 		delete _driver;
64 		_driver = nullptr;
65 	}
66 
SetFilter(const String & id,String & filter_error)67 	PGfxFilter SetFilter(const String &id, String &filter_error) override {
68 		TGfxDriverClass *driver = EnsureDriverCreated();
69 		if (!driver) {
70 			filter_error = "Graphics driver was not created";
71 			return PGfxFilter();
72 		}
73 
74 		const int color_depth = driver->GetDisplayMode().ColorDepth;
75 		if (color_depth == 0) {
76 			filter_error = "Graphics mode is not set";
77 			return PGfxFilter();
78 		}
79 
80 		std::shared_ptr<TGfxFilterClass> filter(CreateFilter(id));
81 		if (!filter) {
82 			filter_error = "Filter does not exist";
83 			return PGfxFilter();
84 		}
85 
86 		if (!filter->Initialize(color_depth, filter_error)) {
87 			return PGfxFilter();
88 		}
89 
90 		driver->SetGraphicsFilter(filter);
91 		return filter;
92 	}
93 
94 protected:
GfxDriverFactoryBase()95 	GfxDriverFactoryBase()
96 		: _driver(nullptr) {
97 	}
98 
99 	virtual TGfxDriverClass *EnsureDriverCreated() = 0;
100 	virtual TGfxFilterClass *CreateFilter(const String &id) = 0;
101 
102 	TGfxDriverClass *_driver;
103 };
104 
105 } // namespace Engine
106 } // namespace AGS
107 } // namespace AGS3
108 
109 #endif
110