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
31 #include <wayland-client.h>
35 struct display *display;
36 struct window *window;
37 struct widget *widget;
41 struct { float *d, *u, *v; } b[2];
44 static void diffuse(struct smoke *smoke, uint32_t time,
45 float *source, float *dest)
51 stride = smoke->width;
53 for (k = 0; k < 5; k++) {
54 for (y = 1; y < smoke->height - 1; y++) {
55 s = source + y * stride;
56 d = dest + y * stride;
57 for (x = 1; x < smoke->width - 1; x++) {
58 t = d[x - 1] + d[x + 1] +
59 d[x - stride] + d[x + stride];
60 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
66 static void advect(struct smoke *smoke, uint32_t time,
67 float *uu, float *vv, float *source, float *dest)
75 stride = smoke->width;
77 for (y = 1; y < smoke->height - 1; y++) {
78 d = dest + y * stride;
82 for (x = 1; x < smoke->width - 1; x++) {
89 if (px > smoke->width - 0.5)
90 px = smoke->width - 0.5;
91 if (py > smoke->height - 0.5)
92 py = smoke->height - 0.5;
97 s = source + j * stride + i;
98 d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
99 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
104 static void project(struct smoke *smoke, uint32_t time,
105 float *u, float *v, float *p, float *div)
110 h = 1.0 / smoke->width;
112 memset(p, 0, smoke->height * smoke->width);
113 for (y = 1; y < smoke->height - 1; y++) {
115 for (x = 1; x < smoke->width - 1; x++) {
116 div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
117 v[l + x + s] - v[l + x - s]);
122 for (k = 0; k < 5; k++) {
123 for (y = 1; y < smoke->height - 1; y++) {
125 for (x = 1; x < smoke->width - 1; x++) {
126 p[l + x] = (div[l + x] +
135 for (y = 1; y < smoke->height - 1; y++) {
137 for (x = 1; x < smoke->width - 1; x++) {
138 u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
139 v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
144 static void render(struct smoke *smoke, cairo_surface_t *surface)
147 int x, y, width, height, stride;
151 dest = cairo_image_surface_get_data(surface);
152 width = cairo_image_surface_get_width(surface);
153 height = cairo_image_surface_get_height(surface);
154 stride = cairo_image_surface_get_stride(surface);
156 for (y = 1; y < height - 1; y++) {
157 s = smoke->b[smoke->current].d + y * smoke->height;
158 d = (uint32_t *) (dest + y * stride);
159 for (x = 1; x < width - 1; x++) {
160 c = (int) (s[x] * 800);
166 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
172 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
174 struct smoke *smoke = data;
176 window_schedule_redraw(smoke->window);
180 wl_callback_destroy(callback);
183 static const struct wl_callback_listener listener = {
188 redraw_handler(struct widget *widget, void *data)
190 struct smoke *smoke = data;
191 uint32_t time = smoke->time;
192 struct wl_callback *callback;
193 cairo_surface_t *surface;
195 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
196 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
197 project(smoke, time / 30,
198 smoke->b[1].u, smoke->b[1].v,
199 smoke->b[0].u, smoke->b[0].v);
200 advect(smoke, time / 30,
201 smoke->b[1].u, smoke->b[1].v,
202 smoke->b[1].u, smoke->b[0].u);
203 advect(smoke, time / 30,
204 smoke->b[1].u, smoke->b[1].v,
205 smoke->b[1].v, smoke->b[0].v);
206 project(smoke, time / 30,
207 smoke->b[0].u, smoke->b[0].v,
208 smoke->b[1].u, smoke->b[1].v);
210 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
211 advect(smoke, time / 30,
212 smoke->b[0].u, smoke->b[0].v,
213 smoke->b[1].d, smoke->b[0].d);
215 surface = window_get_surface(smoke->window);
217 render(smoke, surface);
219 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
221 cairo_surface_destroy(surface);
223 callback = wl_surface_frame(window_get_wl_surface(smoke->window));
224 wl_callback_add_listener(callback, &listener, smoke);
225 wl_surface_commit(window_get_wl_surface(smoke->window));
229 smoke_motion_handler(struct smoke *smoke, float x, float y)
231 int i, i0, i1, j, j0, j1, k, d = 5;
237 if (i0 + 2 * d > smoke->width - 1)
238 i1 = smoke->width - 1;
246 if (j0 + 2 * d > smoke->height - 1)
247 j1 = smoke->height - 1;
251 for (i = i0; i < i1; i++)
252 for (j = j0; j < j1; j++) {
253 k = j * smoke->width + i;
254 smoke->b[0].u[k] += 256 - (random() & 512);
255 smoke->b[0].v[k] += 256 - (random() & 512);
256 smoke->b[0].d[k] += 1;
261 mouse_motion_handler(struct widget *widget, struct input *input,
262 uint32_t time, float x, float y, void *data)
264 smoke_motion_handler(data, x, y);
270 touch_motion_handler(struct widget *widget, struct input *input,
271 uint32_t time, int32_t id, float x, float y, void *data)
273 smoke_motion_handler(data, x, y);
277 resize_handler(struct widget *widget,
278 int32_t width, int32_t height, void *data)
280 struct smoke *smoke = data;
283 widget_set_size(smoke->widget, smoke->width, smoke->height);
286 int main(int argc, char *argv[])
293 d = display_create(&argc, argv);
295 fprintf(stderr, "failed to create display: %m\n");
302 smoke.window = window_create(d);
303 smoke.widget = window_add_widget(smoke.window, &smoke);
304 window_set_title(smoke.window, "smoke");
306 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
307 clock_gettime(CLOCK_MONOTONIC, &ts);
311 size = smoke.height * smoke.width;
312 smoke.b[0].d = calloc(size, sizeof(float));
313 smoke.b[0].u = calloc(size, sizeof(float));
314 smoke.b[0].v = calloc(size, sizeof(float));
315 smoke.b[1].d = calloc(size, sizeof(float));
316 smoke.b[1].u = calloc(size, sizeof(float));
317 smoke.b[1].v = calloc(size, sizeof(float));
319 widget_set_motion_handler(smoke.widget, mouse_motion_handler);
320 widget_set_touch_motion_handler(smoke.widget, touch_motion_handler);
321 widget_set_resize_handler(smoke.widget, resize_handler);
322 widget_set_redraw_handler(smoke.widget, redraw_handler);
324 window_set_user_data(smoke.window, &smoke);
326 widget_schedule_resize(smoke.widget, smoke.width, smoke.height);