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

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

Powered by Google App Engine
This is Rietveld 408576698