1 //=============================================================================
2 //
3 // Adventure Game Studio (AGS)
4 //
5 // Copyright (C) 1999-2011 Chris Jones and 2011-20xx others
6 // The full list of copyright holders can be found in the Copyright.txt
7 // file, which is part of this source code distribution.
8 //
9 // The AGS source code is provided under the Artistic License 2.0.
10 // A copy of this license can be found in the file License.txt and at
11 // http://www.opensource.org/licenses/artistic-license-2.0.php
12 //
13 //=============================================================================
14 //
15 // Software graphics factory, based on Allegro
16 //
17 //=============================================================================
18 
19 #ifndef __AGS_EE_GFX__ALI3DSW_H
20 #define __AGS_EE_GFX__ALI3DSW_H
21 
22 #include "util/stdtr1compat.h"
23 #include TR1INCLUDE(memory)
24 #include <allegro.h>
25 #if defined (WINDOWS_VERSION)
26 #include <winalleg.h>
27 #include <ddraw.h>
28 #endif
29 
30 #include "gfx/bitmap.h"
31 #include "gfx/ddb.h"
32 #include "gfx/gfxdriverfactorybase.h"
33 #include "gfx/gfxdriverbase.h"
34 
35 namespace AGS
36 {
37 namespace Engine
38 {
39 namespace ALSW
40 {
41 
42 class AllegroGfxFilter;
43 using AGS::Common::Bitmap;
44 
45 class ALSoftwareBitmap : public IDriverDependantBitmap
46 {
47 public:
48     // NOTE by CJ:
49     // Transparency is a bit counter-intuitive
50     // 0=not transparent, 255=invisible, 1..254 barely visible .. mostly visible
SetTransparency(int transparency)51     virtual void SetTransparency(int transparency) { _transparency = transparency; }
SetFlippedLeftRight(bool isFlipped)52     virtual void SetFlippedLeftRight(bool isFlipped) { _flipped = isFlipped; }
53     virtual void SetStretch(int width, int height, bool useResampler = true)
54     {
55         _stretchToWidth = width;
56         _stretchToHeight = height;
57     }
GetWidth()58     virtual int GetWidth() { return _width; }
GetHeight()59     virtual int GetHeight() { return _height; }
GetColorDepth()60     virtual int GetColorDepth() { return _colDepth; }
SetLightLevel(int lightLevel)61     virtual void SetLightLevel(int lightLevel)  { }
SetTint(int red,int green,int blue,int tintSaturation)62     virtual void SetTint(int red, int green, int blue, int tintSaturation) { }
63 
64     Bitmap *_bmp;
65     int _width, _height;
66     int _colDepth;
67     bool _flipped;
68     int _stretchToWidth, _stretchToHeight;
69     bool _opaque;
70     bool _hasAlpha;
71     int _transparency;
72 
ALSoftwareBitmap(Bitmap * bmp,bool opaque,bool hasAlpha)73     ALSoftwareBitmap(Bitmap *bmp, bool opaque, bool hasAlpha)
74     {
75         _bmp = bmp;
76         _width = bmp->GetWidth();
77         _height = bmp->GetHeight();
78         _colDepth = bmp->GetColorDepth();
79         _flipped = false;
80         _stretchToWidth = 0;
81         _stretchToHeight = 0;
82         _transparency = 0;
83         _opaque = opaque;
84         _hasAlpha = hasAlpha;
85     }
86 
GetWidthToRender()87     int GetWidthToRender() { return (_stretchToWidth > 0) ? _stretchToWidth : _width; }
GetHeightToRender()88     int GetHeightToRender() { return (_stretchToHeight > 0) ? _stretchToHeight : _height; }
89 
Dispose()90     void Dispose()
91     {
92         // do we want to free the bitmap?
93     }
94 
~ALSoftwareBitmap()95     ~ALSoftwareBitmap()
96     {
97         Dispose();
98     }
99 };
100 
101 
102 class ALSoftwareGfxModeList : public IGfxModeList
103 {
104 public:
ALSoftwareGfxModeList(GFX_MODE_LIST * alsw_gfx_mode_list)105     ALSoftwareGfxModeList(GFX_MODE_LIST *alsw_gfx_mode_list)
106         : _gfxModeList(alsw_gfx_mode_list)
107     {
108     }
109 
GetModeCount()110     virtual int GetModeCount() const
111     {
112         return _gfxModeList ? _gfxModeList->num_modes : 0;
113     }
114 
115     virtual bool GetMode(int index, DisplayMode &mode) const;
116 
117 private:
118     GFX_MODE_LIST *_gfxModeList;
119 };
120 
121 
122 typedef SpriteDrawListEntry<ALSoftwareBitmap> ALDrawListEntry;
123 
124 class ALSoftwareGraphicsDriver : public GraphicsDriverBase
125 {
126 public:
127     ALSoftwareGraphicsDriver();
128 
GetDriverName()129     virtual const char*GetDriverName() { return "Software renderer"; }
GetDriverID()130     virtual const char*GetDriverID() { return "Software"; }
131     virtual void SetTintMethod(TintMethod method);
132     virtual bool SetDisplayMode(const DisplayMode &mode, volatile int *loopTimer);
133     virtual bool SetNativeSize(const Size &src_size);
134     virtual bool SetRenderFrame(const Rect &dst_rect);
135     virtual bool IsModeSupported(const DisplayMode &mode);
136     virtual int  GetDisplayDepthForNativeDepth(int native_color_depth) const;
137     virtual IGfxModeList *GetSupportedModeList(int color_depth);
138     virtual PGfxFilter GetGraphicsFilter() const;
139     virtual void UnInit();
140     virtual void ClearRectangle(int x1, int y1, int x2, int y2, RGB *colorToUse);
141     virtual int  GetCompatibleBitmapFormat(int color_depth);
142     virtual IDriverDependantBitmap* CreateDDBFromBitmap(Bitmap *bitmap, bool hasAlpha, bool opaque);
143     virtual void UpdateDDBFromBitmap(IDriverDependantBitmap* bitmapToUpdate, Bitmap *bitmap, bool hasAlpha);
144     virtual void DestroyDDB(IDriverDependantBitmap* bitmap);
145     virtual void DrawSprite(int x, int y, IDriverDependantBitmap* bitmap);
146     virtual void ClearDrawList();
147     virtual void RenderToBackBuffer();
148     virtual void Render();
149     virtual void Render(GlobalFlipType flip);
150     virtual bool GetCopyOfScreenIntoBitmap(Bitmap *destination, bool at_native_res, Size *want_size);
151     virtual void FadeOut(int speed, int targetColourRed, int targetColourGreen, int targetColourBlue);
152     virtual void FadeIn(int speed, PALETTE pal, int targetColourRed, int targetColourGreen, int targetColourBlue);
153     virtual void BoxOutEffect(bool blackingOut, int speed, int delay);
154     virtual bool PlayVideo(const char *filename, bool useAVISound, VideoSkipType skipType, bool stretchToFullScreen);
155     virtual bool SupportsGammaControl() ;
156     virtual void SetGamma(int newGamma);
UseSmoothScaling(bool enabled)157     virtual void UseSmoothScaling(bool enabled) { }
EnableVsyncBeforeRender(bool enabled)158     virtual void EnableVsyncBeforeRender(bool enabled) { _autoVsync = enabled; }
159     virtual void Vsync();
RenderSpritesAtScreenResolution(bool enabled,int supersampling)160     virtual void RenderSpritesAtScreenResolution(bool enabled, int supersampling) { }
RequiresFullRedrawEachFrame()161     virtual bool RequiresFullRedrawEachFrame() { return false; }
HasAcceleratedStretchAndFlip()162     virtual bool HasAcceleratedStretchAndFlip() { return false; }
UsesMemoryBackBuffer()163     virtual bool UsesMemoryBackBuffer() { return true; }
GetMemoryBackBuffer()164     virtual Bitmap *GetMemoryBackBuffer() { return virtualScreen; }
165     virtual void SetMemoryBackBuffer(Bitmap *backBuffer);
SetScreenTint(int red,int green,int blue)166     virtual void SetScreenTint(int red, int green, int blue) {
167         _tint_red = red; _tint_green = green; _tint_blue = blue; }
168     virtual ~ALSoftwareGraphicsDriver();
169 
170     typedef stdtr1compat::shared_ptr<AllegroGfxFilter> PALSWFilter;
171 
172     void SetGraphicsFilter(PALSWFilter filter);
173 
174 private:
175     PALSWFilter _filter;
176 
177     bool _autoVsync;
178     Bitmap *_allegroScreenWrapper;
179     // Virtual screen bitmap is either a wrapper over Allegro's real screen
180     // bitmap, or bitmap provided by the graphics filter. It should not be
181     // disposed by the renderer: it is up to filter object to manage it.
182     Bitmap *_origVirtualScreen;
183     // Current virtual screen bitmap; may be provided either by graphics
184     // filter or by external user. It should not be disposed by the renderer.
185     Bitmap *virtualScreen;
186     Bitmap *_spareTintingScreen;
187     int _tint_red, _tint_green, _tint_blue;
188 
189     std::vector<ALDrawListEntry> drawlist;
190     GFX_MODE_LIST *_gfxModeList;
191 
192 #ifdef _WIN32
193     IDirectDrawGammaControl* dxGammaControl;
194     // The gamma ramp is a lookup table for each possible R, G and B value
195     // in 32-bit colour (from 0-255) it maps them to a brightness value
196     // from 0-65535. The default gamma ramp just multiplies each value by 256
197     DDGAMMARAMP gammaRamp;
198     DDGAMMARAMP defaultGammaRamp;
199     DDCAPS ddrawCaps;
200 #endif
201 
202     // Use gfx filter to create a new virtual screen
203     void CreateVirtualScreen();
204     void DestroyVirtualScreen();
205     // Unset parameters and release resources related to the display mode
206     void ReleaseDisplayMode();
207 
208     void highcolor_fade_out(int speed, int targetColourRed, int targetColourGreen, int targetColourBlue);
209     void highcolor_fade_in(Bitmap *bmp_orig, int speed, int targetColourRed, int targetColourGreen, int targetColourBlue);
210     void __fade_from_range(PALLETE source, PALLETE dest, int speed, int from, int to) ;
211     void __fade_out_range(int speed, int from, int to, int targetColourRed, int targetColourGreen, int targetColourBlue) ;
212     int  GetAllegroGfxDriverID(bool windowed);
213 };
214 
215 
216 class ALSWGraphicsFactory : public GfxDriverFactoryBase<ALSoftwareGraphicsDriver, AllegroGfxFilter>
217 {
218 public:
219     virtual ~ALSWGraphicsFactory();
220 
221     virtual size_t               GetFilterCount() const;
222     virtual const GfxFilterInfo *GetFilterInfo(size_t index) const;
223     virtual String               GetDefaultFilterID() const;
224 
225     static  ALSWGraphicsFactory *GetFactory();
226 
227 private:
228     virtual ALSoftwareGraphicsDriver *EnsureDriverCreated();
229     virtual AllegroGfxFilter         *CreateFilter(const String &id);
230 
231     static ALSWGraphicsFactory *_factory;
232 };
233 
234 } // namespace ALSW
235 } // namespace Engine
236 } // namespace AGS
237 
238 #endif // __AGS_EE_GFX__ALI3DSW_H
239