1 /*
2 ** v_video.h
3 **
4 **---------------------------------------------------------------------------
5 ** Copyright 1998-2008 Randy Heit
6 ** All rights reserved.
7 **
8 ** Redistribution and use in source and binary forms, with or without
9 ** modification, are permitted provided that the following conditions
10 ** are met:
11 **
12 ** 1. Redistributions of source code must retain the above copyright
13 **    notice, this list of conditions and the following disclaimer.
14 ** 2. Redistributions in binary form must reproduce the above copyright
15 **    notice, this list of conditions and the following disclaimer in the
16 **    documentation and/or other materials provided with the distribution.
17 ** 3. The name of the author may not be used to endorse or promote products
18 **    derived from this software without specific prior written permission.
19 **
20 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 **---------------------------------------------------------------------------
31 **
32 */
33 
34 #ifndef __V_VIDEO_H__
35 #define __V_VIDEO_H__
36 
37 #include "doomtype.h"
38 
39 #include "doomdef.h"
40 #include "dobject.h"
41 #include "r_data/renderstyle.h"
42 #include "c_cvars.h"
43 
44 extern int CleanWidth, CleanHeight, CleanXfac, CleanYfac;
45 extern int CleanWidth_1, CleanHeight_1, CleanXfac_1, CleanYfac_1;
46 extern int DisplayWidth, DisplayHeight, DisplayBits;
47 
48 bool V_DoModeSetup (int width, int height, int bits);
49 void V_CalcCleanFacs (int designwidth, int designheight, int realwidth, int realheight, int *cleanx, int *cleany, int *cx1=NULL, int *cx2=NULL);
50 
51 class FTexture;
52 
53 // TagItem definitions for DrawTexture. As far as I know, tag lists
54 // originated on the Amiga.
55 //
56 // Think of TagItems as an array of the following structure:
57 //
58 // struct TagItem {
59 //     DWORD ti_Tag;
60 //     DWORD ti_Data;
61 // };
62 
63 #define TAG_DONE	(0)  /* Used to indicate the end of the Tag list */
64 #define TAG_END		(0)  /* Ditto									*/
65 #define TAG_IGNORE	(1)  /* Ignore this Tag							*/
66 #define TAG_MORE	(2)  /* Ends this list and continues with the	*/
67 						 /* list pointed to in ti_Data 				*/
68 
69 #define TAG_USER	((DWORD)(1u<<30))
70 
71 enum
72 {
73 	DTA_Base = TAG_USER + 5000,
74 	DTA_DestWidth,		// width of area to draw to
75 	DTA_DestHeight,		// height of area to draw to
76 	DTA_Alpha,			// alpha value for translucency
77 	DTA_FillColor,		// color to stencil onto the destination (RGB is the color for truecolor drawers, A is the palette index for paletted drawers)
78 	DTA_Translation,	// translation table to recolor the source
79 	DTA_AlphaChannel,	// bool: the source is an alpha channel; used with DTA_FillColor
80 	DTA_Clean,			// bool: scale texture size and position by CleanXfac and CleanYfac
81 	DTA_320x200,		// bool: scale texture size and position to fit on a virtual 320x200 screen
82 	DTA_Bottom320x200,	// bool: same as DTA_320x200 but centers virtual screen on bottom for 1280x1024 targets
83 	DTA_CleanNoMove,	// bool: like DTA_Clean but does not reposition output position
84 	DTA_CleanNoMove_1,	// bool: like DTA_CleanNoMove, but uses Clean[XY]fac_1 instead
85 	DTA_FlipX,			// bool: flip image horizontally	//FIXME: Does not work with DTA_Window(Left|Right)
86 	DTA_ShadowColor,	// color of shadow
87 	DTA_ShadowAlpha,	// alpha of shadow
88 	DTA_Shadow,			// set shadow color and alphas to defaults
89 	DTA_VirtualWidth,	// pretend the canvas is this wide
90 	DTA_VirtualHeight,	// pretend the canvas is this tall
91 	DTA_TopOffset,		// override texture's top offset
92 	DTA_LeftOffset,		// override texture's left offset
93 	DTA_CenterOffset,	// bool: override texture's left and top offsets and set them for the texture's middle
94 	DTA_CenterBottomOffset,// bool: override texture's left and top offsets and set them for the texture's bottom middle
95 	DTA_WindowLeft,		// don't draw anything left of this column (on source, not dest)
96 	DTA_WindowRight,	// don't draw anything at or to the right of this column (on source, not dest)
97 	DTA_ClipTop,		// don't draw anything above this row (on dest, not source)
98 	DTA_ClipBottom,		// don't draw anything at or below this row (on dest, not source)
99 	DTA_ClipLeft,		// don't draw anything to the left of this column (on dest, not source)
100 	DTA_ClipRight,		// don't draw anything at or to the right of this column (on dest, not source)
101 	DTA_Masked,			// true(default)=use masks from texture, false=ignore masks
102 	DTA_HUDRules,		// use fullscreen HUD rules to position and size textures
103 	DTA_KeepRatio,		// doesn't adjust screen size for DTA_Virtual* if the aspect ratio is not 4:3
104 	DTA_RenderStyle,	// same as render style for actors
105 	DTA_ColorOverlay,	// DWORD: ARGB to overlay on top of image; limited to black for software
106 	DTA_BilinearFilter,	// bool: apply bilinear filtering to the image
107 	DTA_SpecialColormap,// pointer to FSpecialColormapParameters (likely to be forever hardware-only)
108 	DTA_ColormapStyle,	// pointer to FColormapStyle (hardware-only)
109 	DTA_Fullscreen,		// Draw image fullscreen (same as DTA_VirtualWidth/Height with graphics size.)
110 
111 	// floating point duplicates of some of the above:
112 	DTA_DestWidthF,
113 	DTA_DestHeightF,
114 	DTA_TopOffsetF,
115 	DTA_LeftOffsetF,
116 	DTA_VirtualWidthF,
117 	DTA_VirtualHeightF,
118 	DTA_WindowLeftF,
119 	DTA_WindowRightF,
120 
121 	// For DrawText calls:
122 	DTA_TextLen,		// stop after this many characters, even if \0 not hit
123 	DTA_CellX,			// horizontal size of character cell
124 	DTA_CellY,			// vertical size of character cell
125 };
126 
127 enum
128 {
129 	HUD_Normal,
130 	HUD_HorizCenter
131 };
132 
133 
134 class FFont;
135 struct FRemapTable;
136 class player_t;
137 typedef uint32 angle_t;
138 
139 //
140 // VIDEO
141 //
142 // [RH] Made screens more implementation-independant:
143 //
144 class DCanvas : public DObject
145 {
146 	DECLARE_ABSTRACT_CLASS (DCanvas, DObject)
147 public:
148 	DCanvas (int width, int height);
149 	virtual ~DCanvas ();
150 
151 	// Member variable access
GetBuffer()152 	inline BYTE *GetBuffer () const { return Buffer; }
GetWidth()153 	inline int GetWidth () const { return Width; }
GetHeight()154 	inline int GetHeight () const { return Height; }
GetPitch()155 	inline int GetPitch () const { return Pitch; }
156 
157 	virtual bool IsValid ();
158 
159 	// Access control
160 	virtual bool Lock (bool buffered=true) = 0;		// Returns true if the surface was lost since last time
161 	virtual void Unlock () = 0;
IsLocked()162 	virtual bool IsLocked () { return Buffer != NULL; }	// Returns true if the surface is locked
163 
164 	// Draw a linear block of pixels into the canvas
165 	virtual void DrawBlock (int x, int y, int width, int height, const BYTE *src) const;
166 
167 	// Reads a linear block of pixels into the view buffer.
168 	virtual void GetBlock (int x, int y, int width, int height, BYTE *dest) const;
169 
170 	// Dim the entire canvas for the menus
171 	virtual void Dim (PalEntry color = 0);
172 
173 	// Dim part of the canvas
174 	virtual void Dim (PalEntry color, float amount, int x1, int y1, int w, int h);
175 
176 	// Fill an area with a texture
177 	virtual void FlatFill (int left, int top, int right, int bottom, FTexture *src, bool local_origin=false);
178 
179 	// Fill a simple polygon with a texture
180 	virtual void FillSimplePoly(FTexture *tex, FVector2 *points, int npoints,
181 		double originx, double originy, double scalex, double scaley, angle_t rotation,
182 		struct FDynamicColormap *colormap, int lightlevel);
183 
184 	// Set an area to a specified color
185 	virtual void Clear (int left, int top, int right, int bottom, int palcolor, uint32 color);
186 
187 	// Draws a line
188 	virtual void DrawLine(int x0, int y0, int x1, int y1, int palColor, uint32 realcolor);
189 
190 	// Draws a single pixel
191 	virtual void DrawPixel(int x, int y, int palcolor, uint32 rgbcolor);
192 
193 	// Calculate gamma table
194 	void CalcGamma (float gamma, BYTE gammalookup[256]);
195 
196 
197 	// Retrieves a buffer containing image data for a screenshot.
198 	// Hint: Pitch can be negative for upside-down images, in which case buffer
199 	// points to the last row in the buffer, which will be the first row output.
200 	virtual void GetScreenshotBuffer(const BYTE *&buffer, int &pitch, ESSType &color_type);
201 
202 	// Releases the screenshot buffer.
203 	virtual void ReleaseScreenshotBuffer();
204 
205 	// Text drawing functions -----------------------------------------------
206 
207 	// 2D Texture drawing
208 	void STACK_ARGS DrawTexture (FTexture *img, double x, double y, int tags, ...);
209 	void FillBorder (FTexture *img);	// Fills the border around a 4:3 part of the screen on non-4:3 displays
210 	void VirtualToRealCoords(double &x, double &y, double &w, double &h, double vwidth, double vheight, bool vbottom=false, bool handleaspect=true) const;
211 
212 	// Code that uses these (i.e. SBARINFO) should probably be evaluated for using doubles all around instead.
213 	void VirtualToRealCoordsFixed(fixed_t &x, fixed_t &y, fixed_t &w, fixed_t &h, int vwidth, int vheight, bool vbottom=false, bool handleaspect=true) const;
214 	void VirtualToRealCoordsInt(int &x, int &y, int &w, int &h, int vwidth, int vheight, bool vbottom=false, bool handleaspect=true) const;
215 
216 	// 2D Text drawing
217 	void STACK_ARGS DrawText (FFont *font, int normalcolor, int x, int y, const char *string, ...);
218 #ifndef DrawText	// See WinUser.h for the definition of DrawText as a macro
219 	void STACK_ARGS DrawTextA (FFont *font, int normalcolor, int x, int y, const char *string, ...);
220 #endif
221 	void DrawTextV (FFont *font, int normalcolor, int x, int y, const char *string, va_list tags);
222 	void STACK_ARGS DrawChar (FFont *font, int normalcolor, int x, int y, BYTE character, ...);
223 
224 	struct DrawParms
225 	{
226 		double x, y;
227 		double texwidth;
228 		double texheight;
229 		double destwidth;
230 		double destheight;
231 		double virtWidth;
232 		double virtHeight;
233 		double windowleft;
234 		double windowright;
235 		int dclip;
236 		int uclip;
237 		int lclip;
238 		int rclip;
239 		double top;
240 		double left;
241 		fixed_t alpha;
242 		uint32 fillcolor;
243 		FRemapTable *remap;
244 		const BYTE *translation;
245 		uint32 colorOverlay;
246 		INTBOOL alphaChannel;
247 		INTBOOL flipX;
248 		fixed_t shadowAlpha;
249 		int shadowColor;
250 		INTBOOL keepratio;
251 		INTBOOL masked;
252 		INTBOOL bilinear;
253 		FRenderStyle style;
254 		struct FSpecialColormap *specialcolormap;
255 		struct FColormapStyle *colormapstyle;
256 	};
257 
258 protected:
259 	BYTE *Buffer;
260 	int Width;
261 	int Height;
262 	int Pitch;
263 	int LockCount;
264 
265 	bool ClipBox (int &left, int &top, int &width, int &height, const BYTE *&src, const int srcpitch) const;
266 	virtual void STACK_ARGS DrawTextureV (FTexture *img, double x, double y, uint32 tag, va_list tags);
267 	bool ParseDrawTextureTags (FTexture *img, double x, double y, uint32 tag, va_list tags, DrawParms *parms, bool hw) const;
268 
DCanvas()269 	DCanvas() {}
270 
271 private:
272 	// Keep track of canvases, for automatic destruction at exit
273 	DCanvas *Next;
274 	static DCanvas *CanvasChain;
275 
276 	void PUTTRANSDOT (int xx, int yy, int basecolor, int level);
277 };
278 
279 // A canvas in system memory.
280 
281 class DSimpleCanvas : public DCanvas
282 {
283 	DECLARE_CLASS (DSimpleCanvas, DCanvas)
284 public:
285 	DSimpleCanvas (int width, int height);
286 	~DSimpleCanvas ();
287 
288 	bool IsValid ();
289 	bool Lock (bool buffered=true);
290 	void Unlock ();
291 
292 protected:
293 	BYTE *MemBuffer;
294 
DSimpleCanvas()295 	DSimpleCanvas() {}
296 };
297 
298 // This class represents a native texture, as opposed to an FTexture.
299 class FNativeTexture
300 {
301 public:
302 	virtual ~FNativeTexture();
303 	virtual bool Update() = 0;
304 	virtual bool CheckWrapping(bool wrapping);
305 };
306 
307 // This class represents a texture lookup palette.
308 class FNativePalette
309 {
310 public:
311 	virtual ~FNativePalette();
312 	virtual bool Update() = 0;
313 };
314 
315 // A canvas that represents the actual display. The video code is responsible
316 // for actually implementing this. Built on top of SimpleCanvas, because it
317 // needs a system memory buffer when buffered output is enabled.
318 
319 class DFrameBuffer : public DSimpleCanvas
320 {
321 	DECLARE_ABSTRACT_CLASS (DFrameBuffer, DSimpleCanvas)
322 public:
323 	DFrameBuffer (int width, int height);
324 
325 	// Force the surface to use buffered output if true is passed.
326 	virtual bool Lock (bool buffered) = 0;
327 
328 	// Make the surface visible. Also implies Unlock().
329 	virtual void Update () = 0;
330 
331 	// Return a pointer to 256 palette entries that can be written to.
332 	virtual PalEntry *GetPalette () = 0;
333 
334 	// Stores the palette with flash blended in into 256 dwords
335 	virtual void GetFlashedPalette (PalEntry palette[256]) = 0;
336 
337 	// Mark the palette as changed. It will be updated on the next Update().
338 	virtual void UpdatePalette () = 0;
339 
340 	// Sets the gamma level. Returns false if the hardware does not support
341 	// gamma changing. (Always true for now, since palettes can always be
342 	// gamma adjusted.)
343 	virtual bool SetGamma (float gamma) = 0;
344 
345 	// Sets a color flash. RGB is the color, and amount is 0-256, with 256
346 	// being all flash and 0 being no flash. Returns false if the hardware
347 	// does not support this. (Always true for now, since palettes can always
348 	// be flashed.)
349 	virtual bool SetFlash (PalEntry rgb, int amount) = 0;
350 
351 	// Converse of SetFlash
352 	virtual void GetFlash (PalEntry &rgb, int &amount) = 0;
353 
354 	// Returns the number of video pages the frame buffer is using.
355 	virtual int GetPageCount () = 0;
356 
357 	// Returns true if running fullscreen.
358 	virtual bool IsFullscreen () = 0;
359 
360 	// Changes the vsync setting, if supported by the device.
361 	virtual void SetVSync (bool vsync);
362 
363 	// Tells the device to recreate itself with the new setting from vid_refreshrate.
364 	virtual void NewRefreshRate ();
365 
366 	// Set the rect defining the area affected by blending.
367 	virtual void SetBlendingRect (int x1, int y1, int x2, int y2);
368 
369 	bool Accel2D;	// If true, 2D drawing can be accelerated.
370 
371 	// Begin 2D drawing operations. This is like Update, but it doesn't end
372 	// the scene, and it doesn't present the image yet. If you are going to
373 	// be covering the entire screen with 2D elements, then pass false to
374 	// avoid copying the software buffer to the screen.
375 	// Returns true if hardware-accelerated 2D has been entered, false if not.
376 	virtual bool Begin2D(bool copy3d);
377 
378 	// DrawTexture calls after Begin2D use native textures.
379 
380 	// Draws the blending rectangle over the viewwindow if in hardware-
381 	// accelerated 2D mode.
382 	virtual void DrawBlendingRect();
383 
384 	// Create a native texture from a game texture.
385 	virtual FNativeTexture *CreateTexture(FTexture *gametex, bool wrapping);
386 
387 	// Create a palette texture from a remap/palette table.
388 	virtual FNativePalette *CreatePalette(FRemapTable *remap);
389 
390 	// Precaches or unloads a texture
391 	virtual void GetHitlist(BYTE *hitlist);
392 
393 	// Report a game restart
394 	virtual void GameRestart();
395 
396 	// Screen wiping
397 	virtual bool WipeStartScreen(int type);
398 	virtual void WipeEndScreen();
399 	virtual bool WipeDo(int ticks);
400 	virtual void WipeCleanup();
401 
ScaleCoordsFromWindow(SWORD & x,SWORD & y)402 	virtual void ScaleCoordsFromWindow(SWORD &x, SWORD &y) {}
403 
GetLastFPS()404 	uint32 GetLastFPS() const { return LastCount; }
405 
406 #ifdef _WIN32
407 	virtual void PaletteChanged () = 0;
408 	virtual int QueryNewPalette () = 0;
409 	virtual bool Is8BitMode() = 0;
410 #endif
411 
412 protected:
413 	void DrawRateStuff ();
414 	void CopyFromBuff (BYTE *src, int srcPitch, int width, int height, BYTE *dest);
415 
DFrameBuffer()416 	DFrameBuffer () {}
417 
418 private:
419 	uint32 LastMS, LastSec, FrameCount, LastCount, LastTic;
420 };
421 
422 
423 // This is the screen updated by I_FinishUpdate.
424 extern DFrameBuffer *screen;
425 
426 #define SCREENWIDTH (screen->GetWidth ())
427 #define SCREENHEIGHT (screen->GetHeight ())
428 #define SCREENPITCH (screen->GetPitch ())
429 
430 EXTERN_CVAR (Float, Gamma)
431 
432 // Translucency tables
433 
434 // RGB32k is a normal R5G5B5 -> palette lookup table.
435 
436 // Use a union so we can "overflow" without warnings.
437 // Otherwise, we get stuff like this from Clang (when compiled
438 // with -fsanitize=bounds) while running:
439 //   src/v_video.cpp:390:12: runtime error: index 1068 out of bounds for type 'BYTE [32]'
440 //   src/r_draw.cpp:273:11: runtime error: index 1057 out of bounds for type 'BYTE [32]'
441 union ColorTable32k
442 {
443 	BYTE RGB[32][32][32];
444 	BYTE All[32 *32 *32];
445 };
446 extern "C" ColorTable32k RGB32k;
447 
448 // Col2RGB8 is a pre-multiplied palette for color lookup. It is stored in a
449 // special R10B10G10 format for efficient blending computation.
450 //		--RRRRRrrr--BBBBBbbb--GGGGGggg--   at level 64
451 //		--------rrrr------bbbb------gggg   at level 1
452 extern "C" DWORD Col2RGB8[65][256];
453 
454 // Col2RGB8_LessPrecision is the same as Col2RGB8, but the LSB for red
455 // and blue are forced to zero, so if the blend overflows, it won't spill
456 // over into the next component's value.
457 //		--RRRRRrrr-#BBBBBbbb-#GGGGGggg--  at level 64
458 //      --------rrr#------bbb#------gggg  at level 1
459 extern "C" DWORD *Col2RGB8_LessPrecision[65];
460 
461 // Col2RGB8_Inverse is the same as Col2RGB8_LessPrecision, except the source
462 // palette has been inverted.
463 extern "C" DWORD Col2RGB8_Inverse[65][256];
464 
465 // "Magic" numbers used during the blending:
466 //		--000001111100000111110000011111	= 0x01f07c1f
467 //		-0111111111011111111101111111111	= 0x3FEFFBFF
468 //		-1000000000100000000010000000000	= 0x40100400
469 //		------10000000001000000000100000	= 0x40100400 >> 5
470 //		--11111-----11111-----11111-----	= 0x40100400 - (0x40100400 >> 5) aka "white"
471 //		--111111111111111111111111111111	= 0x3FFFFFFF
472 
473 // Allocates buffer screens, call before R_Init.
474 void V_Init (bool restart);
475 
476 // Initializes graphics mode for the first time.
477 void V_Init2 ();
478 
479 void V_Shutdown ();
480 
481 void V_MarkRect (int x, int y, int width, int height);
482 
483 // Returns the closest color to the one desired. String
484 // should be of the form "rr gg bb".
485 int V_GetColorFromString (const DWORD *palette, const char *colorstring);
486 // Scans through the X11R6RGB lump for a matching color
487 // and returns a color string suitable for V_GetColorFromString.
488 FString V_GetColorStringByName (const char *name);
489 
490 // Tries to get color by name, then by string
491 int V_GetColor (const DWORD *palette, const char *str);
492 void V_DrawFrame (int left, int top, int width, int height);
493 
494 // If the view size is not full screen, draws a border around it.
495 void V_DrawBorder (int x1, int y1, int x2, int y2);
496 void V_RefreshViewBorder ();
497 
498 void V_SetBorderNeedRefresh();
499 
500 #if defined(X86_ASM) || defined(X64_ASM)
501 extern "C" void ASM_PatchPitch (void);
502 #endif
503 
504 int CheckRatio (int width, int height, int *trueratio=NULL);
CheckRatio(double width,double height)505 static inline int CheckRatio (double width, double height) { return CheckRatio(int(width), int(height)); }
506 extern const int BaseRatioSizes[5][4];
507 
508 
509 
510 #endif // __V_VIDEO_H__
511