blob: edd036c465931a8491f2d9e0f2ad1f843f68a854 [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{
68 cairo_t *cr;
69 float *s, *d;
70 int x, y, k, stride;
71 float t, a = 0.0002;
72
73 stride = smoke->width;
74
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;
83 }
84 }
85 }
86}
87
88static void advect(struct smoke *smoke, uint32_t time,
89 float *uu, float *vv, float *source, float *dest)
90{
91 cairo_t *cr;
92 float *s, *d;
93 float *u, *v;
94 int x, y, k, stride;
95 int i, j;
96 float px, py, fx, fy;
97
98 stride = smoke->width;
99
100 for (y = 1; y < smoke->height - 1; y++) {
101 d = dest + y * stride;
102 u = uu + y * stride;
103 v = vv + y * stride;
104
105 for (x = 1; x < smoke->width - 1; x++) {
106 px = x - u[x];
107 py = y - v[x];
108 if (px < 0.5)
109 px = 0.5;
110 if (py < 0.5)
111 py = 0.5;
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;
116 i = (int) px;
117 j = (int) py;
118 fx = px - i;
119 fy = py - j;
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;
123 }
124 }
125}
126
127static void project(struct smoke *smoke, uint32_t time,
128 float *u, float *v, float *p, float *div)
129{
130 int x, y, k, l, s;
131 float h, *d, *q;
132
133 h = 1.0 / smoke->width;
134 s = smoke->width;
135 memset(p, 0, smoke->height * smoke->width);
136 for (y = 1; y < smoke->height - 1; y++) {
137 l = y * s;
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]);
141 p[l + x] = 0;
142 }
143 }
144
145 for (k = 0; k < 5; k++) {
146 for (y = 1; y < smoke->height - 1; y++) {
147 l = y * s;
148 for (x = 1; x < smoke->width - 1; x++) {
149 p[l + x] = (div[l + x] +
150 p[l + x - 1] +
151 p[l + x + 1] +
152 p[l + x - s] +
153 p[l + x + s]) / 4;
154 }
155 }
156 }
157
158 for (y = 1; y < smoke->height - 1; y++) {
159 l = y * s;
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;
163 }
164 }
165}
166
167static void render(struct smoke *smoke)
168{
169 cairo_t *cr;
170 unsigned char *source, *dest;
171 int x, y, width, height, stride;
172 int k, t;
173 float *s;
174 uint32_t *d, c, a;
175
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);
180
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);
186 if (c > 255)
187 c = 255;
188 a = c;
189 if (a < 0x33)
190 a = 0x33;
191 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
192 }
193 }
194}
195
196static void
197frame_callback(void *data, uint32_t time)
198{
199 cairo_surface_t *t;
200 struct smoke *smoke = data;
201 static int i;
202
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);
217
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);
222
223 render(smoke);
224
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);
228}
229
230static int
231smoke_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)
235{
236 struct smoke *smoke = data;
237 int i, i0, i1, j, j0, j1, k, d = 5;
238
239 if (surface_x - d < 1)
240 i0 = 1;
241 else
242 i0 = surface_x - d;
243 if (i0 + 2 * d > smoke->width - 1)
244 i1 = smoke->width - 1;
245 else
246 i1 = i0 + 2 * d;
247
248 if (surface_y - d < 1)
249 j0 = 1;
250 else
251 j0 = surface_y - d;
252 if (j0 + 2 * d > smoke->height - 1)
253 j1 = smoke->height - 1;
254 else
255 j1 = j0 + 2 * d;
256
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;
263 }
264
265 return POINTER_HAND1;
266}
267
268int main(int argc, char *argv[])
269{
270 struct timespec ts;
271 struct smoke smoke;
272 struct display *d;
273 int size, x, y;
274
275 d = display_create(&argc, &argv, NULL);
276
277 smoke.x = 200;
278 smoke.y = 200;
279 smoke.width = 200;
280 smoke.height = 200;
281 smoke.display = d;
282 smoke.window = window_create(d, "smoke", smoke.x, smoke.y,
283 smoke.width, smoke.height);
284
285 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
286 clock_gettime(CLOCK_MONOTONIC, &ts);
287 srandom(ts.tv_nsec);
288 smoke.offset = random();
289
290 smoke.current = 0;
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));
298
299 window_set_decoration(smoke.window, 0);
300 window_create_surface(smoke.window);
301 smoke.surface = window_get_surface(smoke.window);
302
303 window_flush(smoke.window);
304
305 window_set_motion_handler(smoke.window,
306 smoke_motion_handler);
307
308 window_set_user_data(smoke.window, &smoke);
309 wl_display_frame_callback(display_get_display(d),
310 frame_callback, &smoke);
311
312 display_run(d);
313
314 return 0;
315}