Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
#include "stdafx.h"

#include "Headers/DisplayWindow.h"

#include "Core/Headers/Kernel.h"
#include "Core/Headers/Console.h"
#include "Core/Headers/Application.h"
#include "Core/Headers/PlatformContext.h"
#include "Utility/Headers/Localization.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Input/Headers/InputInterface.h"

#ifndef HAVE_M_PI
#define HAVE_M_PI
#endif //HAVE_M_PI

#include <SDL.h>
#include <AntTweakBar/include/AntTweakBar.h>

namespace Divide {

namespace {
    SDL_SystemCursor CursorToSDL(CursorStyle style) {
        switch (style) {
            case CursorStyle::ARROW: return SDL_SYSTEM_CURSOR_ARROW;
            case CursorStyle::HAND: return SDL_SYSTEM_CURSOR_HAND;
            case CursorStyle::NONE: return SDL_SYSTEM_CURSOR_NO;
            case CursorStyle::RESIZE_EW: return SDL_SYSTEM_CURSOR_SIZEWE;
            case CursorStyle::RESIZE_NS: return SDL_SYSTEM_CURSOR_SIZENS;
            case CursorStyle::RESIZE_NESW: return SDL_SYSTEM_CURSOR_SIZENESW;
            case CursorStyle::RESIZE_NWSE: return SDL_SYSTEM_CURSOR_SIZENWSE;
            case CursorStyle::TEXT_INPUT: return SDL_SYSTEM_CURSOR_IBEAM;
        };

        return SDL_SYSTEM_CURSOR_NO;
    }
}; // namespace 

DisplayWindow::DisplayWindow(WindowManager& parent, PlatformContext& context)
 : GUIDWrapper(),
   PlatformContextComponent(context),
   _parent(parent),
   _swapBuffers(true),
   _hasFocus(true),
   _minimized(false),
   _maximized(false),
   _hidden(true),
   _warp(false),
   _warpRect(-1),
   _opacity(255),
   _type(WindowType::COUNT),
   _previousType(WindowType::COUNT),
   _queuedType(WindowType::COUNT),
   _sdlWindow(nullptr),
   _internalMoveEvent(false),
   _externalResizeEvent(false),
   _clearColour(DefaultColours::DIVIDE_BLUE),
   _windowID(std::numeric_limits<Uint32>::max()),
   _inputHandler(std::make_unique<Input::InputInterface>(*this))
{
    _windowPosition.set(0);
    _prevDimensions.set(1);
    _windowDimensions.set(1);
}

DisplayWindow::~DisplayWindow() 
{
   destroyWindow();
}

ErrorCode DisplayWindow::destroyWindow() {

    if (_type != WindowType::COUNT && _sdlWindow != nullptr) {
        if (_destroyCbk) {
            _destroyCbk();
        }

        _inputHandler->terminate();
        SDL_DestroyWindow(_sdlWindow);
        _sdlWindow = nullptr;
    }

    return ErrorCode::NO_ERR;
}

ErrorCode DisplayWindow::init(U32 windowFlags,
                              WindowType initialType,
                              const vec2<U16>& dimensions,
                              const char* windowTitle)
{
    _type = initialType;
    _title = windowTitle;
    _windowDimensions = dimensions;

    _sdlWindow = SDL_CreateWindow(windowTitle,
                                  SDL_WINDOWPOS_CENTERED_DISPLAY(_parent.targetDisplay()),
                                  SDL_WINDOWPOS_CENTERED_DISPLAY(_parent.targetDisplay()),
                                  1,
                                  1,
                                  windowFlags);

    _windowID = SDL_GetWindowID(_sdlWindow);
    
    I32 positionX, positionY;
    SDL_GetWindowPosition(_sdlWindow, &positionX, &positionY);
    setPosition(positionX, positionY);

    getWindowHandle(_sdlWindow, _handle);

    // Check if we have a valid window
    if (!_sdlWindow) {
        SDL_Quit();
        Console::errorfn(Locale::get(_ID("ERROR_GFX_DEVICE")),
                         Locale::get(_ID("ERROR_SDL_WINDOW")));
        Console::printfn(Locale::get(_ID("WARN_APPLICATION_CLOSE")));
        return ErrorCode::SDL_WINDOW_INIT_ERROR;
    }

    return _inputHandler->init(dimensions);
}

void DisplayWindow::update(const U64 deltaTimeUS) {
    if (hasFocus()) {
        _inputHandler->update(deltaTimeUS);
    }
    if (_queuedType != WindowType::COUNT) {
        //handleChangeWindowType(_queuedType);
        _queuedType = WindowType::COUNT;
    }
}

void DisplayWindow::notifyListeners(WindowEvent event, const WindowEventArgs& args) {
    for (EventListener& listener : _eventListeners[to_base(event)]) {
        listener(args);
    }
}

void DisplayWindow::handleEvent(SDL_Event event) {
    if (event.type != SDL_WINDOWEVENT) {
        return;
    }
    if (event.window.windowID != _windowID) {
        return;
    }

    WindowEventArgs args;
    args._windowGUID = getGUID();

    switch (event.window.event) {
        case SDL_WINDOWEVENT_CLOSE: {
            notifyListeners(WindowEvent::CLOSE_REQUESTED, args);
            _parent.handleWindowEvent(WindowEvent::CLOSE_REQUESTED,
                                        getGUID(),
                                        event.quit.type,
                                        event.quit.timestamp);
        } break;
        case SDL_WINDOWEVENT_ENTER:
        case SDL_WINDOWEVENT_FOCUS_GAINED: {
            args._flag = true;
            notifyListeners(WindowEvent::GAINED_FOCUS, args);
            _parent.handleWindowEvent(WindowEvent::GAINED_FOCUS,
                                      getGUID(),
                                      event.window.data1,
                                      event.window.data2);
        } break;
        case SDL_WINDOWEVENT_LEAVE:
        case SDL_WINDOWEVENT_FOCUS_LOST: {
            args._flag = false;
            notifyListeners(WindowEvent::LOST_FOCUS, args);

            _parent.handleWindowEvent(WindowEvent::LOST_FOCUS,
                getGUID(),
                event.window.data1,
                event.window.data2);
        } break;
        case SDL_WINDOWEVENT_SIZE_CHANGED:
        case SDL_WINDOWEVENT_RESIZED: {
            _externalResizeEvent = event.window.event == SDL_WINDOWEVENT_RESIZED;
            args.x = event.window.data1;
            args.y = event.window.data2;
            args._flag = _externalResizeEvent;
            notifyListeners(WindowEvent::RESIZED, args);

            _parent.handleWindowEvent(WindowEvent::RESIZED,
                                        getGUID(),
                                        event.window.data1,
                                        event.window.data2,
                                        _externalResizeEvent);
            _externalResizeEvent = false;
        } break;
        case SDL_WINDOWEVENT_MOVED: {
            args.x = event.window.data1;
            args.y = event.window.data2;
            notifyListeners(WindowEvent::MOVED, args);

            _parent.handleWindowEvent(WindowEvent::MOVED,
                getGUID(),
                event.window.data1,
                event.window.data2);

            if (!_internalMoveEvent) {
                setPosition(event.window.data1,
                            event.window.data2);
                _internalMoveEvent = false;
            }
        } break;
        case SDL_WINDOWEVENT_SHOWN: {
            notifyListeners(WindowEvent::SHOWN, args);
                        
            _parent.handleWindowEvent(WindowEvent::SHOWN,
                getGUID(),
                event.window.data1,
                event.window.data2);
        } break;
        case SDL_WINDOWEVENT_HIDDEN: {
            notifyListeners(WindowEvent::HIDDEN, args);

            _parent.handleWindowEvent(WindowEvent::HIDDEN,
                getGUID(),
                event.window.data1,
                event.window.data2);
        } break;
        case SDL_WINDOWEVENT_MINIMIZED: {
            notifyListeners(WindowEvent::MINIMIZED, args);

            _parent.handleWindowEvent(WindowEvent::MINIMIZED,
                getGUID(),
                event.window.data1,
                event.window.data2);
        } break;
        case SDL_WINDOWEVENT_MAXIMIZED: {
            notifyListeners(WindowEvent::MAXIMIZED, args);

            _parent.handleWindowEvent(WindowEvent::MAXIMIZED,
                getGUID(),
                event.window.data1,
                event.window.data2);
        } break;
        case SDL_WINDOWEVENT_RESTORED: {
            notifyListeners(WindowEvent::RESTORED, args);

            _parent.handleWindowEvent(WindowEvent::RESTORED,
                getGUID(),
                event.window.data1,
                event.window.data2);
        } break;
    };
}

void DisplayWindow::setDimensionsInternal(U16& w, U16& h) {
    if (_externalResizeEvent && 
        (_type != WindowType::WINDOW &&
         _type != WindowType::SPLASH))
    {
        return;
    }

    if (_type == WindowType::FULLSCREEN) {
        // Find a decent resolution close to our dragged dimensions
        SDL_DisplayMode mode, closestMode;
        SDL_GetCurrentDisplayMode(_parent.targetDisplay(), &mode);
        mode.w = w;
        mode.h = h;
        SDL_GetClosestDisplayMode(_parent.targetDisplay(), &mode, &closestMode);
        w = to_U16(closestMode.w);
        h = to_U16(closestMode.h);
        SDL_SetWindowDisplayMode(_sdlWindow, &closestMode);
    } else {
        SDL_SetWindowSize(_sdlWindow, w, h);
    }

    if (_inputHandler->isInit()) {
        _inputHandler->onChangeWindowSize(w, h);
    }
}

vec2<U16> DisplayWindow::getDrawableSize() const {
    return context().gfx().getDrawableSize(*this);
}

void DisplayWindow::opacity(U8 opacity) {
    if (SDL_SetWindowOpacity(_sdlWindow, opacity / 255.0f) != -1) {
        _opacity = opacity;
    }
}

/// Window positioning is handled by SDL
void DisplayWindow::setPositionInternal(I32 w, I32 h) {
    _internalMoveEvent = true;
    SDL_SetWindowPosition(_sdlWindow, w, h);
}

/// Centering is also easier via SDL
void DisplayWindow::centerWindowPosition() {
    _internalMoveEvent = true;

    setPosition(SDL_WINDOWPOS_CENTERED_DISPLAY(_parent.targetDisplay()),
                SDL_WINDOWPOS_CENTERED_DISPLAY(_parent.targetDisplay()));
}

/// Mouse positioning is handled by SDL
void DisplayWindow::setCursorPosition(I32 x, I32 y) {
    SDL_WarpMouseInWindow(_sdlWindow, x, y);
}

void DisplayWindow::setCursorStyle(CursorStyle style) {
    SDL_SetCursor(SDL_CreateSystemCursor(CursorToSDL(style)));
}

vec2<I32> DisplayWindow::getCursorPosition() const {
    I32 x = 0, y = 0;
    SDL_GetMouseState(&x, &y);
    return vec2<I32>(x, y);
}

void DisplayWindow::hidden(const bool state) {
    if (BitCompare(SDL_GetWindowFlags(_sdlWindow), to_U32(SDL_WINDOW_SHOWN)) == state)
    {
        if (state) {
            SDL_HideWindow(_sdlWindow);
        } else {
            SDL_ShowWindow(_sdlWindow);
        }
        _hidden = state;
    }
}

void DisplayWindow::restore() {
    SDL_RestoreWindow(_sdlWindow);
    _maximized = _minimized = false;
}

void DisplayWindow::minimized(const bool state) {
    if (BitCompare(SDL_GetWindowFlags(_sdlWindow), to_U32(SDL_WINDOW_MINIMIZED)) != state)
    {
        if (state) {
            SDL_MinimizeWindow(_sdlWindow);
        } else {
            restore();
        }

        _minimized = state;
    }
}

void DisplayWindow::maximized(const bool state) {
    if (BitCompare(SDL_GetWindowFlags(_sdlWindow), to_U32(SDL_WINDOW_MAXIMIZED)) != state)
    {
        if (state) {
            SDL_MaximizeWindow(_sdlWindow);
        } else {
            restore();
        }
        _maximized = state;
    }
}

void DisplayWindow::title(const stringImpl& title) {
    if (title != _title) {
        SDL_SetWindowTitle(_sdlWindow, title.c_str());
        _title = title;
    }
}

void DisplayWindow::handleChangeWindowType(WindowType newWindowType) {
    _previousType = _type;
    _type = newWindowType;
    I32 switchState = 0;
    switch (newWindowType) {
        case WindowType::SPLASH: {
            switchState = SDL_SetWindowFullscreen(_sdlWindow, 0);
            assert(switchState >= 0);

            SDL_SetWindowBordered(_sdlWindow, SDL_FALSE);
            SDL_SetWindowGrab(_sdlWindow, SDL_FALSE);
        } break;
        case WindowType::WINDOW: {
            switchState = SDL_SetWindowFullscreen(_sdlWindow, 0);
            assert(switchState >= 0);

            SDL_SetWindowBordered(_sdlWindow, SDL_TRUE);
            SDL_SetWindowGrab(_sdlWindow, SDL_FALSE);
        } break;
        case WindowType::FULLSCREEN_WINDOWED: {
            switchState = SDL_SetWindowFullscreen(_sdlWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
            assert(switchState >= 0);

            SDL_SetWindowBordered(_sdlWindow, SDL_FALSE);
            SDL_SetWindowGrab(_sdlWindow, SDL_FALSE);
        } break;
        case WindowType::FULLSCREEN: {
            switchState = SDL_SetWindowFullscreen(_sdlWindow, SDL_WINDOW_FULLSCREEN);
            assert(switchState >= 0);

            SDL_SetWindowBordered(_sdlWindow, SDL_FALSE);
            SDL_SetWindowGrab(_sdlWindow, SDL_TRUE);
        } break;
    };

    centerWindowPosition();

    _parent.pollSDLEvents();
}

vec2<U16> DisplayWindow::getPreviousDimensions() const {
    if (fullscreen()) {
        return _parent.getFullscreenResolution();
    }
    return _prevDimensions;
}

void DisplayWindow::setDimensions(U16& dimensionX, U16& dimensionY) {
    if (!fullscreen()) {
        setDimensionsInternal(dimensionX, dimensionY);

        _prevDimensions.set(_windowDimensions);
        _windowDimensions.set(dimensionX, dimensionY);
        _parent.pollSDLEvents();
    }
}

void DisplayWindow::setDimensions(vec2<U16>& dimensions) {
    setDimensions(dimensions.x, dimensions.y);
}

vec2<U16> DisplayWindow::getDimensions() const {
    if (fullscreen()) {
        return _parent.getFullscreenResolution();
    }

    return _windowDimensions;
}

/// Key pressed: return true if input was consumed
bool DisplayWindow::onKeyDown(const Input::KeyEvent& key) {
    DisplayWindow::WindowEventArgs args;
    args._windowGUID = getGUID();
    args._key = key._key;
    args._flag = true;
    notifyListeners(WindowEvent::KEY_PRESS, args);

    return _context.app().kernel().onKeyDown(key);
}

/// Key released: return true if input was consumed
bool DisplayWindow::onKeyUp(const Input::KeyEvent& key) {
    DisplayWindow::WindowEventArgs args;
    args._windowGUID = getGUID();
    args._key = key._key;
    args._flag = false;
    notifyListeners(WindowEvent::KEY_PRESS, args);

    return _context.app().kernel().onKeyUp(key);
}

/// Joystick axis change: return true if input was consumed
bool DisplayWindow::joystickAxisMoved(const Input::JoystickEvent& arg, I8 axis) {
    return _context.app().kernel().joystickAxisMoved(arg, axis);
}

/// Joystick direction change: return true if input was consumed
bool DisplayWindow::joystickPovMoved(const Input::JoystickEvent& arg, I8 pov) {
    return _context.app().kernel().joystickPovMoved(arg, pov);
}

/// Joystick button pressed: return true if input was consumed
bool DisplayWindow::joystickButtonPressed(const Input::JoystickEvent& arg, Input::JoystickButton button) {
    return _context.app().kernel().joystickButtonPressed(arg, button);
}
/// Joystick button released: return true if input was consumed
bool DisplayWindow::joystickButtonReleased(const Input::JoystickEvent& arg, Input::JoystickButton button) {
    return _context.app().kernel().joystickButtonReleased(arg, button);
}
bool DisplayWindow::joystickSliderMoved(const Input::JoystickEvent& arg, I8 index) {
    return _context.app().kernel().joystickSliderMoved(arg, index);
}

bool DisplayWindow::joystickvector3Moved(const Input::JoystickEvent& arg, I8 index) {
    return _context.app().kernel().joystickvector3Moved(arg, index);
}
/// Mouse moved: return true if input was consumed
bool DisplayWindow::mouseMoved(const Input::MouseEvent& arg) {
    DisplayWindow::WindowEventArgs args;
    args._windowGUID = getGUID();

    if (arg.Z().rel != 0) {
        args._mod = arg.Z().rel / 60;
        notifyListeners(WindowEvent::MOUSE_WHEEL, args);
    } else {
        args.x = arg.X().abs;
        args.y = arg.Y().abs;
        notifyListeners(WindowEvent::MOUSE_MOVE, args);
    }

    return _context.app().kernel().mouseMoved(arg);
}

/// Mouse button pressed: return true if input was consumed
bool DisplayWindow::mouseButtonPressed(const Input::MouseEvent& arg, Input::MouseButton button) {
    DisplayWindow::WindowEventArgs args;
    args._windowGUID = getGUID();
    args.id = to_I32(button);
    args._flag = true;
    notifyListeners(WindowEvent::MOUSE_BUTTON, args);

    return _context.app().kernel().mouseButtonPressed(arg, button);
}

/// Mouse button released: return true if input was consumed
bool DisplayWindow::mouseButtonReleased(const Input::MouseEvent& arg, Input::MouseButton button) {
    DisplayWindow::WindowEventArgs args;
    args._windowGUID = getGUID();
    args.id = to_I32(button);
    args._flag = false;
    notifyListeners(WindowEvent::MOUSE_BUTTON, args);

    return _context.app().kernel().mouseButtonReleased(arg, button);
}

void DisplayWindow::warp(bool state, const Rect<I32>& rect) {
    _warp = state;
    if (_warp) {
        _warpRect.set(rect);
    }
}

void DisplayWindow::renderingViewport(const Rect<I32>& viewport) {
    _renderingViewport.set(viewport);
}

}; //namespace Divide

Commits for Divide-Framework/trunk/Source Code/Platform/DisplayWindow.cpp

Diff revisions: vs.
Revision Author Commited Message
988 Diff Diff IonutCava picture IonutCava Thu 30 Nov, 2017 23:08:51 +0000

[Ionut]
- Add a Rect class (basic wrapper around a vec4)
- More work on MouseEvent system

985 Diff Diff IonutCava picture IonutCava Tue 28 Nov, 2017 23:54:21 +0000

[Ionut]
- Started adapting mouse input events to handle viewport shifts

980 Diff Diff IonutCava picture IonutCava Wed 22 Nov, 2017 17:18:46 +0000

[Ionut]
- Started porting CEGUI to RTT

978 Diff Diff IonutCava picture IonutCava Mon 20 Nov, 2017 23:42:23 +0000

[Ionut]
- More decoupling between window size and render resolution.
- Attempt to preserve render target aspect on window resize events

  • Resolution change is currently broken *
976 Diff Diff IonutCava picture IonutCava Sun 19 Nov, 2017 23:10:18 +0000

[Ionut]
- Texture mipmaps are now compute before bind if they need a refresh (to avoid recalculation if they’re not used)
- More work on the editor: cleanup, load/save functionality, etc
- Input scaling: Mouse position is now relative to the scene preview position in editor mode. (the scene preview rect is used to re-map X/Y absolute and relative mouse positions)

975 Diff Diff IonutCava picture IonutCava Thu 16 Nov, 2017 14:49:12 +0000

[Ionut]
- Editor work. Port most of imgui-addons demo2.
- Add Editor render target into which we will render the scene when the editor is on

970 Diff Diff IonutCava picture IonutCava Mon 13 Nov, 2017 17:16:44 +0000

[Ionut]
- ImWindow <-> DisplayWindow fixes

969 Diff Diff IonutCava picture IonutCava Mon 13 Nov, 2017 00:15:57 +0000

[Ionut]
- Further improve DisplayWindow <-> Input relationship
- Add Flip-Y 2D camera (D3D style projection)
- Fix IMGUI rendering
- Add IMGUI sample and IMWINDOW sample rendering at the same time
- Improve IMWINDOW multi-window support
— Bug: nothing rendering in child platform windows
- Add draw command to move render context from one window to another

968 Diff Diff IonutCava picture IonutCava Tue 07 Nov, 2017 17:32:31 +0000

[Ionut]
- Consistent naming between timing variables

965 IonutCava picture IonutCava Sun 05 Nov, 2017 21:54:57 +0000

[Ionut]
- ImWindow updates