1 /*
2  *******************************************************************************************
3  * Dgame (a D game framework) - Copyright (c) Randy Schütt
4  * 
5  * This software is provided 'as-is', without any express or implied warranty.
6  * In no event will the authors be held liable for any damages arising from
7  * the use of this software.
8  * 
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  * 
13  * 1. The origin of this software must not be misrepresented; you must not claim
14  *    that you wrote the original software. If you use this software in a product,
15  *    an acknowledgment in the product documentation would be appreciated but is
16  *    not required.
17  * 
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  *    misrepresented as being the original software.
20  * 
21  * 3. This notice may not be removed or altered from any source distribution.
22  *******************************************************************************************
23  */
24 module Dgame.Window.VideoMode;
25 
26 private {
27 	import std.conv : to;
28 	
29 	import derelict.sdl2.sdl;
30 	
31 	import Dgame.Internal.Log;
32 }
33 
34 /**
35  * The VideoMode struct contains informations about the current window video mode.
36  * It is passed to Window which extract the informations and use them to build a window context.
37  * 
38  * Author: rschuett
39  */
40 struct VideoMode {
41 	ushort width;	/** The width of this video mode */
42 	ushort height;	/** The height of this video mode */
43 	ubyte refreshRate;	/** The refresh rate of this video mode */
44 	
45 	/**
46 	 * CTor
47 	 */
48 	this(uint width, uint height, uint hz = 0) {
49 		this.width  = cast(ushort) width;
50 		this.height = cast(ushort) height;
51 		
52 		this.refreshRate = cast(ubyte) hz;
53 	}
54 	
55 	/**
56 	 * Returns: if the this video mode is valid which means, if it is listed from the OS
57 	 */
58 	bool isValid(ubyte display = 1) const {
59 		foreach (ref const VideoMode vm; VideoMode.listModes(display)) {
60 			if (vm == this)
61 				return true;
62 		}
63 		
64 		return false;
65 	}
66 	
67 	/**
68 	 * opEquals
69 	 */
70 	bool opEquals(ref const VideoMode vm) const pure nothrow {
71 		return vm.width == this.width && vm.height == this.height;
72 	}
73 	
74 	/**
75 	 * Returns: the desktop video mode
76 	 */
77 	static VideoMode getDesktopMode(ubyte display = 1) {
78 		SDL_DisplayMode mode = void;
79 		int result = SDL_GetDesktopDisplayMode(display, &mode);
80 		if (result != 0)
81 			Log.error(to!string(SDL_GetError()));
82 		
83 		return VideoMode(mode.w, mode.h, mode.refresh_rate);
84 	}
85 	
86 	/**
87 	 * Returns: the video mode on the given index
88 	 */
89 	static VideoMode getMode(uint index, ubyte display = 1) {
90 		SDL_DisplayMode mode = void;
91 		int result = SDL_GetDisplayMode(display, index, &mode);
92 		if (result != 0)
93 			Log.error(to!string(SDL_GetError()));
94 		
95 		return VideoMode(mode.w, mode.h, mode.refresh_rate);
96 	}
97 	
98 	/**
99 	 * Returns: A List of all valid supported video modes
100 	 */
101 	static VideoMode[] listModes(ubyte display = 1) {
102 		VideoMode[] displayModes;
103 		
104 		for (int i = 0; i < VideoMode.countModes(); ++i) {
105 			displayModes ~= VideoMode.getMode(i, display);
106 		}
107 		
108 		return displayModes;
109 	}
110 	
111 	/**
112 	 * Returns how many valid video modes are supported
113 	 */
114 	static int countModes(ubyte display = 1) {
115 		return SDL_GetNumDisplayModes(display);
116 	}
117 	
118 	/**
119 	 * Returns how many display are available.
120 	 */
121 	static int countDisplays() {
122 		return SDL_GetNumVideoDisplays();
123 	}
124 }