-
Notifications
You must be signed in to change notification settings - Fork 22
/
luigi_sdl2.cpp
285 lines (247 loc) · 10.5 KB
/
luigi_sdl2.cpp
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
// Compile with: clang++ luigi_sdl2.cpp -lsdl2 -lopengl32 -o luigi_sdl.exe -O2 && luigi_sdl2.exe
#ifdef _WIN32
#include <windows.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <GL/gl.h>
#include "SDL2/SDL.h"
// TODO Menu support.
#define UI_ASSERT assert
#define UI_CALLOC(x) calloc(1, (x))
#define UI_FREE free
#define UI_MALLOC malloc
#define UI_REALLOC realloc
#define UI_CLOCK SDL_GetTicks
#define UI_CLOCKS_PER_SECOND (1000)
#define UI_CLOCK_T uint32_t
#define UI_IMPLEMENTATION
#include "luigi.h"
const int UI_KEYCODE_A = SDL_SCANCODE_A;
const int UI_KEYCODE_BACKSPACE = SDL_SCANCODE_BACKSPACE;
const int UI_KEYCODE_DELETE = SDL_SCANCODE_DELETE;
const int UI_KEYCODE_DOWN = SDL_SCANCODE_DOWN;
const int UI_KEYCODE_END = SDL_SCANCODE_END;
const int UI_KEYCODE_ENTER = SDL_SCANCODE_RETURN;
const int UI_KEYCODE_ESCAPE = SDL_SCANCODE_ESCAPE;
const int UI_KEYCODE_F1 = SDL_SCANCODE_F1;
const int UI_KEYCODE_F10 = SDL_SCANCODE_F10;
const int UI_KEYCODE_F11 = SDL_SCANCODE_F11;
const int UI_KEYCODE_F12 = SDL_SCANCODE_F12;
const int UI_KEYCODE_F2 = SDL_SCANCODE_F2;
const int UI_KEYCODE_F3 = SDL_SCANCODE_F3;
const int UI_KEYCODE_F4 = SDL_SCANCODE_F4;
const int UI_KEYCODE_F5 = SDL_SCANCODE_F5;
const int UI_KEYCODE_F6 = SDL_SCANCODE_F6;
const int UI_KEYCODE_F7 = SDL_SCANCODE_F7;
const int UI_KEYCODE_F8 = SDL_SCANCODE_F8;
const int UI_KEYCODE_F9 = SDL_SCANCODE_F9;
const int UI_KEYCODE_HOME = SDL_SCANCODE_HOME;
const int UI_KEYCODE_LEFT = SDL_SCANCODE_LEFT;
const int UI_KEYCODE_RIGHT = SDL_SCANCODE_RIGHT;
const int UI_KEYCODE_SPACE = SDL_SCANCODE_SPACE;
const int UI_KEYCODE_TAB = SDL_SCANCODE_TAB;
const int UI_KEYCODE_UP = SDL_SCANCODE_UP;
const int UI_KEYCODE_INSERT = SDL_SCANCODE_INSERT;
SDL_Cursor *cursors[UI_CURSOR_COUNT];
SDL_Window *sdlWindow;
UIWindow *window;
UIColorPicker *colorPicker;
UISlider *sliderX1;
UISlider *sliderX2;
UISlider *sliderX3;
UISlider *sliderY1;
UISlider *sliderY2;
UISlider *sliderY3;
UIWindow *UIWindowCreate(UIWindow *owner, uint32_t flags, const char *cTitle, int width, int height) {
return NULL;
}
void _UIWindowGetScreenPosition(UIWindow *window, int *x, int *y) {
*x = *y = 0;
}
void _UIWindowEndPaint(UIWindow *window, UIPainter *painter) {
}
void _UIWindowSetCursor(UIWindow *window, int cursor) {
SDL_SetCursor(cursors[cursor]);
}
int _UIWindowMessage(UIElement *element, UIMessage message, int di, void *dp) {
if (message == UI_MSG_LAYOUT && element->children) {
UIElementMove(element->children, element->bounds, false);
UIElementRepaint(element, NULL);
} else if (message == UI_MSG_DESTROY) {
UIWindow *window = (UIWindow *) element;
_UIWindowDestroyCommon(window);
exit(0);
}
return 0;
}
void _UIWindowUpdateSize(UIWindow *window, SDL_Window *sdlWindow) {
SDL_GetWindowSize(sdlWindow, &window->width, &window->height);
glViewport(0, 0, window->width, window->height);
window->bits = (uint32_t *) UI_REALLOC(window->bits, window->width * window->height * 4);
window->e.bounds = UI_RECT_2S(window->width, window->height);
window->e.clip = UI_RECT_2S(window->width, window->height);
UIElementMessage(&window->e, UI_MSG_LAYOUT, 0, 0);
_UIUpdate();
}
void UISendSDLEvent(UIWindow *window, SDL_Event *event, SDL_Window *sdlWindow) {
if (event->type == SDL_WINDOWEVENT && event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
_UIWindowUpdateSize(window, sdlWindow);
} else if (event->type == SDL_MOUSEMOTION) {
window->cursorX = event->motion.x;
window->cursorY = event->motion.y;
_UIWindowInputEvent(window, UI_MSG_MOUSE_MOVE, 0, 0);
} else if (event->type == SDL_MOUSEWHEEL) {
_UIWindowInputEvent(window, UI_MSG_MOUSE_WHEEL, -50 * event->wheel.y, 0);
} else if (event->type == SDL_MOUSEBUTTONDOWN) {
_UIWindowInputEvent(window, event->button.button == SDL_BUTTON_LEFT ? UI_MSG_LEFT_DOWN
: event->button.button == SDL_BUTTON_MIDDLE ? UI_MSG_MIDDLE_DOWN
: UI_MSG_RIGHT_DOWN, 0, 0);
} else if (event->type == SDL_MOUSEBUTTONUP) {
_UIWindowInputEvent(window, event->button.button == SDL_BUTTON_LEFT ? UI_MSG_LEFT_UP
: event->button.button == SDL_BUTTON_MIDDLE ? UI_MSG_MIDDLE_UP
: UI_MSG_RIGHT_UP, 0, 0);
} else if (event->type == SDL_KEYDOWN) {
const uint8_t *state = SDL_GetKeyboardState(NULL);
window->ctrl = state[SDL_SCANCODE_LCTRL] || state[SDL_SCANCODE_RCTRL];
window->shift = state[SDL_SCANCODE_LSHIFT] || state[SDL_SCANCODE_RSHIFT];
window->alt = state[SDL_SCANCODE_LALT] || state[SDL_SCANCODE_RALT];
UIKeyTyped m = { 0 };
m.code = event->key.keysym.scancode;
_UIWindowInputEvent(window, UI_MSG_KEY_TYPED, 0, &m);
} else if (event->type == SDL_TEXTINPUT) {
UIKeyTyped m = { 0 };
m.text = event->text.text;
m.textBytes = strlen(m.text);
_UIWindowInputEvent(window, UI_MSG_KEY_TYPED, 0, &m);
}
}
void _UIClipboardWriteText(UIWindow *window, char *text) {
// TODO.
}
char *_UIClipboardReadTextStart(UIWindow *window, size_t *bytes) {
// TODO.
*bytes = 0;
return NULL;
}
void _UIClipboardReadTextEnd(UIWindow *window, char *text) {
// TODO.
}
UIWindow *UIInitialiseSDL() {
ui.theme = _uiThemeDark;
cursors[UI_CURSOR_ARROW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
cursors[UI_CURSOR_TEXT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
cursors[UI_CURSOR_SPLIT_V] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
cursors[UI_CURSOR_SPLIT_H] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
cursors[UI_CURSOR_FLIPPED_ARROW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
cursors[UI_CURSOR_CROSS_HAIR] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR);
cursors[UI_CURSOR_HAND] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
cursors[UI_CURSOR_RESIZE_UP] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
cursors[UI_CURSOR_RESIZE_LEFT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
cursors[UI_CURSOR_RESIZE_UP_RIGHT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
cursors[UI_CURSOR_RESIZE_UP_LEFT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
cursors[UI_CURSOR_RESIZE_DOWN] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
cursors[UI_CURSOR_RESIZE_RIGHT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
cursors[UI_CURSOR_RESIZE_DOWN_LEFT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
cursors[UI_CURSOR_RESIZE_DOWN_RIGHT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
UIWindow *window = (UIWindow *) UIElementCreate(sizeof(UIWindow), NULL, UI_ELEMENT_WINDOW, _UIWindowMessage, "Window");
window->scale = 1.0f;
window->e.window = window;
window->hovered = &window->e;
window->next = ui.windows;
ui.windows = window;
return window;
}
bool _UIMessageLoopSingle(int *result) {
*result = 0;
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return false;
}
UISendSDLEvent(window, &event, sdlWindow);
}
if (ui.animating) {
_UIProcessAnimations();
}
glClear(GL_COLOR_BUFFER_BIT);
uint32_t selectedColor;
UIColorToRGB(colorPicker->hue, colorPicker->saturation, colorPicker->value, &selectedColor);
glDisable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLES);
glColor4f(UI_COLOR_RED_F(selectedColor), UI_COLOR_GREEN_F(selectedColor), UI_COLOR_BLUE_F(selectedColor), 1);
glVertex2f(sliderX1->position * 2 - 1, sliderY1->position * 2 - 1);
glVertex2f(sliderX2->position * 2 - 1, sliderY2->position * 2 - 1);
glVertex2f(sliderX3->position * 2 - 1, sliderY3->position * 2 - 1);
glEnd();
GLuint texture;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, window->width, window->height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, window->bits);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_TRIANGLES);
glColor4f(1, 1, 1, 1);
glTexCoord2f(0, 1); glVertex2f(-1, -1);
glTexCoord2f(1, 1); glVertex2f(1, -1);
glTexCoord2f(0, 0); glVertex2f(-1, 1);
glTexCoord2f(1, 1); glVertex2f(1, -1);
glTexCoord2f(0, 0); glVertex2f(-1, 1);
glTexCoord2f(1, 0); glVertex2f(1, 1);
glEnd();
glDeleteTextures(1, &texture);
SDL_GL_SwapWindow(sdlWindow);
return true;
}
#ifdef _WIN32
int WinMain(HINSTANCE instance, HINSTANCE previousInstance, char *commandLine, int showCommand) {
#else
int main(int argc, char **argv) {
#endif
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
sdlWindow = SDL_CreateWindow("SDL Example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_GLContext glContext = SDL_GL_CreateContext(sdlWindow);
SDL_GL_MakeCurrent(sdlWindow, glContext);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 1);
window = UIInitialiseSDL();
UIMDIClient *client = UIMDIClientCreate(&window->e, UI_MDI_CLIENT_TRANSPARENT);
UIMDIChild *child = UIMDIChildCreate(&client->e, 0, UI_RECT_4(10, 300, 10, 300), "Test Window", -1);
UIPanel *panel = UIPanelCreate(&child->e, UI_PANEL_GRAY | UI_PANEL_EXPAND | UI_PANEL_MEDIUM_SPACING | UI_PANEL_SCROLL | UI_ELEMENT_PARENT_PUSH);
UILabelCreate(0, 0, "Vertex 1", -1);
UIPanelCreate(0, UI_PANEL_HORIZONTAL | UI_PANEL_SMALL_SPACING | UI_ELEMENT_PARENT_PUSH);
sliderX1 = UISliderCreate(0, UI_ELEMENT_H_FILL);
sliderX1->position = 0.5f;
sliderY1 = UISliderCreate(0, UI_ELEMENT_H_FILL);
sliderY1->position = 1.0f;
UIParentPop();
UILabelCreate(0, 0, "Vertex 2", -1);
UIPanelCreate(0, UI_PANEL_HORIZONTAL | UI_PANEL_SMALL_SPACING | UI_ELEMENT_PARENT_PUSH);
sliderX2 = UISliderCreate(0, UI_ELEMENT_H_FILL);
sliderY2 = UISliderCreate(0, UI_ELEMENT_H_FILL);
UIParentPop();
UILabelCreate(0, 0, "Vertex 3", -1);
UIPanelCreate(0, UI_PANEL_HORIZONTAL | UI_PANEL_SMALL_SPACING | UI_ELEMENT_PARENT_PUSH);
sliderX3 = UISliderCreate(0, UI_ELEMENT_H_FILL);
sliderX3->position = 1.0f;
sliderY3 = UISliderCreate(0, UI_ELEMENT_H_FILL);
UIParentPop();
UILabelCreate(0, 0, "Color", -1);
colorPicker = UIColorPickerCreate(&UIPanelCreate(0, 0)->e, 0);
colorPicker->value = 1;
UIParentPop();
_UIWindowUpdateSize(window, sdlWindow);
int result;
while (_UIMessageLoopSingle(&result));
return result;
}