1 /*
2 DSFML - The Simple and Fast Multimedia Library for D
3 
4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5 
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose, including commercial applications,
10 and to alter it and redistribute it freely, subject to the following restrictions:
11 
12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 
15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
16 
17 3. This notice may not be removed or altered from any source distribution
18 */
19 
20 ///A module containing the VideoMode Struct
21 module dsfml.window.videomode;
22 
23 /**
24  *VideoMode defines a video mode (width, height, bpp)
25  *
26  *A video mode is defined by a width and a height (in pixels) and a depth (in bits per pixel).
27  *
28  *Video modes are used to setup windows (Window class) at creation time.
29  *
30  *The main usage of video modes is for fullscreen mode: indeed you must use one of the valid video modes allowed by the OS (which are defined by what the monitor and the graphics card support), otherwise your window creation will just fail.
31  *
32  *VideoMode provides a static function for retrieving the list of all the video modes supported by the system: getFullscreenModes().
33  *
34  *A custom video mode can also be checked directly for fullscreen compatibility with its isValid() function.
35  *
36  *Additionnally, VideoMode provides a static function to get the mode currently used by the desktop: getDesktopMode(). This allows to build windows with the same size or pixel depth as the current resolution.
37  */
38 struct VideoMode
39 {
40 	///Video mode width, in pixels.
41 	uint width;
42 	///Video mode height, in pixels. 
43 	uint height;
44 	///Video mode pixel depth, in bits per pixels. 
45 	uint bitsPerPixel;
46 	
47 	///Construct the video mode with its attributes.
48 	///
49 	///Params:
50     ///		modeWidth = Width in pixels.
51     ///		modeHeight = Height in pixels.
52     ///		modeBitsPerPixel = Pixel depths in bits per pixel.
53 	this(uint Width, uint Height, uint bits= 32)
54 	{
55 		width = Width;
56 		height = Height;
57 		bitsPerPixel = bits;
58 	}
59 
60 	///Get the current desktop video mode.
61 	///
62 	///Returns: Current desktop video mode.
63 	static VideoMode getDesktopMode()
64 	{
65 		VideoMode temp;
66 		sfVideoMode_getDesktopMode(&temp.width, &temp.height, &temp.bitsPerPixel);
67 		return temp;
68 	}
69 	
70 	///Retrieve all the video modes supported in fullscreen mode.
71 	///
72 	///When creating a fullscreen window, the video mode is restricted to be compatible with what the graphics driver and monitor support. This function returns the complete list of all video modes that can be used in fullscreen mode. The returned array is sorted from best to worst, so that the first element will always give the best mode (higher width, height and bits-per-pixel).
73 	///
74 	///Returns: Array containing all the supported fullscreen modes.
75 	static VideoMode[] getFullscreenModes()
76 	{
77 		static VideoMode[] videoModes;//stores all video modes after the first call
78 		
79 		//if getFullscreenModes hasn't been called yet
80 		if(videoModes.length == 0)
81 		{
82 			uint* modes;
83 			size_t counts;
84 			
85 			//returns uints instead of structs due to 64 bit bug
86 			modes = sfVideoMode_getFullscreenModes(&counts);
87 			
88 			//calculate real length
89 			videoModes.length = counts/3;
90 			
91 			//populate videoModes
92 			int videoModeIndex = 0;
93 			for(uint i = 0; i < counts; i+=3)
94 			{
95 				VideoMode temp = VideoMode(modes[i], modes[i+1], modes[i+2]);
96 				
97 				videoModes[videoModeIndex] = temp;
98 				++videoModeIndex;
99 			}
100 			
101 		}
102 		
103 		return videoModes;
104 	}
105 
106 	///Tell whether or not the video mode is valid.
107 	///
108 	///The validity of video modes is only relevant when using fullscreen windows; otherwise any video mode can be used with no restriction.
109 	///
110 	///Returns: True if the video mode is valid for fullscreen mode.
111 	bool isValid() const
112 	{
113 		return sfVideoMode_isValid(width, height, bitsPerPixel);
114 	}
115 
116 	///Returns a string representation of the video mode.
117 	///
118 	///Returns: The video mode as a string in terms of width, height, and bits per pixel.
119 	string toString()
120 	{
121 		import std.conv: text;
122 		return "Width: " ~ text(width) ~ " Height: " ~ text(height) ~ " Bits per pixel: " ~ text(bitsPerPixel);
123 	}
124 }
125 
126 unittest
127 {
128 	version(DSFML_Unittest_Window)
129 	{
130 		import std.stdio;
131 		
132 		writeln("Unit test for VideoMode struct");
133 		
134 		size_t modesCount = VideoMode.getFullscreenModes().length;
135 		
136 		writeln("There are ", modesCount, " full screen modes available.");
137 		writeln("Your current desktop video mode is ",VideoMode.getDesktopMode().toString());
138 		
139 		writeln("Confirming all fullscreen modes are valid.");
140 		foreach(mode; VideoMode.getFullscreenModes())
141 		{
142 			assert(mode.isValid());
143 		}
144 		writeln("All video modes are valid.");
145 		
146 		writeln();
147 	}
148 }
149 
150 private extern(C)
151 {
152 	void sfVideoMode_getDesktopMode(uint* width, uint* height, uint* bitsPerPixel);
153 	uint* sfVideoMode_getFullscreenModes(size_t* Count);
154 	bool sfVideoMode_isValid(uint width, uint height, uint bitsPerPixel);
155 }