Chromium Code Reviews| 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 | |
| 22 void FlushCallback(void* data, int32_t result); | |
| 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 pending_paint_(false), | |
| 43 waiting_for_flush_completion_(false), | |
| 44 callback_factory_(this), | |
| 45 console_(NULL), | |
| 46 gamepad_(NULL) { | |
| 47 } | |
| 48 virtual ~MyInstance() {} | |
| 49 | |
| 50 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | |
| 51 console_ = reinterpret_cast<const PPB_Console_Dev*>( | |
| 52 pp::Module::Get()->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE)); | |
| 53 if (!console_) | |
| 54 return false; | |
| 55 | |
| 56 gamepad_ = reinterpret_cast<const PPB_Gamepad_Dev*>( | |
| 57 pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_DEV_INTERFACE)); | |
| 58 if (!gamepad_) | |
| 59 return false; | |
| 60 | |
| 61 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | | |
|
brettw
2012/01/06 23:36:44
It looks like you don't ever actually use the inpu
| |
| 62 PP_INPUTEVENT_CLASS_KEYBOARD); | |
| 63 return true; | |
| 64 } | |
| 65 | |
| 66 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { | |
|
brettw
2012/01/06 23:36:44
Would you mind using the new version of DidChangeV
| |
| 67 if (position.size().width() == width_ && | |
| 68 position.size().height() == height_) | |
| 69 return; // We don't care about the position, only the size. | |
| 70 | |
| 71 width_ = position.size().width(); | |
| 72 height_ = position.size().height(); | |
| 73 | |
| 74 device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false); | |
| 75 if (!BindGraphics(device_context_)) | |
| 76 return; | |
| 77 | |
| 78 Paint(); | |
| 79 } | |
| 80 | |
| 81 void OnFlush() { | |
| 82 Paint(); | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 void Paint() { | |
| 87 pp::ImageData image = PaintImage(device_context_.size()); | |
| 88 if (!image.is_null()) { | |
| 89 device_context_.ReplaceContents(&image); | |
| 90 device_context_.Flush(pp::CompletionCallback(&FlushCallback, this)); | |
|
brettw
2012/01/06 23:36:44
Here you should say
callback_factory_.NewRequiredC
| |
| 91 } else { | |
| 92 printf("NullImage\n"); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 pp::ImageData PaintImage(const pp::Size& size) { | |
| 97 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false); | |
| 98 if (image.is_null()) | |
| 99 return image; | |
| 100 | |
| 101 PP_GamepadsData_Dev gamepad_data; | |
| 102 gamepad_->SampleGamepads(pp_instance(), &gamepad_data); | |
| 103 | |
| 104 int width2 = size.width() / 2; | |
| 105 int height2 = size.height() / 2; | |
| 106 // Draw 2 axes | |
| 107 for (int i = 0; i < 4; i += 2) { | |
| 108 int x = static_cast<int>( | |
| 109 gamepad_data.items[0].axes[i + 0] * width2 + width2); | |
| 110 int y = static_cast<int>( | |
| 111 gamepad_data.items[0].axes[i + 1] * height2 + height2); | |
| 112 uint32_t box_bgra = 0x80ff0000; // Alpha 50%. | |
| 113 FillRect(&image, x - 3, y - 3, 7, 7, box_bgra); | |
| 114 } | |
| 115 return image; | |
| 116 } | |
| 117 | |
| 118 int width_; | |
| 119 int height_; | |
| 120 | |
| 121 bool pending_paint_; | |
| 122 bool waiting_for_flush_completion_; | |
| 123 | |
| 124 pp::CompletionCallbackFactory<MyInstance> callback_factory_; | |
| 125 | |
| 126 const PPB_Console_Dev* console_; | |
| 127 const PPB_Gamepad_Dev* gamepad_; | |
| 128 | |
| 129 pp::Graphics2D device_context_; | |
| 130 }; | |
| 131 | |
| 132 void FlushCallback(void* data, int32_t result) { | |
| 133 static_cast<MyInstance*>(data)->OnFlush(); | |
| 134 } | |
| 135 | |
| 136 // This object is the global object representing this plugin library as long | |
| 137 // as it is loaded. | |
| 138 class MyModule : public pp::Module { | |
| 139 public: | |
| 140 MyModule() : pp::Module() {} | |
| 141 virtual ~MyModule() {} | |
| 142 | |
| 143 // Override CreateInstance to create your customized Instance object. | |
| 144 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
| 145 return new MyInstance(instance); | |
| 146 } | |
| 147 }; | |
| 148 | |
| 149 namespace pp { | |
| 150 | |
| 151 // Factory function for your specialization of the Module object. | |
| 152 Module* CreateModule() { | |
| 153 return new MyModule(); | |
| 154 } | |
| 155 | |
| 156 } // namespace pp | |
| 157 | |
| OLD | NEW |