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