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 FLOCKING_GEESE_APP_H_ | |
6 #define FLOCKING_GEESE_APP_H_ | |
7 | |
8 #include "nacl_app/flock.h" | |
9 #include "nacl_app/locking_image_data.h" | |
10 #include "nacl_app/png_loader.h" | |
11 #include "ppapi/cpp/graphics_2d.h" | |
12 #include "ppapi/cpp/instance.h" | |
13 #include "ppapi/cpp/rect.h" | |
14 #include "ppapi/cpp/size.h" | |
15 #include "ppapi/cpp/var.h" | |
16 #include "scripting/scripting_bridge.h" | |
17 | |
18 namespace flocking_geese { | |
19 // Manager class that contains the Flock simulation, and handles all the | |
20 // Pepper interaction (scripting, 2D graphics). Pepper calls are all made | |
21 // on the main (browser) thread. The pixel buffer that the simulation draws | |
22 // into is managed by this object, and shared with the simulation thread. | |
23 class FlockingGeeseApp : public pp::Instance { | |
24 public: | |
25 explicit FlockingGeeseApp(PP_Instance instance); | |
26 virtual ~FlockingGeeseApp(); | |
27 | |
28 // Called by the browser when the NaCl module is loaded and all ready to go. | |
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 // Called by the browser to handle the postMessage() call in Javascript. | |
36 virtual void HandleMessage(const pp::Var& message); | |
37 | |
38 // Flushes the contents of the simulation's pixel buffer to the 2D graphics | |
39 // context. If needed, a new pixel buffer is created (for example, a new | |
40 // pixel buffer is created when the simulation's view changes size). | |
41 void Update(); | |
42 | |
43 // Create a new flock of geese for the simulation. Pauses the simulation | |
44 // first, then calls ResetFlock() on the simulation with an initial location | |
45 // that is the center of the simulation canvas. If the simulation was | |
46 // running before making this call, then start it up again with the newly- | |
47 // created flock. Takes one parameter: 'size' which is the new size of the | |
48 // flock. | |
49 void ResetFlock(const scripting::ScriptingBridge& bridge, | |
50 const scripting::MethodParameter& parameters); | |
51 | |
52 // Run the simulation. Exposed to the browser as "runSimulation". Takes | |
53 // no parameters. | |
54 void RunSimulation(const scripting::ScriptingBridge& bridge, | |
55 const scripting::MethodParameter& parameters); | |
56 | |
57 // Pause the simulation. Does nothing if the simulation is stopped. | |
58 // Exposed to the browser as "pauseSimulation". Takes no parameters. | |
59 void PauseSimulation(const scripting::ScriptingBridge& bridge, | |
60 const scripting::MethodParameter& parameters); | |
61 | |
62 // Set the attractor location. | |
63 void SetAttractorLocation(const scripting::ScriptingBridge& bridge, | |
64 const scripting::MethodParameter& parameters); | |
65 | |
66 // Package up the current stats about the simulation (tick duration, etc.) | |
67 // and post it as a formatted string back to the browser. The string | |
68 // starts with the method name "setSimulationInfo" followed and a list of | |
69 // named parameters separated by spaces. The parameter names are separated | |
70 // from their values by a ':'. | |
71 // For example: | |
72 // setSimulationInfo tickDuration:<int> | |
73 // Note: this method can't be 'const' because PostMessage() isn't const. | |
74 void PostSimulationInfo(); | |
75 | |
76 int width() const { | |
77 return shared_pixel_buffer_ ? shared_pixel_buffer_->size().width() : 0; | |
78 } | |
79 int height() const { | |
80 return shared_pixel_buffer_ ? shared_pixel_buffer_->size().height() : 0; | |
81 } | |
82 | |
83 // Indicate whether the simulation is running or paused. | |
84 bool is_running() const { | |
85 return flock_simulation_.simulation_mode() != | |
86 flocking_geese::Flock::kPaused; | |
87 } | |
88 | |
89 // Indicate whether a flush is pending. This can only be called from the | |
90 // main thread; it is not thread safe. | |
91 bool flush_pending() const { | |
92 return flush_pending_; | |
93 } | |
94 void set_flush_pending(bool flag) { | |
95 flush_pending_ = flag; | |
96 } | |
97 private: | |
98 // Create and initialize the 2D context used for drawing. | |
99 void CreateContext(const pp::Size& size); | |
100 // Destroy the 2D drawing context. | |
101 void DestroyContext(); | |
102 // Push the pixels to the browser, then attempt to flush the 2D context. If | |
103 // there is a pending flush on the 2D context, then update the pixels only | |
104 // and do not flush. | |
105 void FlushPixelBuffer(); | |
106 | |
107 bool IsContextValid() const { | |
108 return graphics_2d_context_ != NULL; | |
109 } | |
110 | |
111 // The simulation. | |
112 Flock flock_simulation_; | |
113 | |
114 // Browser connectivity and scripting support. | |
115 scripting::ScriptingBridge scripting_bridge_; | |
116 | |
117 // 2D context variables. | |
118 std::tr1::shared_ptr<LockingImageData> shared_pixel_buffer_; | |
119 pp::Graphics2D* graphics_2d_context_; | |
120 bool flush_pending_; | |
121 bool view_changed_size_; | |
122 pp::Size view_size_; | |
123 | |
124 // Resource management. | |
125 PngLoader png_loader_; | |
126 }; | |
127 } // namespace flocking_geese | |
128 | |
129 #endif // FLOCKING_GEESE_APP_H_ | |
OLD | NEW |