1 /++
2  + Authors: Avaxar <avaxar@nekkl.org>
3  + Copyright: Copyright © 2023, Avaxar
4  + License: $(LINK2 https://mit-license.org, MIT License)
5  +/
6 
7 module dsdl2.video;
8 @safe:
9 
10 import bindbc.sdl;
11 import dsdl2.sdl;
12 
13 import std.conv : to;
14 import std.string : toStringz;
15 
16 /++
17  + Wraps `SDL_VideoInit` which initializes the video subsystem while specifying the video driver used
18  +
19  + Params:
20  +   driverName = the name of the video driver
21  + Throws: `dsdl2.SDLException` if the video driver could not be initialized
22  +/
23 void initVideo(string driverName) @trusted {
24     if (SDL_VideoInit(driverName.toStringz()) != 0) {
25         throw new SDLException;
26     }
27 }
28 
29 /++
30  + Wraps `SDL_VideoQuit` which quits the video subsystem
31  +/
32 void quitVideo() @trusted {
33     SDL_VideoQuit();
34 }
35 
36 /++
37  + Wraps `SDL_GetNumVideoDrivers` and `SDL_GetVideoDriver` which return a list of available video drivers
38  +
39  + Returns: names of the available video drivers
40  + Throws: `dsdl2.SDLException` if failed to get the available video drivers
41  +/
42 const(string[]) getVideoDrivers() @trusted {
43     int numDrivers = SDL_GetNumVideoDrivers();
44     if (numDrivers <= 0) {
45         throw new SDLException;
46     }
47 
48     static string[] drivers;
49     if (drivers !is null) {
50         size_t originalLength = drivers.length;
51         drivers.length = numDrivers;
52 
53         if (numDrivers > originalLength) {
54             foreach (i; originalLength .. numDrivers) {
55                 drivers[i] = SDL_GetVideoDriver(i.to!int).to!string;
56             }
57         }
58     }
59     else {
60         drivers = new string[](numDrivers);
61 
62         foreach (i; 0 .. numDrivers) {
63             drivers[i] = SDL_GetVideoDriver(i).to!string;
64         }
65     }
66 
67     return drivers;
68 }
69 
70 /++
71  + Wraps `SDL_GetCurrentVideoDriver` which returns the current video driver
72  +
73  + Returns: name of the current video driver
74  +/
75 string getCurrentVideoDriver() @trusted {
76     return SDL_GetCurrentVideoDriver().to!string;
77 }