Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <time.h> |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 30 | #include <math.h> |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 31 | #include <cairo.h> |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 32 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 33 | #include <wayland-client.h> |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 34 | #include "window.h" |
| 35 | |
| 36 | struct smoke { |
| 37 | struct display *display; |
| 38 | struct window *window; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 39 | struct widget *widget; |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 40 | int width, height; |
Daniel Stone | 4eb445a | 2012-11-07 17:51:36 +1100 | [diff] [blame] | 41 | int current; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 42 | struct { float *d, *u, *v; } b[2]; |
| 43 | }; |
| 44 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 45 | static void diffuse(struct smoke *smoke, uint32_t time, |
| 46 | float *source, float *dest) |
| 47 | { |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 48 | float *s, *d; |
| 49 | int x, y, k, stride; |
| 50 | float t, a = 0.0002; |
| 51 | |
| 52 | stride = smoke->width; |
| 53 | |
| 54 | for (k = 0; k < 5; k++) { |
| 55 | for (y = 1; y < smoke->height - 1; y++) { |
| 56 | s = source + y * stride; |
| 57 | d = dest + y * stride; |
| 58 | for (x = 1; x < smoke->width - 1; x++) { |
| 59 | t = d[x - 1] + d[x + 1] + |
| 60 | d[x - stride] + d[x + stride]; |
| 61 | d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static void advect(struct smoke *smoke, uint32_t time, |
| 68 | float *uu, float *vv, float *source, float *dest) |
| 69 | { |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 70 | float *s, *d; |
| 71 | float *u, *v; |
Bryce Harrington | 40269a6 | 2010-11-19 12:15:36 -0800 | [diff] [blame] | 72 | int x, y, stride; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 73 | int i, j; |
| 74 | float px, py, fx, fy; |
| 75 | |
| 76 | stride = smoke->width; |
| 77 | |
| 78 | for (y = 1; y < smoke->height - 1; y++) { |
| 79 | d = dest + y * stride; |
| 80 | u = uu + y * stride; |
| 81 | v = vv + y * stride; |
| 82 | |
| 83 | for (x = 1; x < smoke->width - 1; x++) { |
| 84 | px = x - u[x]; |
| 85 | py = y - v[x]; |
| 86 | if (px < 0.5) |
| 87 | px = 0.5; |
| 88 | if (py < 0.5) |
| 89 | py = 0.5; |
| 90 | if (px > smoke->width - 0.5) |
| 91 | px = smoke->width - 0.5; |
| 92 | if (py > smoke->height - 0.5) |
| 93 | py = smoke->height - 0.5; |
| 94 | i = (int) px; |
| 95 | j = (int) py; |
| 96 | fx = px - i; |
| 97 | fy = py - j; |
| 98 | s = source + j * stride + i; |
| 99 | d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) + |
| 100 | (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static void project(struct smoke *smoke, uint32_t time, |
| 106 | float *u, float *v, float *p, float *div) |
| 107 | { |
| 108 | int x, y, k, l, s; |
Bryce Harrington | 40269a6 | 2010-11-19 12:15:36 -0800 | [diff] [blame] | 109 | float h; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 110 | |
| 111 | h = 1.0 / smoke->width; |
| 112 | s = smoke->width; |
| 113 | memset(p, 0, smoke->height * smoke->width); |
| 114 | for (y = 1; y < smoke->height - 1; y++) { |
| 115 | l = y * s; |
| 116 | for (x = 1; x < smoke->width - 1; x++) { |
| 117 | div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] + |
| 118 | v[l + x + s] - v[l + x - s]); |
| 119 | p[l + x] = 0; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | for (k = 0; k < 5; k++) { |
| 124 | for (y = 1; y < smoke->height - 1; y++) { |
| 125 | l = y * s; |
| 126 | for (x = 1; x < smoke->width - 1; x++) { |
| 127 | p[l + x] = (div[l + x] + |
| 128 | p[l + x - 1] + |
| 129 | p[l + x + 1] + |
| 130 | p[l + x - s] + |
| 131 | p[l + x + s]) / 4; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | for (y = 1; y < smoke->height - 1; y++) { |
| 137 | l = y * s; |
| 138 | for (x = 1; x < smoke->width - 1; x++) { |
| 139 | u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h; |
| 140 | v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 145 | static void render(struct smoke *smoke, cairo_surface_t *surface) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 146 | { |
Bryce Harrington | 40269a6 | 2010-11-19 12:15:36 -0800 | [diff] [blame] | 147 | unsigned char *dest; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 148 | int x, y, width, height, stride; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 149 | float *s; |
| 150 | uint32_t *d, c, a; |
| 151 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 152 | dest = cairo_image_surface_get_data(surface); |
| 153 | width = cairo_image_surface_get_width(surface); |
| 154 | height = cairo_image_surface_get_height(surface); |
| 155 | stride = cairo_image_surface_get_stride(surface); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 156 | |
| 157 | for (y = 1; y < height - 1; y++) { |
| 158 | s = smoke->b[smoke->current].d + y * smoke->height; |
| 159 | d = (uint32_t *) (dest + y * stride); |
| 160 | for (x = 1; x < width - 1; x++) { |
| 161 | c = (int) (s[x] * 800); |
| 162 | if (c > 255) |
| 163 | c = 255; |
| 164 | a = c; |
| 165 | if (a < 0x33) |
| 166 | a = 0x33; |
| 167 | d[x] = (a << 24) | (c << 16) | (c << 8) | c; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static void |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 173 | redraw_handler(struct widget *widget, void *data) |
| 174 | { |
| 175 | struct smoke *smoke = data; |
Jasper St. Pierre | c8e4186 | 2014-10-12 18:57:31 -0700 | [diff] [blame^] | 176 | uint32_t time = widget_get_last_time(smoke->widget); |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 177 | cairo_surface_t *surface; |
| 178 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 179 | diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u); |
| 180 | diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v); |
| 181 | project(smoke, time / 30, |
| 182 | smoke->b[1].u, smoke->b[1].v, |
| 183 | smoke->b[0].u, smoke->b[0].v); |
| 184 | advect(smoke, time / 30, |
| 185 | smoke->b[1].u, smoke->b[1].v, |
| 186 | smoke->b[1].u, smoke->b[0].u); |
| 187 | advect(smoke, time / 30, |
| 188 | smoke->b[1].u, smoke->b[1].v, |
| 189 | smoke->b[1].v, smoke->b[0].v); |
| 190 | project(smoke, time / 30, |
| 191 | smoke->b[0].u, smoke->b[0].v, |
| 192 | smoke->b[1].u, smoke->b[1].v); |
| 193 | |
| 194 | diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d); |
| 195 | advect(smoke, time / 30, |
| 196 | smoke->b[0].u, smoke->b[0].v, |
| 197 | smoke->b[1].d, smoke->b[0].d); |
| 198 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 199 | surface = window_get_surface(smoke->window); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 200 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 201 | render(smoke, surface); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 202 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 203 | window_damage(smoke->window, 0, 0, smoke->width, smoke->height); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 204 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 205 | cairo_surface_destroy(surface); |
| 206 | |
Jasper St. Pierre | c8e4186 | 2014-10-12 18:57:31 -0700 | [diff] [blame^] | 207 | widget_schedule_redraw(smoke->widget); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Rusty Lynch | 3bb2b8c | 2013-08-08 21:22:40 -0700 | [diff] [blame] | 210 | static void |
| 211 | smoke_motion_handler(struct smoke *smoke, float x, float y) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 212 | { |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 213 | int i, i0, i1, j, j0, j1, k, d = 5; |
| 214 | |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 215 | if (x - d < 1) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 216 | i0 = 1; |
| 217 | else |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 218 | i0 = x - d; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 219 | if (i0 + 2 * d > smoke->width - 1) |
| 220 | i1 = smoke->width - 1; |
| 221 | else |
| 222 | i1 = i0 + 2 * d; |
| 223 | |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 224 | if (y - d < 1) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 225 | j0 = 1; |
| 226 | else |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 227 | j0 = y - d; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 228 | if (j0 + 2 * d > smoke->height - 1) |
| 229 | j1 = smoke->height - 1; |
| 230 | else |
| 231 | j1 = j0 + 2 * d; |
| 232 | |
| 233 | for (i = i0; i < i1; i++) |
| 234 | for (j = j0; j < j1; j++) { |
| 235 | k = j * smoke->width + i; |
| 236 | smoke->b[0].u[k] += 256 - (random() & 512); |
| 237 | smoke->b[0].v[k] += 256 - (random() & 512); |
| 238 | smoke->b[0].d[k] += 1; |
| 239 | } |
Rusty Lynch | 3bb2b8c | 2013-08-08 21:22:40 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static int |
| 243 | mouse_motion_handler(struct widget *widget, struct input *input, |
| 244 | uint32_t time, float x, float y, void *data) |
| 245 | { |
| 246 | smoke_motion_handler(data, x, y); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 247 | |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 248 | return CURSOR_HAND1; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Juan Zhao | 6665063 | 2012-02-02 16:02:06 -0800 | [diff] [blame] | 251 | static void |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 252 | touch_motion_handler(struct widget *widget, struct input *input, |
| 253 | uint32_t time, int32_t id, float x, float y, void *data) |
Rusty Lynch | 3bb2b8c | 2013-08-08 21:22:40 -0700 | [diff] [blame] | 254 | { |
| 255 | smoke_motion_handler(data, x, y); |
| 256 | } |
| 257 | |
| 258 | static void |
Juan Zhao | 6665063 | 2012-02-02 16:02:06 -0800 | [diff] [blame] | 259 | resize_handler(struct widget *widget, |
| 260 | int32_t width, int32_t height, void *data) |
| 261 | { |
| 262 | struct smoke *smoke = data; |
| 263 | |
| 264 | /* Dont resize me */ |
| 265 | widget_set_size(smoke->widget, smoke->width, smoke->height); |
| 266 | } |
| 267 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 268 | int main(int argc, char *argv[]) |
| 269 | { |
| 270 | struct timespec ts; |
| 271 | struct smoke smoke; |
| 272 | struct display *d; |
Bryce Harrington | 40269a6 | 2010-11-19 12:15:36 -0800 | [diff] [blame] | 273 | int size; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 274 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 275 | d = display_create(&argc, argv); |
Yuval Fledel | e9f5e36 | 2010-11-22 21:34:19 +0200 | [diff] [blame] | 276 | if (d == NULL) { |
| 277 | fprintf(stderr, "failed to create display: %m\n"); |
| 278 | return -1; |
| 279 | } |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 280 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 281 | smoke.width = 200; |
| 282 | smoke.height = 200; |
| 283 | smoke.display = d; |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 284 | smoke.window = window_create(d); |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 285 | smoke.widget = window_add_widget(smoke.window, &smoke); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 286 | window_set_title(smoke.window, "smoke"); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 287 | |
| 288 | window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM); |
| 289 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 290 | srandom(ts.tv_nsec); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 291 | |
| 292 | smoke.current = 0; |
| 293 | size = smoke.height * smoke.width; |
| 294 | smoke.b[0].d = calloc(size, sizeof(float)); |
| 295 | smoke.b[0].u = calloc(size, sizeof(float)); |
| 296 | smoke.b[0].v = calloc(size, sizeof(float)); |
| 297 | smoke.b[1].d = calloc(size, sizeof(float)); |
| 298 | smoke.b[1].u = calloc(size, sizeof(float)); |
| 299 | smoke.b[1].v = calloc(size, sizeof(float)); |
| 300 | |
Rusty Lynch | 3bb2b8c | 2013-08-08 21:22:40 -0700 | [diff] [blame] | 301 | widget_set_motion_handler(smoke.widget, mouse_motion_handler); |
| 302 | widget_set_touch_motion_handler(smoke.widget, touch_motion_handler); |
Juan Zhao | 6665063 | 2012-02-02 16:02:06 -0800 | [diff] [blame] | 303 | widget_set_resize_handler(smoke.widget, resize_handler); |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 304 | widget_set_redraw_handler(smoke.widget, redraw_handler); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 305 | |
| 306 | window_set_user_data(smoke.window, &smoke); |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame] | 307 | |
| 308 | widget_schedule_resize(smoke.widget, smoke.width, smoke.height); |
| 309 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 310 | display_run(d); |
| 311 | |
vivek | 31732f7 | 2014-05-15 18:58:16 +0530 | [diff] [blame] | 312 | widget_destroy(smoke.widget); |
| 313 | window_destroy(smoke.window); |
| 314 | display_destroy(d); |
| 315 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 316 | return 0; |
| 317 | } |