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

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 nits, rename 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/ppapi_event.h"
20 #include "ppapi_simple/ppapi_simple_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.
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.ctx =
102 g_pGraphics2D->Create(PSGetInstanceId(), &rect.size, PP_TRUE);
103 g_Context.bound =
104 g_pInstance->BindGraphics(PSGetInstanceId(), g_Context.ctx);
105 g_Context.size = rect.size;
106 }
107
108 void ProcessEvent(PSEvent* event) {
109 switch(event->type) {
110 /* If the view updates, build a new Graphics 2D Context */
111 case PSE_INSTANCE_DIDCHANGEVIEW: {
112 struct PP_Rect rect;
113
114 g_pView->GetRect(event->as_resource, &rect);
115 UpdateContext(rect.size.width, rect.size.height);
116 }
117
118 case PSE_INSTANCE_HANDLEINPUT: {
119 PP_InputEvent_Type type = g_pInputEvent->GetType(event->as_resource);
120 PP_InputEvent_Modifier modifiers =
121 g_pInputEvent->GetModifiers(event->as_resource);
122 int32_t width = g_Context.size.width;
123 int32_t height = g_Context.size.height;
124
125 switch(type) {
126 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
127 {
128 struct PP_Point location =
129 g_pMouseInput->GetPosition(event->as_resource);
130
131 if (!g_Context.cell_in) break;
132
133 // If the button is down, draw
134 if (modifiers & PP_INPUTEVENT_MODIFIER_LEFTBUTTONDOWN) {
135 int x = location.x;
136 int y = location.y;
137
138 if (x > 0 && x < width && y > 0 && y < height) {
139 g_Context.cell_in[x - 1 + y * width] = 1;
140 g_Context.cell_in[x + 1 + y * width] = 1;
141 g_Context.cell_in[x + (y - 1) * width] = 1;
142 g_Context.cell_in[x + (y + 1) * width] = 1;
143 }
144 }
145 }
146 break;
147
148 case PP_INPUTEVENT_TYPE_KEYUP: {
149 PP_Bool fullscreen = g_pFullscreen->IsFullscreen(PSGetInstanceId());
150 g_pFullscreen->SetFullscreen(PSGetInstanceId(),
151 fullscreen ? PP_FALSE : PP_TRUE);
152 break;
153
154 PSE_INPUTEVENT_EVENT
155 PSE_VIEW_EVENT
156 PSE_3D_LOST_EVENT
157 PSE_
nfullagar1 2013/05/30 00:47:04 remove 4 lines above?
noelallen1 2013/05/30 03:10:05 Done.
158 }
159 default:
160 break;
161 }
162 break;
163 }
164
165 default:
166 break;
167 }
168 }
169
170
171 void Stir(uint32_t width, uint32_t height) {
172 int i;
173 if (g_Context.cell_in == NULL || g_Context.cell_out == NULL)
174 return;
175
176 for (i = 0; i < width; ++i) {
177 g_Context.cell_in[i] = rand() & 1;
178 g_Context.cell_in[i + (height - 1) * width] = rand() & 1;
179 }
180 for (i = 0; i < height; ++i) {
181 g_Context.cell_in[i * width] = rand() & 1;
182 g_Context.cell_in[i * width + (width - 1)] = rand() & 1;
183 }
184 }
185
186 void Render() {
187 struct PP_Size* psize = &g_Context.size;
188 PP_ImageDataFormat format = g_pImageData->GetNativeImageDataFormat();
189
190 // Create a buffer to draw into. Since we are waiting until the next flush
191 // chrome has an opportunity to cache this buffer see ppb_graphics_2d.h.
192 PP_Resource image =
193 g_pImageData->Create(PSGetInstanceId(), format, psize, PP_FALSE);
194 uint8_t* pixels = g_pImageData->Map(image);
195
196 struct PP_ImageDataDesc desc;
197 uint8_t* cell_temp;
198 uint32_t x, y;
199
200 // If we somehow have not allocated these pointers yet, skip this frame.
201 if (!g_Context.cell_in || !g_Context.cell_out) return;
202
203 // Get the stride
204 g_pImageData->Describe(image, &desc);
205
206 // Stir up the edges to prevent the simulation from reaching steady state.
207 Stir(desc.size.width, desc.size.height);
208
209 // Do neighbor summation; apply rules, output pixel color.
210 for (y = 1; y < desc.size.height - 1; ++y) {
211 uint8_t *src0 = (g_Context.cell_in + (y - 1) * desc.size.width) + 1;
212 uint8_t *src1 = src0 + desc.size.width;
213 uint8_t *src2 = src1 + desc.size.width;
214 int count;
215 uint32_t color;
216 uint8_t *dst = (g_Context.cell_out + y * desc.size.width) + 1;
217 uint32_t *pixel_line = (uint32_t*) (pixels + y * desc.stride);
218
219 for (x = 1; x < (desc.size.width - 1); ++x) {
220 // Build sum, weight center by 9x.
221 count = src0[-1] + src0[0] + src0[1] +
222 src1[-1] + src1[0] * 9 + src1[1] +
223 src2[-1] + src2[0] + src2[1];
224 color = kNeighborColors[count];
225
226 *pixel_line++ = color;
227 *dst++ = kIsAlive[count];
228 ++src0;
229 ++src1;
230 ++src2;
231 }
232 }
233
234 cell_temp = g_Context.cell_in;
235 g_Context.cell_in = g_Context.cell_out;
236 g_Context.cell_out = cell_temp;
237
238 /* Unmap the range, we no longer need it. */
239 g_pImageData->Unmap(image);
240
241 /* Replace the contexts, and block until it's on the screen. */
242 g_pGraphics2D->ReplaceContents(g_Context.ctx, image);
243 g_pGraphics2D->Flush(g_Context.ctx, PP_BlockUntilComplete());
244
245 /* Release the image data, we no longer need it. */
246 g_pCore->ReleaseResource(image);
247 }
248
249 // Starting point for the module. We do not use main since it would
250 // collide with main in libppapi_cpp.
251 int example_main(int argc, const char *argv[]) {
252 fprintf(stdout,"Started main.\n");
253 g_pCore = (PPB_Core*)PSGetInterface(PPB_CORE_INTERFACE);
254 g_pFullscreen = (PPB_Fullscreen*)PSGetInterface(PPB_FULLSCREEN_INTERFACE);
255 g_pGraphics2D = (PPB_Graphics2D*)PSGetInterface(PPB_GRAPHICS_2D_INTERFACE);
256 g_pInstance = (PPB_Instance*)PSGetInterface(PPB_INSTANCE_INTERFACE);
257 g_pImageData = (PPB_ImageData*)PSGetInterface(PPB_IMAGEDATA_INTERFACE);
258 g_pView = (PPB_View*)PSGetInterface(PPB_VIEW_INTERFACE);
259
260 g_pInputEvent =
261 (PPB_InputEvent*) PSGetInterface(PPB_INPUT_EVENT_INTERFACE);
262 g_pKeyboardInput = (PPB_KeyboardInputEvent*)
263 PSGetInterface(PPB_KEYBOARD_INPUT_EVENT_INTERFACE);
264 g_pMouseInput =
265 (PPB_MouseInputEvent*) PSGetInterface(PPB_MOUSE_INPUT_EVENT_INTERFACE);
266 g_pTouchInput =
267 (PPB_TouchInputEvent*) PSGetInterface(PPB_TOUCH_INPUT_EVENT_INTERFACE);
268
269 PSSetEventFilter(PS_ALL_EVENTS);
270 while (1) {
271 /* Process all waiting events without blocking */
272 PSEvent* event;
273 while ((event = PSTryToAcquireEvent()) != NULL) {
274 ProcessEvent(event);
275 PSReleaseEvent(event);
276 }
277
278 /* Render a frame, blocking until complete. */
279 if (g_Context.bound) {
280 Render();
281 }
282 }
283 return 0;
284 }
285
286 // Regsiter the function to call once the Instance Object is initialized.
287 // see: pappi_simple/ppapi_simple_main.h
288 PPAPI_SIMPLE_REGISTER_MAIN(example_main);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698