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
32 #include <wayland-client.h>
36 struct display *display;
37 struct window *window;
38 struct widget *widget;
42 struct { float *d, *u, *v; } b[2];
45 static void diffuse(struct smoke *smoke, uint32_t time,
46 float *source, float *dest)
52 stride = smoke->width;
54 for (k = 0; k < 5; k++) {
55 for (y = 1; y < smoke->height - 1; y++) {
56 s = source + y * stride;
57 d = dest + y * stride;
58 for (x = 1; x < smoke->width - 1; x++) {
59 t = d[x - 1] + d[x + 1] +
60 d[x - stride] + d[x + stride];
61 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
67 static void advect(struct smoke *smoke, uint32_t time,
68 float *uu, float *vv, float *source, float *dest)
76 stride = smoke->width;
78 for (y = 1; y < smoke->height - 1; y++) {
79 d = dest + y * stride;
83 for (x = 1; x < smoke->width - 1; x++) {
90 if (px > smoke->width - 0.5)
91 px = smoke->width - 0.5;
92 if (py > smoke->height - 0.5)
93 py = smoke->height - 0.5;
98 s = source + j * stride + i;
99 d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
100 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
105 static void project(struct smoke *smoke, uint32_t time,
106 float *u, float *v, float *p, float *div)
111 h = 1.0 / smoke->width;
113 memset(p, 0, smoke->height * smoke->width);
114 for (y = 1; y < smoke->height - 1; y++) {
116 for (x = 1; x < smoke->width - 1; x++) {
117 div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
118 v[l + x + s] - v[l + x - s]);
123 for (k = 0; k < 5; k++) {
124 for (y = 1; y < smoke->height - 1; y++) {
126 for (x = 1; x < smoke->width - 1; x++) {
127 p[l + x] = (div[l + x] +
136 for (y = 1; y < smoke->height - 1; y++) {
138 for (x = 1; x < smoke->width - 1; x++) {
139 u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
140 v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
145 static void render(struct smoke *smoke, cairo_surface_t *surface)
148 int x, y, width, height, stride;
152 dest = cairo_image_surface_get_data(surface);
153 width = cairo_image_surface_get_width(surface);
154 height = cairo_image_surface_get_height(surface);
155 stride = cairo_image_surface_get_stride(surface);
157 for (y = 1; y < height - 1; y++) {
158 s = smoke->b[smoke->current].d + y * smoke->height;
159 d = (uint32_t *) (dest + y * stride);
160 for (x = 1; x < width - 1; x++) {
161 c = (int) (s[x] * 800);
167 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
173 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
175 struct smoke *smoke = data;
177 window_schedule_redraw(smoke->window);
181 wl_callback_destroy(callback);
184 static const struct wl_callback_listener listener = {
189 redraw_handler(struct widget *widget, void *data)
191 struct smoke *smoke = data;
192 uint32_t time = smoke->time;
193 struct wl_callback *callback;
194 cairo_surface_t *surface;
196 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
197 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
198 project(smoke, time / 30,
199 smoke->b[1].u, smoke->b[1].v,
200 smoke->b[0].u, smoke->b[0].v);
201 advect(smoke, time / 30,
202 smoke->b[1].u, smoke->b[1].v,
203 smoke->b[1].u, smoke->b[0].u);
204 advect(smoke, time / 30,
205 smoke->b[1].u, smoke->b[1].v,
206 smoke->b[1].v, smoke->b[0].v);
207 project(smoke, time / 30,
208 smoke->b[0].u, smoke->b[0].v,
209 smoke->b[1].u, smoke->b[1].v);
211 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
212 advect(smoke, time / 30,
213 smoke->b[0].u, smoke->b[0].v,
214 smoke->b[1].d, smoke->b[0].d);
216 surface = window_get_surface(smoke->window);
218 render(smoke, surface);
220 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
222 cairo_surface_destroy(surface);
224 callback = wl_surface_frame(window_get_wl_surface(smoke->window));
225 wl_callback_add_listener(callback, &listener, smoke);
229 smoke_motion_handler(struct widget *widget, struct input *input,
230 uint32_t time, float x, float y, void *data)
232 struct smoke *smoke = data;
233 int i, i0, i1, j, j0, j1, k, d = 5;
239 if (i0 + 2 * d > smoke->width - 1)
240 i1 = smoke->width - 1;
248 if (j0 + 2 * d > smoke->height - 1)
249 j1 = smoke->height - 1;
253 for (i = i0; i < i1; i++)
254 for (j = j0; j < j1; j++) {
255 k = j * smoke->width + i;
256 smoke->b[0].u[k] += 256 - (random() & 512);
257 smoke->b[0].v[k] += 256 - (random() & 512);
258 smoke->b[0].d[k] += 1;
261 return POINTER_HAND1;
265 resize_handler(struct widget *widget,
266 int32_t width, int32_t height, void *data)
268 struct smoke *smoke = data;
271 widget_set_size(smoke->widget, smoke->width, smoke->height);
274 int main(int argc, char *argv[])
281 d = display_create(argc, argv);
283 fprintf(stderr, "failed to create display: %m\n");
290 smoke.window = window_create(d);
291 smoke.widget = window_add_widget(smoke.window, &smoke);
292 window_set_title(smoke.window, "smoke");
294 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
295 clock_gettime(CLOCK_MONOTONIC, &ts);
297 smoke.offset = random();
300 size = smoke.height * smoke.width;
301 smoke.b[0].d = calloc(size, sizeof(float));
302 smoke.b[0].u = calloc(size, sizeof(float));
303 smoke.b[0].v = calloc(size, sizeof(float));
304 smoke.b[1].d = calloc(size, sizeof(float));
305 smoke.b[1].u = calloc(size, sizeof(float));
306 smoke.b[1].v = calloc(size, sizeof(float));
308 widget_set_motion_handler(smoke.widget, smoke_motion_handler);
309 widget_set_resize_handler(smoke.widget, resize_handler);
310 widget_set_redraw_handler(smoke.widget, redraw_handler);
312 window_set_user_data(smoke.window, &smoke);
314 widget_schedule_resize(smoke.widget, smoke.width, smoke.height);