ge211  2021.5.1
A student game engine
ge211_window.cxx
1 #include "ge211_window.hxx"
2 #include "ge211_error.hxx"
3 
4 #include <SDL.h>
5 
6 namespace ge211 {
7 
8 using namespace detail;
9 
10 Window::Window(const std::string& title, Dims<int> dim)
11  : ptr_{SDL_CreateWindow(title.c_str(),
12  SDL_WINDOWPOS_UNDEFINED,
13  SDL_WINDOWPOS_UNDEFINED,
14  dim.width,
15  dim.height,
16  SDL_WINDOW_SHOWN)}
17 {
18  if (!ptr_)
19  throw Host_error{"Could not create window"};
20 }
21 
22 uint32_t Window::get_flags_() const NOEXCEPT
23 {
24  return SDL_GetWindowFlags(get_raw_());
25 }
26 
28 {
29  Dims<int> result{0, 0};
30  SDL_GetWindowSize(get_raw_(), &result.width, &result.height);
31  return result;
32 }
33 
35 {
36  SDL_SetWindowSize(get_raw_(), dims.width, dims.height);
37 
38  if (get_dimensions() != dims)
39  throw Environment_error{"Window::set_dimensions: out of range"};
40 }
41 
42 #if SDL_VERSION_ATLEAST(2, 0, 5)
43 bool Window::get_resizeable() const NOEXCEPT
44 {
45  return (get_flags_() & SDL_WINDOW_RESIZABLE) != 0;
46 }
47 
48 void Window::set_resizeable(bool resizable) NOEXCEPT
49 {
50  SDL_SetWindowResizable(get_raw_(), resizable? SDL_TRUE : SDL_FALSE);
51 }
52 #endif
53 
55 {
56  Posn<int> result{0, 0};
57  SDL_GetWindowPosition(get_raw_(), &result.x, &result.y);
58  return result;
59 }
60 
62 {
63  SDL_SetWindowPosition(get_raw_(), position.x, position.y);
64 }
65 
66 const Posn<int> Window::centered{SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED};
67 
68 bool Window::get_fullscreen() const NOEXCEPT
69 {
70  return (get_flags_() & SDL_WINDOW_FULLSCREEN) != 0;
71 }
72 
73 void Window::set_fullscreen(bool fullscreen)
74 {
75  uint32_t flags = fullscreen? SDL_WINDOW_FULLSCREEN : 0;
76 
77  if (SDL_SetWindowFullscreen(get_raw_(), flags) < 0)
78  throw Host_error{"Window::set_fullscreen: failed"};
79 }
80 
82 {
83  SDL_Rect rect;
84  SDL_GetDisplayBounds(0, &rect);
85  return {rect.w, rect.h};
86 }
87 
89 {
90  int top, left, bottom, right;
91  SDL_GetWindowBordersSize(get_raw_(), &top, &left, &bottom, &right);
92 
93  SDL_Rect rect;
94  SDL_GetDisplayUsableBounds(0, &rect);
95 
96  return {rect.w - left - right, rect.h - top - bottom};
97 }
98 
99 }
ge211::Window::get_fullscreen
bool get_fullscreen() const
Returns whether the program is in fullscreen mode.
Definition: ge211_window.cxx:68
ge211::Window::get_position
Posn< int > get_position() const
Gets the position of the upper-left corner of the window with respect to the upper-left corner of the...
Definition: ge211_window.cxx:54
ge211::Window::get_dimensions
Dims< int > get_dimensions() const
Returns the current dimensions of this window.
Definition: ge211_window.cxx:27
std::string
ge211::Window::set_fullscreen
void set_fullscreen(bool)
Sets whether the program should be in fullscreen mode.
Definition: ge211_window.cxx:73
ge211::Window::max_fullscreen_dimensions
static Dims< int > max_fullscreen_dimensions()
Returns the maximum dimensions for a fullscreen window.
Definition: ge211_window.cxx:81
ge211
The game engine namespace.
Definition: ge211.hxx:4
ge211::Window::set_position
void set_position(Posn< int >)
Sets the position of the upper-left corner of the window with respect to the upper-left corner of the...
Definition: ge211_window.cxx:61
ge211::geometry::Posn::x
Coordinate x
The x coordinate.
Definition: ge211_geometry.hxx:275
ge211::geometry::Dims< int >
std::string::c_str
T c_str(T... args)
ge211::geometry::Dims::height
Coordinate height
The height of the object.
Definition: ge211_geometry.hxx:41
ge211::Window::max_window_dimensions
Dims< int > max_window_dimensions() const
Returns the maximum dimensions for a non-fullscreen window.
Definition: ge211_window.cxx:88
ge211::Window::centered
static const Posn< int > centered
A special value to pass to set_position(Posn<int>) to center the window on the screen.
Definition: ge211_window.hxx:33
ge211::geometry::Posn::y
Coordinate y
The y coordiante.
Definition: ge211_geometry.hxx:276
ge211::exceptions::Environment_error
Indicates that an error was encountered by the game engine or in the client's environment.
Definition: ge211_error.hxx:91
ge211::geometry::Dims::width
Coordinate width
The width of the object.
Definition: ge211_geometry.hxx:40
ge211::Window::set_dimensions
void set_dimensions(Dims< int >)
Changes the size of the window.
Definition: ge211_window.cxx:34
ge211::geometry::Posn< int >
ge211::exceptions::Host_error
Indicates an exception from the host environment being passed along by ge211.
Definition: ge211_error.hxx:119