-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
139 lines (120 loc) · 3.32 KB
/
window.cpp
File metadata and controls
139 lines (120 loc) · 3.32 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "window.hpp"
Window* Window::instance = nullptr;
Window::Window(const unsigned width, const unsigned height) {
_height = SCREEN_HEIGHT;
_width = SCREEN_WIDTH;
_mouseFocus = false;
_keyboardFocus = false;
_fullScreen = false;
_minimized = false;
_quit = false;
_window = nullptr;
_renderer = nullptr;
if (SDL_Init(SDL_INIT_EVERYTHING) >= 0) {
_window = SDL_CreateWindow("SDL2 Grapher", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, _width, _height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
//std::cout << "Warning: Linear texture filtering not enabled!" << "\n";
}
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
//std::cout << "SDL_image could not initialize! SDL_image Error :" << IMG_GetError() << "\n";
}
if (!initFont()) {
//std::cout << "Init font error SDL_ttf: " << TTF_GetError() << "\n";
}
//SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
}
}
Window* Window::getWindow() {
if (instance == nullptr) instance = new Window();
return instance;
}
Window::~Window() {
SDL_DestroyRenderer(_renderer);
SDL_DestroyWindow(_window);
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
void Window::handleWindowEvent(SDL_Event& e) {
if (e.type == SDL_WINDOWEVENT) {
bool updateCaption = false;
switch (e.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
_width = e.window.data1;
_height = e.window.data2;
if (_width <= SCREEN_WIDTH) {
_width = SCREEN_WIDTH;
}
if (_height <= SCREEN_HEIGHT) {
_height = SCREEN_HEIGHT;
}
SDL_SetWindowSize(_window, _width, _height);
SDL_RenderPresent(_renderer);
break;
case SDL_WINDOWEVENT_EXPOSED:
SDL_RenderPresent(_renderer);
break;
case SDL_WINDOWEVENT_ENTER:
_mouseFocus = true;
updateCaption = true;
break;
case SDL_WINDOWEVENT_LEAVE:
_mouseFocus = false;
updateCaption = true;
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
_keyboardFocus = true;
updateCaption = true;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
_keyboardFocus = false;
updateCaption = true;
break;
case SDL_WINDOWEVENT_MINIMIZED:
_minimized = true;
break;
case SDL_WINDOWEVENT_MAXIMIZED:
_minimized = false;
break;
case SDL_WINDOWEVENT_RESTORED:
_minimized = false;
break;
case SDL_WINDOWEVENT_CLOSE:
_quit = true;
break;
}
}
else if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_RETURN) {
if (_fullScreen) {
SDL_SetWindowFullscreen(_window, 0);
_fullScreen = false;
}
else {
SDL_SetWindowFullscreen(_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
_fullScreen = true;
_minimized = false;
}
}
}
const unsigned int& Window::getWidth() const {
return _width;
}
const unsigned int& Window::getHeight() const {
return _height;
}
const bool& Window::getMouseFocus() const {
return _mouseFocus;
}
const bool& Window::getKeyboardFocus() const {
return _keyboardFocus;
}
const bool& Window::getFullscreen() const {
return _fullScreen;
}
const bool& Window::getMinimized() const {
return _minimized;
}
const bool& Window::getQuit() const {
return _quit;
}