sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
app.cpp
1#include "app.h"
2
3using namespace sgltk;
4
5bool App::initialized = false;
6bool App::gl_version_manual = false;
7struct SYS_INFO App::sys_info;
8std::vector<std::string> App::error_string = {};
9
11 glewExperimental=GL_TRUE;
12 if(glewInit()) {
13 App::error_string.push_back("glewInit failed");
14 return false;
15 }
16 return true;
17}
18
20 unsigned int flags = IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF;
21 if((IMG_Init(flags) & flags) != flags) {
22 App::error_string.push_back(std::string("IMG_Init Error: ") +
23 SDL_GetError());
24 return false;
25 }
26 return true;
27}
28
30 IMG_Quit();
31}
32
34 if(SDL_Init(SDL_INIT_VIDEO |
35 SDL_INIT_JOYSTICK |
36 SDL_INIT_HAPTIC |
37 SDL_INIT_GAMECONTROLLER)) {
38 App::error_string.push_back(std::string("SDL_Init Error: ") +
39 SDL_GetError());
40 return false;
41 }
42 return true;
43}
44
46 SDL_Quit();
47}
48
49#ifdef HAVE_SDL_TTF_H
50bool App::init_ttf() {
51 if(TTF_Init()) {
52 App::error_string.push_back(std::string("SDL_Init Error: ") +
53 SDL_GetError());
54 return false;
55 }
56 return true;
57}
58
59void App::quit_ttf() {
60 TTF_Quit();
61}
62#endif //HAVE_SDL_TTF_H
63
64bool App::init() {
65 if(App::initialized)
66 return true;
67
68 if(App::init_sdl())
69 if(App::init_img()) {
70#ifdef HAVE_SDL_TTF_H
71 if(App::init_ttf()) {
72#endif //HAVE_SDL_TTF_H
73 App::initialized = true;
75 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
76 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
77 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
78 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
79 return true;
80#ifdef HAVE_SDL_TTF_H
81 }
82#endif //HAVE_SDL_TTF_H
83 }
84
85 App::quit();
86 return false;
87}
88
89void App::set_gl_version(int major, int minor) {
90 auto maj = int(3);
91 auto min = int(0);
92
93 if(major == 3) {
94 if(minor < 0 || minor > 3) {
95 App::error_string.push_back("Unsupported version number. Defaulting to version 3.0");
96 } else {
97 maj = major;
98 min = minor;
99 }
100 } else if(major == 4) {
101 if(minor < 0 || minor > 6) {
102 App::error_string.push_back("Unsupported version number. Defaulting to version 3.0");
103 } else {
104 maj = major;
105 min = minor;
106 }
107 } else {
108 App::error_string.push_back("Unsupported version number. Defaulting to version 3.0");
109 }
110
111 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, maj);
112 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, min);
113
114 gl_version_manual = true;
115}
116
117void App::set_depth_stencil_size(int depth_size, int stencil_size) {
118 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depth_size);
119 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, stencil_size);
120}
121
122void App::set_msaa_sample_number(int number_samples) {
123 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
124 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, number_samples);
125}
126
127void App::quit() {
128 App::initialized = false;
129 gl_version_manual = false;
131#ifdef HAVE_SDL_TTF_H
132 App::quit_ttf();
133#endif //HAVE_SDL_TTF_H
135}
136
137void App::enable_screensaver(bool enable) {
138 if(enable)
139 SDL_EnableScreenSaver();
140 else
141 SDL_DisableScreenSaver();
142}
143
144bool App::enable_vsync(bool on) {
145 int ret;
146 if (on) {
147 ret = SDL_GL_SetSwapInterval(-1);
148 if (ret < 0) {
149 ret = SDL_GL_SetSwapInterval(1);
150 }
151 }
152 else {
153 ret = SDL_GL_SetSwapInterval(0);
154 }
155 return (ret == 0);
156}
157
158bool App::chdir_to_bin(char **argv) {
159 std::string path(argv[0]);
160 size_t pos = path.find_last_of("\\/");
161 if(pos == std::string::npos) {
162 return true;
163 }
164 path.erase(pos);
165 int ret;
166#ifdef __linux__
167 ret = chdir(path.c_str());
168#else
169 ret = _chdir(path.c_str());
170#endif
171 if(ret) {
172 std::string error("An error occured while trying to change "
173 "current working directory to the directory containing "
174 "the executable file.");
175 error_string.push_back(error);
176 return false;
177 }
178 return true;
179}
180
181void App::_check_error(std::string message, std::string file, unsigned int line) {
182 std::string err_string;
183 GLenum err = GL_NO_ERROR;
184
185 do {
186 err = glGetError();
187 switch(err) {
188 case GL_INVALID_ENUM:
189 err_string = "INVALID_ENUM";
190 break;
191 case GL_INVALID_VALUE:
192 err_string = "INVALID_VALUE";
193 break;
194 case GL_INVALID_OPERATION:
195 err_string = "INVALID_OPERATION";
196 break;
197 case GL_INVALID_FRAMEBUFFER_OPERATION:
198 err_string = "INVALID_FRAMEBUFFER_OPERATION";
199 break;
200 case GL_OUT_OF_MEMORY:
201 err_string = "OUT_OF_MEMORY";
202 break;
203 case GL_STACK_OVERFLOW:
204 err_string = "STACK_OVERFLOW";
205 break;
206 case GL_STACK_UNDERFLOW:
207 err_string = "STACK_UNDERFLOW";
208 break;
209 default:
210 return;
211 }
212
213 std::cerr << file << " - " << line << ": " << err_string << " - " << message << std::endl;
214 } while(err != GL_NO_ERROR);
215}
216
218 sys_info.platform_name = std::string(SDL_GetPlatform());
219
220 sys_info.num_logical_cores = SDL_GetCPUCount();
221 sys_info.system_ram = SDL_GetSystemRAM();
222
223 sys_info.num_displays = SDL_GetNumVideoDisplays();
224 sys_info.desktop_display_modes.resize(sys_info.num_displays);
225 sys_info.supported_display_modes.resize(sys_info.num_displays);
226 for(int i = 0; i < sys_info.num_displays; i++) {
227 int num_modes = SDL_GetNumDisplayModes(i);
228 sys_info.supported_display_modes[i].resize(num_modes);
229 SDL_GetDesktopDisplayMode(i, &sys_info.desktop_display_modes[i]);
230 for(int j = 0; j < num_modes; j++)
231 SDL_GetDisplayMode(i, j, &sys_info.supported_display_modes[i][j]);
232 }
233 sys_info.display_bounds.resize(sys_info.num_displays);
234 for(int i = 0; i < sys_info.num_displays; i++)
235 SDL_GetDisplayBounds(i, &sys_info.display_bounds[i]);
236}
Handles library initialization and provides system information.
Definition app.h:133
static void get_sys_info()
Gathers system information and populates the sys_info attribute.
Definition app.cpp:217
static std::vector< std::string > error_string
A list of all error strings.
Definition app.h:151
static void set_gl_version(int major, int minor)
Sets the OpenGL version.
Definition app.cpp:89
static bool gl_version_manual
True if set_gl_version was called, false otherwise.
Definition app.h:143
static bool init()
Initializes SGLTK.
Definition app.cpp:64
static void _check_error(std::string message, std::string file, unsigned int line)
Outputs OpenGL error messages.
Definition app.cpp:181
static bool enable_vsync(bool on)
Turns VSync on or off.
Definition app.cpp:144
static struct SYS_INFO sys_info
System information.
Definition app.h:147
static void set_depth_stencil_size(int depth_size, int stencil_size)
Sets the size of the depth and stencil buffers.
Definition app.cpp:117
static bool chdir_to_bin(char **argv)
Changes the current working directory to the directory containing the executable.
Definition app.cpp:158
static void enable_screensaver(bool enable)
Enables the screensaver.
Definition app.cpp:137
static bool init_glew()
Initializes GLEW.
Definition app.cpp:10
static bool init_img()
Initializes SDL2_img.
Definition app.cpp:19
static void quit()
Deinitializes SGLTK.
Definition app.cpp:127
static void quit_sdl()
Deinitializes SDL2.
Definition app.cpp:45
static void quit_img()
Deinitializes SDL2_img.
Definition app.cpp:29
static bool init_sdl()
Initializes SDL2.
Definition app.cpp:33
static void set_msaa_sample_number(int number_samples)
Sets the number of samples used for multisample anti-aliasing.
Definition app.cpp:122
System information.
Definition app.h:88