]> git.localhorst.tv Git - tacos.git/blob - src/app/init.cpp
146719b3c039eb128c9541b8cf067311126000e9
[tacos.git] / src / app / init.cpp
1 #include "init.hpp"
2
3 #include "error.hpp"
4
5 #include <alut.h>
6 #include <SDL.h>
7 #include <SDL_image.h>
8 #include <SDL_ttf.h>
9
10
11 namespace tacos {
12
13 InitAlut::InitAlut() {
14         if (!alutInit(nullptr, nullptr)) {
15                 throw AlutError("alutInit(nullptr, nullptr)");
16         }
17 }
18
19 InitAlut::~InitAlut() {
20         if (!alutExit()) {
21                 throw AlutError("alutExit()");
22         }
23 }
24
25
26 InitGL::InitGL(bool double_buffer, int multi_sample) {
27         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
28                 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
29         }
30         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
31                 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
32         }
33         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
34                 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
35         }
36
37         if (!double_buffer) {
38                 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0) != 0) {
39                         throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0)");
40                 }
41         }
42
43         if (multi_sample > 1) {
44                 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
45                         throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1)");
46                 }
47                 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multi_sample) != 0) {
48                         throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, x)");
49                 }
50         }
51 }
52
53
54 InitIMG::InitIMG() {
55         if (IMG_Init(IMG_INIT_PNG) == 0) {
56                 throw SDLError("IMG_Init(IMG_INIT_PNG)");
57         }
58 }
59
60 InitIMG::~InitIMG() noexcept {
61         IMG_Quit();
62 }
63
64
65 InitSDL::InitSDL() {
66         if (SDL_Init(SDL_INIT_VIDEO) != 0) {
67                 throw SDLError("SDL_Init(SDL_INIT_VIDEO)");
68         }
69         // SDL seems to start out in text input state
70         SDL_StopTextInput();
71 }
72
73 InitSDL::~InitSDL() noexcept {
74         SDL_Quit();
75 }
76
77
78 InitTTF::InitTTF() {
79         if (TTF_Init() != 0) {
80                 throw SDLError("TTF_Init()");
81         }
82 }
83
84 InitTTF::~InitTTF() noexcept {
85         TTF_Quit();
86 }
87
88 }