summaryrefslogtreecommitdiffstats
path: root/demos/colors.c
blob: 41712e1071ef81ddafd9a6ad79bbdbd8e31bd310 (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
/* Simplistic demo that just makes the window colorful, including alpha
 * transparency if supported by the windowing system.
 *
 * License: CC0 / Public Domain
 */

#include <assert.h>
#include <errno.h>
#include <math.h>
#include <string.h>

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

static pl_log logger;
static struct window *win;

static void uninit(int ret)
{
    window_destroy(&win);
    pl_log_destroy(&logger);
    exit(ret);
}

int main(int argc, char **argv)
{
    logger = pl_log_create(PL_API_VER, pl_log_params(
        .log_cb = pl_log_color,
        .log_level = PL_LOG_DEBUG,
    ));

    win = window_create(logger, &(struct window_params) {
        .title = "colors demo",
        .width = 640,
        .height = 480,
        .alpha = true,
    });
    if (!win)
        uninit(1);

    pl_clock_t ts_start, ts;
    if ((ts_start = pl_clock_now()) == 0) {
        uninit(1);
    }

    while (!win->window_lost) {
        if (window_get_key(win, KEY_ESC))
            break;

        struct pl_swapchain_frame frame;
        bool ok = pl_swapchain_start_frame(win->swapchain, &frame);
        if (!ok) {
            // Something unexpected happened, perhaps the window is not
            // visible? Wait for events and try again.
            window_poll(win, true);
            continue;
        }

        if ((ts = pl_clock_now()) == 0)
            uninit(1);

        const double period = 10.; // in seconds
        double secs = fmod(pl_clock_diff(ts, ts_start), period);

        double pos = 2 * M_PI * secs / period;
        float alpha = (cos(pos) + 1.0) / 2.0;

        assert(frame.fbo->params.blit_dst);
        pl_tex_clear(win->gpu, frame.fbo, (float[4]) {
            alpha * (sinf(2 * pos + 0.0) + 1.0) / 2.0,
            alpha * (sinf(2 * pos + 2.0) + 1.0) / 2.0,
            alpha * (sinf(2 * pos + 4.0) + 1.0) / 2.0,
            alpha,
        });

        ok = pl_swapchain_submit_frame(win->swapchain);
        if (!ok) {
            fprintf(stderr, "libplacebo: failed submitting frame!\n");
            uninit(3);
        }

        pl_swapchain_swap_buffers(win->swapchain);
        window_poll(win, false);
    }

    uninit(0);
}