2 * Copyright © 2010 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
35 #include <wayland-client.h>
39 struct display *display;
40 struct window *window;
41 cairo_surface_t *surface;
42 int x, y, width, height;
44 struct { float *d, *u, *v; } b[2];
47 static void diffuse(struct smoke *smoke, uint32_t time,
48 float *source, float *dest)
54 stride = smoke->width;
56 for (k = 0; k < 5; k++) {
57 for (y = 1; y < smoke->height - 1; y++) {
58 s = source + y * stride;
59 d = dest + y * stride;
60 for (x = 1; x < smoke->width - 1; x++) {
61 t = d[x - 1] + d[x + 1] +
62 d[x - stride] + d[x + stride];
63 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
69 static void advect(struct smoke *smoke, uint32_t time,
70 float *uu, float *vv, float *source, float *dest)
78 stride = smoke->width;
80 for (y = 1; y < smoke->height - 1; y++) {
81 d = dest + y * stride;
85 for (x = 1; x < smoke->width - 1; x++) {
92 if (px > smoke->width - 0.5)
93 px = smoke->width - 0.5;
94 if (py > smoke->height - 0.5)
95 py = smoke->height - 0.5;
100 s = source + j * stride + i;
101 d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
102 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
107 static void project(struct smoke *smoke, uint32_t time,
108 float *u, float *v, float *p, float *div)
113 h = 1.0 / smoke->width;
115 memset(p, 0, smoke->height * smoke->width);
116 for (y = 1; y < smoke->height - 1; y++) {
118 for (x = 1; x < smoke->width - 1; x++) {
119 div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
120 v[l + x + s] - v[l + x - s]);
125 for (k = 0; k < 5; k++) {
126 for (y = 1; y < smoke->height - 1; y++) {
128 for (x = 1; x < smoke->width - 1; x++) {
129 p[l + x] = (div[l + x] +
138 for (y = 1; y < smoke->height - 1; y++) {
140 for (x = 1; x < smoke->width - 1; x++) {
141 u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
142 v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
147 static void render(struct smoke *smoke)
150 int x, y, width, height, stride;
154 dest = cairo_image_surface_get_data (smoke->surface);
155 width = cairo_image_surface_get_width (smoke->surface);
156 height = cairo_image_surface_get_height (smoke->surface);
157 stride = cairo_image_surface_get_stride (smoke->surface);
159 for (y = 1; y < height - 1; y++) {
160 s = smoke->b[smoke->current].d + y * smoke->height;
161 d = (uint32_t *) (dest + y * stride);
162 for (x = 1; x < width - 1; x++) {
163 c = (int) (s[x] * 800);
169 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
175 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
177 static const struct wl_callback_listener listener = {
180 struct smoke *smoke = data;
182 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
183 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
184 project(smoke, time / 30,
185 smoke->b[1].u, smoke->b[1].v,
186 smoke->b[0].u, smoke->b[0].v);
187 advect(smoke, time / 30,
188 smoke->b[1].u, smoke->b[1].v,
189 smoke->b[1].u, smoke->b[0].u);
190 advect(smoke, time / 30,
191 smoke->b[1].u, smoke->b[1].v,
192 smoke->b[1].v, smoke->b[0].v);
193 project(smoke, time / 30,
194 smoke->b[0].u, smoke->b[0].v,
195 smoke->b[1].u, smoke->b[1].v);
197 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
198 advect(smoke, time / 30,
199 smoke->b[0].u, smoke->b[0].v,
200 smoke->b[1].d, smoke->b[0].d);
205 wl_callback_destroy(callback);
207 display_surface_damage(smoke->display, smoke->surface,
208 0, 0, smoke->width, smoke->height);
209 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
211 callback = wl_surface_frame(window_get_wl_surface(smoke->window));
212 wl_callback_add_listener(callback, &listener, smoke);
216 smoke_motion_handler(struct widget *widget, struct input *input,
217 uint32_t time, int32_t x, int32_t y, void *data)
219 struct smoke *smoke = data;
220 int i, i0, i1, j, j0, j1, k, d = 5;
226 if (i0 + 2 * d > smoke->width - 1)
227 i1 = smoke->width - 1;
235 if (j0 + 2 * d > smoke->height - 1)
236 j1 = smoke->height - 1;
240 for (i = i0; i < i1; i++)
241 for (j = j0; j < j1; j++) {
242 k = j * smoke->width + i;
243 smoke->b[0].u[k] += 256 - (random() & 512);
244 smoke->b[0].v[k] += 256 - (random() & 512);
245 smoke->b[0].d[k] += 1;
248 return POINTER_HAND1;
251 int main(int argc, char *argv[])
258 d = display_create(&argc, &argv, NULL);
260 fprintf(stderr, "failed to create display: %m\n");
269 smoke.window = window_create(d, smoke.width, smoke.height);
270 window_set_title(smoke.window, "smoke");
272 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
273 clock_gettime(CLOCK_MONOTONIC, &ts);
275 smoke.offset = random();
278 size = smoke.height * smoke.width;
279 smoke.b[0].d = calloc(size, sizeof(float));
280 smoke.b[0].u = calloc(size, sizeof(float));
281 smoke.b[0].v = calloc(size, sizeof(float));
282 smoke.b[1].d = calloc(size, sizeof(float));
283 smoke.b[1].u = calloc(size, sizeof(float));
284 smoke.b[1].v = calloc(size, sizeof(float));
286 window_set_decoration(smoke.window, 0);
287 window_create_surface(smoke.window);
288 smoke.surface = window_get_surface(smoke.window);
290 window_flush(smoke.window);
292 widget_set_motion_handler(window_get_widget(smoke.window),
293 smoke_motion_handler);
295 window_set_user_data(smoke.window, &smoke);
296 frame_callback(&smoke, NULL, 0);