Repository navigation
Expand file tree
/
Copy pathframework-window.cpp
More file actions
654 lines (558 loc) · 12.1 KB
/
Copy pathframework-window.cpp
File metadata and controls
654 lines (558 loc) · 12.1 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
Copyright (C) 2020 Marcel Smit
https://www.facebook.com/marcel.smit981
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "framework.h"
#include "internal.h"
#if WINDOW_HAS_A_SURFACE
#include "gx_render.h"
#endif
#if FRAMEWORK_USE_SDL
#if ENABLE_METAL
#include "gx-metal/metal.h"
#endif
#endif
#define DEFAULT_PIXELS_PER_METER 700 // screens often use a value of around 72dpi = ~2800 dots per meter. we use a value a few times smaller here, to make the virtual windows appear larger by default
#if FRAMEWORK_USE_SDL
Window::Window(SDL_Window * window)
: m_prev(nullptr)
, m_next(nullptr)
#if WINDOW_HAS_A_SURFACE
, m_colorTarget(nullptr)
, m_depthTarget(nullptr)
#endif
#if FRAMEWORK_USE_SDL
, m_window(nullptr)
#endif
, m_title()
, m_width(0)
, m_height(0)
, m_isVisible(true)
, m_hasFocus(false)
#if WINDOW_IS_3D
, m_transform(true)
, m_pixelsPerMeter(DEFAULT_PIXELS_PER_METER)
, m_isInteracting(false)
#endif
, m_windowData(nullptr)
{
#if FRAMEWORK_USE_SDL
m_window = window;
#endif
m_windowData = new WindowData();
framework.registerWindow(this);
}
#endif
Window::Window(const char * title, const int sx, const int sy, const bool resizable, const bool wantsSurface, const bool wantsSdlWindow)
: m_prev(nullptr)
, m_next(nullptr)
#if WINDOW_HAS_A_SURFACE
, m_colorTarget(nullptr)
, m_depthTarget(nullptr)
#endif
#if FRAMEWORK_USE_SDL
, m_window(nullptr)
#endif
, m_title()
, m_width(0)
, m_height(0)
, m_isVisible(true)
, m_hasFocus(false)
#if WINDOW_IS_3D
, m_transform(true)
, m_pixelsPerMeter(DEFAULT_PIXELS_PER_METER)
, m_isInteracting(false)
#endif
, m_windowData(nullptr)
{
#if WINDOW_HAS_A_SURFACE
if (sx > 0 && sy > 0 && wantsSurface)
{
m_colorTarget = new ColorTarget();
m_depthTarget = new DepthTarget();
m_colorTarget->init(sx, sy, SURFACE_RGBA8, colorBlackTranslucent);
m_depthTarget->init(sx, sy, DEPTH_FLOAT32, true, 0.f);
}
#endif
#if FRAMEWORK_USE_SDL
if (wantsSdlWindow)
{
int flags = (SDL_WINDOW_ALLOW_HIGHDPI * framework.allowHighDpi) | (SDL_WINDOW_RESIZABLE * resizable);
#if ENABLE_OPENGL
flags |= SDL_WINDOW_OPENGL;
#endif
m_window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sx, sy, flags);
}
else
{
m_title = title;
m_width = sx;
m_height = sy;
}
#if ENABLE_METAL
metal_attach(this);
#endif
#else
m_title = title;
m_width = sx;
m_height = sy;
#endif
#if WINDOW_IS_3D
m_transform = Mat4x4(true).Translate(0, 1.f, -1.f);
#endif
m_windowData = new WindowData();
framework.registerWindow(this);
}
Window::~Window()
{
framework.unregisterWindow(this);
delete m_windowData;
m_windowData = nullptr;
#if WINDOW_HAS_A_SURFACE
if (m_colorTarget)
{
m_colorTarget->free();
delete m_colorTarget;
m_colorTarget = nullptr;
}
if (m_depthTarget)
{
m_depthTarget->free();
delete m_depthTarget;
m_depthTarget = nullptr;
}
#endif
#if FRAMEWORK_USE_SDL
#if ENABLE_METAL
metal_detach(this);
#endif
if (m_window)
{
SDL_DestroyWindow(m_window);
m_window = nullptr;
}
#endif
}
void Window::setPosition(const int x, const int y)
{
#if FRAMEWORK_USE_SDL
if (m_window)
SDL_SetWindowPosition(m_window, x, y);
#endif
}
void Window::setPositionCentered()
{
#if FRAMEWORK_USE_SDL
if (m_window)
SDL_SetWindowPosition(m_window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
#endif
}
void Window::setSize(const int sx, const int sy)
{
#if WINDOW_HAS_A_SURFACE
if (sx != getWidth() || sy != getHeight())
{
if (m_colorTarget)
{
m_colorTarget->free();
m_colorTarget->init(sx, sy, SURFACE_RGBA8, colorBlackTranslucent);
}
if (m_depthTarget)
{
m_depthTarget->free();
m_depthTarget->init(sx, sy, DEPTH_FLOAT32, false, 0.f);
}
}
#endif
#if FRAMEWORK_USE_SDL
if (m_window)
SDL_SetWindowSize(m_window, sx, sy);
else
{
m_width = sx;
m_height = sy;
}
#else
m_width = sx;
m_height = sy;
#endif
}
void Window::setFullscreen(const bool fullscreen)
{
#if FRAMEWORK_USE_SDL
if (m_window)
{
// https://wiki.libsdl.org/SDL_SetWindowFullscreen
// SDL_WINDOW_FULLSCREEN, SDL_WINDOW_FULLSCREEN_DESKTOP or 0
if (fullscreen)
SDL_SetWindowFullscreen(m_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
else
SDL_SetWindowFullscreen(m_window, 0);
}
#endif
}
void Window::setTitle(const char * title)
{
#if FRAMEWORK_USE_SDL
if (m_window)
SDL_SetWindowTitle(m_window, title);
#else
m_title = title;
#endif
}
void Window::show()
{
#if FRAMEWORK_USE_SDL
if (m_window)
SDL_ShowWindow(m_window);
#else
m_isVisible = true;
#endif
}
void Window::hide()
{
#if FRAMEWORK_USE_SDL
if (m_window)
SDL_HideWindow(m_window);
#else
m_isVisible = false;
#endif
}
bool Window::isHidden() const
{
#if FRAMEWORK_USE_SDL
if (m_window)
return (SDL_GetWindowFlags(m_window) & SDL_WINDOW_HIDDEN) != 0;
else
return false;
#else
return !m_isVisible;
#endif
}
bool Window::hasFocus() const
{
#if FRAMEWORK_USE_SDL
if (m_window)
return (SDL_GetWindowFlags(m_window) & SDL_WINDOW_INPUT_FOCUS) != 0;
else
return false;
#else
return m_hasFocus;
#endif
}
void Window::raise()
{
#if FRAMEWORK_USE_SDL
if (m_window)
SDL_RaiseWindow(m_window);
#endif
}
void Window::getPosition(int & x, int & y) const
{
#if FRAMEWORK_USE_SDL
if (m_window)
SDL_GetWindowPosition(m_window, &x, &y);
else
x = y = 0;
#else
x = y = 0;
#endif
}
int Window::getWidth() const
{
#if WINDOW_HAS_A_SURFACE
if (m_colorTarget)
return m_colorTarget->getWidth();
#endif
#if FRAMEWORK_USE_SDL
if (m_window)
{
int sx;
int sy;
SDL_GetWindowSize(m_window, &sx, &sy);
return sx;
}
else
{
return m_width;
}
#else
return m_width;
#endif
}
int Window::getHeight() const
{
#if WINDOW_HAS_A_SURFACE
if (m_colorTarget)
return m_colorTarget->getHeight();
#endif
#if FRAMEWORK_USE_SDL
if (m_window)
{
int sx;
int sy;
SDL_GetWindowSize(m_window, &sx, &sy);
return sy;
}
else
{
return m_height;
}
#else
return m_height;
#endif
}
const char * Window::getTitle() const
{
#if FRAMEWORK_USE_SDL
if (m_window)
return SDL_GetWindowTitle(m_window);
else
return "";
#else
return m_title.c_str();
#endif
}
bool Window::isFullscreen() const
{
#if FRAMEWORK_USE_SDL
if (m_window)
return (SDL_GetWindowFlags(m_window) & SDL_WINDOW_FULLSCREEN) != 0;
#endif
return false;
}
bool Window::getQuitRequested() const
{
return m_windowData->quitRequested;
}
bool Window::getMousePosition(float & x, float & y) const
{
x = m_windowData->mouseData.mouseX;
y = m_windowData->mouseData.mouseY;
return true;
}
bool Window::hasSurface() const
{
#if WINDOW_HAS_A_SURFACE
if (m_colorTarget)
return true;
#endif
return false;
}
ColorTarget * Window::getColorTarget() const
{
#if WINDOW_HAS_A_SURFACE
return m_colorTarget;
#else
return nullptr;
#endif
}
DepthTarget * Window::getDepthTarget() const
{
#if WINDOW_HAS_A_SURFACE
return m_depthTarget;
#else
return nullptr;
#endif
}
float Window::getBackingScale() const
{
#if WINDOW_HAS_A_SURFACE
return 1.f;
#else
return globals.initialScreenBackingScale;
#endif
}
float Window::getScreenBackingScale() const
{
// todo : the screen backing scale should be updated when the window is dragged onto another screen
return globals.initialScreenBackingScale;
}
#if FRAMEWORK_USE_SDL
SDL_Window * Window::getWindow() const
{
return m_window;
}
#else
void Window::setHasFocus(const bool hasFocus)
{
m_hasFocus = hasFocus;
}
#endif
#if WINDOW_IS_3D
void Window::setTransform(const Mat4x4 & transform)
{
m_transform = transform;
}
void Window::setPixelsPerMeter(const float ppm)
{
m_pixelsPerMeter = ppm;
}
bool Window::intersectRay(Vec3Arg rayOrigin, Vec3Arg rayDirection, const float depthThreshold, Vec2 & out_pixelPos, float & out_distance) const
{
const Mat4x4 worldToWindow = m_transform.CalcInv();
const Vec3 rayOrigin_window = worldToWindow.Mul4(rayOrigin);
const Vec3 rayDirection_window = worldToWindow.Mul3(rayDirection).CalcNormalized();
const float d1 = rayOrigin_window[2];
const float dd = rayDirection_window[2];
if (dd == 0.f)
return false;
const float t = -d1 / dd;
if (t < -depthThreshold)
return false;
const Vec2 p_meter(
+ rayOrigin_window[0] + rayDirection_window[0] * t,
- rayOrigin_window[1] - rayDirection_window[1] * t);
//logDebug("pos: %.2f, %.2f", p_meter[0], p_meter[1]);
const Vec2 p_pixel = p_meter * m_pixelsPerMeter + Vec2(getWidth()/2.f, getHeight()/2.f);
if (p_pixel[0] >= 0.f && p_pixel[0] < getWidth() &&
p_pixel[1] >= 0.f && p_pixel[1] < getHeight())
{
out_pixelPos = p_pixel;
out_distance = t;
return true;
}
else
{
return false;
}
}
void Window::draw3d() const
{
Assert(hasSurface());
gxPushMatrix();
{
const Mat4x4 transformForDraw = getTransformForDraw();
gxMultMatrixf(transformForDraw.m_v);
gxSetTexture(getColorTarget()->getTextureId(), GX_SAMPLE_LINEAR, true);
{
setColor(colorWhite);
const int x1 = 0;
const int y1 = getHeight();
const int x2 = getWidth();
const int y2 = 0;
gxBegin(GX_QUADS);
{
gxNormal3f(0, 0, -1);
gxTexCoord2f(0.f, 0.f); gxVertex2f(x1, y1);
gxTexCoord2f(1.f, 0.f); gxVertex2f(x2, y1);
gxTexCoord2f(1.f, 1.f); gxVertex2f(x2, y2);
gxTexCoord2f(0.f, 1.f); gxVertex2f(x1, y2);
}
gxEnd();
}
gxClearTexture();
}
gxPopMatrix();
}
void Window::draw3dCursor() const
{
Assert(hasSurface());
gxPushMatrix();
{
const Mat4x4 transformForDraw = getTransformForDraw();
gxMultMatrixf(transformForDraw.m_v);
gxTranslatef(0, 0, -.001f); // 1mm above the surface
fillCircle(
m_windowData->mouseData.mouseX,
getHeight() - m_windowData->mouseData.mouseY,
3,
10);
}
gxPopMatrix();
}
const Mat4x4 & Window::getTransform() const
{
return m_transform;
}
Mat4x4 Window::getTransformForDraw() const
{
const int sx = getWidth();
const int sy = getHeight();
return
m_transform
.Scale(1.f / m_pixelsPerMeter, 1.f / m_pixelsPerMeter, 1.f)
.Translate(-sx/2.f, -sy/2.f, 0);
}
#endif
WindowData * Window::getWindowData() const
{
return m_windowData;
}
static Stack<Window*, 32> s_windowStack(nullptr);
void pushWindow(Window & window)
{
s_windowStack.push(globals.currentWindow);
globals.currentWindow = &window;
globals.currentWindow->getWindowData()->makeActive();
#if WINDOW_HAS_A_SURFACE
if (window.hasSurface())
{
// nothing to do
}
else
#endif
{
Assert(globals.renderPass.isActive == false);
#if FRAMEWORK_USE_SDL
if (globals.currentWindow->getWindow())
{
#if ENABLE_OPENGL
SDL_GL_MakeCurrent(globals.currentWindow->getWindow(), globals.glContext);
#endif
}
#if ENABLE_METAL
metal_make_active(globals.currentWindow);
#endif
#endif
}
pushContentScale(window.getBackingScale());
}
void popWindow()
{
popContentScale();
Window * window = s_windowStack.popValue();
globals.currentWindow = window;
if (globals.currentWindow != nullptr)
{
globals.currentWindow->getWindowData()->makeActive();
if (globals.currentWindow->hasSurface())
{
// nothing to do
}
else
{
Assert(globals.renderPass.isActive == false);
#if FRAMEWORK_USE_SDL
if (globals.currentWindow->getWindow())
{
#if ENABLE_OPENGL
SDL_GL_MakeCurrent(globals.currentWindow->getWindow(), globals.glContext);
#endif
}
#if ENABLE_METAL
metal_make_active(globals.currentWindow);
#endif
#endif
}
}
}