Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: native_client_sdk/src/examples/demo/life/life.c

Issue 15011003: ppapi_simple (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix enums Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Native Client SDK Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can
3 // be found in the LICENSE file.
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "ppapi/c/pp_resource.h"
11 #include "ppapi/c/ppb_core.h"
12 #include "ppapi/c/ppb_fullscreen.h"
13 #include "ppapi/c/ppb_graphics_2d.h"
14 #include "ppapi/c/ppb_image_data.h"
15 #include "ppapi/c/ppb_input_event.h"
16 #include "ppapi/c/ppb_instance.h"
17 #include "ppapi/c/ppb_view.h"
18
19 #include "ppapi_simple/ps_event.h"
20 #include "ppapi_simple/ps_main.h"
21
22 PPB_Core* g_pCore;
23 PPB_Fullscreen* g_pFullscreen;
24 PPB_Graphics2D* g_pGraphics2D;
25 PPB_ImageData* g_pImageData;
26 PPB_Instance* g_pInstance;
27 PPB_View* g_pView;
28 PPB_InputEvent* g_pInputEvent;
29 PPB_KeyboardInputEvent* g_pKeyboardInput;
30 PPB_MouseInputEvent* g_pMouseInput;
31 PPB_TouchInputEvent* g_pTouchInput;
32
33 struct {
34 PP_Resource ctx;
35 struct PP_Size size;
36 int bound;
37 uint8_t* cell_in;
38 uint8_t* cell_out;
39 } g_Context;
40
41
42 const unsigned int kInitialRandSeed = 0xC0DE533D;
43
44 #define MakeRGBA(r, g, b, a) \
45 (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
46
47 // Given a count of cells in a 3x3 grid where cells are worth 1 except for
48 // the center which is worth 9, this is a color representation of how
49 // "alive" that cell is making for a more interesting representation than
50 // a binary alive or dead.
51 const uint32_t kNeighborColors[] = {
52 MakeRGBA(0x00, 0x00, 0x00, 0xff),
53 MakeRGBA(0x00, 0x40, 0x00, 0xff),
54 MakeRGBA(0x00, 0x60, 0x00, 0xff),
55 MakeRGBA(0x00, 0x80, 0x00, 0xff),
56 MakeRGBA(0x00, 0xA0, 0x00, 0xff),
57 MakeRGBA(0x00, 0xC0, 0x00, 0xff),
58 MakeRGBA(0x00, 0xE0, 0x00, 0xff),
59 MakeRGBA(0x00, 0x00, 0x00, 0xff),
60 MakeRGBA(0x00, 0x40, 0x00, 0xff),
61 MakeRGBA(0x00, 0x60, 0x00, 0xff),
62 MakeRGBA(0x00, 0x80, 0x00, 0xff),
63 MakeRGBA(0x00, 0xA0, 0x00, 0xff),
64 MakeRGBA(0x00, 0xC0, 0x00, 0xff),
65 MakeRGBA(0x00, 0xE0, 0x00, 0xff),
66 MakeRGBA(0x00, 0xFF, 0x00, 0xff),
67 MakeRGBA(0x00, 0xFF, 0x00, 0xff),
68 MakeRGBA(0x00, 0xFF, 0x00, 0xff),
69 MakeRGBA(0x00, 0xFF, 0x00, 0xff),
70 };
71
72 // These represent the new health value of a cell based on its neighboring
73 // values. The health is binary: either alive or dead.
nfullagar1 2013/05/30 18:04:52 There's a mix of // and /* */ style comments in th
noelallen1 2013/05/30 21:01:44 Thanks, C style in C files.
74 const uint8_t kIsAlive[] = {
75 0, 0, 0, 1, 0, 0, 0, 0, 0, // Values if the center cell is dead.
76 0, 0, 1, 1, 0, 0, 0, 0, 0 // Values if the center cell is alive.
77 };
78
79 void UpdateContext(uint32_t width, uint32_t height) {
80 if (width != g_Context.size.width || height != g_Context.size.height) {
81 size_t size = width * height;
82 size_t index;
83
84 free(g_Context.cell_in);
85 free(g_Context.cell_out);
86
87 //
88 // Create a new context
89 //
90 g_Context.cell_in = (uint8_t*) malloc(size);
91 g_Context.cell_out = (uint8_t*) malloc(size);
92
93 memset(g_Context.cell_out, 0, size);
94 for (index = 0; index < size; index++) {
95 g_Context.cell_in[index] = rand() & 1;
96 }
97 }
98
99 // Recreate the graphics context on a view change
100 g_pCore->ReleaseResource(g_Context.ctx);
101 g_Context.size.width = width;
102 g_Context.size.height = height;
103 g_Context.ctx =
104 g_pGraphics2D->Create(PSGetInstanceId(), &g_Context.size, PP_TRUE);
105 g_Context.bound =
106 g_pInstance->BindGraphics(PSGetInstanceId(), g_Context.ctx);
107 }
108
109 void ProcessEvent(PSEvent* event) {
110 switch(event->type) {
111 /* If the view updates, build a new Graphics 2D Context */
112 case PSE_INSTANCE_DIDCHANGEVIEW: {
113 struct PP_Rect rect;
114
115 g_pView->GetRect(event->as_resource, &rect);
116 UpdateContext(rect.size.width, rect.size.height);
117 }
118
119 case PSE_INSTANCE_HANDLEINPUT: {
120 PP_InputEvent_Type type = g_pInputEvent->GetType(event->as_resource);
121 PP_InputEvent_Modifier modifiers =
122 g_pInputEvent->GetModifiers(event->as_resource);
123 int32_t width = g_Context.size.width;
124 int32_t height = g_Context.size.height;
125
126 switch(type) {
127 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
128 {
nfullagar1 2013/05/30 18:04:52 This code (to scribble dots into the current cell
noelallen1 2013/05/30 21:01:44 Done.
129 struct PP_Point location =
130 g_pMouseInput->GetPosition(event->as_resource);
131
132 if (!g_Context.cell_in) break;
133
134 // If the button is down, draw
135 if (modifiers & PP_INPUTEVENT_MODIFIER_LEFTBUTTONDOWN) {
136 int x = location.x;
137 int y = location.y;
138
139 if (x > 0 && x < width && y > 0 && y < height) {
140 g_Context.cell_in[x - 1 + y * width] = 1;
141 g_Context.cell_in[x + 1 + y * width] = 1;
142 g_Context.cell_in[x + (y - 1) * width] = 1;
143 g_Context.cell_in[x + (y + 1) * width] = 1;
144 }
145 }
146 }
147 break;
148
149 case PP_INPUTEVENT_TYPE_KEYUP: {
nfullagar1 2013/05/30 18:04:52 Why trigger on keyup? Wouldn't keydown be more re
noelallen1 2013/05/30 21:01:44 Done.
150 PP_Bool fullscreen = g_pFullscreen->IsFullscreen(PSGetInstanceId());
151 g_pFullscreen->SetFullscreen(PSGetInstanceId(),
152 fullscreen ? PP_FALSE : PP_TRUE);
153 break;
154 }
155 default:
156 break;
157 }
158 break;
159 }
160
161 default:
162 break;
163 }
164 }
165
166
167 void Stir(uint32_t width, uint32_t height) {
168 int i;
169 if (g_Context.cell_in == NULL || g_Context.cell_out == NULL)
170 return;
171
172 for (i = 0; i < width; ++i) {
173 g_Context.cell_in[i] = rand() & 1;
174 g_Context.cell_in[i + (height - 1) * width] = rand() & 1;
175 }
176 for (i = 0; i < height; ++i) {
177 g_Context.cell_in[i * width] = rand() & 1;
178 g_Context.cell_in[i * width + (width - 1)] = rand() & 1;
179 }
180 }
181
182 void Render() {
183 struct PP_Size* psize = &g_Context.size;
184 PP_ImageDataFormat format = g_pImageData->GetNativeImageDataFormat();
185
186 // Create a buffer to draw into. Since we are waiting until the next flush
187 // chrome has an opportunity to cache this buffer see ppb_graphics_2d.h.
188 PP_Resource image =
189 g_pImageData->Create(PSGetInstanceId(), format, psize, PP_FALSE);
190 uint8_t* pixels = g_pImageData->Map(image);
191
192 struct PP_ImageDataDesc desc;
193 uint8_t* cell_temp;
194 uint32_t x, y;
195
196 // If we somehow have not allocated these pointers yet, skip this frame.
197 if (!g_Context.cell_in || !g_Context.cell_out) return;
198
199 // Get the stride
200 g_pImageData->Describe(image, &desc);
201
202 // Stir up the edges to prevent the simulation from reaching steady state.
203 Stir(desc.size.width, desc.size.height);
204
205 // Do neighbor summation; apply rules, output pixel color.
206 for (y = 1; y < desc.size.height - 1; ++y) {
207 uint8_t *src0 = (g_Context.cell_in + (y - 1) * desc.size.width) + 1;
208 uint8_t *src1 = src0 + desc.size.width;
209 uint8_t *src2 = src1 + desc.size.width;
210 int count;
211 uint32_t color;
212 uint8_t *dst = (g_Context.cell_out + y * desc.size.width) + 1;
213 uint32_t *pixel_line = (uint32_t*) (pixels + y * desc.stride);
214
215 for (x = 1; x < (desc.size.width - 1); ++x) {
216 // Build sum, weight center by 9x.
217 count = src0[-1] + src0[0] + src0[1] +
218 src1[-1] + src1[0] * 9 + src1[1] +
219 src2[-1] + src2[0] + src2[1];
220 color = kNeighborColors[count];
221
222 *pixel_line++ = color;
223 *dst++ = kIsAlive[count];
224 ++src0;
225 ++src1;
226 ++src2;
227 }
228 }
229
230 cell_temp = g_Context.cell_in;
231 g_Context.cell_in = g_Context.cell_out;
232 g_Context.cell_out = cell_temp;
233
234 /* Unmap the range, we no longer need it. */
235 g_pImageData->Unmap(image);
236
237 /* Replace the contexts, and block until it's on the screen. */
238 g_pGraphics2D->ReplaceContents(g_Context.ctx, image);
239 g_pGraphics2D->Flush(g_Context.ctx, PP_BlockUntilComplete());
240
241 /* Release the image data, we no longer need it. */
242 g_pCore->ReleaseResource(image);
243 }
244
245 // Starting point for the module. We do not use main since it would
246 // collide with main in libppapi_cpp.
247 int example_main(int argc, const char *argv[]) {
248 fprintf(stdout,"Started main.\n");
249 g_pCore = (PPB_Core*)PSGetInterface(PPB_CORE_INTERFACE);
250 g_pFullscreen = (PPB_Fullscreen*)PSGetInterface(PPB_FULLSCREEN_INTERFACE);
251 g_pGraphics2D = (PPB_Graphics2D*)PSGetInterface(PPB_GRAPHICS_2D_INTERFACE);
252 g_pInstance = (PPB_Instance*)PSGetInterface(PPB_INSTANCE_INTERFACE);
253 g_pImageData = (PPB_ImageData*)PSGetInterface(PPB_IMAGEDATA_INTERFACE);
254 g_pView = (PPB_View*)PSGetInterface(PPB_VIEW_INTERFACE);
255
256 g_pInputEvent =
257 (PPB_InputEvent*) PSGetInterface(PPB_INPUT_EVENT_INTERFACE);
258 g_pKeyboardInput = (PPB_KeyboardInputEvent*)
259 PSGetInterface(PPB_KEYBOARD_INPUT_EVENT_INTERFACE);
260 g_pMouseInput =
261 (PPB_MouseInputEvent*) PSGetInterface(PPB_MOUSE_INPUT_EVENT_INTERFACE);
262 g_pTouchInput =
263 (PPB_TouchInputEvent*) PSGetInterface(PPB_TOUCH_INPUT_EVENT_INTERFACE);
264
265 PSEventSetFilter(PSE_ALL);
266 while (1) {
267 /* Process all waiting events without blocking */
268 PSEvent* event;
269 while ((event = PSEventTryAcquire()) != NULL) {
270 ProcessEvent(event);
271 PSEventRelease(event);
272 }
273
274 /* Render a frame, blocking until complete. */
275 if (g_Context.bound) {
276 Render();
277 }
278 }
279 return 0;
280 }
281
282 // Regsiter the function to call once the Instance Object is initialized.
binji 2013/05/30 05:07:50 sp: Register
noelallen1 2013/05/30 18:07:19 Done.
283 // see: pappi_simple/ppapi_simple_main.h
binji 2013/05/30 05:07:50 ps_main.h
noelallen1 2013/05/30 18:07:19 Done.
284 PPAPI_SIMPLE_REGISTER_MAIN(example_main);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698