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

Side by Side Diff: native_client_sdk/src/examples/api/gamepad/gamepad.cc

Issue 14607005: [NaCl SDK] Cleanup examples. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "gamepad.h" 5 #include "gamepad.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <cassert> 9 #include <cassert>
10 #include <cmath> 10 #include <cmath>
11 #include <cstring> 11 #include <cstring>
12 #include <string> 12 #include <string>
13 #include "ppapi/c/ppb_gamepad.h" 13 #include "ppapi/c/ppb_gamepad.h"
14 #include "ppapi/cpp/completion_callback.h" 14 #include "ppapi/cpp/completion_callback.h"
15 #include "ppapi/cpp/var.h" 15 #include "ppapi/cpp/var.h"
16 16
17 namespace { 17 namespace {
18 18
19 // This is called by the browser when the 2D context has been flushed to the 19 // This is called by the browser when the 2D context has been flushed to the
20 // browser window. 20 // browser window.
21 void FlushCallback(void* data, int32_t result) { 21 void FlushCallback(void* data, int32_t result) {
noelallen1 2013/05/07 19:07:29 Why isn't this the C++ callback? (Callback factor
binji 2013/05/07 20:37:35 Done.
22 static_cast<gamepad::Gamepad*>(data)->set_flush_pending(false); 22 static_cast<GamepadInstance*>(data)->set_flush_pending(false);
23 static_cast<gamepad::Gamepad*>(data)->Paint(); 23 static_cast<GamepadInstance*>(data)->Paint();
24 } 24 }
25 25
26 } // namespace 26 } // namespace
27 27
28 namespace gamepad { 28 GamepadInstance::GamepadInstance(PP_Instance instance)
29
30 Gamepad::Gamepad(PP_Instance instance)
31 : pp::Instance(instance), 29 : pp::Instance(instance),
32 graphics_2d_context_(NULL), 30 graphics_2d_context_(NULL),
33 pixel_buffer_(NULL), 31 pixel_buffer_(NULL),
34 flush_pending_(false), 32 flush_pending_(false),
35 quit_(false) { 33 quit_(false) {
36 pp::Module* module = pp::Module::Get(); 34 pp::Module* module = pp::Module::Get();
37 assert(module); 35 assert(module);
38 gamepad_ = static_cast<const PPB_Gamepad*>( 36 gamepad_ = static_cast<const PPB_Gamepad*>(
39 module->GetBrowserInterface(PPB_GAMEPAD_INTERFACE)); 37 module->GetBrowserInterface(PPB_GAMEPAD_INTERFACE));
40 assert(gamepad_); 38 assert(gamepad_);
41 } 39 }
42 40
43 Gamepad::~Gamepad() { 41 GamepadInstance::~GamepadInstance() {
44 quit_ = true; 42 quit_ = true;
45 DestroyContext(); 43 DestroyContext();
46 delete pixel_buffer_; 44 delete pixel_buffer_;
47 } 45 }
48 46
49 void Gamepad::DidChangeView(const pp::View& view) { 47 void GamepadInstance::DidChangeView(const pp::View& view) {
50 pp::Rect position = view.GetRect(); 48 pp::Rect position = view.GetRect();
51 if (position.size().width() == width() && 49 if (position.size().width() == width() &&
52 position.size().height() == height()) 50 position.size().height() == height())
53 return; // Size didn't change, no need to update anything. 51 return; // Size didn't change, no need to update anything.
54 52
55 // Create a new device context with the new size. 53 // Create a new device context with the new size.
56 DestroyContext(); 54 DestroyContext();
57 CreateContext(position.size()); 55 CreateContext(position.size());
58 // Delete the old pixel buffer and create a new one. 56 // Delete the old pixel buffer and create a new one.
59 delete pixel_buffer_; 57 delete pixel_buffer_;
(...skipping 16 matching lines...) Expand all
76 for (int y = std::max(0, top); 74 for (int y = std::max(0, top);
77 y < std::min(image->size().height() - 1, top + height); 75 y < std::min(image->size().height() - 1, top + height);
78 y++) { 76 y++) {
79 for (int x = std::max(0, left); 77 for (int x = std::max(0, left);
80 x < std::min(image->size().width() - 1, left + width); 78 x < std::min(image->size().width() - 1, left + width);
81 x++) 79 x++)
82 *image->GetAddr32(pp::Point(x, y)) = color; 80 *image->GetAddr32(pp::Point(x, y)) = color;
83 } 81 }
84 } 82 }
85 83
86 void Gamepad::Paint() { 84 void GamepadInstance::Paint() {
87 // Clear the background. 85 // Clear the background.
88 FillRect(pixel_buffer_, 0, 0, width(), height(), 0xfff0f0f0); 86 FillRect(pixel_buffer_, 0, 0, width(), height(), 0xfff0f0f0);
89 87
90 // Get current gamepad data. 88 // Get current gamepad data.
91 PP_GamepadsSampleData gamepad_data; 89 PP_GamepadsSampleData gamepad_data;
92 gamepad_->Sample(pp_instance(), &gamepad_data); 90 gamepad_->Sample(pp_instance(), &gamepad_data);
93 91
94 // Draw the current state for each connected gamepad. 92 // Draw the current state for each connected gamepad.
95 for (size_t p = 0; p < gamepad_data.length; ++p) { 93 for (size_t p = 0; p < gamepad_data.length; ++p) {
96 int width2 = width() / gamepad_data.length / 2; 94 int width2 = width() / gamepad_data.length / 2;
(...skipping 19 matching lines...) Expand all
116 int x = i * 8 + 10 + offset; 114 int x = i * 8 + 10 + offset;
117 int y = 10; 115 int y = 10;
118 FillRect(pixel_buffer_, x - 3, y - 3, 7, 7, colour); 116 FillRect(pixel_buffer_, x - 3, y - 3, 7, 7, colour);
119 } 117 }
120 } 118 }
121 119
122 // Output to the screen. 120 // Output to the screen.
123 FlushPixelBuffer(); 121 FlushPixelBuffer();
124 } 122 }
125 123
126 void Gamepad::CreateContext(const pp::Size& size) { 124 void GamepadInstance::CreateContext(const pp::Size& size) {
127 if (IsContextValid()) 125 if (IsContextValid())
128 return; 126 return;
129 graphics_2d_context_ = new pp::Graphics2D(this, size, false); 127 graphics_2d_context_ = new pp::Graphics2D(this, size, false);
130 if (!BindGraphics(*graphics_2d_context_)) { 128 if (!BindGraphics(*graphics_2d_context_)) {
131 printf("Couldn't bind the device context\n"); 129 printf("Couldn't bind the device context\n");
132 } 130 }
133 } 131 }
134 132
135 void Gamepad::DestroyContext() { 133 void GamepadInstance::DestroyContext() {
136 if (!IsContextValid()) 134 if (!IsContextValid())
137 return; 135 return;
138 delete graphics_2d_context_; 136 delete graphics_2d_context_;
139 graphics_2d_context_ = NULL; 137 graphics_2d_context_ = NULL;
140 } 138 }
141 139
142 void Gamepad::FlushPixelBuffer() { 140 void GamepadInstance::FlushPixelBuffer() {
143 if (!IsContextValid()) 141 if (!IsContextValid())
144 return; 142 return;
145 // Note that the pixel lock is held while the buffer is copied into the 143 // Note that the pixel lock is held while the buffer is copied into the
146 // device context and then flushed. 144 // device context and then flushed.
147 graphics_2d_context_->PaintImageData(*pixel_buffer_, pp::Point()); 145 graphics_2d_context_->PaintImageData(*pixel_buffer_, pp::Point());
148 if (flush_pending()) 146 if (flush_pending())
149 return; 147 return;
150 set_flush_pending(true); 148 set_flush_pending(true);
151 graphics_2d_context_->Flush(pp::CompletionCallback(&FlushCallback, this)); 149 graphics_2d_context_->Flush(pp::CompletionCallback(&FlushCallback, this));
152 } 150 }
153 151
154 } // namespace gamepad 152 class GamepadModule : public pp::Module {
153 public:
154 GamepadModule() : pp::Module() {}
155 virtual ~GamepadModule() {}
156
157 // Create and return a GamepadInstance object.
158 virtual pp::Instance* CreateInstance(PP_Instance instance) {
159 return new GamepadInstance(instance);
160 }
161 };
162
163 namespace pp {
164 Module* CreateModule() { return new GamepadModule(); }
165 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698