-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsdl2windowbackend.cpp
More file actions
146 lines (122 loc) · 3.44 KB
/
sdl2windowbackend.cpp
File metadata and controls
146 lines (122 loc) · 3.44 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
#include <fea/ui/sdl2windowbackend.hpp>
namespace fea
{
SDL2WindowBackend::SDL2WindowBackend() : mWindow(nullptr)
{
SDL_Init(SDL_INIT_VIDEO);
}
void SDL2WindowBackend::open(VideoMode mode, const std::string& title, uint32_t style, const ContextSettings& settings)
{
//todo: fix style
(void) style;
(void) settings;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings.mMajorVersion);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings.mMinorVersion);
mWindow = SDL_CreateWindow(title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
mode.mWidth, mode.mHeight,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
mGlContext = SDL_GL_CreateContext(mWindow);
}
void SDL2WindowBackend::close()
{
SDL_GL_DeleteContext(mGlContext);
SDL_DestroyWindow(mWindow);
SDL_Quit();
mWindow = nullptr;
}
bool SDL2WindowBackend::isOpen() const
{
return mWindow != nullptr;
}
const ContextSettings SDL2WindowBackend::getSettings() const
{
//settings not finalized;
return ContextSettings();
}
Vec2I SDL2WindowBackend::getPosition() const
{
Vec2I s;
s.x = 0;
s.y = 0;
if(isOpen())
SDL_GetWindowPosition(mWindow, &s.x, &s.y);
return s;
}
void SDL2WindowBackend::setPosition(int32_t x, int32_t y)
{
if(isOpen())
{
SDL_SetWindowPosition(mWindow, x, y);
}
}
Vec2I SDL2WindowBackend::getSize() const
{
Vec2I s;
s.x = 0;
s.y = 0;
if(isOpen())
SDL_GetWindowSize(mWindow, &s.x, &s.y);
return s;
}
void SDL2WindowBackend::setSize(int32_t w, int32_t h)
{
SDL_SetWindowSize(mWindow, w, h);
}
void SDL2WindowBackend::setTitle(const std::string& title)
{
SDL_SetWindowTitle(mWindow, title.c_str());
}
void SDL2WindowBackend::setIcon(uint32_t width, uint32_t height, const uint8_t* pixels)
{
SDL_Surface* f = SDL_CreateRGBSurfaceFrom((void*)pixels, width, height, 32, width*4, 0x00ff0000,0x0000ff00,0x000000ff,0xff000000);
if(f == NULL){
printf("Window icon could not be loaded: %s\n", SDL_GetError());
//return;
}
SDL_SetWindowIcon(mWindow, f);
}
void SDL2WindowBackend::setVisible(bool visible)
{
//should be fixed
(void) visible;
}
void SDL2WindowBackend::setVSyncEnabled(bool enabled)
{
SDL_GL_SetSwapInterval(enabled?1:0);
}
void SDL2WindowBackend::setMouseCursorVisible(bool visible)
{
//should be fixed
(void)visible;
}
void SDL2WindowBackend::setFramerateLimit(uint32_t limit)
{
//should be fixed
(void)limit;
}
bool SDL2WindowBackend::setRenderingActive(bool active) const
{
//should be fixed
(void)active;
return true;
}
void SDL2WindowBackend::swapBuffers()
{
SDL_GL_SwapWindow(mWindow);
}
void SDL2WindowBackend::lockCursor(bool lock)
{
if(isOpen())
{
SDL_SetRelativeMouseMode((SDL_bool)lock);
SDL_SetWindowGrab(mWindow, (SDL_bool)lock);
}
}
SDL2WindowBackend::~SDL2WindowBackend()
{
close();
//free(mWindow);
}
}