-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_util.cpp
98 lines (79 loc) · 2.64 KB
/
sdl_util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "sdl_util.h"
SDLManager::SDLManager(int win_width, int win_height)
: main_window(nullptr), main_ctx()
{
init(win_width, win_height);
if (!main_window) {
check_error(std::cout);
throw std::runtime_error("Could not initialize SDL and OPENGL");
}
}
SDLManager::~SDLManager()
{
SDL_GL_DeleteContext(main_ctx);
SDL_DestroyWindow(main_window);
SDL_Quit();
}
void SDLManager::configure_opengl()
{
// Set our OpenGL version.
// SDL_GL_CONTEXT_CORE gives us only the newer version, deprecated functions are disabled
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// 3.2 is part of the modern versions of OpenGL, but most video cards whould be able to run it
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
// use hardware accel
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
// Turn on double buffering with a 24bit Z buffer.
// You may need to change this to 16 or 32 for your system
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// synchronizes to monitor refresh rate
SDL_GL_SetSwapInterval(1);
// multisample
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
}
void SDLManager::init(int win_width, int win_height)
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return;
}
configure_opengl();
main_window = SDL_CreateWindow("Shader Playground",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
win_width,
win_height,
SDL_WINDOW_OPENGL);
if (!main_window) {
return;
}
main_ctx = SDL_GL_CreateContext(main_window);
GLenum err = glewInit();
if (GLEW_OK != err) {
std::cout << "Couldn't init glew" << std::endl;
}
// blending
glEnable(GL_BLEND);
// opengl depth testing
glEnable(GL_DEPTH_TEST);
// gl multisampling
glEnable(GL_MULTISAMPLE);
int Buffers, Samples;
SDL_GL_GetAttribute( SDL_GL_MULTISAMPLEBUFFERS, &Buffers );
SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES, &Samples );
std::cout << "buf = " << Buffers << ", samples = " << Samples << "." << std::endl;
SDL_SetRelativeMouseMode(SDL_TRUE);
}
void SDLManager::check_error(std::ostream& os)
{
std::string err = SDL_GetError();
if (err != "") {
os << "SDL ERROR: " << err << std::endl;
SDL_ClearError();
}
}
void SDLManager::swap_window()
{
SDL_GL_SwapWindow(main_window);
}