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 struct widget *widget;
45 struct { float *d, *u, *v; } b[2];
48 static void diffuse(struct smoke *smoke, uint32_t time,
49 float *source, float *dest)
55 stride = smoke->width;
57 for (k = 0; k < 5; k++) {
58 for (y = 1; y < smoke->height - 1; y++) {
59 s = source + y * stride;
60 d = dest + y * stride;
61 for (x = 1; x < smoke->width - 1; x++) {
62 t = d[x - 1] + d[x + 1] +
63 d[x - stride] + d[x + stride];
64 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
70 static void advect(struct smoke *smoke, uint32_t time,
71 float *uu, float *vv, float *source, float *dest)
79 stride = smoke->width;
81 for (y = 1; y < smoke->height - 1; y++) {
82 d = dest + y * stride;
86 for (x = 1; x < smoke->width - 1; x++) {
93 if (px > smoke->width - 0.5)
94 px = smoke->width - 0.5;
95 if (py > smoke->height - 0.5)
96 py = smoke->height - 0.5;
101 s = source + j * stride + i;
102 d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
103 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
108 static void project(struct smoke *smoke, uint32_t time,
109 float *u, float *v, float *p, float *div)
114 h = 1.0 / smoke->width;
116 memset(p, 0, smoke->height * smoke->width);
117 for (y = 1; y < smoke->height - 1; y++) {
119 for (x = 1; x < smoke->width - 1; x++) {
120 div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
121 v[l + x + s] - v[l + x - s]);
126 for (k = 0; k < 5; k++) {
127 for (y = 1; y < smoke->height - 1; y++) {
129 for (x = 1; x < smoke->width - 1; x++) {
130 p[l + x] = (div[l + x] +
139 for (y = 1; y < smoke->height - 1; y++) {
141 for (x = 1; x < smoke->width - 1; x++) {
142 u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
143 v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
148 static void render(struct smoke *smoke, cairo_surface_t *surface)
151 int x, y, width, height, stride;
155 dest = cairo_image_surface_get_data(surface);
156 width = cairo_image_surface_get_width(surface);
157 height = cairo_image_surface_get_height(surface);
158 stride = cairo_image_surface_get_stride(surface);
160 for (y = 1; y < height - 1; y++) {
161 s = smoke->b[smoke->current].d + y * smoke->height;
162 d = (uint32_t *) (dest + y * stride);
163 for (x = 1; x < width - 1; x++) {
164 c = (int) (s[x] * 800);
170 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
176 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
178 struct smoke *smoke = data;
180 window_schedule_redraw(smoke->window);
184 wl_callback_destroy(callback);
187 static const struct wl_callback_listener listener = {
192 redraw_handler(struct widget *widget, void *data)
194 struct smoke *smoke = data;
195 uint32_t time = smoke->time;
196 struct wl_callback *callback;
197 cairo_surface_t *surface;
199 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
200 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
201 project(smoke, time / 30,
202 smoke->b[1].u, smoke->b[1].v,
203 smoke->b[0].u, smoke->b[0].v);
204 advect(smoke, time / 30,
205 smoke->b[1].u, smoke->b[1].v,
206 smoke->b[1].u, smoke->b[0].u);
207 advect(smoke, time / 30,
208 smoke->b[1].u, smoke->b[1].v,
209 smoke->b[1].v, smoke->b[0].v);
210 project(smoke, time / 30,
211 smoke->b[0].u, smoke->b[0].v,
212 smoke->b[1].u, smoke->b[1].v);
214 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
215 advect(smoke, time / 30,
216 smoke->b[0].u, smoke->b[0].v,
217 smoke->b[1].d, smoke->b[0].d);
219 surface = window_get_surface(smoke->window);
221 render(smoke, surface);
223 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
225 cairo_surface_destroy(surface);
227 callback = wl_surface_frame(window_get_wl_surface(smoke->window));
228 wl_callback_add_listener(callback, &listener, smoke);
232 smoke_motion_handler(struct widget *widget, struct input *input,
233 uint32_t time, GLfloat x, GLfloat y, void *data)
235 struct smoke *smoke = data;
236 int i, i0, i1, j, j0, j1, k, d = 5;
242 if (i0 + 2 * d > smoke->width - 1)
243 i1 = smoke->width - 1;
251 if (j0 + 2 * d > smoke->height - 1)
252 j1 = smoke->height - 1;
256 for (i = i0; i < i1; i++)
257 for (j = j0; j < j1; j++) {
258 k = j * smoke->width + i;
259 smoke->b[0].u[k] += 256 - (random() & 512);
260 smoke->b[0].v[k] += 256 - (random() & 512);
261 smoke->b[0].d[k] += 1;
264 return POINTER_HAND1;
268 resize_handler(struct widget *widget,
269 int32_t width, int32_t height, void *data)
271 struct smoke *smoke = data;
274 widget_set_size(smoke->widget, smoke->width, smoke->height);
277 int main(int argc, char *argv[])
284 d = display_create(argc, argv);
286 fprintf(stderr, "failed to create display: %m\n");
293 smoke.window = window_create(d);
294 smoke.widget = window_add_widget(smoke.window, &smoke);
295 window_set_title(smoke.window, "smoke");
297 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
298 clock_gettime(CLOCK_MONOTONIC, &ts);
300 smoke.offset = random();
303 size = smoke.height * smoke.width;
304 smoke.b[0].d = calloc(size, sizeof(float));
305 smoke.b[0].u = calloc(size, sizeof(float));
306 smoke.b[0].v = calloc(size, sizeof(float));
307 smoke.b[1].d = calloc(size, sizeof(float));
308 smoke.b[1].u = calloc(size, sizeof(float));
309 smoke.b[1].v = calloc(size, sizeof(float));
311 widget_set_motion_handler(smoke.widget, smoke_motion_handler);
312 widget_set_resize_handler(smoke.widget, resize_handler);
313 widget_set_redraw_handler(smoke.widget, redraw_handler);
315 window_set_user_data(smoke.window, &smoke);
317 widget_schedule_resize(smoke.widget, smoke.width, smoke.height);