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)
73 stride = smoke->width;
75 for (k = 0; k < 5; k++) {
76 for (y = 1; y < smoke->height - 1; y++) {
77 s = source + y * stride;
78 d = dest + y * stride;
79 for (x = 1; x < smoke->width - 1; x++) {
80 t = d[x - 1] + d[x + 1] +
81 d[x - stride] + d[x + stride];
82 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
88 static void advect(struct smoke *smoke, uint32_t time,
89 float *uu, float *vv, float *source, float *dest)
98 stride = smoke->width;
100 for (y = 1; y < smoke->height - 1; y++) {
101 d = dest + y * stride;
105 for (x = 1; x < smoke->width - 1; x++) {
112 if (px > smoke->width - 0.5)
113 px = smoke->width - 0.5;
114 if (py > smoke->height - 0.5)
115 py = smoke->height - 0.5;
120 s = source + j * stride + i;
121 d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
122 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
127 static void project(struct smoke *smoke, uint32_t time,
128 float *u, float *v, float *p, float *div)
133 h = 1.0 / smoke->width;
135 memset(p, 0, smoke->height * smoke->width);
136 for (y = 1; y < smoke->height - 1; y++) {
138 for (x = 1; x < smoke->width - 1; x++) {
139 div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
140 v[l + x + s] - v[l + x - s]);
145 for (k = 0; k < 5; k++) {
146 for (y = 1; y < smoke->height - 1; y++) {
148 for (x = 1; x < smoke->width - 1; x++) {
149 p[l + x] = (div[l + x] +
158 for (y = 1; y < smoke->height - 1; y++) {
160 for (x = 1; x < smoke->width - 1; x++) {
161 u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
162 v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
167 static void render(struct smoke *smoke)
170 unsigned char *source, *dest;
171 int x, y, width, height, stride;
176 dest = cairo_image_surface_get_data (smoke->surface);
177 width = cairo_image_surface_get_width (smoke->surface);
178 height = cairo_image_surface_get_height (smoke->surface);
179 stride = cairo_image_surface_get_stride (smoke->surface);
181 for (y = 1; y < height - 1; y++) {
182 s = smoke->b[smoke->current].d + y * smoke->height;
183 d = (uint32_t *) (dest + y * stride);
184 for (x = 1; x < width - 1; x++) {
185 c = (int) (s[x] * 800);
191 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
197 frame_callback(void *data, uint32_t time)
200 struct smoke *smoke = data;
203 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
204 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
205 project(smoke, time / 30,
206 smoke->b[1].u, smoke->b[1].v,
207 smoke->b[0].u, smoke->b[0].v);
208 advect(smoke, time / 30,
209 smoke->b[1].u, smoke->b[1].v,
210 smoke->b[1].u, smoke->b[0].u);
211 advect(smoke, time / 30,
212 smoke->b[1].u, smoke->b[1].v,
213 smoke->b[1].v, smoke->b[0].v);
214 project(smoke, time / 30,
215 smoke->b[0].u, smoke->b[0].v,
216 smoke->b[1].u, smoke->b[1].v);
218 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
219 advect(smoke, time / 30,
220 smoke->b[0].u, smoke->b[0].v,
221 smoke->b[1].d, smoke->b[0].d);
225 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
226 wl_display_frame_callback(display_get_display(smoke->display),
227 frame_callback, smoke);
231 smoke_motion_handler(struct window *window,
232 struct input *input, uint32_t time,
233 int32_t x, int32_t y,
234 int32_t surface_x, int32_t surface_y, void *data)
236 struct smoke *smoke = data;
237 int i, i0, i1, j, j0, j1, k, d = 5;
239 if (surface_x - d < 1)
243 if (i0 + 2 * d > smoke->width - 1)
244 i1 = smoke->width - 1;
248 if (surface_y - d < 1)
252 if (j0 + 2 * d > smoke->height - 1)
253 j1 = smoke->height - 1;
257 for (i = i0; i < i1; i++)
258 for (j = j0; j < j1; j++) {
259 k = j * smoke->width + i;
260 smoke->b[0].u[k] += 256 - (random() & 512);
261 smoke->b[0].v[k] += 256 - (random() & 512);
262 smoke->b[0].d[k] += 1;
265 return POINTER_HAND1;
268 int main(int argc, char *argv[])
275 d = display_create(&argc, &argv, NULL);
282 smoke.window = window_create(d, "smoke", smoke.x, smoke.y,
283 smoke.width, smoke.height);
285 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
286 clock_gettime(CLOCK_MONOTONIC, &ts);
288 smoke.offset = random();
291 size = smoke.height * smoke.width;
292 smoke.b[0].d = calloc(size, sizeof(float));
293 smoke.b[0].u = calloc(size, sizeof(float));
294 smoke.b[0].v = calloc(size, sizeof(float));
295 smoke.b[1].d = calloc(size, sizeof(float));
296 smoke.b[1].u = calloc(size, sizeof(float));
297 smoke.b[1].v = calloc(size, sizeof(float));
299 window_set_decoration(smoke.window, 0);
300 window_create_surface(smoke.window);
301 smoke.surface = window_get_surface(smoke.window);
303 window_flush(smoke.window);
305 window_set_motion_handler(smoke.window,
306 smoke_motion_handler);
308 window_set_user_data(smoke.window, &smoke);
309 wl_display_frame_callback(display_get_display(d),
310 frame_callback, &smoke);