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

Unified Diff: ppapi/examples/gamepad/gamepad.cc

Issue 9085027: Pepper gamepad support (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix example Created 8 years, 12 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 side-by-side diff with in-line comments
Download patch
Index: ppapi/examples/gamepad/gamepad.cc
diff --git a/ppapi/examples/gamepad/gamepad.cc b/ppapi/examples/gamepad/gamepad.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f1292783432c385ae669b90c4cf9cacae51791ec
--- /dev/null
+++ b/ppapi/examples/gamepad/gamepad.cc
@@ -0,0 +1,152 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <cmath>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "ppapi/c/dev/ppb_console_dev.h"
+#include "ppapi/c/dev/ppb_gamepad_dev.h"
+#include "ppapi/c/ppb_input_event.h"
+#include "ppapi/cpp/completion_callback.h"
+#include "ppapi/cpp/graphics_2d.h"
+#include "ppapi/cpp/image_data.h"
+#include "ppapi/cpp/input_event.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/logging.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/cpp/dev/gamepad_dev.h"
+#include "ppapi/cpp/rect.h"
+#include "ppapi/cpp/var.h"
+
+void FlushCallback(void* data, int32_t result);
+
+void FillRect(pp::ImageData* image, int left, int top, int width, int height,
+ uint32_t color) {
+ for (int y = std::max(0, top);
+ y < std::min(image->size().height() - 1, top + height);
+ y++) {
+ for (int x = std::max(0, left);
+ x < std::min(image->size().width() - 1, left + width);
+ x++)
+ *image->GetAddr32(pp::Point(x, y)) = color;
+ }
+}
+
+class MyInstance : public pp::Instance, public pp::Gamepad {
+ public:
+ explicit MyInstance(PP_Instance instance)
+ : pp::Instance(instance),
+ pp::Gamepad(this),
+ width_(0),
+ height_(0),
+ pending_paint_(false),
+ waiting_for_flush_completion_(false),
+ callback_factory_(this),
+ console_(NULL) {
+ }
+ virtual ~MyInstance() {}
+
+ virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
+ console_ = reinterpret_cast<const PPB_Console_Dev*>(
+ pp::Module::Get()->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE));
+ if (!console_)
+ return false;
+
+ RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE |
+ PP_INPUTEVENT_CLASS_KEYBOARD);
+ return true;
+ }
+
+ virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) {
+ if (position.size().width() == width_ &&
+ position.size().height() == height_)
+ return; // We don't care about the position, only the size.
+
+ width_ = position.size().width();
+ height_ = position.size().height();
+
+ device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false);
+ if (!BindGraphics(device_context_))
+ return;
+
+ Paint();
+ }
+
+ void OnFlush() {
+ Paint();
+ }
+
+ private:
+ void Paint() {
+ pp::ImageData image = PaintImage(device_context_.size());
+ if (!image.is_null()) {
+ device_context_.ReplaceContents(&image);
+ device_context_.Flush(pp::CompletionCallback(&FlushCallback, this));
+ } else {
+ printf("NullImage\n");
+ }
+ }
+
+ pp::ImageData PaintImage(const pp::Size& size) {
+ pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false);
+ if (image.is_null())
+ return image;
+
+ PP_GamepadsData_Dev gamepad_data;
+ SampleGamepads(&gamepad_data);
+
+ int width2 = size.width() / 2;
+ int height2 = size.height() / 2;
+ // Draw 2 axes
+ for (int i = 0; i < 4; i += 2) {
+ int x = static_cast<int>(
+ gamepad_data.items[0].axes[i + 0] * width2 + width2);
+ int y = static_cast<int>(
+ gamepad_data.items[0].axes[i + 1] * height2 + height2);
+ uint32_t box_bgra = 0x80ff0000; // Alpha 50%.
+ FillRect(&image, x - 3, y - 3, 7, 7, box_bgra);
+ }
+ return image;
+ }
+
+ int width_;
+ int height_;
+
+ bool pending_paint_;
+ bool waiting_for_flush_completion_;
+
+ pp::CompletionCallbackFactory<MyInstance> callback_factory_;
+
+ const PPB_Console_Dev* console_;
+
+ pp::Graphics2D device_context_;
+};
+
+void FlushCallback(void* data, int32_t result) {
+ static_cast<MyInstance*>(data)->OnFlush();
+}
+
+// This object is the global object representing this plugin library as long
+// as it is loaded.
+class MyModule : public pp::Module {
+ public:
+ MyModule() : pp::Module() {}
+ virtual ~MyModule() {}
+
+ // Override CreateInstance to create your customized Instance object.
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new MyInstance(instance);
+ }
+};
+
+namespace pp {
+
+// Factory function for your specialization of the Module object.
+Module* CreateModule() {
+ return new MyModule();
+}
+
+} // namespace pp
+

Powered by Google App Engine
This is Rietveld 408576698