blob: 6b557974a871850acf5313bd707a046d41851009 [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>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040028#include <math.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040029#include <cairo.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040030
Pekka Paalanen50719bc2011-11-22 14:18:50 +020031#include <wayland-client.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040032#include "window.h"
33
34struct smoke {
35 struct display *display;
36 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050037 struct widget *widget;
Kristian Høgsberg02127232012-02-08 14:47:53 -050038 int width, height;
Daniel Stone4eb445a2012-11-07 17:51:36 +110039 int current;
Kristian Høgsberg02127232012-02-08 14:47:53 -050040 uint32_t time;
Kristian Høgsberg012a0072010-10-26 00:02:20 -040041 struct { float *d, *u, *v; } b[2];
42};
43
Kristian Høgsberg012a0072010-10-26 00:02:20 -040044static void diffuse(struct smoke *smoke, uint32_t time,
45 float *source, float *dest)
46{
Kristian Høgsberg012a0072010-10-26 00:02:20 -040047 float *s, *d;
48 int x, y, k, stride;
49 float t, a = 0.0002;
50
51 stride = smoke->width;
52
53 for (k = 0; k < 5; k++) {
54 for (y = 1; y < smoke->height - 1; y++) {
55 s = source + y * stride;
56 d = dest + y * stride;
57 for (x = 1; x < smoke->width - 1; x++) {
58 t = d[x - 1] + d[x + 1] +
59 d[x - stride] + d[x + stride];
60 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
61 }
62 }
63 }
64}
65
66static void advect(struct smoke *smoke, uint32_t time,
67 float *uu, float *vv, float *source, float *dest)
68{
Kristian Høgsberg012a0072010-10-26 00:02:20 -040069 float *s, *d;
70 float *u, *v;
Bryce Harrington40269a62010-11-19 12:15:36 -080071 int x, y, stride;
Kristian Høgsberg012a0072010-10-26 00:02:20 -040072 int i, j;
73 float px, py, fx, fy;
74
75 stride = smoke->width;
76
77 for (y = 1; y < smoke->height - 1; y++) {
78 d = dest + y * stride;
79 u = uu + y * stride;
80 v = vv + y * stride;
81
82 for (x = 1; x < smoke->width - 1; x++) {
83 px = x - u[x];
84 py = y - v[x];
85 if (px < 0.5)
86 px = 0.5;
87 if (py < 0.5)
88 py = 0.5;
89 if (px > smoke->width - 0.5)
90 px = smoke->width - 0.5;
91 if (py > smoke->height - 0.5)
92 py = smoke->height - 0.5;
93 i = (int) px;
94 j = (int) py;
95 fx = px - i;
96 fy = py - j;
97 s = source + j * stride + i;
98 d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
99 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
100 }
101 }
102}
103
104static void project(struct smoke *smoke, uint32_t time,
105 float *u, float *v, float *p, float *div)
106{
107 int x, y, k, l, s;
Bryce Harrington40269a62010-11-19 12:15:36 -0800108 float h;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400109
110 h = 1.0 / smoke->width;
111 s = smoke->width;
112 memset(p, 0, smoke->height * smoke->width);
113 for (y = 1; y < smoke->height - 1; y++) {
114 l = y * s;
115 for (x = 1; x < smoke->width - 1; x++) {
116 div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
117 v[l + x + s] - v[l + x - s]);
118 p[l + x] = 0;
119 }
120 }
121
122 for (k = 0; k < 5; k++) {
123 for (y = 1; y < smoke->height - 1; y++) {
124 l = y * s;
125 for (x = 1; x < smoke->width - 1; x++) {
126 p[l + x] = (div[l + x] +
127 p[l + x - 1] +
128 p[l + x + 1] +
129 p[l + x - s] +
130 p[l + x + s]) / 4;
131 }
132 }
133 }
134
135 for (y = 1; y < smoke->height - 1; y++) {
136 l = y * s;
137 for (x = 1; x < smoke->width - 1; x++) {
138 u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
139 v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
140 }
141 }
142}
143
Kristian Høgsberg02127232012-02-08 14:47:53 -0500144static void render(struct smoke *smoke, cairo_surface_t *surface)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400145{
Bryce Harrington40269a62010-11-19 12:15:36 -0800146 unsigned char *dest;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400147 int x, y, width, height, stride;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400148 float *s;
149 uint32_t *d, c, a;
150
Kristian Høgsberg02127232012-02-08 14:47:53 -0500151 dest = cairo_image_surface_get_data(surface);
152 width = cairo_image_surface_get_width(surface);
153 height = cairo_image_surface_get_height(surface);
154 stride = cairo_image_surface_get_stride(surface);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400155
156 for (y = 1; y < height - 1; y++) {
157 s = smoke->b[smoke->current].d + y * smoke->height;
158 d = (uint32_t *) (dest + y * stride);
159 for (x = 1; x < width - 1; x++) {
160 c = (int) (s[x] * 800);
161 if (c > 255)
162 c = 255;
163 a = c;
164 if (a < 0x33)
165 a = 0x33;
166 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
167 }
168 }
169}
170
171static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400172frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400173{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400174 struct smoke *smoke = data;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400175
Kristian Høgsberg02127232012-02-08 14:47:53 -0500176 window_schedule_redraw(smoke->window);
177 smoke->time = time;
178
179 if (callback)
180 wl_callback_destroy(callback);
181}
182
183static const struct wl_callback_listener listener = {
184 frame_callback,
185};
186
187static void
188redraw_handler(struct widget *widget, void *data)
189{
190 struct smoke *smoke = data;
191 uint32_t time = smoke->time;
192 struct wl_callback *callback;
193 cairo_surface_t *surface;
194
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400195 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
196 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
197 project(smoke, time / 30,
198 smoke->b[1].u, smoke->b[1].v,
199 smoke->b[0].u, smoke->b[0].v);
200 advect(smoke, time / 30,
201 smoke->b[1].u, smoke->b[1].v,
202 smoke->b[1].u, smoke->b[0].u);
203 advect(smoke, time / 30,
204 smoke->b[1].u, smoke->b[1].v,
205 smoke->b[1].v, smoke->b[0].v);
206 project(smoke, time / 30,
207 smoke->b[0].u, smoke->b[0].v,
208 smoke->b[1].u, smoke->b[1].v);
209
210 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
211 advect(smoke, time / 30,
212 smoke->b[0].u, smoke->b[0].v,
213 smoke->b[1].d, smoke->b[0].d);
214
Kristian Høgsberg02127232012-02-08 14:47:53 -0500215 surface = window_get_surface(smoke->window);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400216
Kristian Høgsberg02127232012-02-08 14:47:53 -0500217 render(smoke, surface);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400218
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400219 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400220
Kristian Høgsberg02127232012-02-08 14:47:53 -0500221 cairo_surface_destroy(surface);
222
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400223 callback = wl_surface_frame(window_get_wl_surface(smoke->window));
224 wl_callback_add_listener(callback, &listener, smoke);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300225 wl_surface_commit(window_get_wl_surface(smoke->window));
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400226}
227
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700228static void
229smoke_motion_handler(struct smoke *smoke, float x, float y)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400230{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400231 int i, i0, i1, j, j0, j1, k, d = 5;
232
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500233 if (x - d < 1)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400234 i0 = 1;
235 else
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500236 i0 = x - d;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400237 if (i0 + 2 * d > smoke->width - 1)
238 i1 = smoke->width - 1;
239 else
240 i1 = i0 + 2 * d;
241
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500242 if (y - d < 1)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400243 j0 = 1;
244 else
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500245 j0 = y - d;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400246 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 }
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700258}
259
260static int
261mouse_motion_handler(struct widget *widget, struct input *input,
262 uint32_t time, float x, float y, void *data)
263{
264 smoke_motion_handler(data, x, y);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400265
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300266 return CURSOR_HAND1;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400267}
268
Juan Zhao66650632012-02-02 16:02:06 -0800269static void
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700270touch_motion_handler(struct widget *widget, uint32_t time,
271 int32_t id, float x, float y, void *data)
272{
273 smoke_motion_handler(data, x, y);
274}
275
276static void
Juan Zhao66650632012-02-02 16:02:06 -0800277resize_handler(struct widget *widget,
278 int32_t width, int32_t height, void *data)
279{
280 struct smoke *smoke = data;
281
282 /* Dont resize me */
283 widget_set_size(smoke->widget, smoke->width, smoke->height);
284}
285
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400286int main(int argc, char *argv[])
287{
288 struct timespec ts;
289 struct smoke smoke;
290 struct display *d;
Bryce Harrington40269a62010-11-19 12:15:36 -0800291 int size;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400292
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500293 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200294 if (d == NULL) {
295 fprintf(stderr, "failed to create display: %m\n");
296 return -1;
297 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400298
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400299 smoke.width = 200;
300 smoke.height = 200;
301 smoke.display = d;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500302 smoke.window = window_create(d);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500303 smoke.widget = window_add_widget(smoke.window, &smoke);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500304 window_set_title(smoke.window, "smoke");
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400305
306 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
307 clock_gettime(CLOCK_MONOTONIC, &ts);
308 srandom(ts.tv_nsec);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400309
310 smoke.current = 0;
311 size = smoke.height * smoke.width;
312 smoke.b[0].d = calloc(size, sizeof(float));
313 smoke.b[0].u = calloc(size, sizeof(float));
314 smoke.b[0].v = calloc(size, sizeof(float));
315 smoke.b[1].d = calloc(size, sizeof(float));
316 smoke.b[1].u = calloc(size, sizeof(float));
317 smoke.b[1].v = calloc(size, sizeof(float));
318
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700319 widget_set_motion_handler(smoke.widget, mouse_motion_handler);
320 widget_set_touch_motion_handler(smoke.widget, touch_motion_handler);
Juan Zhao66650632012-02-02 16:02:06 -0800321 widget_set_resize_handler(smoke.widget, resize_handler);
Kristian Høgsberg02127232012-02-08 14:47:53 -0500322 widget_set_redraw_handler(smoke.widget, redraw_handler);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400323
324 window_set_user_data(smoke.window, &smoke);
Kristian Høgsberg02127232012-02-08 14:47:53 -0500325
326 widget_schedule_resize(smoke.widget, smoke.width, smoke.height);
327
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400328 display_run(d);
329
330 return 0;
331}