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"
36 #include "wayland-glib.h"
40 struct display *display;
41 struct window *window;
42 cairo_surface_t *surface;
43 int x, y, width, height;
45 struct { float *d, *u, *v; } b[2];
48 static void set_boundary(struct smoke *smoke, float x, float y, float *p)
52 l = (smoke->height - 2) * smoke->width;
53 for (i = 1; i < smoke->width - 1; i++) {
54 p[i] = y * p[i + smoke->width];
55 p[l + i + smoke->width] = y * p[l + i];
58 for (i = 1; i < smoke->height - 1; i++) {
59 p[i * smoke->width] = x * p[i * smoke->width + 1];
60 p[i * smoke->width + smoke->width - 1] =
61 x * p[i * smoke->width + smoke->width - 2];
65 static void diffuse(struct smoke *smoke, uint32_t time,
66 float *source, float *dest)
72 stride = smoke->width;
74 for (k = 0; k < 5; k++) {
75 for (y = 1; y < smoke->height - 1; y++) {
76 s = source + y * stride;
77 d = dest + y * stride;
78 for (x = 1; x < smoke->width - 1; x++) {
79 t = d[x - 1] + d[x + 1] +
80 d[x - stride] + d[x + stride];
81 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
87 static void advect(struct smoke *smoke, uint32_t time,
88 float *uu, float *vv, float *source, float *dest)
96 stride = smoke->width;
98 for (y = 1; y < smoke->height - 1; y++) {
99 d = dest + y * stride;
103 for (x = 1; x < smoke->width - 1; x++) {
110 if (px > smoke->width - 0.5)
111 px = smoke->width - 0.5;
112 if (py > smoke->height - 0.5)
113 py = smoke->height - 0.5;
118 s = source + j * stride + i;
119 d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
120 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
125 static void project(struct smoke *smoke, uint32_t time,
126 float *u, float *v, float *p, float *div)
131 h = 1.0 / smoke->width;
133 memset(p, 0, smoke->height * smoke->width);
134 for (y = 1; y < smoke->height - 1; y++) {
136 for (x = 1; x < smoke->width - 1; x++) {
137 div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
138 v[l + x + s] - v[l + x - s]);
143 for (k = 0; k < 5; k++) {
144 for (y = 1; y < smoke->height - 1; y++) {
146 for (x = 1; x < smoke->width - 1; x++) {
147 p[l + x] = (div[l + x] +
156 for (y = 1; y < smoke->height - 1; y++) {
158 for (x = 1; x < smoke->width - 1; x++) {
159 u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
160 v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
165 static void render(struct smoke *smoke)
168 int x, y, width, height, stride;
172 dest = cairo_image_surface_get_data (smoke->surface);
173 width = cairo_image_surface_get_width (smoke->surface);
174 height = cairo_image_surface_get_height (smoke->surface);
175 stride = cairo_image_surface_get_stride (smoke->surface);
177 for (y = 1; y < height - 1; y++) {
178 s = smoke->b[smoke->current].d + y * smoke->height;
179 d = (uint32_t *) (dest + y * stride);
180 for (x = 1; x < width - 1; x++) {
181 c = (int) (s[x] * 800);
187 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
193 frame_callback(void *data, uint32_t time)
195 struct smoke *smoke = data;
197 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
198 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
199 project(smoke, time / 30,
200 smoke->b[1].u, smoke->b[1].v,
201 smoke->b[0].u, smoke->b[0].v);
202 advect(smoke, time / 30,
203 smoke->b[1].u, smoke->b[1].v,
204 smoke->b[1].u, smoke->b[0].u);
205 advect(smoke, time / 30,
206 smoke->b[1].u, smoke->b[1].v,
207 smoke->b[1].v, smoke->b[0].v);
208 project(smoke, time / 30,
209 smoke->b[0].u, smoke->b[0].v,
210 smoke->b[1].u, smoke->b[1].v);
212 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
213 advect(smoke, time / 30,
214 smoke->b[0].u, smoke->b[0].v,
215 smoke->b[1].d, smoke->b[0].d);
219 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
220 wl_display_frame_callback(display_get_display(smoke->display),
221 frame_callback, smoke);
225 smoke_motion_handler(struct window *window,
226 struct input *input, uint32_t time,
227 int32_t x, int32_t y,
228 int32_t surface_x, int32_t surface_y, void *data)
230 struct smoke *smoke = data;
231 int i, i0, i1, j, j0, j1, k, d = 5;
233 if (surface_x - d < 1)
237 if (i0 + 2 * d > smoke->width - 1)
238 i1 = smoke->width - 1;
242 if (surface_y - d < 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;
259 return POINTER_HAND1;
262 int main(int argc, char *argv[])
269 d = display_create(&argc, &argv, NULL);
271 fprintf(stderr, "failed to create display: %m\n");
280 smoke.window = window_create(d, "smoke", smoke.width, smoke.height);
282 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
283 clock_gettime(CLOCK_MONOTONIC, &ts);
285 smoke.offset = random();
288 size = smoke.height * smoke.width;
289 smoke.b[0].d = calloc(size, sizeof(float));
290 smoke.b[0].u = calloc(size, sizeof(float));
291 smoke.b[0].v = calloc(size, sizeof(float));
292 smoke.b[1].d = calloc(size, sizeof(float));
293 smoke.b[1].u = calloc(size, sizeof(float));
294 smoke.b[1].v = calloc(size, sizeof(float));
296 window_set_decoration(smoke.window, 0);
297 window_create_surface(smoke.window);
298 smoke.surface = window_get_surface(smoke.window);
300 window_flush(smoke.window);
302 window_set_motion_handler(smoke.window,
303 smoke_motion_handler);
305 window_set_user_data(smoke.window, &smoke);
306 wl_display_frame_callback(display_get_display(d),
307 frame_callback, &smoke);