-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdlWindow.h
169 lines (152 loc) · 4.21 KB
/
sdlWindow.h
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
// sdlWindow.hh
//
#ifndef LZZ_sdlWindow_hh
#define LZZ_sdlWindow_hh
struct SDL_Window;
// Be sure to set macro BGFXH_USE_WAYLAND if using wayland on linux
#define LZZ_INLINE inline
namespace bgfxh
{
void * getSdlNativeWindowHandle (SDL_Window * _window);
}
namespace bgfxh
{
void * getNativeDisplayHandle (SDL_Window * _window);
}
namespace bgfxh
{
bool initSdlWindow (SDL_Window * _window, bgfx::PlatformData & pdOut);
}
namespace bgfxh
{
bool initSdlWindowAndBgfx (SDL_Window * _window, bgfx::Init * _init = NULL);
}
#undef LZZ_INLINE
#endif
////////////////////////////////////////////////////////////////////////
#ifdef BGFXH_IMPL
#ifndef BGFXH_DOUBLE_GUARD_sdlWindow
#define BGFXH_DOUBLE_GUARD_sdlWindow
// sdlWindow.cpp
//
#ifndef BGFXH_SDL_SYSWM_HANDLED
#include <SDL2/SDL_syswm.h> //Needed for window handling
#endif
#define LZZ_INLINE inline
namespace bgfxh
{
void * getSdlNativeWindowHandle (SDL_Window * _window)
{
// From bgfx/examples/common/entry_sdl.cpp
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(_window, &wmi) ) {
return NULL;
}
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
# if BGFXH_USE_WAYLAND
wl_egl_window *win_impl = (wl_egl_window*)SDL_GetWindowData(_window, "wl_egl_window");
if(!win_impl)
{
int width, height;
SDL_GetWindowSize(_window, &width, &height);
struct wl_surface* surface = wmi.info.wl.surface;
if(!surface)
return nullptr;
win_impl = wl_egl_window_create(surface, width, height);
SDL_SetWindowData(_window, "wl_egl_window", win_impl);
}
return (void*)(uintptr_t)win_impl;
# else
return (void*)wmi.info.x11.window;
# endif
# elif BX_PLATFORM_OSX || BX_PLATFORM_IOS
return wmi.info.cocoa.window;
# elif BX_PLATFORM_WINDOWS
return wmi.info.win.window;
# elif BX_PLATFORM_ANDROID
return wmi.info.android.window;
# endif // BX_PLATFORM_
}
}
namespace bgfxh
{
void * getNativeDisplayHandle (SDL_Window * _window)
{
// From bgfx/examples/common/entry_sdl.cpp
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(_window, &wmi) ) {
return NULL;
}
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
# if BGFXH_USE_WAYLAND
return wmi.info.wl.display;
# else
return wmi.info.x11.display;
# endif // ENTRY_CONFIG_USE_WAYLAND
# else
return NULL;
# endif // BX_PLATFORM_*
}
}
namespace bgfxh
{
bool initSdlWindow (SDL_Window * _window, bgfx::PlatformData & pdOut)
{
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(_window, &wmi) ) {
return false;
}
pdOut.nwh = bgfxh::getSdlNativeWindowHandle(_window);
pdOut.ndt = bgfxh::getNativeDisplayHandle(_window);
pdOut.context = NULL;
pdOut.backBuffer = NULL;
pdOut.backBufferDS = NULL;
return true;
}
}
namespace bgfxh
{
bool initSdlWindowAndBgfx (SDL_Window * _window, bgfx::Init * _init)
{
bgfx::PlatformData pd;
if (!initSdlWindow (_window, pd))
return false;
int ww;
int wh;
SDL_GetWindowSize(_window, &ww, &wh);
const uint32_t m_resetFlags = BGFX_RESET_VSYNC | BGFX_RESET_MAXANISOTROPY | BGFX_RESET_FLIP_AFTER_RENDER ;// | BGFX_RESET_MSAA_X4;
//bgfx::renderFrame();
//bool initSet = (_init);
if (!_init) {
bgfx::Init init;
init.type = bgfx::RendererType::Count;
//init.type = bgfx::RendererType::OpenGL;
init.vendorId = BGFX_PCI_ID_NONE;
init.debug = true;
init.resolution.width = ww;
init.resolution.height = wh;
init.resolution.reset = m_resetFlags;
_init = &init;
}
_init->platformData = pd;
bgfx::init(*_init);
bgfx::reset(ww,wh, _init->resolution.reset);
// Enable debug text.
if (_init->debug)
bgfx::setDebug(BGFX_DEBUG_TEXT | BGFX_DEBUG_WIREFRAME);
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
return true;
}
}
#undef LZZ_INLINE
#endif //BGFXH_DOUBLE_GUARD_sdlWindow
#endif //BGFXH_IMPL