blob: 43226adf3aa485d815c6bb3166e27c575ed175b7 [file] [log] [blame]
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001/*
2 * Copyright © 2010 Kristian Høgsberg
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <time.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <time.h>
32#include <cairo.h>
33#include <glib.h>
34
35#include "wayland-client.h"
36#include "wayland-glib.h"
37#include "window.h"
38
39struct smoke {
40 struct display *display;
41 struct window *window;
42 cairo_surface_t *surface;
43 int x, y, width, height;
44 int offset, current;
45 struct { float *d, *u, *v; } b[2];
46};
47
48static void set_boundary(struct smoke *smoke, float x, float y, float *p)
49{
50 int i, l;
51
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];
56 }
57
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];
62 }
63}
64
65static void diffuse(struct smoke *smoke, uint32_t time,
66 float *source, float *dest)
67{
Kristian Høgsberg012a0072010-10-26 00:02:20 -040068 float *s, *d;
69 int x, y, k, stride;
70 float t, a = 0.0002;
71
72 stride = smoke->width;
73
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;
82 }
83 }
84 }
85}
86
87static void advect(struct smoke *smoke, uint32_t time,
88 float *uu, float *vv, float *source, float *dest)
89{
Kristian Høgsberg012a0072010-10-26 00:02:20 -040090 float *s, *d;
91 float *u, *v;
Bryce Harrington40269a62010-11-19 12:15:36 -080092 int x, y, stride;
Kristian Høgsberg012a0072010-10-26 00:02:20 -040093 int i, j;
94 float px, py, fx, fy;
95
96 stride = smoke->width;
97
98 for (y = 1; y < smoke->height - 1; y++) {
99 d = dest + y * stride;
100 u = uu + y * stride;
101 v = vv + y * stride;
102
103 for (x = 1; x < smoke->width - 1; x++) {
104 px = x - u[x];
105 py = y - v[x];
106 if (px < 0.5)
107 px = 0.5;
108 if (py < 0.5)
109 py = 0.5;
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;
114 i = (int) px;
115 j = (int) py;
116 fx = px - i;
117 fy = py - j;
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;
121 }
122 }
123}
124
125static void project(struct smoke *smoke, uint32_t time,
126 float *u, float *v, float *p, float *div)
127{
128 int x, y, k, l, s;
Bryce Harrington40269a62010-11-19 12:15:36 -0800129 float h;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400130
131 h = 1.0 / smoke->width;
132 s = smoke->width;
133 memset(p, 0, smoke->height * smoke->width);
134 for (y = 1; y < smoke->height - 1; y++) {
135 l = y * s;
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]);
139 p[l + x] = 0;
140 }
141 }
142
143 for (k = 0; k < 5; k++) {
144 for (y = 1; y < smoke->height - 1; y++) {
145 l = y * s;
146 for (x = 1; x < smoke->width - 1; x++) {
147 p[l + x] = (div[l + x] +
148 p[l + x - 1] +
149 p[l + x + 1] +
150 p[l + x - s] +
151 p[l + x + s]) / 4;
152 }
153 }
154 }
155
156 for (y = 1; y < smoke->height - 1; y++) {
157 l = y * s;
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;
161 }
162 }
163}
164
165static void render(struct smoke *smoke)
166{
Bryce Harrington40269a62010-11-19 12:15:36 -0800167 unsigned char *dest;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400168 int x, y, width, height, stride;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400169 float *s;
170 uint32_t *d, c, a;
171
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);
176
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);
182 if (c > 255)
183 c = 255;
184 a = c;
185 if (a < 0x33)
186 a = 0x33;
187 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
188 }
189 }
190}
191
192static void
193frame_callback(void *data, uint32_t time)
194{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400195 struct smoke *smoke = data;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400196
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);
211
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);
216
217 render(smoke);
218
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);
222}
223
224static int
225smoke_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)
229{
230 struct smoke *smoke = data;
231 int i, i0, i1, j, j0, j1, k, d = 5;
232
233 if (surface_x - d < 1)
234 i0 = 1;
235 else
236 i0 = surface_x - d;
237 if (i0 + 2 * d > smoke->width - 1)
238 i1 = smoke->width - 1;
239 else
240 i1 = i0 + 2 * d;
241
242 if (surface_y - d < 1)
243 j0 = 1;
244 else
245 j0 = surface_y - d;
246 if (j0 + 2 * d > smoke->height - 1)
247 j1 = smoke->height - 1;
248 else
249 j1 = j0 + 2 * d;
250
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;
257 }
258
259 return POINTER_HAND1;
260}
261
262int main(int argc, char *argv[])
263{
264 struct timespec ts;
265 struct smoke smoke;
266 struct display *d;
Bryce Harrington40269a62010-11-19 12:15:36 -0800267 int size;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400268
269 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200270 if (d == NULL) {
271 fprintf(stderr, "failed to create display: %m\n");
272 return -1;
273 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400274
275 smoke.x = 200;
276 smoke.y = 200;
277 smoke.width = 200;
278 smoke.height = 200;
279 smoke.display = d;
280 smoke.window = window_create(d, "smoke", smoke.x, smoke.y,
281 smoke.width, smoke.height);
282
283 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
284 clock_gettime(CLOCK_MONOTONIC, &ts);
285 srandom(ts.tv_nsec);
286 smoke.offset = random();
287
288 smoke.current = 0;
289 size = smoke.height * smoke.width;
290 smoke.b[0].d = calloc(size, sizeof(float));
291 smoke.b[0].u = calloc(size, sizeof(float));
292 smoke.b[0].v = calloc(size, sizeof(float));
293 smoke.b[1].d = calloc(size, sizeof(float));
294 smoke.b[1].u = calloc(size, sizeof(float));
295 smoke.b[1].v = calloc(size, sizeof(float));
296
297 window_set_decoration(smoke.window, 0);
298 window_create_surface(smoke.window);
299 smoke.surface = window_get_surface(smoke.window);
300
301 window_flush(smoke.window);
302
303 window_set_motion_handler(smoke.window,
304 smoke_motion_handler);
305
306 window_set_user_data(smoke.window, &smoke);
307 wl_display_frame_callback(display_get_display(d),
308 frame_callback, &smoke);
309
310 display_run(d);
311
312 return 0;
313}