OLD | NEW |
1 /* Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 #include <assert.h> | 6 #include <assert.h> |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 | 43 |
44 const unsigned int kInitialRandSeed = 0xC0DE533D; | 44 const unsigned int kInitialRandSeed = 0xC0DE533D; |
45 | 45 |
46 /* BGRA helper macro, for constructing a pixel for a BGRA buffer. */ | 46 /* BGRA helper macro, for constructing a pixel for a BGRA buffer. */ |
47 #define MakeBGRA(b, g, r, a) \ | 47 #define MakeBGRA(b, g, r, a) \ |
48 (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)) | 48 (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)) |
49 | 49 |
50 | 50 |
51 /* | 51 /* |
52 * Given a count of cells in a 3x3 grid where cells are worth 1 except for | 52 * Convert a count value into a live (green) or dead color value. |
53 * the center which is worth 9, this is a color representation of how | |
54 * "alive" that cell is making for a more interesting representation than | |
55 * a binary alive or dead. | |
56 */ | 53 */ |
57 const uint32_t kNeighborColors[] = { | 54 const uint32_t kNeighborColors[] = { |
58 MakeBGRA(0x00, 0x00, 0x00, 0xff), | 55 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
59 MakeBGRA(0x00, 0x40, 0x00, 0xff), | 56 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
60 MakeBGRA(0x00, 0x60, 0x00, 0xff), | 57 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
61 MakeBGRA(0x00, 0x80, 0x00, 0xff), | 58 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
62 MakeBGRA(0x00, 0xA0, 0x00, 0xff), | 59 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
63 MakeBGRA(0x00, 0xC0, 0x00, 0xff), | 60 MakeBGRA(0x00, 0xFF, 0x00, 0xFF), |
64 MakeBGRA(0x00, 0xE0, 0x00, 0xff), | 61 MakeBGRA(0x00, 0xFF, 0x00, 0xFF), |
65 MakeBGRA(0x00, 0x00, 0x00, 0xff), | 62 MakeBGRA(0x00, 0xFF, 0x00, 0xFF), |
66 MakeBGRA(0x00, 0x40, 0x00, 0xff), | 63 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
67 MakeBGRA(0x00, 0x60, 0x00, 0xff), | 64 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
68 MakeBGRA(0x00, 0x80, 0x00, 0xff), | 65 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
69 MakeBGRA(0x00, 0xA0, 0x00, 0xff), | 66 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
70 MakeBGRA(0x00, 0xC0, 0x00, 0xff), | 67 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
71 MakeBGRA(0x00, 0xE0, 0x00, 0xff), | 68 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
72 MakeBGRA(0x00, 0xFF, 0x00, 0xff), | 69 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
73 MakeBGRA(0x00, 0xFF, 0x00, 0xff), | 70 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
74 MakeBGRA(0x00, 0xFF, 0x00, 0xff), | 71 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
75 MakeBGRA(0x00, 0xFF, 0x00, 0xff), | 72 MakeBGRA(0x00, 0x00, 0x00, 0xFF), |
76 }; | 73 }; |
77 | 74 |
78 /* | 75 /* |
79 * These represent the new health value of a cell based on its neighboring | 76 * These represent the new health value of a cell based on its neighboring |
80 * values. The health is binary: either alive or dead. | 77 * values. The health is binary: either alive or dead. |
81 */ | 78 */ |
82 const uint8_t kIsAlive[] = { | 79 const uint8_t kIsAlive[] = { |
83 0, 0, 0, 1, 0, 0, 0, 0, 0, /* Values if the center cell is dead. */ | 80 0, 0, 0, 0, 0, 1, 1, 1, 0, |
84 0, 0, 1, 1, 0, 0, 0, 0, 0 /* Values if the center cell is alive. */ | 81 0, 0, 0, 0, 0, 0, 0, 0, 0 |
85 }; | 82 }; |
86 | 83 |
87 void UpdateContext(uint32_t width, uint32_t height) { | 84 void UpdateContext(uint32_t width, uint32_t height) { |
88 if (width != g_Context.size.width || height != g_Context.size.height) { | 85 if (width != g_Context.size.width || height != g_Context.size.height) { |
89 size_t size = width * height; | 86 size_t size = width * height; |
90 size_t index; | 87 size_t index; |
91 | 88 |
92 free(g_Context.cell_in); | 89 free(g_Context.cell_in); |
93 free(g_Context.cell_out); | 90 free(g_Context.cell_out); |
94 | 91 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 for (y = 1; y < desc.size.height - 1; ++y) { | 242 for (y = 1; y < desc.size.height - 1; ++y) { |
246 uint8_t *src0 = (g_Context.cell_in + (y - 1) * desc.size.width) + 1; | 243 uint8_t *src0 = (g_Context.cell_in + (y - 1) * desc.size.width) + 1; |
247 uint8_t *src1 = src0 + desc.size.width; | 244 uint8_t *src1 = src0 + desc.size.width; |
248 uint8_t *src2 = src1 + desc.size.width; | 245 uint8_t *src2 = src1 + desc.size.width; |
249 int count; | 246 int count; |
250 uint32_t color; | 247 uint32_t color; |
251 uint8_t *dst = (g_Context.cell_out + y * desc.size.width) + 1; | 248 uint8_t *dst = (g_Context.cell_out + y * desc.size.width) + 1; |
252 uint32_t *pixel_line = (uint32_t*) (pixels + y * desc.stride); | 249 uint32_t *pixel_line = (uint32_t*) (pixels + y * desc.stride); |
253 | 250 |
254 for (x = 1; x < (desc.size.width - 1); ++x) { | 251 for (x = 1; x < (desc.size.width - 1); ++x) { |
255 /* Build sum, weight center by 9x. */ | 252 /* Jitter and sum neighbors. */ |
256 count = src0[-1] + src0[0] + src0[1] + | 253 count = src0[-1] + src0[0] + src0[1] + |
257 src1[-1] + src1[0] * 9 + src1[1] + | 254 src1[-1] + + src1[1] + |
258 src2[-1] + src2[0] + src2[1]; | 255 src2[-1] + src2[0] + src2[1]; |
| 256 /* Include center cell. */ |
| 257 count = count + count + src1[0]; |
| 258 /* Use table lookup indexed by count to determine pixel & alive state. */ |
259 color = kNeighborColors[count]; | 259 color = kNeighborColors[count]; |
260 | |
261 *pixel_line++ = color; | 260 *pixel_line++ = color; |
262 *dst++ = kIsAlive[count]; | 261 *dst++ = kIsAlive[count]; |
263 ++src0; | 262 ++src0; |
264 ++src1; | 263 ++src1; |
265 ++src2; | 264 ++src2; |
266 } | 265 } |
267 } | 266 } |
268 | 267 |
269 cell_temp = g_Context.cell_in; | 268 cell_temp = g_Context.cell_in; |
270 g_Context.cell_in = g_Context.cell_out; | 269 g_Context.cell_in = g_Context.cell_out; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 } | 317 } |
319 } | 318 } |
320 return 0; | 319 return 0; |
321 } | 320 } |
322 | 321 |
323 /* | 322 /* |
324 * Register the function to call once the Instance Object is initialized. | 323 * Register the function to call once the Instance Object is initialized. |
325 * see: pappi_simple/ps_main.h | 324 * see: pappi_simple/ps_main.h |
326 */ | 325 */ |
327 PPAPI_SIMPLE_REGISTER_MAIN(example_main); | 326 PPAPI_SIMPLE_REGISTER_MAIN(example_main); |
OLD | NEW |