-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.c
More file actions
134 lines (71 loc) · 2.12 KB
/
window.c
File metadata and controls
134 lines (71 loc) · 2.12 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
#include "window.h"
Uint32 window_flags = 0x00;
SDL_Rect window_rect = { 0, 0, 0, 0 };
SDL_Window *window = NULL;
SDL_SysWMinfo window_sysinfo = { 0 };
bool window_enter = false;
bool window_focus_gained = false;
bool setup_window(const char *title, int w, int h, Uint32 flags)
{
if ((window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags)) == NULL)
{
fprintf(stderr, "Failed to create window: %s\n", SDL_GetError());
return false;
}
update_window_attributes();
SDL_VERSION(&window_sysinfo.version);
if (SDL_GetWindowWMInfo(window, &window_sysinfo) == SDL_FALSE)
{
fprintf(stderr, "Failed to get sysinfo from window: %s\n", SDL_GetError());
return false;
}
return true;
}
void update_window_attributes()
{
window_flags = SDL_GetWindowFlags(window);
SDL_GetWindowPosition(window, &window_rect.x, &window_rect.y);
SDL_GetWindowSize (window, &window_rect.w, &window_rect.h);
}
void handle_window(SDL_WindowEvent *window_event)
{
switch (window_event->event)
{
case SDL_WINDOWEVENT_MOVED:
window_rect.x = window_event->data1;
window_rect.y = window_event->data2;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
window_rect.w = window_event->data1;
window_rect.h = window_event->data2;
break;
case SDL_WINDOWEVENT_ENTER:
window_enter = true;
break;
case SDL_WINDOWEVENT_LEAVE:
window_enter = false;
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
window_focus_gained = true;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
window_focus_gained = false;
break;
default:
#ifndef NDEBUG
fprintf(stderr, "window event not handled: %d.\n", window_event->event);
#endif
break;
}
}
void quit_window()
{
window_flags = 0x00;
window_rect = (SDL_Rect){ 0, 0, 0, 0 };
if (window != NULL)
{
SDL_DestroyWindow(window);
window = NULL;
}
window_sysinfo = (SDL_SysWMinfo){ 0 };
}