ScummVM API documentation
ali_3d_scummvm.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 // Software graphics factory, draws raw bitmaps onto a virtual screen,
25 // converts to SDL_Texture and finally presents with SDL_Renderer.
26 //
27 // TODO: replace nearest-neighbour software filter with SDL's own accelerated
28 // scaling, maybe add more filter types if SDL renderer supports them.
29 // Only keep Hqx filter as a software option (might need to change how the
30 // filter code works).
31 //
32 //=============================================================================
33 
34 #ifndef AGS_ENGINE_GFX_ALI_3D_SCUMMVM_H
35 #define AGS_ENGINE_GFX_ALI_3D_SCUMMVM_H
36 
37 #include "common/std/memory.h"
38 #include "common/std/vector.h"
39 #include "ags/shared/core/platform.h"
40 #include "ags/shared/gfx/bitmap.h"
41 #include "ags/engine/gfx/ddb.h"
42 #include "ags/engine/gfx/gfx_driver_factory_base.h"
43 #include "ags/engine/gfx/gfx_driver_base.h"
44 
45 namespace AGS3 {
46 namespace AGS {
47 namespace Engine {
48 namespace ALSW {
49 
50 class ScummVMRendererGraphicsDriver;
51 class ScummVMRendererGfxFilter;
52 using AGS::Shared::Bitmap;
53 
54 enum RendererFlip {
55  FLIP_NONE = 0x00000000,
56  FLIP_HORIZONTAL = 0x00000001,
57  FLIP_VERTICAL = 0x00000002
58 };
59 
60 class ALSoftwareBitmap : public BaseDDB {
61 public:
62  uint32_t GetRefID() const override { return UINT32_MAX /* not supported */; }
63 
64  int GetAlpha() const override {
65  return _alpha;
66  }
67  void SetAlpha(int alpha) override {
68  _alpha = alpha;
69  }
70  void SetFlippedLeftRight(bool isFlipped) override {
71  _flipped = isFlipped;
72  }
73  void SetStretch(int width, int height, bool /*useResampler*/) override {
74  _stretchToWidth = width;
75  _stretchToHeight = height;
76  }
77  void SetLightLevel(int /*lightLevel*/) override {}
78  void SetTint(int /*red*/, int /*green*/, int /*blue*/, int /*tintSaturation*/) override {}
79 
80  Bitmap *_bmp = nullptr;
81  bool _flipped = false;
82  int _stretchToWidth = 0, _stretchToHeight = 0;
83  int _alpha = 255;
84 
85  ALSoftwareBitmap(int width, int height, int color_depth, bool opaque) {
86  _width = width;
87  _height = height;
88  _colDepth = color_depth;
89  _opaque = opaque;
90  _stretchToWidth = _width;
91  _stretchToHeight = _height;
92  }
93 
94  ALSoftwareBitmap(Bitmap *bmp, bool opaque, bool hasAlpha) {
95  _bmp = bmp;
96  _width = bmp->GetWidth();
97  _height = bmp->GetHeight();
98  _colDepth = bmp->GetColorDepth();
99  _opaque = opaque;
100  _hasAlpha = hasAlpha;
101  _stretchToWidth = _width;
102  _stretchToHeight = _height;
103  }
104 
105  int GetWidthToRender() {
106  return _stretchToWidth;
107  }
108  int GetHeightToRender() {
109  return _stretchToHeight;
110  }
111 
112  ~ALSoftwareBitmap() override = default;
113 };
114 
115 
117 public:
119  : _modes(modes) {
120  }
121 
122  int GetModeCount() const override {
123  return _modes.size();
124  }
125 
126  bool GetMode(int index, DisplayMode &mode) const override {
127  if (index >= 0 && (size_t)index < _modes.size()) {
128  mode = _modes[index];
129  return true;
130  }
131  return false;
132  }
133 
134 private:
136 };
137 
138 
140 // Software renderer's sprite batch
142  uint32_t ID = 0u;
143  // Clipping viewport, also used as a destination for blitting optional Surface;
144  // in *relative* coordinates to parent surface.
145  Rect Viewport;
146  // Optional model transformation, to be applied to each sprite
147  SpriteTransform Transform;
148  // Intermediate surface which will be drawn upon and transformed if necessary
150  // Whether surface is a parent surface's region (e.g. virtual screen)
151  bool IsParentRegion = false;
152  // Tells whether the surface is treated as opaque or transparent
153  bool Opaque = false;
154 };
156 
157 
159 public:
161  ~ScummVMRendererGraphicsDriver() override;
162 
163  const char *GetDriverID() override {
164  return "Software";
165  }
166  const char *GetDriverName() override {
167  return "ScummVM 2D renderer";
168  }
169 
170  void SetTintMethod(TintMethod /*method*/) override;
171  bool SetDisplayMode(const DisplayMode &mode) override;
172  void UpdateDeviceScreen(const Size &screen_sz) override;
173  bool SetNativeResolution(const GraphicResolution &native_res) override;
174  bool SetRenderFrame(const Rect &dst_rect) override;
175  bool IsModeSupported(const DisplayMode &mode) override;
176  int GetDisplayDepthForNativeDepth(int native_color_depth) const override;
177  IGfxModeList *GetSupportedModeList(int color_depth) override;
178  PGfxFilter GetGraphicsFilter() const override;
179  void UnInit();
180  // Clears the screen rectangle. The coordinates are expected in the **native game resolution**.
181  void ClearRectangle(int x1, int y1, int x2, int y2, RGB *colorToUse) override;
182  int GetCompatibleBitmapFormat(int color_depth) override;
183  IDriverDependantBitmap *CreateDDB(int width, int height, int color_depth, bool opaque) override;
184  IDriverDependantBitmap *CreateDDBFromBitmap(Bitmap *bitmap, bool hasAlpha, bool opaque) override;
185  IDriverDependantBitmap *CreateRenderTargetDDB(int width, int height, int color_depth, bool opaque) override;
186  void UpdateDDBFromBitmap(IDriverDependantBitmap *ddb, Bitmap *bitmap, bool hasAlpha) override;
187  void DestroyDDB(IDriverDependantBitmap *ddb) override;
188 
189  IDriverDependantBitmap *GetSharedDDB(uint32_t /*sprite_id*/,
190  Bitmap *bitmap, bool hasAlpha, bool opaque) override {
191  // Software renderer does not require a texture cache, because it uses bitmaps directly
192  return CreateDDBFromBitmap(bitmap, hasAlpha, opaque);
193  }
194 
195  void UpdateSharedDDB(uint32_t /*sprite_id*/, Bitmap */*bitmap*/, bool /*hasAlpha*/, bool /*opaque*/) override {
196  /* do nothing */
197  }
198  void ClearSharedDDB(uint32_t /*sprite_id*/) override {
199  /* do nothing */
200  }
201 
202  void DrawSprite(int x, int y, IDriverDependantBitmap *ddb) override;
203  void SetScreenFade(int red, int green, int blue) override;
204  void SetScreenTint(int red, int green, int blue) override;
205  void SetStageScreen(const Size &sz, int x = 0, int y = 0) override;
206 
207  void RenderToBackBuffer() override;
208  void Render() override;
209  void Render(int xoff, int yoff, Shared::GraphicFlip flip) override;
210  bool GetCopyOfScreenIntoBitmap(Bitmap *destination, bool at_native_res, GraphicResolution *want_fmt) override;
211  void FadeOut(int speed, int targetColourRed, int targetColourGreen, int targetColourBlue) override;
212  void FadeIn(int speed, PALETTE pal, int targetColourRed, int targetColourGreen, int targetColourBlue) override;
213  void BoxOutEffect(bool blackingOut, int speed, int delay) override;
214  bool SupportsGammaControl() override;
215  void SetGamma(int newGamma) override;
216  void UseSmoothScaling(bool /*enabled*/) override {}
217  bool DoesSupportVsyncToggle() override;
218  void RenderSpritesAtScreenResolution(bool /*enabled*/, int /*supersampling*/) override {}
219  bool RequiresFullRedrawEachFrame() override {
220  return false;
221  }
222  bool HasAcceleratedTransform() override {
223  return false;
224  }
225  bool UsesMemoryBackBuffer() override {
226  return true;
227  }
228  Bitmap *GetMemoryBackBuffer() override;
229  void SetMemoryBackBuffer(Bitmap *backBuffer) override;
230  Bitmap *GetStageBackBuffer(bool mark_dirty) override;
231  void SetStageBackBuffer(Bitmap *backBuffer) override;
232  bool GetStageMatrixes(RenderMatrixes & /*rm*/) override {
233  return false; /* not supported */
234  }
235 
237 
238  void SetGraphicsFilter(PSDLRenderFilter filter);
239 
240 protected:
241  bool SetVsyncImpl(bool vsync, bool &vsync_res) override;
242  size_t GetLastDrawEntryIndex() override {
243  return _spriteList.size();
244  }
245 
246 private:
247  Graphics::Screen *_screen = nullptr;
248  PSDLRenderFilter _filter;
249 
250  bool _hasGamma = false;
251 #ifdef TODO
252  uint16 _defaultGammaRed[256] {};
253  uint16 _defaultGammaGreen[256] {};
254  uint16 _defaultGammaBlue[256] {};
255  int _gamma = 100;
256 #endif
257 
258  /* SDL_Renderer *_renderer = nullptr;
259  SDL_Texture *_screenTex = nullptr; */
260  // BITMAP struct for wrapping screen texture locked pixels, so that we may use blit()
261  BITMAP *_fakeTexBitmap = nullptr;
262  unsigned char *_lastTexPixels = nullptr;
263  int _lastTexPitch = -1;
264 
265  // Original virtual screen created and managed by the renderer.
266  std::unique_ptr<Bitmap> _origVirtualScreen;
267  // Current virtual screen bitmap; may be either pointing to _origVirtualScreen,
268  // or provided by external user (for example - plugin).
269  // Its pixels are copied to the video texture to be presented by SDL_Renderer.
270  Bitmap *virtualScreen;
271  // Stage screen meant for particular rendering stages, may be referencing
272  // actual virtual screen or separate bitmap of different size that is
273  // blitted to virtual screen at the stage finalization.
274  Bitmap *_stageVirtualScreen;
275  int _tint_red, _tint_green, _tint_blue;
276 
277  // Sprite batches (parent scene nodes)
278  ALSpriteBatches _spriteBatches;
279  // List of sprites to render
280  std::vector<ALDrawListEntry> _spriteList;
281 
282  void InitSpriteBatch(size_t index, const SpriteBatchDesc &desc) override;
283  void ResetAllBatches() override;
284 
285  // Use gfx filter to create a new virtual screen
286  void CreateVirtualScreen();
287  void DestroyVirtualScreen();
288  // Unset parameters and release resources related to the display mode
289  void ReleaseDisplayMode();
290  // Renders single sprite batch on the precreated surface
291  size_t RenderSpriteBatch(const ALSpriteBatch &batch, size_t from, Shared::Bitmap *surface, int surf_offx, int surf_offy);
292 
293  void highcolor_fade_in(Bitmap *vs, void(*draw_callback)(), int speed, int targetColourRed, int targetColourGreen, int targetColourBlue);
294  void highcolor_fade_out(Bitmap *vs, void(*draw_callback)(), int speed, int targetColourRed, int targetColourGreen, int targetColourBlue);
295  void __fade_from_range(PALETTE source, PALETTE dest, int speed, int from, int to);
296  void __fade_out_range(int speed, int from, int to, int targetColourRed, int targetColourGreen, int targetColourBlue);
297  // Copy raw screen bitmap pixels to the screen
298  void copySurface(const Graphics::Surface &src, bool mode);
299  // Render bitmap on screen
300  void Present(int xoff = 0, int yoff = 0, Shared::GraphicFlip flip = Shared::kFlip_None);
301 };
302 
303 
304 class ScummVMRendererGraphicsFactory : public GfxDriverFactoryBase<ScummVMRendererGraphicsDriver, ScummVMRendererGfxFilter> {
305 public:
306  ~ScummVMRendererGraphicsFactory() override;
307 
308  size_t GetFilterCount() const override;
309  const GfxFilterInfo *GetFilterInfo(size_t index) const override;
310  String GetDefaultFilterID() const override;
311 
312  static ScummVMRendererGraphicsFactory *GetFactory();
313 
314 private:
315  ScummVMRendererGraphicsDriver *EnsureDriverCreated() override;
316  ScummVMRendererGfxFilter *CreateFilter(const String &id) override;
317 
318  static ScummVMRendererGraphicsFactory *_factory;
319 };
320 
321 } // namespace ALSW
322 } // namespace Engine
323 } // namespace AGS
324 } // namespace AGS3
325 
326 #endif
Definition: achievements_tables.h:27
Definition: vector.h:39
Definition: graphics_driver.h:70
Definition: surface.h:67
Definition: allegro_bitmap.h:44
Definition: ali_3d_scummvm.h:60
Definition: gfx_driver_base.h:194
Definition: ali_3d_scummvm.h:141
Definition: gfxfilter_scummvm_renderer.h:39
Definition: surface.h:32
Definition: screen.h:48
Definition: gfx_defines.h:60
Definition: ptr.h:572
Definition: viewport.h:128
Definition: gfxfilter.h:41
Definition: gfx_driver_base.h:47
Definition: gfx_driver_factory_base.h:44
Definition: surface.h:329
Definition: geometry.h:215
Definition: color.h:49
Definition: string.h:62
Definition: geometry.h:144
Definition: gfx_defines.h:33
Definition: gfx_mode_list.h:38
Definition: gfx_driver_base.h:93
Definition: ptr.h:159
Definition: engine.h:143
Definition: graphics_driver.h:85
Definition: ags.h:40
Definition: gfx_driver_base.h:74