OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef LIFE_H_ | |
6 #define LIFE_H_ | |
7 | |
8 #include <string> | |
9 #include <tr1/memory> | |
10 #include <vector> | |
11 | |
12 #include "experimental/conways_life/locking_image_data.h" | |
13 #include "experimental/conways_life/scoped_pixel_lock.h" | |
14 #include "experimental/conways_life/stamp.h" | |
15 #include "experimental/conways_life/threading/condition_lock.h" | |
16 #include "experimental/conways_life/threading/pthread_ext.h" | |
17 #include "ppapi/cpp/point.h" | |
18 | |
19 namespace life { | |
20 // The main object that runs Conway's Life simulation (for details, see: | |
21 // http://en.wikipedia.org/wiki/Conway's_Game_of_Life). | |
22 class Life { | |
23 public: | |
24 // The possible simulation modes. Modes with "Run" in their name will cause | |
25 // the simulation thread to start running if it was paused. | |
26 enum SimulationMode { | |
27 kRunRandomSeed, | |
28 kRunStamp, | |
29 kPaused | |
30 }; | |
31 | |
32 Life(); | |
33 virtual ~Life(); | |
34 | |
35 // Start up the simulation thread. | |
36 void StartSimulation(); | |
37 | |
38 // Set the automaton rules. The rules are expressed as a string, with the | |
39 // Birth and Keep Alive rules separated by a '/'. The format follows the .LIF | |
40 // 1.05 format here: http://psoup.math.wisc.edu/mcell/ca_files_formats.html | |
41 // Survival/Birth. | |
42 void SetAutomatonRules(const std::string& rule_string); | |
43 | |
44 // Resize the simulation to |width|, |height|. This will delete and | |
45 // reallocate all necessary buffer to the new size, and set all the | |
46 // buffers to a new initial state. | |
47 void Resize(int width, int height); | |
48 | |
49 // Delete all the cell buffers. Sets the buffers to NULL. | |
50 void DeleteCells(); | |
51 | |
52 // Clear out the cell buffers (reset to all-dead). | |
53 void ClearCells(); | |
54 | |
55 // Stamp |stamp| at point |point| in both the pixel and cell buffers. | |
56 void PutStampAtPoint(const Stamp& stamp, const pp::Point& point); | |
57 | |
58 // Stop the simulation. Does nothing if the simulation is stopped. | |
59 void StopSimulation(); | |
60 | |
61 // Sleep on the condition that the current simulation mode is not the | |
62 // paused mode. Returns the new run mode. | |
63 SimulationMode WaitForRunMode(); | |
64 | |
65 SimulationMode simulation_mode() const { | |
66 return static_cast<SimulationMode>(simulation_mode_.condition_value()); | |
67 } | |
68 void set_simulation_mode(SimulationMode new_mode) { | |
69 simulation_mode_.Lock(); | |
70 simulation_mode_.UnlockWithCondition(new_mode); | |
71 } | |
72 | |
73 int width() const { | |
74 return width_; | |
75 } | |
76 int height() const { | |
77 return height_; | |
78 } | |
79 | |
80 void set_pixel_buffer( | |
81 const std::tr1::shared_ptr<LockingImageData>& pixel_buffer) { | |
82 shared_pixel_buffer_ = pixel_buffer; | |
83 } | |
84 | |
85 pthread_mutex_t* simulation_mutex() { | |
86 return &life_simulation_mutex_; | |
87 } | |
88 | |
89 // Set the condition lock to indicate whether the simulation thread is | |
90 // running. | |
91 bool is_simulation_running() const { | |
92 return sim_state_condition_.condition_value() != kSimulationStopped; | |
93 } | |
94 void set_is_simulation_running(bool flag) { | |
95 sim_state_condition_.Lock(); | |
96 sim_state_condition_.UnlockWithCondition( | |
97 flag ? kSimulationRunning : kSimulationStopped); | |
98 } | |
99 | |
100 private: | |
101 // The state of the main simulaiton thread. These are values that the | |
102 // simulation condition lock can have. | |
103 enum SimulationState { | |
104 kSimulationStopped, | |
105 kSimulationRunning | |
106 }; | |
107 | |
108 // Produce single bit random values. Successive calls to value() should | |
109 // return 0 or 1 with a random distribution. | |
110 class RandomBitGenerator { | |
111 public: | |
112 // Initialize the random number generator with |initial_seed|. | |
113 explicit RandomBitGenerator(unsigned int initial_seed) | |
114 : random_bit_seed_(initial_seed) {} | |
115 // Return the next random bit value. Note that value() can't be a const | |
116 // function because it changes the internal state machine as part of its | |
117 // mechanism. | |
118 uint8_t value(); | |
119 | |
120 private: | |
121 unsigned int random_bit_seed_; | |
122 RandomBitGenerator(); // Not implemented, do not use. | |
123 }; | |
124 | |
125 // Take each character in |rule_string| and convert it into an index value, | |
126 // set the bit in |life_rules_table_| at each of these indices, applying | |
127 // |rule_offset|. Assumes that all necessary locks have been acquired. Does | |
128 // no range checking or validation of the strings. | |
129 void SetRuleFromString(size_t rule_offset, | |
130 const std::string& rule_string); | |
131 | |
132 // Add in some random noise to the borders of the simulation, which is used | |
133 // to determine the life of adjacent cells. This is part of a simulation | |
134 // tick. | |
135 void AddRandomSeed(); | |
136 | |
137 // Draw the current state of the simulation into the pixel buffer. | |
138 void UpdateCells(); | |
139 | |
140 // Swap the input and output cell arrays. | |
141 void Swap(); | |
142 | |
143 // The main simulation loop. This loop runs the Life simulation. |param| is | |
144 // a pointer to the Life instance. This routine is run on its own thread. | |
145 static void* LifeSimulation(void* param); | |
146 | |
147 // Thread support variables. | |
148 pthread_t life_simulation_thread_; | |
149 pthread_mutex_t life_simulation_mutex_; | |
150 threading::ConditionLock sim_state_condition_; | |
151 std::tr1::shared_ptr<LockingImageData> shared_pixel_buffer_; | |
152 | |
153 // Simulation variables. | |
154 int width_; | |
155 int height_; | |
156 threading::ConditionLock simulation_mode_; | |
157 RandomBitGenerator random_bits_; | |
158 std::vector<uint8_t> life_rules_table_; | |
159 uint8_t* cell_in_; | |
160 uint8_t* cell_out_; | |
161 }; | |
162 | |
163 } // namespace life | |
164 | |
165 #endif // LIFE_H_ | |
166 | |
OLD | NEW |