UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
sdl_window.cpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2010-2021 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <stdexcept>
39#include "sdl_window.hpp"
40
41Sdl_window::Sdl_window(bool double_buffer) : Sdl_window("UltraGrid",
42 SDL_WINDOWPOS_CENTERED,
43 SDL_WINDOWPOS_CENTERED,
44 640,
45 480,
46 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE, double_buffer) { }
47
48Sdl_window::Sdl_window(const char *title,
49 int x, int y,
50 int w, int h,
51 Uint32 flags, bool double_buffer) : width(w), height(h)
52{
53 SDL_InitSubSystem(SDL_INIT_VIDEO);
54 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
55 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
56 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
57
58 if(!double_buffer)
59 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
60
61 sdl_window = SDL_CreateWindow(title,
62 x,
63 y,
64 w,
65 h,
66 flags);
67
68 if(!sdl_window){
69 throw std::runtime_error("Failed to create SDL window!");
70 }
71
72 SDL_SetWindowMinimumSize(sdl_window, 200, 200);
73
74 sdl_gl_context = SDL_GL_CreateContext(sdl_window);
75 if(!sdl_gl_context){
76 throw std::runtime_error("Failed to create gl context!");
77 }
78
79#ifndef __APPLE__
80 glewExperimental = GL_TRUE;
81 GLenum glewError = glewInit();
82 if(glewError != GLEW_OK){
83 throw std::runtime_error("Failed to initialize gl context!");
84 }
85#endif //__APPLE__
86
87 glClearColor(0,0,0,1);
88 glClear(GL_COLOR_BUFFER_BIT);
89 SDL_GL_SwapWindow(sdl_window);
90}
91
93 SDL_GL_DeleteContext(sdl_gl_context);
94 SDL_GL_DeleteContext(sdl_gl_worker_context);
95 SDL_DestroyWindow(sdl_window);
96 SDL_QuitSubSystem(SDL_INIT_VIDEO);
97}
98
99#ifdef __linux__
100void Sdl_window::getXlibHandles(Display **xDisplay,
101 GLXContext *glxContext,
102 GLXDrawable *glxDrawable)
103{
104 SDL_GL_MakeCurrent(sdl_window, sdl_gl_context);
105 *xDisplay = XOpenDisplay(NULL);
106 *glxContext = glXGetCurrentContext();
107 *glxDrawable = glXGetCurrentDrawable();
108}
109#endif // defined __linux__
110
114
116 SDL_GLContext worker_context = get_worker_context();
117 SDL_GL_MakeCurrent(sdl_window, worker_context);
118}
119
122 SDL_GL_MakeCurrent(sdl_window, sdl_gl_context);
123 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
124 sdl_gl_worker_context = SDL_GL_CreateContext(sdl_window);
125 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
126 SDL_GL_MakeCurrent(sdl_window, sdl_gl_context);
127 }
128
130}
131
133 std::swap(sdl_window, o.sdl_window);
134 std::swap(sdl_gl_context, o.sdl_gl_context);
135 std::swap(width, o.width);
136 std::swap(height, o.height);
137}
const char unsigned int flags
Sdl_window(bool double_buffer=false)
void swap(Sdl_window &o)
void make_render_context_current()
SDL_GLContext sdl_gl_context
SDL_GLContext get_worker_context()
SDL_GLContext sdl_gl_worker_context
void make_worker_context_current()
SDL_Window * sdl_window