OSVR-Core
SDL2Helpers.h
Go to the documentation of this file.
1 
12 // Copyright 2015 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 #ifndef INCLUDED_SDL2Helpers_h_GUID_D901E282_40A2_4CB1_DA01_A88E21A45C94
27 #define INCLUDED_SDL2Helpers_h_GUID_D901E282_40A2_4CB1_DA01_A88E21A45C94
28 
29 // Internal Includes
30 // - none
31 
32 // Library/third-party includes
33 #include <SDL.h>
34 #include <SDL_opengl.h>
35 
36 // Standard includes
37 #include <stdexcept>
38 #include <string>
39 #include <memory>
40 
41 namespace osvr {
42 namespace SDL2 {
44  class Lib {
45  public:
46  Lib() {
47  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
48  throw std::runtime_error(
49  std::string("Could not initialize SDL") + SDL_GetError());
50  }
51  }
52  ~Lib() { SDL_Quit(); }
53 
54  Lib(Lib const &) = delete; //< non-copyable
55  Lib &operator=(Lib const &) = delete; //< non-assignable
56  };
57 
59  typedef std::shared_ptr<SDL_Window> WindowPtr;
60 
62  template <typename... Args> inline WindowPtr createWindow(Args &&... args) {
63  WindowPtr ret(SDL_CreateWindow(std::forward<Args>(args)...),
64  &SDL_DestroyWindow);
65  return ret;
66  }
67 
69  class TextInput {
70  public:
71  TextInput() { SDL_StartTextInput(); }
72  ~TextInput() { SDL_StopTextInput(); }
73 
74  TextInput(TextInput const &) = delete; //< non-copyable
75  TextInput &operator=(TextInput const &) = delete; //< non-assignable
76  };
77 
79  class GLContext {
80  public:
82  template <typename... Args> GLContext(Args &&... args) {
83  m_context = SDL_GL_CreateContext(std::forward<Args>(args)...);
84  }
85 
87  ~GLContext() { SDL_GL_DeleteContext(m_context); }
88 
90  operator SDL_GLContext() const { return m_context; }
91 
92  GLContext(GLContext const &) = delete; //< non-copyable
93  GLContext &operator=(GLContext const &) = delete; //< non-assignable
94 
95  private:
96  SDL_GLContext m_context;
97  };
98 } // namespace SDL2
99 } // namespace osvr
100 
101 #endif // INCLUDED_SDL2Helpers_h_GUID_D901E282_40A2_4CB1_DA01_A88E21A45C94
GLContext(Args &&... args)
Forwarding constructor.
Definition: SDL2Helpers.h:82
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
RAII wrapper for SDL text input start/stop.
Definition: SDL2Helpers.h:69
RAII wrapper for SDL startup/shutdown.
Definition: SDL2Helpers.h:44
WindowPtr createWindow(Args &&... args)
Smart pointer constructor for SDL_Window (forwarding)
Definition: SDL2Helpers.h:62
~GLContext()
Destructor for cleanup.
Definition: SDL2Helpers.h:87
RAII wrapper for SDL OpenGL context.
Definition: SDL2Helpers.h:79
std::shared_ptr< SDL_Window > WindowPtr
Smart pointer for SDL_Window.
Definition: SDL2Helpers.h:59