| OLD | NEW |
| (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 // Initialize the random number generator with |initial_seed|. |
| 102 explicit RandomBitGenerator(unsigned int initial_seed) |
| 103 : random_bit_seed_(initial_seed) {} |
| 104 // Return the next random bit value. Note that value() can't be a const |
| 105 // function because it changes the internal state machine as part of its |
| 106 // mechanism. |
| 107 uint8_t value(); |
| 108 |
| 109 private: |
| 110 unsigned int random_bit_seed_; |
| 111 RandomBitGenerator(); // Not implemented, do not use. |
| 112 }; |
| 113 |
| 114 // Plot a new seed cell in the simulation. If |x| or |y| fall outside of the |
| 115 // size of the 2D context, then do nothing. |
| 116 void Plot(int x, int y); |
| 117 |
| 118 // Add in some random noise to the borders of the simulation, which is used |
| 119 // to determine the life of adjacent cells. This is part of a simulation |
| 120 // tick. |
| 121 void Stir(); |
| 122 |
| 123 // Draw the current state of the simulation into the pixel buffer. |
| 124 void UpdateCells(); |
| 125 |
| 126 // Swap the input and output cell arrays. |
| 127 void Swap(); |
| 128 |
| 129 // Create and initialize the 2D context used for drawing. |
| 130 void CreateContext(const pp::Size& size); |
| 131 // Destroy the 2D drawing context. |
| 132 void DestroyContext(); |
| 133 // Push the pixels to the browser, then attempt to flush the 2D context. If |
| 134 // there is a pending flush on the 2D context, then update the pixels only |
| 135 // and do not flush. |
| 136 void FlushPixelBuffer(); |
| 137 |
| 138 bool IsContextValid() const { |
| 139 return graphics_2d_context_ != NULL; |
| 140 } |
| 141 |
| 142 pp::Graphics2D* graphics_2d_context_; |
| 143 pp::ImageData* pixel_buffer_; |
| 144 RandomBitGenerator random_bits_; |
| 145 bool flush_pending_; |
| 146 uint8_t* cell_in_; |
| 147 uint8_t* cell_out_; |
| 148 }; |
| 149 |
| 150 } // namespace life |
| 151 |
| 152 #endif // EXAMPLES_GRAPHICS_LIFE_LIFE_H_ |
| 153 |
| OLD | NEW |