| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium 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 #include <cmath> |
| 6 #include <stdarg.h> |
| 7 #include <stdio.h> |
| 8 |
| 9 #include "ppapi/c/dev/ppb_console_dev.h" |
| 10 #include "ppapi/c/dev/ppb_gamepad_dev.h" |
| 11 #include "ppapi/c/ppb_input_event.h" |
| 12 #include "ppapi/cpp/completion_callback.h" |
| 13 #include "ppapi/cpp/graphics_2d.h" |
| 14 #include "ppapi/cpp/image_data.h" |
| 15 #include "ppapi/cpp/input_event.h" |
| 16 #include "ppapi/cpp/instance.h" |
| 17 #include "ppapi/cpp/logging.h" |
| 18 #include "ppapi/cpp/module.h" |
| 19 #include "ppapi/cpp/rect.h" |
| 20 #include "ppapi/cpp/var.h" |
| 21 #include "ppapi/cpp/view.h" |
| 22 #include "ppapi/utility/completion_callback_factory.h" |
| 23 |
| 24 void FillRect(pp::ImageData* image, int left, int top, int width, int height, |
| 25 uint32_t color) { |
| 26 for (int y = std::max(0, top); |
| 27 y < std::min(image->size().height() - 1, top + height); |
| 28 y++) { |
| 29 for (int x = std::max(0, left); |
| 30 x < std::min(image->size().width() - 1, left + width); |
| 31 x++) |
| 32 *image->GetAddr32(pp::Point(x, y)) = color; |
| 33 } |
| 34 } |
| 35 |
| 36 class MyInstance : public pp::Instance { |
| 37 public: |
| 38 explicit MyInstance(PP_Instance instance) |
| 39 : pp::Instance(instance), |
| 40 width_(0), |
| 41 height_(0), |
| 42 callback_factory_(this), |
| 43 gamepad_(NULL) { |
| 44 } |
| 45 virtual ~MyInstance() {} |
| 46 |
| 47 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 48 gamepad_ = reinterpret_cast<const PPB_Gamepad_Dev*>( |
| 49 pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_DEV_INTERFACE)); |
| 50 if (!gamepad_) |
| 51 return false; |
| 52 |
| 53 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | |
| 54 PP_INPUTEVENT_CLASS_KEYBOARD); |
| 55 return true; |
| 56 } |
| 57 |
| 58 virtual void DidChangeView(const pp::View& view) { |
| 59 pp::Rect rect = view.GetRect(); |
| 60 if (rect.size().width() == width_ && |
| 61 rect.size().height() == height_) |
| 62 return; // We don't care about the position, only the size. |
| 63 |
| 64 width_ = rect.size().width(); |
| 65 height_ = rect.size().height(); |
| 66 |
| 67 device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false); |
| 68 if (!BindGraphics(device_context_)) |
| 69 return; |
| 70 |
| 71 Paint(); |
| 72 } |
| 73 |
| 74 void OnFlush(int32_t) { |
| 75 Paint(); |
| 76 } |
| 77 |
| 78 private: |
| 79 void Paint() { |
| 80 pp::ImageData image = PaintImage(device_context_.size()); |
| 81 if (!image.is_null()) { |
| 82 device_context_.ReplaceContents(&image); |
| 83 device_context_.Flush( |
| 84 callback_factory_.NewRequiredCallback(&MyInstance::OnFlush)); |
| 85 } else { |
| 86 printf("NullImage\n"); |
| 87 } |
| 88 } |
| 89 |
| 90 pp::ImageData PaintImage(const pp::Size& size) { |
| 91 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false); |
| 92 if (image.is_null()) |
| 93 return image; |
| 94 |
| 95 PP_GamepadsData_Dev gamepad_data; |
| 96 gamepad_->SampleGamepads(pp_instance(), &gamepad_data); |
| 97 |
| 98 int width2 = size.width() / 2; |
| 99 int height2 = size.height() / 2; |
| 100 // Draw 2 axes |
| 101 for (size_t i = 0; i < gamepad_data.items[0].axes_length; i += 2) { |
| 102 int x = static_cast<int>( |
| 103 gamepad_data.items[0].axes[i + 0] * width2 + width2); |
| 104 int y = static_cast<int>( |
| 105 gamepad_data.items[0].axes[i + 1] * height2 + height2); |
| 106 uint32_t box_bgra = 0x80000000; // Alpha 50%. |
| 107 FillRect(&image, x - 3, y - 3, 7, 7, box_bgra); |
| 108 } |
| 109 |
| 110 for (size_t i = 0; i < gamepad_data.items[0].buttons_length; ++i) { |
| 111 float button_val = gamepad_data.items[0].buttons[i]; |
| 112 uint32_t colour = static_cast<uint32_t>((button_val * 192) + 63) << 24; |
| 113 int x = i * 8 + 10; |
| 114 int y = 10; |
| 115 FillRect(&image, x - 3, y - 3, 7, 7, colour); |
| 116 } |
| 117 return image; |
| 118 } |
| 119 |
| 120 int width_; |
| 121 int height_; |
| 122 |
| 123 pp::CompletionCallbackFactory<MyInstance> callback_factory_; |
| 124 |
| 125 const PPB_Gamepad_Dev* gamepad_; |
| 126 |
| 127 pp::Graphics2D device_context_; |
| 128 }; |
| 129 |
| 130 // This object is the global object representing this plugin library as long |
| 131 // as it is loaded. |
| 132 class MyModule : public pp::Module { |
| 133 public: |
| 134 MyModule() : pp::Module() {} |
| 135 virtual ~MyModule() {} |
| 136 |
| 137 // Override CreateInstance to create your customized Instance object. |
| 138 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 139 return new MyInstance(instance); |
| 140 } |
| 141 }; |
| 142 |
| 143 namespace pp { |
| 144 |
| 145 // Factory function for your specialization of the Module object. |
| 146 Module* CreateModule() { |
| 147 return new MyModule(); |
| 148 } |
| 149 |
| 150 } // namespace pp |
| 151 |
| OLD | NEW |