| OLD | NEW |
| 1 // Copyright (c) 2012 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2012 The Native Client 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/dev/ppb_gamepad_dev.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) { |
| 22 static_cast<gamepad::Gamepad*>(data)->set_flush_pending(false); | 22 static_cast<gamepad::Gamepad*>(data)->set_flush_pending(false); |
| 23 static_cast<gamepad::Gamepad*>(data)->Paint(); | 23 static_cast<gamepad::Gamepad*>(data)->Paint(); |
| 24 } | 24 } |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 namespace gamepad { | 28 namespace gamepad { |
| 29 | 29 |
| 30 Gamepad::Gamepad(PP_Instance instance) | 30 Gamepad::Gamepad(PP_Instance instance) |
| 31 : pp::Instance(instance), | 31 : pp::Instance(instance), |
| 32 graphics_2d_context_(NULL), | 32 graphics_2d_context_(NULL), |
| 33 pixel_buffer_(NULL), | 33 pixel_buffer_(NULL), |
| 34 flush_pending_(false), | 34 flush_pending_(false), |
| 35 quit_(false) { | 35 quit_(false) { |
| 36 pp::Module* module = pp::Module::Get(); | 36 pp::Module* module = pp::Module::Get(); |
| 37 assert(module); | 37 assert(module); |
| 38 gamepad_ = static_cast<const PPB_Gamepad_Dev*>( | 38 gamepad_ = static_cast<const PPB_Gamepad*>( |
| 39 module->GetBrowserInterface(PPB_GAMEPAD_DEV_INTERFACE)); | 39 module->GetBrowserInterface(PPB_GAMEPAD_INTERFACE)); |
| 40 assert(gamepad_); | 40 assert(gamepad_); |
| 41 } | 41 } |
| 42 | 42 |
| 43 Gamepad::~Gamepad() { | 43 Gamepad::~Gamepad() { |
| 44 quit_ = true; | 44 quit_ = true; |
| 45 DestroyContext(); | 45 DestroyContext(); |
| 46 delete pixel_buffer_; | 46 delete pixel_buffer_; |
| 47 } | 47 } |
| 48 | 48 |
| 49 void Gamepad::DidChangeView(const pp::Rect& position, | 49 void Gamepad::DidChangeView(const pp::Rect& position, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 77 x++) | 77 x++) |
| 78 *image->GetAddr32(pp::Point(x, y)) = color; | 78 *image->GetAddr32(pp::Point(x, y)) = color; |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 void Gamepad::Paint() { | 82 void Gamepad::Paint() { |
| 83 // Clear the background. | 83 // Clear the background. |
| 84 FillRect(pixel_buffer_, 0, 0, width(), height(), 0xfff0f0f0); | 84 FillRect(pixel_buffer_, 0, 0, width(), height(), 0xfff0f0f0); |
| 85 | 85 |
| 86 // Get current gamepad data. | 86 // Get current gamepad data. |
| 87 PP_GamepadsSampleData_Dev gamepad_data; | 87 PP_GamepadsSampleData gamepad_data; |
| 88 gamepad_->Sample(pp_instance(), &gamepad_data); | 88 gamepad_->Sample(pp_instance(), &gamepad_data); |
| 89 | 89 |
| 90 // Draw the current state for each connected gamepad. | 90 // Draw the current state for each connected gamepad. |
| 91 for (size_t p = 0; p < gamepad_data.length; ++p) { | 91 for (size_t p = 0; p < gamepad_data.length; ++p) { |
| 92 int width2 = width() / gamepad_data.length / 2; | 92 int width2 = width() / gamepad_data.length / 2; |
| 93 int height2 = height() / 2; | 93 int height2 = height() / 2; |
| 94 int offset = width2 * 2 * p; | 94 int offset = width2 * 2 * p; |
| 95 PP_GamepadSampleData_Dev& pad = gamepad_data.items[p]; | 95 PP_GamepadSampleData& pad = gamepad_data.items[p]; |
| 96 | 96 |
| 97 if (!pad.connected) | 97 if (!pad.connected) |
| 98 continue; | 98 continue; |
| 99 | 99 |
| 100 // Draw axes. | 100 // Draw axes. |
| 101 for (size_t i = 0; i < pad.axes_length; i += 2) { | 101 for (size_t i = 0; i < pad.axes_length; i += 2) { |
| 102 int x = static_cast<int>(pad.axes[i + 0] * width2 + width2) + offset; | 102 int x = static_cast<int>(pad.axes[i + 0] * width2 + width2) + offset; |
| 103 int y = static_cast<int>(pad.axes[i + 1] * height2 + height2); | 103 int y = static_cast<int>(pad.axes[i + 1] * height2 + height2); |
| 104 uint32_t box_bgra = 0x80000000; // Alpha 50%. | 104 uint32_t box_bgra = 0x80000000; // Alpha 50%. |
| 105 FillRect(pixel_buffer_, x - 3, y - 3, 7, 7, box_bgra); | 105 FillRect(pixel_buffer_, x - 3, y - 3, 7, 7, box_bgra); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // Note that the pixel lock is held while the buffer is copied into the | 141 // Note that the pixel lock is held while the buffer is copied into the |
| 142 // device context and then flushed. | 142 // device context and then flushed. |
| 143 graphics_2d_context_->PaintImageData(*pixel_buffer_, pp::Point()); | 143 graphics_2d_context_->PaintImageData(*pixel_buffer_, pp::Point()); |
| 144 if (flush_pending()) | 144 if (flush_pending()) |
| 145 return; | 145 return; |
| 146 set_flush_pending(true); | 146 set_flush_pending(true); |
| 147 graphics_2d_context_->Flush(pp::CompletionCallback(&FlushCallback, this)); | 147 graphics_2d_context_->Flush(pp::CompletionCallback(&FlushCallback, this)); |
| 148 } | 148 } |
| 149 | 149 |
| 150 } // namespace gamepad | 150 } // namespace gamepad |
| OLD | NEW |