1 module prova.core.init;
2 
3 import derelict.freetype,
4        derelict.openal.al,
5        derelict.sdl2.sdl,
6        derelict.vorbis,
7        prova,
8        std.conv;
9 
10 package void init()
11 {
12   initSDL();
13   initOpenAL();
14   initVorbis();
15   initOpenGL();
16   initFreeType();
17 }
18 
19 private void initSDL()
20 {
21   DerelictSDL2.load();
22 
23   if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
24     throw new Exception("Initialization Error: " ~ to!string(SDL_GetError()));
25 }
26 
27 private void initOpenAL()
28 {
29   DerelictAL.load();
30 
31   Audio.device = alcOpenDevice(null);
32 
33   if(!Audio.device)
34     throw new Exception("Initialization Error: Failed to open audio device");
35 
36   Audio.context = alcCreateContext(Audio.device, null);
37 
38   if(!Audio.context)
39     throw new Exception("Initialization Error: Failed audio context creation");
40 
41   alcMakeContextCurrent(Audio.context);
42 }
43 
44 private void initVorbis()
45 {
46   DerelictVorbisFile.load();
47 }
48 
49 private void initOpenGL()
50 {
51   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
52   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
53   SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
54 
55   DerelictGL3.load();
56 }
57 
58 private void initFreeType()
59 {
60   DerelictFT.load();
61   int error = FT_Init_FreeType(&Font.ftlibrary);
62 
63   if(error != 0)
64     throw new Exception("Initialization Error: Failed to initalize FreeType library");
65 }
66 
67 package void finalize()
68 {
69   finalizeOpenAL();
70   finalizeOpenGL();
71   finalizeSDL();
72   finalizeFreeType();
73 }
74 
75 private void finalizeOpenAL()
76 {
77   alcMakeContextCurrent(null);
78   alcDestroyContext(Audio.context);
79   alcCloseDevice(Audio.device);
80 }
81 
82 private void finalizeOpenGL()
83 {
84   SpriteBatch.defaultShaderProgram = null;
85   Texture.cleanUp();
86 }
87 
88 private void finalizeSDL()
89 {
90   SDL_Quit();
91 }
92 
93 private void finalizeFreeType()
94 {
95   FT_Done_FreeType(Font.ftlibrary);
96 }