summaryrefslogtreecommitdiffstats
path: root/demos/window_sdl.c
blob: 1fd22cea280c5a2e31f5eed6b1ec81a6e15bf31e (plain)
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
// License: CC0 / Public Domain

#if !defined(USE_GL) && !defined(USE_VK) || defined(USE_GL) && defined(USE_VK)
#error Specify exactly one of -DUSE_GL or -DUSE_VK when compiling!
#endif

#include <SDL.h>

#include "common.h"
#include "window.h"

#ifdef USE_VK
#define VK_NO_PROTOTYPES
#include <libplacebo/vulkan.h>
#include <SDL_vulkan.h>
#define WINFLAG_API SDL_WINDOW_VULKAN
#define IMPL win_impl_sdl_vk
#define IMPL_NAME "SDL2 (vulkan)"
#define IMPL_TAG "sdl2-vk"
#endif

#ifdef USE_GL
#include <libplacebo/opengl.h>
#define WINFLAG_API SDL_WINDOW_OPENGL
#define IMPL win_impl_sdl_gl
#define IMPL_NAME "SDL2 (opengl)"
#define IMPL_TAG "sdl2-gl"
#endif

#ifdef NDEBUG
#define DEBUG false
#else
#define DEBUG true
#endif

const struct window_impl IMPL;

struct priv {
    struct window w;
    SDL_Window *win;

#ifdef USE_VK
    VkSurfaceKHR surf;
    pl_vulkan vk;
    pl_vk_inst vk_inst;
#endif

#ifdef USE_GL
    SDL_GLContext gl_ctx;
    pl_opengl gl;
#endif

    int scroll_dx, scroll_dy;
    char **files;
    size_t files_num;
    size_t files_size;
    bool file_seen;
    char *clip_text;
};

#ifdef USE_GL
static bool make_current(void *priv)
{
    struct priv *p = priv;
    return SDL_GL_MakeCurrent(p->win, p->gl_ctx) == 0;
}

static void release_current(void *priv)
{
    struct priv *p = priv;
    SDL_GL_MakeCurrent(p->win, NULL);
}
#endif

static struct window *sdl_create(pl_log log, const struct window_params *params)
{
    struct priv *p = calloc(1, sizeof(struct priv));
    if (!p)
        return NULL;

    p->w.impl = &IMPL;
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(stderr, "SDL2: Failed initializing: %s\n", SDL_GetError());
        goto error;
    }

    uint32_t sdl_flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | WINFLAG_API;
    p->win = SDL_CreateWindow(params->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                              params->width, params->height, sdl_flags);
    if (!p->win) {
        fprintf(stderr, "SDL2: Failed creating window: %s\n", SDL_GetError());
        goto error;
    }

    int w, h;

#ifdef USE_VK

    unsigned int num = 0;
    if (!SDL_Vulkan_GetInstanceExtensions(p->win, &num, NULL)) {
        fprintf(stderr, "SDL2: Failed enumerating vulkan extensions: %s\n", SDL_GetError());
        goto error;
    }

    const char **exts = malloc(num * sizeof(const char *));
    SDL_Vulkan_GetInstanceExtensions(p->win, &num, exts);

    p->vk_inst = pl_vk_inst_create(log, pl_vk_inst_params(
        .get_proc_addr = SDL_Vulkan_GetVkGetInstanceProcAddr(),
        .debug = DEBUG,
        .extensions = exts,
        .num_extensions = num,
    ));
    free(exts);
    if (!p->vk_inst) {
        fprintf(stderr, "libplacebo: Failed creating vulkan instance!\n");
        goto error;
    }

    if (!SDL_Vulkan_CreateSurface(p->win, p->vk_inst->instance, &p->surf)) {
        fprintf(stderr, "SDL2: Failed creating surface: %s\n", SDL_GetError());
        goto error;
    }

    p->vk = pl_vulkan_create(log, pl_vulkan_params(
        .instance = p->vk_inst->instance,
        .get_proc_addr = p->vk_inst->get_proc_addr,
        .surface = p->surf,
        .allow_software = true,
    ));
    if (!p->vk) {
        fprintf(stderr, "libplacebo: Failed creating vulkan device\n");
        goto error;
    }

    p->w.swapchain = pl_vulkan_create_swapchain(p->vk, pl_vulkan_swapchain_params(
        .surface = p->surf,
        .present_mode = VK_PRESENT_MODE_FIFO_KHR,
    ));

    if (!p->w.swapchain) {
        fprintf(stderr, "libplacebo: Failed creating vulkan swapchain\n");
        goto error;
    }

    p->w.gpu = p->vk->gpu;

    SDL_Vulkan_GetDrawableSize(p->win, &w, &h);
#endif // USE_VK

#ifdef USE_GL
    p->gl_ctx = SDL_GL_CreateContext(p->win);
    if (!p->gl_ctx) {
        fprintf(stderr, "SDL2: Failed creating GL context: %s\n", SDL_GetError());
        goto error;
    }

    p->gl = pl_opengl_create(log, pl_opengl_params(
        .allow_software = true,
        .debug = DEBUG,
        .make_current = make_current,
        .release_current = release_current,
        .get_proc_addr = (void *) SDL_GL_GetProcAddress,
        .priv = p,
    ));
    if (!p->gl) {
        fprintf(stderr, "libplacebo: Failed creating opengl device\n");
        goto error;
    }

    p->w.swapchain = pl_opengl_create_swapchain(p->gl, pl_opengl_swapchain_params(
        .swap_buffers = (void (*)(void *)) SDL_GL_SwapWindow,
        .priv = p->win,
    ));

    if (!p->w.swapchain) {
        fprintf(stderr, "libplacebo: Failed creating opengl swapchain\n");
        goto error;
    }

    p->w.gpu = p->gl->gpu;

    SDL_GL_GetDrawableSize(p->win, &w, &h);
#endif // USE_GL

    pl_swapchain_colorspace_hint(p->w.swapchain, &params->colors);
    if (!pl_swapchain_resize(p->w.swapchain, &w, &h)) {
        fprintf(stderr, "libplacebo: Failed initializing swapchain\n");
        goto error;
    }

    return &p->w;

error:
    window_destroy((struct window **) &p);
    return NULL;
}

static void sdl_destroy(struct window **window)
{
    struct priv *p = (struct priv *) *window;
    if (!p)
        return;

    pl_swapchain_destroy(&p->w.swapchain);

#ifdef USE_VK
    pl_vulkan_destroy(&p->vk);
    if (p->surf) {
        PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR = (PFN_vkDestroySurfaceKHR)
            p->vk_inst->get_proc_addr(p->vk_inst->instance, "vkDestroySurfaceKHR");
        vkDestroySurfaceKHR(p->vk_inst->instance, p->surf, NULL);
    }
    pl_vk_inst_destroy(&p->vk_inst);
#endif

#ifdef USE_GL
    pl_opengl_destroy(&p->gl);
    SDL_GL_DeleteContext(p->gl_ctx);
#endif

    for (int i = 0; i < p->files_num; i++)
        SDL_free(p->files[i]);
    free(p->files);

    SDL_free(p->clip_text);
    SDL_DestroyWindow(p->win);
    SDL_Quit();
    free(p);
    *window = NULL;
}

static inline void handle_event(struct priv *p, SDL_Event *event)
{
    switch (event->type) {
    case SDL_QUIT:
        p->w.window_lost = true;
        return;

    case SDL_WINDOWEVENT:
        if (event->window.windowID != SDL_GetWindowID(p->win))
            return;

        if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
            int width = event->window.data1, height = event->window.data2;
            if (!pl_swapchain_resize(p->w.swapchain, &width, &height)) {
                fprintf(stderr, "libplacebo: Failed resizing swapchain? Exiting...\n");
                p->w.window_lost = true;
            }
        }
        return;

    case SDL_MOUSEWHEEL:
        p->scroll_dx += event->wheel.x;
        p->scroll_dy += event->wheel.y;
        return;

    case SDL_DROPFILE:
        if (p->files_num == p->files_size) {
            size_t new_size = p->files_size ? p->files_size * 2 : 16;
            char **new_files = realloc(p->files, new_size * sizeof(char *));
            if (!new_files)
                return;
            p->files = new_files;
            p->files_size = new_size;
        }

        p->files[p->files_num++] = event->drop.file;
        return;
    }
}

static void sdl_poll(struct window *window, bool block)
{
    struct priv *p = (struct priv *) window;
    SDL_Event event;
    int ret;

    do {
        ret = block ? SDL_WaitEvent(&event) : SDL_PollEvent(&event);
        if (ret)
            handle_event(p, &event);

        // Only block on the first iteration
        block = false;
    } while (ret);
}

static void sdl_get_cursor(const struct window *window, int *x, int *y)
{
    SDL_GetMouseState(x, y);
}

static bool sdl_get_button(const struct window *window, enum button btn)
{
    static const uint32_t button_mask[] = {
        [BTN_LEFT] = SDL_BUTTON_LMASK,
        [BTN_RIGHT] = SDL_BUTTON_RMASK,
        [BTN_MIDDLE] = SDL_BUTTON_MMASK,
    };

    return SDL_GetMouseState(NULL, NULL) & button_mask[btn];
}

static bool sdl_get_key(const struct window *window, enum key key)
{
    static const size_t key_map[] = {
        [KEY_ESC] = SDL_SCANCODE_ESCAPE,
    };

    return SDL_GetKeyboardState(NULL)[key_map[key]];
}

static void sdl_get_scroll(const struct window *window, float *dx, float *dy)
{
    struct priv *p = (struct priv *) window;
    *dx = p->scroll_dx;
    *dy = p->scroll_dy;
    p->scroll_dx = p->scroll_dy = 0;
}

static char *sdl_get_file(const struct window *window)
{
    struct priv *p = (struct priv *) window;
    if (p->file_seen) {
        assert(p->files_num);
        SDL_free(p->files[0]);
        memmove(&p->files[0], &p->files[1], --p->files_num * sizeof(char *));
        p->file_seen = false;
    }

    if (!p->files_num)
        return NULL;

    p->file_seen = true;
    return p->files[0];
}

static bool sdl_is_fullscreen(const struct window *window)
{
    const struct priv *p = (const struct priv *) window;
    return SDL_GetWindowFlags(p->win) & SDL_WINDOW_FULLSCREEN;
}

static bool sdl_toggle_fullscreen(const struct window *window, bool fullscreen)
{
    struct priv *p = (struct priv *) window;
    bool window_fullscreen = sdl_is_fullscreen(window);

    if (window_fullscreen == fullscreen)
        return true;

    SDL_DisplayMode mode;
    if (SDL_GetDesktopDisplayMode(0, &mode))
    {
        fprintf(stderr, "SDL2: Failed to get display mode: %s\n", SDL_GetError());
        SDL_ClearError();
        return false;
    }

    if (SDL_SetWindowDisplayMode(p->win, &mode))
    {
        fprintf(stderr, "SDL2: Failed to set window display mode: %s\n", SDL_GetError());
        SDL_ClearError();
        return false;
    }

    if (SDL_SetWindowFullscreen(p->win, fullscreen ? SDL_WINDOW_FULLSCREEN : 0)) {
        fprintf(stderr, "SDL2: SetWindowFullscreen failed: %s\n", SDL_GetError());
        SDL_ClearError();
        return false;
    }

    return true;
}

static const char *sdl_get_clipboard(const struct window *window)
{
    struct priv *p = (struct priv *) window;
    SDL_free(p->clip_text);
    return p->clip_text = SDL_GetClipboardText();
}

static void sdl_set_clipboard(const struct window *window, const char *text)
{
    SDL_SetClipboardText(text);
}

const struct window_impl IMPL = {
    .name = IMPL_NAME,
    .tag = IMPL_TAG,
    .create = sdl_create,
    .destroy = sdl_destroy,
    .poll = sdl_poll,
    .get_cursor = sdl_get_cursor,
    .get_button = sdl_get_button,
    .get_key = sdl_get_key,
    .get_scroll = sdl_get_scroll,
    .get_file = sdl_get_file,
    .toggle_fullscreen = sdl_toggle_fullscreen,
    .is_fullscreen = sdl_is_fullscreen,
    .get_clipboard = sdl_get_clipboard,
    .set_clipboard = sdl_set_clipboard,
};