ge211
ge211_window.cpp
1 #include "ge211_window.h"
2 #include "ge211_error.h"
3 
4 #include <SDL.h>
5 
6 namespace ge211 {
7 
8 using namespace detail;
9 
10 Window::Window(const std::string& title, Dimensions 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  &SDL_DestroyWindow}
18 {
19  if (ptr_ == nullptr)
20  throw Host_error{"Could not create window"};
21 }
22 
23 uint32_t Window::get_flags_() const noexcept
24 {
25  return SDL_GetWindowFlags(get_raw_());
26 }
27 
28 Dimensions Window::get_dimensions() const noexcept
29 {
30  Dimensions result{0, 0};
31  SDL_GetWindowSize(get_raw_(), &result.width, &result.height);
32  return result;
33 }
34 
35 void Window::set_dimensions(Dimensions dims)
36 {
37  SDL_SetWindowSize(get_raw_(), dims.width, dims.height);
38 
39  if (get_dimensions() != dims)
40  throw Environment_error{"Window::set_dimensions: out of range"};
41 }
42 
43 const char* Window::get_title() const noexcept
44 {
45  return SDL_GetWindowTitle(get_raw_());
46 }
47 
48 void Window::set_title(const std::string& title) noexcept
49 {
50  SDL_SetWindowTitle(get_raw_(), title.c_str());
51 }
52 
53 bool Window::get_resizeable() const noexcept
54 {
55  return (get_flags_() & SDL_WINDOW_RESIZABLE) != 0;
56 }
57 
58 void Window::set_resizeable(bool resizable) noexcept
59 {
60  SDL_SetWindowResizable(get_raw_(), resizable? SDL_TRUE : SDL_FALSE);
61 }
62 
63 Position Window::get_position() const noexcept
64 {
65  Position result{0, 0};
66  SDL_GetWindowPosition(get_raw_(), &result.x, &result.y);
67  return result;
68 }
69 
70 void Window::set_position(Position position)
71 {
72  SDL_SetWindowPosition(get_raw_(), position.x, position.y);
73 }
74 
75 const Position Window::centered{SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED};
76 
77 bool Window::get_fullscreen() const noexcept
78 {
79  return (get_flags_() & SDL_WINDOW_FULLSCREEN) != 0;
80 }
81 
82 void Window::set_fullscreen(bool fullscreen)
83 {
84  uint32_t flags = fullscreen? SDL_WINDOW_FULLSCREEN : 0;
85 
86  if (SDL_SetWindowFullscreen(get_raw_(), flags) < 0)
87  throw Host_error{"Window::set_fullscreen: failed"};
88 }
89 
90 Dimensions Window::max_fullscreen_dimensions() noexcept
91 {
92  SDL_Rect rect;
93  SDL_GetDisplayBounds(0, &rect);
94  return {rect.w, rect.h};
95 }
96 
97 Dimensions Window::max_window_dimensions() const noexcept
98 {
99  int top, left, bottom, right;
100  SDL_GetWindowBordersSize(get_raw_(), &top, &left, &bottom, &right);
101 
102  SDL_Rect rect;
103  SDL_GetDisplayUsableBounds(0, &rect);
104 
105  return {rect.w - left - right, rect.h - top - bottom};
106 }
107 
108 }
Coordinate width
The width of the object.
Coordinate height
The height of the object.
The game engine namespace.
Definition: ge211.h:17
Coordinate x
The x coordinate.
Coordinate y
The y coordiante.
Indicates an exception from the host environment being passed along by ge211.
Definition: ge211_error.h:87
Represents the dimensions of an object, or more generally, the displacement between two Basic_positio...
Definition: ge211_forward.h:73
A position in the T-valued Cartesian plane.
Definition: ge211_forward.h:74
Indicates that an error was encountered by the game engine or in the client&#39;s environment.
Definition: ge211_error.h:58