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

Side by Side Diff: examples/graphics/life/life.h

Issue 6286025: Port the Life example to Pepper 2. (Closed) Base URL: http://naclports.googlecode.com/svn/trunk/src/
Patch Set: '' Created 9 years, 10 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright 2011 The Native Client 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 #ifndef EXAMPLES_GRAPHICS_LIFE_LIFE_H_
6 #define EXAMPLES_GRAPHICS_LIFE_LIFE_H_
7
8 #include <ppapi/cpp/dev/scriptable_object_deprecated.h>
9 #include <ppapi/cpp/graphics_2d.h>
10 #include <ppapi/cpp/image_data.h>
11 #include <ppapi/cpp/instance.h>
12 #include <ppapi/cpp/rect.h>
13 #include <ppapi/cpp/size.h>
14 #include <pthread.h>
15
16 #include <cstdlib>
17 #include <map>
18 #include <vector>
19
20 namespace life {
21 // The main object that runs Conway's Life simulation (for details, see:
22 // http://en.wikipedia.org/wiki/Conway's_Game_of_Life). The Update() method
23 // is called by the browser to do a single tick of the simulation.
24 class Life : public pp::Instance {
25 public:
26 explicit Life(PP_Instance instance);
27 virtual ~Life();
28
29 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]);
30
31 // Update the graphics context to the new size, and reallocate all new
32 // buffers to the new size.
33 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip);
34
35 // Return a pp::Var that represents the interface exposed to the browser.
36 // The pp::Var takes over ownership of the returned script object.
37 virtual pp::Var GetInstanceObject();
38
39 // Runs a tick of the simulations, updating all buffers. Flushes the
40 // contents of |pixel_buffer_| to the 2D graphics context. This method is
41 // exposed to the browser as "update()".
42 void Update();
43
44 // Plot a new blob of life centered around (|var_x|, |var_y|). This method
45 // is exposed to the browser as "addCellAtPoint()".
46 void AddCellAtPoint(const pp::Var& var_x, const pp::Var& var_y);
47
48 int width() const {
49 return pixel_buffer_ ? pixel_buffer_->size().width() : 0;
50 }
51 int height() const {
52 return pixel_buffer_ ? pixel_buffer_->size().height() : 0;
53 }
54
55 uint32_t* pixels() const {
56 if (pixel_buffer_ != NULL && !pixel_buffer_->is_null()) {
57 return reinterpret_cast<uint32_t*>(pixel_buffer_->data());
58 }
59 return NULL;
60 }
61
62 // Indicate whether a flush is pending. This can only be called from the
63 // main thread; it is not thread safe.
64 bool flush_pending() const {
65 return flush_pending_;
66 }
67 void set_flush_pending(bool flag) {
68 flush_pending_ = flag;
69 }
70
71 private:
72 // This class exposes the scripting interface for this NaCl module. The
73 // HasMethod method is called by the browser when executing a method call on
74 // the |life| object (see, e.g. the update() function in
75 // life.html). The name of the JavaScript function (e.g. "paint") is
76 // passed in the |method| parameter as a string pp::Var. If HasMethod()
77 // returns |true|, then the browser will call the Call() method to actually
78 // invoke the method.
79 class LifeScriptObject : public pp::deprecated::ScriptableObject {
80 public:
81 explicit LifeScriptObject(Life* app_instance)
82 : pp::deprecated::ScriptableObject(),
83 app_instance_(app_instance) {}
84 virtual ~LifeScriptObject() {}
85 // Return |true| if |method| is one of the exposed method names.
86 virtual bool HasMethod(const pp::Var& method, pp::Var* exception);
87
88 // Invoke the function associated with |method|. The argument list passed
89 // in via JavaScript is marshaled into a vector of pp::Vars.
90 virtual pp::Var Call(const pp::Var& method,
91 const std::vector<pp::Var>& args,
92 pp::Var* exception);
93 private:
94 Life* app_instance_; // weak reference.
95 };
96
97 // Produce single bit random values. Successive calls to value() should
98 // return 0 or 1 with a random distribution.
99 class RandomBitGenerator {
100 public:
101 RandomBitGenerator() : random_bit_seed_(kInitialRandSeed) {}
102 // Return the next random bit value. Note that value() can't be a const
103 // function because it changes the internal state machine as part of its
104 // mechanism.
105 uint8_t value();
106
107 private:
108 static const uint32_t kInitialRandSeed;
dmichael(do not use this one) 2011/02/03 20:08:44 I'd rather remove the static and make random_bit_s
David Springer 2011/02/03 21:09:47 Done.
109 unsigned int random_bit_seed_;
110 };
111
112 // Plot a new seed cell in the simulation. If |x| or |y| fall outside of the
113 // size of the 2D context, then do nothing.
114 void Plot(int x, int y);
115
116 // Add in some random noise to the borders of the simulation, which is used
117 // to determine the life of adjacent cells. This is part of a simulation
118 // tick.
119 void Stir();
120
121 // Draw the current state of the simulation into the pixel buffer.
122 void UpdateCells();
123
124 // Swap the input and output cell arrays.
125 void Swap();
126
127 // Create and initialize the 2D context used for drawing.
128 void CreateContext(const pp::Size& size);
129 // Destroy the 2D drawing context.
130 void DestroyContext();
131 // Push the pixels to the browser, then attempt to flush the 2D context. If
132 // there is a pending flush on the 2D context, then update the pixels only
133 // and do not flush.
134 void FlushPixelBuffer();
135
136 bool IsContextValid() const {
137 return graphics_2d_context_ != NULL;
138 }
139
140 pp::Graphics2D* graphics_2d_context_;
141 pp::ImageData* pixel_buffer_;
142 RandomBitGenerator random_bits_;
143 bool flush_pending_;
144 uint8_t* cell_in_;
145 uint8_t* cell_out_;
146 };
147
148 } // namespace life
149
150 #endif // EXAMPLES_GRAPHICS_LIFE_LIFE_H_
151
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698